为上下文路径创建别名?

发布于 2024-10-06 04:42:21 字数 1510 浏览 2 评论 0原文

我希望能够通过 s'Graphc'Graph 来引用 System'GraphCombinatorica'Graph (以及与 Combinatorica 冲突的其他函数)有办法做到这一点吗?

按照西蒙的想法,以下似乎可以工作

{Set @@ {ToExpression["c" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["Combinatorica`*"];
{Set @@ {ToExpression["s" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["System`*"];

现在cCompleteGraph[5]sCompleteGraph[5]返回CombinatoricaSystem 图表分别

1 月 8 日更新 为了供将来参考,这是我最终用来将 GraphUtilities、Combinatorica 和内置图形功能一起使用的方法。它通过将所有 Combinatorica 函数(例如 Graph)重新映射到 cGraph 来解决冲突,并更改 $Post 以在每次评估时从 ContextPath 中删除 GraphUtilities 和 Combinatorica,这是必要的,因为GraphUtilities'ToCombinatoricaGraph 在每次调用时将 Combinatorica 添加到 $ContextPath 中。

总而言之,在每个会话开始时执行以下代码。 Combinatorica func 现在是 cfunc,GraphUtilities func 是 GraphUtilities'func,内置 func 只是 func

Needs["Combinatorica`"];
combNames = Names["Combinatorica`*"];
{Set @@ {ToExpression["c" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["Combinatorica`*"];
Needs["GraphUtilities`"];
$ContextPath = DeleteCases[$ContextPath, "Combinatorica`"];
$Post = ($ContextPath = 
     DeleteCases[$ContextPath, 
      "Combinatorica`" | "GraphUtilities`"]; #) &;

I'd like to be able to do s'Graph and c'Graph to refer to System'Graph and Combinatorica'Graph (and other functions conflicting with Combinatorica) is there a way to do this?

Following Simon's idea, the following seems to work

{Set @@ {ToExpression["c" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["Combinatorica`*"];
{Set @@ {ToExpression["s" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["System`*"];

Now cCompleteGraph[5] and sCompleteGraph[5] return Combinatorica and System graphs respectively

Update Jan 8th
For future reference, this is the method I ended up using to use GraphUtilities, Combinatorica and built-in graph functionality together. It resolves conflict by remapping all combinatorica functions like Graph to cGraph and changes $Post to remove GraphUtilities and Combinatorica from ContextPath on each evaluation, necessary because GraphUtilities'ToCombinatoricaGraph adds Combinatorica to $ContextPath at every call.

To summarize, execute the code below at start of each session. Combinatorica func is now cfunc, GraphUtilities func is GraphUtilities'func, and built-in func is just func

Needs["Combinatorica`"];
combNames = Names["Combinatorica`*"];
{Set @@ {ToExpression["c" <> Last[StringSplit[#, "`"]]], 
      ToExpression[#]}} & /@ Names["Combinatorica`*"];
Needs["GraphUtilities`"];
$ContextPath = DeleteCases[$ContextPath, "Combinatorica`"];
$Post = ($ContextPath = 
     DeleteCases[$ContextPath, 
      "Combinatorica`" | "GraphUtilities`"]; #) &;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2024-10-13 04:42:21

比如:

$PreRead = # /. {str_String :> 
     StringReplace[
      str, {RegularExpression["^s`(.*)"] :> "System`" <> "$1", 
       RegularExpression["^c`(.*)"] :> "Combinatorica`" <> "$1"}]} &

当然,这是在非常低的水平上工作的,所以要小心。

Something like:

$PreRead = # /. {str_String :> 
     StringReplace[
      str, {RegularExpression["^s`(.*)"] :> "System`" <> "$1", 
       RegularExpression["^c`(.*)"] :> "Combinatorica`" <> "$1"}]} &

Of course, this is working at a very low level, so take care.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文