从包中加载 mathematica 包 [英] Load a mathematica package from within a package

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

问题描述

我或多或少有以下设置.在 ~/path/to/my/packages 我有两个包 package1.mpackage2.m.例如,每个包的大纲如下:

I have more or less the following setting. In ~/path/to/my/packages I have two packages package1.m and package2.m. Each package's outline is, for example, the following:

BeginPackage["package1`"]
Unprotect@@Names["package1`*"];
ClearAll@@Names["package1`*"];

Begin["`Private`"]

vecNorm[vec_?VectorQ]:=Module[{},Return[Sqrt[vec.vec]]];

End[]
Protect@@Names["package1`*"];
EndPackage[]

现在,我的问题是我想在 package2.m 中使用 package1.m 中定义的 vecNorm.如何从 package2 中(安全地)加载 package1?

Now, my problem is that I want to use vecNorm defined in package1.m in package2.m. How can I load (safely) package1 from within package2?

目前,我手动加载两个包如下:

At the moment, I load manually both packages as follows:

SetDirectory[StringJoin[NotebookDirectory[], "packages"]];
Needs["package1`"]
Needs["package2`"]

来自保存在 ~/path/to/my 中的笔记本.我想手动加载 only package2 反过来会自动和安全地加载 package1.一般来说,我想要一个解决方案,它尽可能少地改变 mathematica 的路径等.实现这一目标的最佳做法是什么?

from a notebook saved in ~/path/to/my. I want to load manually only package2 which in turn will load automatically and safely package1. In general I want a solution which changes as little as possible paths etc. of mathematica. What should be the best practice to accomplish this?

PS:安全我的意思是在将来,当我定义 package3 时,它也将使用 vecNorm加载 package1 也不会发生冲突.

PS: By safely I mean that in the future, when I'll define package3 which will be using vecNorm as well and will be loading package1 as well no conflicts will happen.

推荐答案

通常推荐两种加载包的方法.一种是所谓的公开导入,在您的设置中将按照

There are two generally recommended ways to load a package. One is so-called public import, and in your setting it will be done as

BeginPackage["package2`",{"package1`"}]

(* Usage messages etc *) 

Begin["`Private`"]

(* code here *)

End[]
EndPackage[]

在这里,您在列表中指明要加载的包的上下文名称,这是 BeginPackage 的第二个可选参数.这种导入方式称为 public,因为加载的包将在您的主包加载后保留在 $ContextPath 上,因此将公开可用.

Here, you indicate the context name of the package you want to load, in the list which is a second optional argument to BeginPackage. This way of importing is called public because the loaded package will remain on the $ContextPath after your main package is loaded, and will thus be publicly available.

第二种方法称为私有导入,示意如下

The second method is called private import, and is schematically done as

BeginPackage["package2`"]

(* Usage messages etc *) 

Begin["`Private`"]
Needs["package1`"]

(* code here *)

End[]
EndPackage[]

在这种方法中,您加载的第二个包将仅对加载它的包可用(使用 Needs),因此是私有导入.

In this method, your loaded second package will only be available to the package that loads it (with Needs), thus private import.

您需要哪种方式取决于具体情况.我尝试将所有导入设为私有,除非我必须将它们设为公开.但是,对于调试,首先进行公共导入可能会很方便,因为这样您就可以直接在顶层使用第二个包.

Which way you need will depend on the situation. I try to make all my imports private unless I have to make them public. For debugging, however, it may be handy to first make a public import, since then you can play with the second package directly at the top-level.

至于安全性,您可以通过任意数量的包加载一个包,这将是安全的.当您同时将多个包加载到同一上下文中时,只要这些包没有具有相同短名称的公共符号,这将是安全的.否则,您会遇到所谓的遮蔽问题,但最好做出必要的努力来避免这种情况(这总是可能的).

As for the safety, you can load a package by any number of packages, and this will be safe. When you load several packages into the same context simultaneously, this will be safe as long as those packages don't have public symbols with the same short name. Otherwise, you will run into what is called a shadowing problem, but it is best to make the effort needed to avoid that (it is always possible).

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

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