matheca中的包导入问题

发布于 2024-10-11 20:45:55 字数 247 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2024-10-18 20:45:55

正如贝利撒留指出的那样,你的问题有点以 v5 为中心。然而,该问题在当前版本中仍然存在。作为一个例子

Needs["Combinatorica`"]
ToCycles[{3, 4, 1, 2}]

,工作正常,而(重新启动内核后)

Needs["Combinatorica`"]; ToCycles[{3, 4, 1, 2}]

失败并出现错误:

“ToCycles::shdw:符号 ToCycles
出现在多个上下文中
{组合`,全球`};定义
在上下文 Combinatorica` 可能会产生阴影
或者被其他定义所掩盖。”

在 Mathematica 术语中,单行行不起作用的原因是 Mathematica 尝试在评估 Needs 之前解析行中的所有符号(这让我感到惊讶) ),在 Needs 有机会加载定义之前,这会将 ToCycles 解析为 Global`ToCycles (从而在符号表中输入该符号)。 Combinatorica`ToCycles 并将 Combinatorica 添加到 $ContextPath 要使单行代码正常工作,您必须使用 ToCyles 的全名:

Needs["Combinatorica`"]; Combinatorica`ToCycles[{3, 4, 1, 2}]

要理解错误,您需要要知道 Mathematica 中的所有符号都有一个 全名,格式为 context`nameToCycles),Mathematica 将查找当前位于 $ 中的上下文。 ContextPath 并查看该符号是否在任何这些上下文中定义。如果不是,该符号将在当前上下文 $Context 中解析,正常使用时为Global
当您加载包时,该包的符号在包上下文中定义(例如 Combinatorica),当包完全加载时,此上下文将添加到 $ContextPath 中> 这样您就可以通过符号的短名称访问它们。
现在,您可以看到错误的含义:由于解析符号时 Combinatorica 尚未加载,ToCycles 解析为 Global`ToCycles >。包加载后,Mathematica 会帮助检查所有短名称是否唯一,并在这种情况下发现短名称 ToCycles 现在在 $ContextPath 的两个上下文中定义,因此“遮蔽”对方。要引用这些符号中的特定符号,您必须使用全名,例如 Combinatorica`ToCycles

要解决阴影冲突,只需删除不需要的符号:

Remove[Global`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

Needs["Combinatorica`"]
ToCycles[{3, 4, 1, 2}]

works fine, while (after restarting the kernel),

Needs["Combinatorica`"]; ToCycles[{3, 4, 1, 2}]

fails with an error that

"ToCycles::shdw: Symbol ToCycles
appears in multiple contexts
{Combinatorica`,Global`}; definitions
in context Combinatorica` may shadow
or be shadowed by other definitions."

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 resolves ToCycles to Global`ToCycles (thus entering this symbol in the symbol table), before Needs gets a chance to load the definition of Combinatorica`ToCycles and add Combinatorica to the $ContextPath. To make the one-liner work, you must use the full name of ToCyles:

Needs["Combinatorica`"]; Combinatorica`ToCycles[{3, 4, 1, 2}]

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 as ToCycles) 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 is Global 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 to Global`ToCycles. After the package loads, Mathematica helpfully checks that all short names are unique, and finds in this case that the short name ToCycles 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:

Remove[Global`ToCycles]

Don't know how readable this was, but hope it helps a bit...

眼眸印温柔 2024-10-18 20:45:55

您应该将 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/or Needs[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文