在 Mathematica 中以编程方式创建多元函数
这是与之前问题的讨论分开的。
假设我需要定义一个函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一些东西。当其他方法都不起作用时,
Hold
和规则通常可以完成工作。我不确定它会针对您的图形着色问题产生正确的结果,但希望能为您提供一个起点。我最终使用Slot
而不是命名变量,因为存在一些范围问题(也存在于我之前的建议中,x$
与x
)当我使用命名变量时,我没有花时间尝试解决它。我觉得它缺乏典型的 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 usingSlot
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.I feel like it lacks typical Mathematica elegance, but it works. I'll update if I'm inspired with something more beautiful.
对于此类问题,还有其他一些解决方案,它们不需要您使用
Hold
或ReleaseHold
,而是依赖于Function
code> 已经具有HoldAll
属性。您首先使用Block
在本地“擦除”Part
的定义,以便可以安全地构造您感兴趣的表达式,然后使用With
将其插值到一个Function
中,然后可以安全地返回到Block
之外,并且还利用了Slot
并不真正存在的事实表示函数
之外的任何内容。使用您的示例:
我不知道这是否比使用
Hold
麻烦得多,但它是不同的。There are a couple other solutions to this kind of problem which don't require you to use
Hold
orReleaseHold
, but instead rely on the fact thatFunction
already has theHoldAll
attribute. You first locally "erase" the definitions ofPart
usingBlock
, so the expression you're interested in can be safely constructed, and then usesWith
to interpolate that into aFunction
which can then be safely returned outside of theBlock
, and also uses the fact thatSlot
doesn't really mean anything outside ofFunction
.Using your example:
I don't know if this is all that much less cumbersome than using
Hold
, but it is different.我仍然对你所遇到的困难感到困惑。这是一个检查列表中没有两个连续元素相同的函数:
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:
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.