Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wolfram mathematica setdelayed内部操纵会启动一个计算循环。为什么?_Wolfram Mathematica - Fatal编程技术网

Wolfram mathematica setdelayed内部操纵会启动一个计算循环。为什么?

Wolfram mathematica setdelayed内部操纵会启动一个计算循环。为什么?,wolfram-mathematica,Wolfram Mathematica,为什么会这样: Manipulate[test[a_] := 2*b; test[c], {b, 0, 1}, {c, 0, 1}] 变成一个评估循环? 不应该只在b或c更改时才对Operation进行评估吗?是的,Operation将在b或c更改时重新评估,但如果test更改,并且每次这些值中的任何一个更改时都会重新分配test。因此,无休止的重新评估循环 通常,应避免在结构的显示表达式中出现副作用,如操纵和动态,以避免计算循环、竞争条件和其他意外行为。在本例中,我建议删除test中对b的隐

为什么会这样:

Manipulate[test[a_] := 2*b; test[c], {b, 0, 1}, {c, 0, 1}]
变成一个评估循环?
不应该只在
b
c
更改时才对
Operation
进行评估吗?

是的,
Operation
将在
b
c
更改时重新评估,但如果
test
更改,并且每次这些值中的任何一个更改时都会重新分配
test
。因此,无休止的重新评估循环

通常,应避免在结构的显示表达式中出现副作用,如
操纵
动态
,以避免计算循环、竞争条件和其他意外行为。在本例中,我建议删除
test
中对
b
的隐式依赖,并将其定义提升到
操作
之外:

test[b_, c_] := 2*b; Manipulate[test[b, c], {b, 0, 1}, {c, 0, 1}]

在实际的应用程序中,这种简单的重构可能会遇到障碍,但关键是要从动态表达式中删除
:=

要以最小的更改解决问题,请执行以下操作

Manipulate[
test[a_] := 2*b;
test[c], {b, 0, 1}, {c, 0, 1},
TrackedSymbols \[Rule] {b, c}]
相反(即,添加
TrackedSymbols
,告诉Mathematica要跟踪哪些更改)