Mathematica - 分离笔记本 [英] Mathematica - Separating Notebooks

查看:66
本文介绍了Mathematica - 分离笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将打开的 Mathematica 笔记本分开,以便它们不共享任何变量?让它共享一些变量而不是全部变量怎么样?

Is there a way to separate open Mathematica notebooks so that they don't share any variables? How about making it so some variables are shared but not all?

推荐答案

是的,有.我建议阅读与 Mathematica 上下文相关的文档.简而言之,所有变量都属于某个上下文(命名空间),并且所有变量都可以通过ContextName`varName"形式的完全限定名称进行访问.如果您只使用varName",Mathematica 将在 $ContextPath 中搜索上下文(尝试评估变量 $ContextPath 以查看它是什么),并将使用第一个上下文它找到那个变量.此外,每个笔记本指定一个上下文(存储在变量 $Context 中),其中存储所有变量(除非使用完全限定名称).

Yes, there is. I recommend reading documentation related to Mathematica contexts. In a nutshell, all variables belong to some context (namespace), and all variables can be accessed via their fully-qualified names of the form "ContextName`varName". If you just use "varName", Mathematica will search contexts in $ContextPath (try evaluating the variable $ContextPath to see what it is), and will use the first context where it finds that variable. In addition, each notebook specifies a context (stored in the variable $Context) where all its variables are stored (unless fully-qualified name is used).

默认情况下,所有笔记本的上下文都是全局".同样默认情况下,所有笔记本的 $ContextPath 包括Global`"上下文(以及System`"和其他一些).最终结果是变量在笔记本之间共享,这很快就会变得很烦人.但是,有一个简单的解决方案.要为笔记本创建私有"上下文,请评估以下内容:

By default, for all notebooks the context is "Global`". Also by default, $ContextPath for all notebooks includes the "Global`" context (as well as "System`" and some others). The net result is that variables are shared across notebooks, and this can rather quickly become annoying. However, there's an easy solution. To create a "private" context for a notebook, evaluate the following:

SetOptions[EvaluationNotebook[], CellContext -> Notebook]

这个笔记本将被分配一个唯一的上下文(评估变量 $Context 以查看它是什么).此外,全局上下文将从 ContextPath 中删除(尝试在上面的 SetOptions[...] 之前和之后评估 $ContextPath 以查看发生了什么.)

This notebook will be assigned a unique context (evaluate the variable $Context to see what it is). Also, global context will be removed from ContextPath (try evaluating $ContextPath before and after the SetOptions[...] above to see what's going on.)

[更新:正如 rcollyer 在新 Mathematica 堆栈交换中所指出的,要将此选项设置为新笔记本的默认值,请执行以下操作:打开选项检查器 (Ctrl+Shift+O),将范围(在顶部的下拉列表中)从选择"更改为全局首选项";在左侧展开节点 Cell Options -> Evaluation Options,并将 CellContext 设置更改为Notebook".]

[Update: As pointed out by rcollyer on the new Mathematica stack exchange, to set this option as the default for new notebooks, do the following: open the Options Inspector (Ctrl+Shift+O), change the scope (in the dropdown on the top) from "Selection" to "Global Preferences"; on the left expand the nodes Cell Options -> Evaluation Options, and change the CellContext setting to "Notebook."]

现在,创建共享上下文的方法如下:

Now, here's how to create a shared context:

Begin["SharedContext`"];
varShared1 = "Shared string";
End[];

或者,你可以直接输入

SharedContext`varShared1 = "Shared string";

现在您可以使用完全限定的名称(SharedContext`varShared1"适用于任何笔记本),也可以将上下文添加到 $ContextPath:

Now you can either use the fully qualified names ("SharedContext`varShared1" will work in any notebook), or you can add the context to $ContextPath:

AppendTo[$ContextPath, "SharedContext`"]

如果您在所有笔记本中执行此操作,varShared1 将在没有完全限定名称的情况下变为可见.

If you do this in all notebooks, varShared1 will become visible without a fully-qualified name.

总而言之,上下文的工作方式与许多其他搜索路径非常相似.但是,有许多微妙之处(例如,如果一个符号已经在其他上下文中定义,则 Begin["SharedContext`"]/End[] 块可能不会像您预期的那样工作——符号的现有上下文将被用来代替 SharedContext`),所以我建议进行健康的实验并仔细阅读文档.

To summarize, context work a lot like many other search paths. However, there are many subtleties (for example, if a symbol has already been defined in some other context, the Begin["SharedContext`"]/End[] block might not work as you expect -- the existing context of the symbol will be used instead of SharedContext`), so I recommend a healthy dose of experimentation and perusing the docs.

这篇关于Mathematica - 分离笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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