matheca中的包导入问题
在mathematica中(我使用的是mma 5.0(猜想很旧)),如果我输入以下内容作为一行:
Needs["Graphics`Master`"]; Animate[Plot[Sin[n x], {x, 0, 2 Pi}, Axes -> False], {n, 1, 6, 1}]
然后我会收到很多错误/警告。但如果我单独输入它们,它就可以正常工作。如何让它在一个代码块中工作?
谢谢!
In mathematica (I am using mma 5.0 ( guess pretty old)), if I type the following as one line:
Needs["Graphics`Master`"]; Animate[Plot[Sin[n x], {x, 0, 2 Pi}, Axes -> False], {n, 1, 6, 1}]
I then got a lot of errors/warnings. But if I type them in separately, it is working fine. How to make it work in one code block?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如贝利撒留指出的那样,你的问题有点以 v5 为中心。然而,该问题在当前版本中仍然存在。作为一个例子
,工作正常,而(重新启动内核后)
失败并出现错误:
在 Mathematica 术语中,单行行不起作用的原因是 Mathematica 尝试在评估
Needs
之前解析行中的所有符号(这让我感到惊讶) ),在Needs
有机会加载定义之前,这会将ToCycles
解析为Global`ToCycles
(从而在符号表中输入该符号)。Combinatorica`ToCycles
并将Combinatorica
添加到$ContextPath
要使单行代码正常工作,您必须使用ToCyles
的全名:要理解错误,您需要要知道 Mathematica 中的所有符号都有一个 全名,格式为
context`nameToCycles
),Mathematica 将查找当前位于$ 中的上下文。 ContextPath
并查看该符号是否在任何这些上下文中定义。如果不是,该符号将在当前上下文$Context
中解析,正常使用时为Global
。当您加载包时,该包的符号在包上下文中定义(例如
Combinatorica
),当包完全加载时,此上下文将添加到$ContextPath
中> 这样您就可以通过符号的短名称访问它们。现在,您可以看到错误的含义:由于解析符号时
Combinatorica
尚未加载,ToCycles
解析为Global`ToCycles
>。包加载后,Mathematica 会帮助检查所有短名称是否唯一,并在这种情况下发现短名称ToCycles
现在在$ContextPath
的两个上下文中定义,因此“遮蔽”对方。要引用这些符号中的特定符号,您必须使用全名,例如Combinatorica`ToCycles
。要解决阴影冲突,只需
删除
不需要的符号:不知道它的可读性如何,但希望它能有所帮助......
As belisarius points out, your question as it stands is a bit v5-centric. The problem, however, still exists in current versions. As an example
works fine, while (after restarting the kernel),
fails with an error that
In Mathematica terms, the reason the one-liner doesn't work is that Mathematica tries to resolve all symbols in the line before evaluating
Needs
(this was a surprise to me). This resolvesToCycles
toGlobal`ToCycles
(thus entering this symbol in the symbol table), beforeNeeds
gets a chance to load the definition ofCombinatorica`ToCycles
and addCombinatorica
to the$ContextPath
. To make the one-liner work, you must use the full name ofToCyles
:To understand the error, you need to know that all Symbols in Mathematica have a full name of the form
context`name
. A context is similar to a namespace in many other languages. Now, if a symbol (such asToCycles
) is referenced without a context, Mathematica will look through the contexts currently in$ContextPath
and see if the symbol is defined in any of those contexts. If not, the symbol is resolved in the current context,$Context
which isGlobal
in normal use.When you load a package, the symbols of that package are defined in a package context (e.g.
Combinatorica
), and when the package is fully loaded this context is added to the$ContextPath
so that you can access the symbols by their short name.Now, you can see what the error means: Since the
Combinatorica
has not yet been loaded when the symbols are resolved,ToCycles
resolves toGlobal`ToCycles
. After the package loads, Mathematica helpfully checks that all short names are unique, and finds in this case that the short nameToCycles
is now defined in two contexts on$ContextPath
one thus "shadowing" the other. To refer to a specific of these symbols, you must use the full name, e.g.Combinatorica`ToCycles
.To resolve a shadow conflict, simply
Remove
the unwanted symbol:Don't know how readable this was, but hope it helps a bit...
您应该将
Needs[]
调用放在笔记本顶部的单独块中,或者放在 package.m 文件的第一个独立行上。内核读取整行并解析它,包括在开始评估它之前决定符号的上下文。为避免出现问题,请勿使用分号。在包中的每个语句后面放置两个换行符。
特别是在
BeginPackage[]
和/或Needs[]
之后。You are supposed to put the
Needs[]
call(s) at the top of a notebook in a separate block, or on the first isolated line of a package.m file.The kernel reads the whole line and parses it, including deciding on the contexts for symbols, before beginning to evaluate it. To avoid problems, do not use semicolons. Put two newlines after every statement in a package.
Especially after the
BeginPackage[]
and/orNeeds[]
.