我正在寻找一种方法来定义一个模式,其中定义不能立即完全评估。
两个并发症:我不能简单地使用:=
而不是=
(可以吗?),因为参数中的某些部分需要进行评估。我也没有成功使用Hold
(使用extract-replace-trick来评估我的参数),因为这个Hold
将永远存在 - 麻烦是我的模式也使用另一个技巧,使表达式实际上并不总是返回包含Hold
的内容(永远不会在我明确地写下我的模式时,只有当其他一些函数将数字放入其中时),所以我的ReleaseHold
找不到保持并消失有效。
我认为只持有一次并且没有把头部Hold
放在那里的东西会拯救我。否则,ReleaseHold
不会消失,但会放置一个留在那里的反保持头也会起作用,但不会那么好。
所以这是我的代码,它只是试图解决n个参数的n个方程,但包含数值积分的eqns:
Do[
(*make an array eq[k] of patterns that do nothing
if they dont get specific numbers in a list ak_ and otherwise do NIntegrate *)
eq[k] [ak_? (Function[list, And @@ NumericQ /@ list]) ] =
(
ReplacePart[#1, #2 -> Extract[#1, #2]] & [
(*NIntegrate needs to be Holded, but the Do-loop-k must be plugged in inside
its argument, so extract it and put it back*)
Hold[ NIntegrate[complicated functions for each k with appearance of ak[[k]]), {t, 0, l},
MaxRecursion -> 50] ]
, { 1, 1}
]
)
, {k, n}]
(*test*)
ReleaseHold[eq[3][{2,4,3,5,2}]] (*gives a number, great! (n=5 here) *)
eqns = Table[ReleaseHold[eq[i][ Table[a[j], {j, n}]]], {i, n}] (*does not give an error since pattern is not evaluated, great!*)
vars = Table[{a[i],0},{i,n}]
FindRoot[eqns,vars]
(*does not work since there is still
the Hold appearing now, ReleaseHold above was useless
since pattern was not evaluated and Hold not appearing*)