gpt4 book ai didi

wolfram-mathematica - 从包中获取当前上下文

转载 作者:行者123 更新时间:2023-12-05 01:21:24 26 4
gpt4 key购买 nike

我的笔记本中有类似以下内容。

test1[g_] := (g == 5);
test2[g_] := (g == 6);
tests={"test1", "test2"}
ToExpression[#][5] & /@ tests

当我将此代码放入包中时,它不起作用,因为 test1 现在称为 MyPackage'Private'test1。如何修改最后一行以使此代码在包内和笔记本内运行?

更新这就是为什么我使用 ToExpression 而不是使用 Symbols 的原因。回想起来,也许改用符号更容易

我有一个函数,我称之为 getGraphs["LeafFree","Planar",!"Tree",...] 来获取所有无叶、平面而不是树的图形.其中一些字符串是 GraphData 中的类,而另一些是我自己的类。对于我自己的每个类,我都有一个名称相同的函数,例如测试属性的 LeafFree。在 notebook 中,使用上述 ToExpression 代码是实现这一点的最快方法。

getGraphs[n_Integer, cl__] := getGraphs[{n, n}, cl];
getGraphs[{nmin_Integer, nmax_Integer}, cl__] :=
Module[{maxgraphnum = 100},
customClasses = {"isLeafFree", ! "isLeafFree"};
classes = {cl}\[Backslash]customClasses;
builtinClasses =
GraphData["Classes"] \[Tilde] (Not /@ GraphData["Classes"]);
Assert[classes \[Subset] builtinClasses];
isLeafFree[gname_] :=
FreeQ[GraphData[gname, "DegreeSequence"], 0 | 1];

posClasses = Cases[classes\[Backslash]customClasses, _String];
posGroup =
If[posClasses == {}, GraphData[nmin ;; nmax],
GraphData[posClasses, nmin ;; nmax]];
negClasses = classes\[Backslash]posClasses;
negGroups = GraphData[#[[1]], nmin ;; nmax] & /@ negClasses;

result = Complement[posGroup, Sequence @@ negGroups];
customTest[g_] :=
And @@ (ToExpression[#][g] & /@ ({cl} \[Intersection]
customClasses));
(*result=Take[result,Min[Length[result],100]];*)

result = Select[result, customTest]
]

最佳答案

我同意上面的评论,您可能应该有一个令人信服的理由这样做,但这里就是这样。这是我在这种情况下使用的代码,它允许在运行时在任何你喜欢的上下文中解析你的符号:

SetAttributes[ParseTimeNameSpaceWrapper,HoldFirst];
Options[ParseTimeNameSpaceWrapper] = {
LocalizingContext->"MyLocalizingContext`",
DefaultImportedContexts:>{"Imported1`", "Imported2`"},
ExtraImportedContexts:> {}
};



ParseTimeNameSpaceWrapper[code_,opts:OptionsPattern[]]:=
Module[{result,
context = OptionValue[LocalizingContext],
defcontexts = OptionValue[DefaultImportedContexts],
extraContexts = OptionValue[ExtraImportedContexts],
allContexts},
allContexts = {Sequence@@defcontexts,Sequence@@extraContexts};
BeginPackage[context,If[allContexts==={},Sequence@@{},allContexts]];
result = code;
EndPackage[];
result
];

您可以使用选项来指定存在这些符号的一些上下文,您希望在解析阶段导入这些上下文。您可以从任何包或笔记本中调用它,符号将根据您指定的任何上下文进行解析。

HTH

编辑:

回复评论(因为它使问题更具体):毫无疑问,在运行时 Context[] 将显示调用函数的当前上下文(在这种情况下是全局的)。我的意思是别的:Context 有一个语法 Context[symbol],如果它在 $ContextPath 上,它可以给出任何符号的上下文。例如,Context[getGraphs] 返回 Bulatov'showGraphs'。因此,如果您需要自动确定某个导出函数的上下文,则调用Context[function]。您可以使用它来构造该包的其他(私有(private))函数的全名。这是一个独立的例子:

In[1]:= 
BeginPackage["MyTest`"];
f[x_, y_, context_: Context[f]] :=
Module[{f1str = "function1", f2str = "function2", f1, f2},
{f1, f2} = ToExpression[context <> "Private`" <> #] & /@ {f1str, f2str};
f1[x, y];
f2[x, y];];

Begin["`Private`"];

function1[x_, y_] := Print["In function1: arguments are ", x, " , ", y];
function2[x_, y_] := Print["In function2: arguments are ", x, " , ", y];

End[]
EndPackage[];

Out[6]= "MyTest`Private`"

In[8]:= f[1, 2]

During evaluation of In[8]:= In function1: arguments are 1 , 2

During evaluation of In[8]:= In function2: arguments are 1 , 2

其中 x,y 只是一些示例参数。然后,您实际上永远不会提供最后一个参数,但您可以在函数中使用 context 变量来为其他函数构造长名称,如上面的示例代码所示。或者你可以在函数体中直接使用 Context[f] ,而不给它添加任何参数。

关于wolfram-mathematica - 从包中获取当前上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com