在 Mathematica 中使用 Context 作为范围构造
在思考关于在大型 Mathematica 项目中在数值和分析“模式”之间切换的上一个问题的解决方案时,我想到了使用Context
作为范围构造的想法。
基本思想是在它们自己的上下文中进行所有数值分配,例如
Begin["Global`Numerical`"]
par1 = 1;
par2 = 2;
...
End[]
在全局上下文中具有所有复杂的分析函数、矩阵等。
理想情况下,我能够在全局上下文中工作,并通过简单的 Begin[Global'Numeric']
切换到所有数字,并使用 End[]
切换回来。
不幸的是,这不起作用,因为例如在全局上下文中定义的 f[par1_,par2_,...] := foo
将不会使用 par1
, par2< /code> 等已在 Global 的子上下文中定义。
有没有办法让子上下文继承父上下文的定义?是否有其他方法可以使用上下文来创建简单的可切换范围?
Thinking about a solution to my previous question about switching between numerical and analytical "modes" in a large Mathematica project, I thought about the idea of using Context
as a scoping construct.
The basic idea is to make all numerical value assignments in their own context, e.g.
Begin["Global`Numerical`"]
par1 = 1;
par2 = 2;
...
End[]
and have all the complicated analytical functions, matrices, etc. in the Global context.
Ideally I would be able to work in the Global context and switch to everything being numeric with a simple Begin[Global'Numeric']
and switch back with End[]
.
Unfortunately this doen not work, since e.g. f[par1_,par2_,...] := foo
defined in the Global context will not use par1
, par2
, etc which have been defined in a sub context of Global.
Is there a way to make sub contexts inherit definitions from their parent context? Is there some other way to use contexts to create a simple switchable scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是通过适当调整
$ContextPath
来解决(我认为)问题的一种方法:现在,这将进行分析评估:
并且从数字上看:
我说它脆弱的原因是,除非你请小心,很容易将符号放入错误的上下文中。例如,假设您在评估“数字”块之前忘记在全局上下文中评估 f 。好吧,现在您的数字块将无法工作,因为它会变成一个(完全有效的)符号
GlobalTestCtxt`Numerical`f
,您在第一次评估数字时无意中将其输入到符号表中堵塞。由于存在这样的潜在错误,我个人不使用这种方法。编辑:修复了一个错误(在数字上下文中进行赋值时需要隐藏“全局”上下文)
Well, here's one way to get around (what I think) is your problem by adjusting
$ContextPath
appropriately:Now, this will evaluate analytically:
And this numerically:
The reason I say it's fragile is that unless you are careful, it's easy to get the symbol into the wrong context. For instance, suppose you forgot to evaluate f in the global context before evaluating the "numerical" block. Well, now your numerical block will not work simply because it'll turn to a (perfectly valid) symbol
GlobalTestCtxt`Numerical`f
, which you have inadvertently entered into the symbol table when you first evaluated the numerical block. Because of potential bugs like this, I personally don't use this approach.Edit: fixed a bug (it is necessary to hide the "Global" context when doing assignments in numerical context)