From: David Park on 26 Apr 2010 04:51 I would like to export rules from a package in such a way that the pattern symbols did not include any contexts and in which I also did not have to export the symbols used in the pattern. In addition, If possible, I would like this to work even if the package was loaded from a notebook that had something other than the Global` context. Here is a sample package and exported rule: BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; End[]; EndPackage[]; Then, if we evaluate Rule1 we obtain a very clumsy version of the rule, although I suppose it will work. Rule1 Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 -> 1 What I would like is the rule as written in the package. Is there a way to do this? David Park djmpark(a)comcast.net <http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/
From: Ingolf Dahl on 26 Apr 2010 07:31 To David, Try with BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[Global`x_]^2 + Sin[Global`x_]^2 -> 1; End[]; EndPackage[]; In the Global` context, Rule1 will display as Cos[x_]^2+Sin[x_]^2->1 Ingolf Dahl Sweden -----Original Message----- From: David Park [mailto:djmpark(a)comcast.net] Sent: den 26 april 2010 10:51 Subject: Context Problem I would like to export rules from a package in such a way that the pattern symbols did not include any contexts and in which I also did not have to export the symbols used in the pattern. In addition, If possible, I would like this to work even if the package was loaded from a notebook that had something other than the Global` context. Here is a sample package and exported rule: BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; End[]; EndPackage[]; Then, if we evaluate Rule1 we obtain a very clumsy version of the rule, although I suppose it will work. Rule1 Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 -> 1 What I would like is the rule as written in the package. Is there a way to do this? David Park djmpark(a)comcast.net <http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/
From: Bob Hanlon on 26 Apr 2010 07:32 BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; End[]; EndPackage[]; ToExpression[ StringReplace[ToString[InputForm[Rule1]], "PackageContext`Private`" -> ""]] Sin[x_]^2 + Cos[x_]^2 -> 1 Bob Hanlon ---- David Park <djmpark(a)comcast.net> wrote: ============= I would like to export rules from a package in such a way that the pattern symbols did not include any contexts and in which I also did not have to export the symbols used in the pattern. In addition, If possible, I would like this to work even if the package was loaded from a notebook that had something other than the Global` context. Here is a sample package and exported rule: BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; End[]; EndPackage[]; Then, if we evaluate Rule1 we obtain a very clumsy version of the rule, although I suppose it will work. Rule1 Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 -> 1 What I would like is the rule as written in the package. Is there a way to do this? David Park djmpark(a)comcast.net <http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/
From: Patrick Scheibe on 26 Apr 2010 07:32 Hi, what about using the global symbol? BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[Global`x_]^2 + Sin[Global`x_]^2 :> Global`x; End[]; EndPackage[]; Cheers Patrick Am Apr 26, 2010 um 10:51 AM schrieb David Park: > I would like to export rules from a package in such a way that the > pattern > symbols did not include any contexts and in which I also did not > have to > export the symbols used in the pattern. In addition, If possible, I > would > like this to work even if the package was loaded from a notebook > that had > something other than the Global` context. > > > > Here is a sample package and exported rule: > > > > BeginPackage["PackageContext`"]; > > > > Rule1::usage = "Rule1 is a test exported rule."; > > > > Begin["`Private`"]; > > > > Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; > > > > End[]; > > > > EndPackage[]; > > > > Then, if we evaluate Rule1 we obtain a very clumsy version of the > rule, > although I suppose it will work. > > > > Rule1 > > Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 - > > 1 > > > > What I would like is the rule as written in the package. Is there a > way to > do this? > > > > > > David Park > > djmpark(a)comcast.net > > <http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/ > > >
From: David Park on 27 Apr 2010 04:04 I want to thank Ingolf Dahl, Bob Hanlon and Patrick Scheibe for their answers. Using the Global` context is a good solution, except if the package had been loaded in something other than the Global` context, say on a Function page. Also I would like to have a simple way to process simply written rules in the package such that it would return simple looking and usable rules to the user. There are a number of issues I would like to raise because I'm not certain of the best procedure if we want a package to generate an expression with new symbols that are in the Context of the notebook using the package. There are two solutions that might be useful but are unavailable. 1) WRI might be able to create a floating context, which had the property that it took on the context of the notebook or cell in which it is created. This could then be used by package writers to return created symbols. 2) WRI might provide a way for a package routine to retrieve the current Context in the InputNotebook[]. This could then be used actively in generating new symbols. Otherwise we can obtain "Context free" output by putting the Rule symbols into existing Contexts such as the Package Context itself, or the System` context. But is it good practice to usurp simple symbols such as x, y, z into some special Context? Maybe it is all right if users always read in the package first. But suppose a number of packages start doing this? In any case, here is the package routine again in a form that statically determines the Context at the time it is read in. The problem is that if the user changes evaluation from one notebook to another, that have different Contexts, then the package must be read in anew. I am taking advantage here of the fact that SymbolName strips any leading Context from a symbol so we can just attach the new Context. System`loadcontext=Context[]; BeginPackage["PackageContext`"]; Rule1::usage="Rule1 is a test exported rule."; Begin["`Private`"]; cs[var_Symbol]:=var->Symbol[System`loadcontext<>SymbolName[var]] cs[vars:{__Symbol}]:=cs/@vars Rule1=(y_ Sin[x_ y_]/;AtomQ[y]->{x,y})//.cs[{x,y}]; End[]; EndPackage[]; Test: Rule1 a Sin[a b] /. Rule1 y_ Sin[x_ y_] /; AtomQ[y] -> {x, y} {b, a} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: David Park [mailto:djmpark(a)comcast.net] I would like to export rules from a package in such a way that the pattern symbols did not include any contexts and in which I also did not have to export the symbols used in the pattern. In addition, If possible, I would like this to work even if the package was loaded from a notebook that had something other than the Global` context. Here is a sample package and exported rule: BeginPackage["PackageContext`"]; Rule1::usage = "Rule1 is a test exported rule."; Begin["`Private`"]; Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; End[]; EndPackage[]; Then, if we evaluate Rule1 we obtain a very clumsy version of the rule, although I suppose it will work. Rule1 Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 -> 1 What I would like is the rule as written in the package. Is there a way to do this? David Park djmpark(a)comcast.net <http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/
|
Next
|
Last
Pages: 1 2 Prev: Stop mathematica reordering expression during matrix Next: Precision of calculations |