mathematica 中的包导入问题 [英] package import problem in mathematica

查看:54
本文介绍了mathematica 中的包导入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 mathematica 中(我使用的是 mma 5.0(猜测很旧)),如果我将以下内容作为一行输入:

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?

谢谢!

推荐答案

正如 belisarius 指出的那样,您的问题目前有点以 v5 为中心.但是,该问题在当前版本中仍然存在.举个例子

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}]

因错误而失败

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

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

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

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}]

<小时>

要理解错误,您需要知道 Mathematica 中的所有 Symbols 都有一个 context`name 形式的全名.上下文类似于许多其他语言中的命名空间.现在,如果在没有上下文的情况下引用一个符号(例如 ToCycles),Mathematica 将查看当前在 $ContextPath 中的上下文,并查看该符号是否在任何那些上下文.如果不是,则符号在当前上下文中解析,$Context 在正常使用中是 Global.
加载包时,该包的符号在包上下文中定义(例如 Combinatorica),当包完全加载时,此上下文将添加到 $ContextPath 以便您可以通过短名称访问符号.
现在,您可以看到错误的含义:由于解析符号时 Combinatorica 尚未加载,ToCycles 解析为 Global`ToCycles.包加载后,Mathematica 会帮助检查所有短名称是否唯一,并在这种情况下发现短名称 ToCycles 现在定义在 $ContextPath 上的两个上下文中,因此遮蔽"另一个.要引用这些符号中的特定符号,您必须使用全名,例如Combinatorica`ToCycles.


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...

这篇关于mathematica 中的包导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆