在 Mathematica 中以编程方式创建多元函数

发布于 2024-10-02 04:36:44 字数 562 浏览 2 评论 0原文

这是与之前问题的讨论分开的。

假设我需要定义一个函数 f 来检查给定的图形标签是否是正确的着色。换句话说,我们为每个节点分配了一个整数,并且没有两个相邻节点得到相同的答案。例如,对于 {"Path",3},f[{1,2,3}] 返回 True,f[{1,1,2}] 返回 False。我将如何为任意图创建这样的函数?

以下内容基本上满足了我的需要,但会生成部分警告。

g[edges_] := Function @@ {{x}, And @@ (x[[First[#]]] != x[[Last[#]]] & /@ edges)}
f = g[GraphData[{"Path", 3}, "EdgeIndices"]];
f[{1, 2, 1}]==False

这是我经常遇到的一个玩具实例问题——我需要以编程方式创建一个多元函数 f,并最终得到以下任一结果:1) 部分警告 2) 推迟对 g 的求值,直到对 f 求值

This is a split from discussion on earlier question.

Suppose I need to define a function f which checks if given labeling of a graph is a proper coloring. In other words, we have an integer assigned to every node and no two adjacent nodes get the same answer. For instance, for {"Path",3}, f[{1,2,3}] returns True and f[{1,1,2}] returns False. How would I go about creating such a function for arbitrary graph?

The following does essentially what I need, but generates Part warnings.

g[edges_] := Function @@ {{x}, And @@ (x[[First[#]]] != x[[Last[#]]] & /@ edges)}
f = g[GraphData[{"Path", 3}, "EdgeIndices"]];
f[{1, 2, 1}]==False

This is a toy instance problem I regularly come across -- I need to programmatically create a multivariate function f, and end up with either 1) part warning 2) deferring evaluation of g until evaluation of f

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

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

发布评论

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

评论(3

远山浅 2024-10-09 04:36:44

这是一些东西。当其他方法都不起作用时,Hold 和规则通常可以完成工作。我不确定它会针对您的图形着色问题产生正确的结果,但希望能为您提供一个起点。我最终使用 Slot 而不是命名变量,因为存在一些范围问题(也存在于我之前的建议中,x$x)当我使用命名变量时,我没有花时间尝试解决它。

g[edges_] := 
 With[{ors = (Hold @@ edges) /. {a_, b_} :> #[[a]] == #[[b]]},
  Function[!ors] /. Hold -> Or
  ]

In[90]:= f = g[GraphData[{"Path", 3}, "EdgeIndices"]]
Out[90]= !(#1[[1]] == #1[[2]] || #1[[2]] == #1[[3]]) &

In[91]:= f[{1, 2, 3}]
Out[91]= True

In[92]:= f[{1, 1, 2}]
Out[92]= False

我觉得它缺乏典型的 Mathematica 优雅,但它确实有效。如果我受到更美丽的东西的启发,我会更新。

Here's something. When nothing else is working, Hold and rules can usually get the job done. I'm not sure it produces the correct results w.r.t. your graph-coloring question but hopefully gives you a starting place. I ended up using Slot instead of named variable because there were some scoping issues (also present in my previous suggestion, x$ vs. x) when I used a named variable that I didn't spend the time trying to work around.

g[edges_] := 
 With[{ors = (Hold @@ edges) /. {a_, b_} :> #[[a]] == #[[b]]},
  Function[!ors] /. Hold -> Or
  ]

In[90]:= f = g[GraphData[{"Path", 3}, "EdgeIndices"]]
Out[90]= !(#1[[1]] == #1[[2]] || #1[[2]] == #1[[3]]) &

In[91]:= f[{1, 2, 3}]
Out[91]= True

In[92]:= f[{1, 1, 2}]
Out[92]= False

I feel like it lacks typical Mathematica elegance, but it works. I'll update if I'm inspired with something more beautiful.

养猫人 2024-10-09 04:36:44

对于此类问题,还有其他一些解决方案,它们不需要您使用 HoldReleaseHold,而是依赖于 Function code> 已经具有 HoldAll 属性。您首先使用 Block 在本地“擦除”Part 的定义,以便可以安全地构造您感兴趣的表达式,然后使用 With 将其插值到一个 Function 中,然后可以安全地返回到 Block 之外,并且还利用了 Slot 并不真正存在的事实表示函数之外的任何内容。

使用您的示例:

coloringChecker[edges_List] := 
 Block[{Part},
  With[{body = And @@ Table[#[[First@edge]] != #[[Last@edge]], {edge, edges}]},
   body &]]

我不知道这是否比使用 Hold 麻烦得多,但它是不同的。

There are a couple other solutions to this kind of problem which don't require you to use Hold or ReleaseHold, but instead rely on the fact that Function already has the HoldAll attribute. You first locally "erase" the definitions of Part using Block, so the expression you're interested in can be safely constructed, and then uses With to interpolate that into a Function which can then be safely returned outside of the Block, and also uses the fact that Slot doesn't really mean anything outside of Function.

Using your example:

coloringChecker[edges_List] := 
 Block[{Part},
  With[{body = And @@ Table[#[[First@edge]] != #[[Last@edge]], {edge, edges}]},
   body &]]

I don't know if this is all that much less cumbersome than using Hold, but it is different.

浮云落日 2024-10-09 04:36:44

我仍然对你所遇到的困难感到困惑。这是一个检查列表中没有两个连续元素相同的函数:

f[l_List] := Length[Split[l]] == Length[l]

Part 没有问题,到目前为止我尝试过的简单示例(包括 OP 的“测试”用例)没有错误消息。我还认为,这比迄今为止看到的其他方法更简洁、更具可读性。

I'm still puzzled by the difficulties that you seem to be having. Here's a function which checks that no 2 consecutive elements in a list are identical:

f[l_List] := Length[Split[l]] == Length[l]

No trouble with Part, no error messages for the simple examples I've tried so far including OP's 'test' cases. I also contend that this is more concise and more readable than either of the other approaches seen so far.

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