本文介绍了带有“私有"的输出Mathematica 包中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Mathematica 7.0 中解决以下实现问题已经有几天了,但我不明白到底发生了什么,所以我希望有人能给我一些提示.我在 Mathematica 中在扩展名为 *.nb 的源文件中实现了 3 个函数.他们对所有示例都正常工作.现在我想将这些函数放入 3 个不同的包中.所以我创建了三个不同的扩展名为 .*m 的包,我把所有需要的 Mathematica 函数放在里面.stereographic.m"包中包含代码的示例:

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`"]

一切正常.但是当我用

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`.任何想法或更好的解释可以说明为什么会发生这种情况?

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?

非常感谢您的帮助.

最好的祝福,玛达琳娜

推荐答案

当你从包中返回符号函数时,你的问题很常见,当我遇到这种情况时,我认为它好像我做了什么写包错误.虽然为所有这些符号加上 Global 前缀将解决"问题,但它违背了包的一些目的:实现隐藏.此外,由于它会用您的符号污染全局命名空间,因此您必须小心运行代码,这进一步违背了包的目的.你的包不应该关心全局环境是什么样的.如果它需要任何东西,它可以在 BeginPackage 中或在包的私有部分使用 Needs 自己加载它.

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 in BeginPackage or using Needs within the private portion of the package.

相反,您可以执行类似 Plot 的功能,接受一个 Symbol 参数,如下所示:

Instead, you can do what functions like Plot do, accept a Symbol parameter, as follows:

 (*Note: if z is not a symbol this won't work, i.e. if it is Set to some value.*)
 In[1]  := f[x_Symbol] := x^2
 In[2]  := f[z]
 Out[2] := z^2  

在内部,符号变量像平常一样被引用,但您的函数现在将使用您选择使用的任何全局符号返回一个符号表达式.这也使您对变量名称的选择与函数的实现细节分离.

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.

这篇关于带有“私有"的输出Mathematica 包中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 19:51