带有“Private”的输出Mathematica 包中的内容
我几天来一直在尝试解决 Mathematica 7.0 中的以下实现问题,但我不明白究竟发生了什么,所以我希望有人能给我一些提示。 我在 Mathematica 中在扩展名为 *.nb 的源文件中实现了 3 个函数。 他们对所有示例都工作正常。现在我想将这些函数放入3个不同的包中。因此,我创建了三个不同的扩展名为 .*m 的包,其中放置了所有所需的 Mathematica 函数。 “steregraphic.m”包中包含以下代码的示例:
BeginPackage["stereographic`"]
stereographic::usage="The package stereographic...."
formEqs::usage="The function formEqs[complexBivPolyEqn..."
makePoly::usage="The function makePoly[algebraicEqn] ..."
getFixPolys::usage="The function..."
milnorFibration::usage="The function..."
Begin["Private`"]
Share[];
formEqs[complex_,{m_,n_}]:=Block[{complexnew,complexnew1, realeq, imageq, expreal,
expimag, polyrealF, polyimagF,s,t,u,v,a,b,c,epsilon,x,y,z},
complexnew:=complex/.{m->s+I*t,n->u+I*v};
complexnew1:=complexnew/.{s->(2 a epsilon)/(1+a^2+b^2+c^2),t->(2 b
epsilon)/(1+a^2+b^2+c^2),u->(2 c epsilon)/(1+a^2+b^2+c^2),v->(-
epsilon+a^2 epsilon+b^2 epsilon+c^2
epsilon)/(1+a^2+b^2+c^2)};
realeq:=ComplexExpand[Re[complexnew1]];
imageq:=ComplexExpand[Im[complexnew1]];
expreal:=makePoly[realeq];
expimag:=makePoly[imageq];
polyrealF:=expreal/.{a->x,b->y,c->z};
polyimagF:=expimag/.{a->x,b->y,c->z};
{polyrealF,polyimagF}
]
End[]
EndPackage[]
现在为了测试该功能,我加载了该包,
Needs["stereographic`"]
一切正常。但是,当我测试该函数时,例如,
formEqs[x^2-y^2,{x,y}]
我得到以下输出:
{Private`epsilon^2 + 2 Private`x^2 Private`epsilon^2 +
Private`x^4 Private`epsilon^2 -
6 Private`y^2 Private`epsilon^2 +
2 Private`x^2 Private`y^2 Private`epsilon^2 +
Private`y^4 Private`epsilon^2 -
6 Private`z^2 Private`epsilon^2 +
2 Private`x^2 Private`z^2 Private`epsilon^2 +
2 Private`y^2 Private`z^2 Private`epsilon^2 +
Private`z^4 Private`epsilon^2,
8 Private`x Private`y Private`epsilon^2 +
4 Private`z Private`epsilon^2 -
4 Private`x^2 Private`z Private`epsilon^2 -
4 Private`y^2 Private`z Private`epsilon^2 -
4 Private`z^3 Private`epsilon^2}
当然,我不明白为什么 Private` 出现在我在最终结果中返回的任何局部变量前面。我不想在计算输出中包含此 Private`。 有什么想法或更好的解释可以告诉我为什么会发生这种情况吗?
非常感谢您的帮助。
最好的祝愿, 马达利纳
I am trying to solve the following implementation problem in Mathematica 7.0 for some days now and I do not understand exactly what is happening so I hope someone can give me some hints.
I have 3 functions that I implemented in Mathematica in a source file with extension *.nb.
They are working okay to all the examples. Now I want to put these functions into 3 different packages. So I created three different packages with extension .*m in which I put all the desired Mathematica function.
An example in the "stereographic.m" package which contain the code:
BeginPackage["stereographic`"]
stereographic::usage="The package stereographic...."
formEqs::usage="The function formEqs[complexBivPolyEqn..."
makePoly::usage="The function makePoly[algebraicEqn] ..."
getFixPolys::usage="The function..."
milnorFibration::usage="The function..."
Begin["Private`"]
Share[];
formEqs[complex_,{m_,n_}]:=Block[{complexnew,complexnew1, realeq, imageq, expreal,
expimag, polyrealF, polyimagF,s,t,u,v,a,b,c,epsilon,x,y,z},
complexnew:=complex/.{m->s+I*t,n->u+I*v};
complexnew1:=complexnew/.{s->(2 a epsilon)/(1+a^2+b^2+c^2),t->(2 b
epsilon)/(1+a^2+b^2+c^2),u->(2 c epsilon)/(1+a^2+b^2+c^2),v->(-
epsilon+a^2 epsilon+b^2 epsilon+c^2
epsilon)/(1+a^2+b^2+c^2)};
realeq:=ComplexExpand[Re[complexnew1]];
imageq:=ComplexExpand[Im[complexnew1]];
expreal:=makePoly[realeq];
expimag:=makePoly[imageq];
polyrealF:=expreal/.{a->x,b->y,c->z};
polyimagF:=expimag/.{a->x,b->y,c->z};
{polyrealF,polyimagF}
]
End[]
EndPackage[]
Now to test the function I load the package
Needs["stereographic`"]
everything is okay. But when I test the function for example with
formEqs[x^2-y^2,{x,y}]
I get the following ouput:
{Private`epsilon^2 + 2 Private`x^2 Private`epsilon^2 +
Private`x^4 Private`epsilon^2 -
6 Private`y^2 Private`epsilon^2 +
2 Private`x^2 Private`y^2 Private`epsilon^2 +
Private`y^4 Private`epsilon^2 -
6 Private`z^2 Private`epsilon^2 +
2 Private`x^2 Private`z^2 Private`epsilon^2 +
2 Private`y^2 Private`z^2 Private`epsilon^2 +
Private`z^4 Private`epsilon^2,
8 Private`x Private`y Private`epsilon^2 +
4 Private`z Private`epsilon^2 -
4 Private`x^2 Private`z Private`epsilon^2 -
4 Private`y^2 Private`z Private`epsilon^2 -
4 Private`z^3 Private`epsilon^2}
Of course I do not understand why Private` appears in front of any local variable which I returned in the final result. I would want not to have this Private` in the computed output.
Any idea or better explanations which could indicate me why this happens?
Thank you very much for your help.
Best wishes,
madalina
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您从包返回符号函数时,您的问题是一个常见问题,当这种情况发生在我身上时,我认为好像我在编写包时做错了什么。虽然在所有此类符号前加上 Global 前缀可以“解决”问题,但它违背了包的一些目的:实现隐藏。此外,由于它会用符号污染全局命名空间,因此您必须小心运行代码的方式,这进一步违背了包的目的。你的包不应该关心全球环境是什么样的。如果需要任何东西,它可以在
BeginPackage
中自行加载,也可以在包的私有部分中使用Needs
加载。相反,您可以执行
Plot
等函数的操作,接受Symbol
参数,如下所示:在内部,符号变量像平常一样被引用,但您的函数现在将返回一个符号使用您选择使用的任何全局符号进行表达式。这还将您对变量名称的选择与函数的实现细节解耦。
Your problem is a common one when you are returning symbolic functions from a package, and when this happens to me, I view it as if I've done something wrong in writing the package. While prefixing all such symbols with
Global
will "fix" the problem, it defeats some of the purpose of a package: implementation hiding. Also, since it pollutes the global namespace with your symbols, you must be careful in how you run your code which further defeats the purpose of a package. Your package should not care what the global environment is like. If it needs anything, it can load it itself either inBeginPackage
or usingNeeds
within the private portion of the package.Instead, you can do what functions like
Plot
do, accept aSymbol
parameter, as follows:Internally, symbolic variables are referenced like normal, but your function will now return a symbolic expression using whatever global symbols you've chosen to use. This also decouples your choice of variable names with the implementation details of your function.
来自此处的讨论,看起来将包内的符号分配给全局上下文将使它们在没有私有上下文前缀的情况下输出。
也就是说,任何可能构成输出一部分的符号都可以使用 Global` 前缀进行声明,如下例所示:
在您的包中,它可能看起来像这样:
编辑:全局变量需要给予 Global` 前缀无论它们发生在哪里,如上所述
From the discussion here, it looks like assigning the symbols inside the package to the global context will make them be output without the private context prefix.
That is, any symbols that might form part of the output could be declared with a Global` prefix, as in this example:
In your package, it might look something like this:
edit: the Global variables need to be given the Global` prefix wherever they occur, as above
尝试将
Begin["Private`"]
更改为Begin["`Private`"]
。Try changing
Begin["Private`"]
toBegin["`Private`"]
.您可以使用正式符号而不是普通符号来避免出现问题。
同时,使用形式符号是输出涉及形式参数的通用表达式的更方便的方式。它们没有值,并且不能无意中设置它们的值,因为它们是受保护的。
您可以使用“特殊字符”调色板来输入形式符号。
You could use formal symbols instead of ordinary symbols to avoid your problem.
At the same time, using of formal symbols is more convenient way to output general expressions involving formal parameters. They have no values and values for them cannot be unintentionally set since they are
Protected
.You can use "Special characters" palette to inter formal symbols.