如何在 Mathematica 中拥有变量参数列表
现在,我有代码,其中某些函数 func
当我在其定义中给出特定参数时以我想要的方式执行(因此我将其设为 func[x1_,x2_]:=.. .
然后我将其设置为 func[x1_,x2_,x3_]:=...
而不更改其他任何内容,并且它按照我希望的方式工作)。有没有办法自动替换我为此函数指定的任何参数?
更新:
我还没有隔离问题代码,但是这里的代码没有达到我想要的效果:
(* Clear all stuff each time before running, just to be safe! *)
\
Clear["Global`*"]
data = {{238.2, 0.049}, {246.8, 0.055}, {255.8, 0.059}, {267.5,
0.063}, {280.5, 0.063}, {294.3, 0.066}, {307.7, 0.069}, {318.2,
0.069}};
errors = {{x1, 0.004}, {x2, 0.005}};
getX[x1_, x2_] := 1/x2^2
getY[x__] =
Evaluate[Simplify[
Sqrt[Sum[(D[getX[x], errors[[i]][[1]]] errors[[i]][[2]])^2, {i,
Length[errors]}]]]]
map[action_, list_] := action @@@ list
y = map[getY, data];
y
getY[2, 3]
这里的代码可以:(给出{67.9989, 48.0841, 38.9524, 31.994, 31.994 , 27.8265, 24.3525, 24.3525}
为y)
(* Clear all stuff each time before running, just to be safe! *) \ Clear["Global`*"]
data = {{238.2, 0.049}, {246.8,
0.055}, {255.8, 0.059}, {267.5,
0.063}, {280.5, 0.063}, {294.3, 0.066}, {307.7, 0.069}, {318.2,
0.069}}; errors = {{x2, 0.004}, {x1, 0.005}};
getX[x1_, x2_] := 1/x2^2
getY[x1_, x2_] := Evaluate[Simplify[ Sqrt[Sum[(D[getX[x1, x2], errors[[i]][[1]]]
errors[[i]][[2]])^2, {i, Length[errors]}]]]]
map[action_, list_] := action @@@ list
y = map[getY, data]; y
getY[2, 3]
更新 2:
我的数学:
我打算取 getX
函数的所有偏导数的平方和的平方根。因此是 getY
函数的主体。然后我想针对 x1
和 x2
的不同值计算该表达式。因此我有了 getY
的参数。
Right now I have code where some function func
executes the way I want it to when I give it specific arguments in its definition (so I make it func[x1_,x2_]:=...
and then later I make it func[x1_,x2_,x3_]:=...
without changing anything else and it works the way I would like it to). Is there a way to automatically substitute whatever arguments I specify for this function?
UPDATE:
I haven't isolated the problem code yet, but this code here does not do what I want:
(* Clear all stuff each time before running, just to be safe! *)
\
Clear["Global`*"]
data = {{238.2, 0.049}, {246.8, 0.055}, {255.8, 0.059}, {267.5,
0.063}, {280.5, 0.063}, {294.3, 0.066}, {307.7, 0.069}, {318.2,
0.069}};
errors = {{x1, 0.004}, {x2, 0.005}};
getX[x1_, x2_] := 1/x2^2
getY[x__] =
Evaluate[Simplify[
Sqrt[Sum[(D[getX[x], errors[[i]][[1]]] errors[[i]][[2]])^2, {i,
Length[errors]}]]]]
map[action_, list_] := action @@@ list
y = map[getY, data];
y
getY[2, 3]
This code here does: (gives {67.9989, 48.0841, 38.9524, 31.994, 31.994, 27.8265, 24.3525, 24.3525}
for y)
(* Clear all stuff each time before running, just to be safe! *) \ Clear["Global`*"]
data = {{238.2, 0.049}, {246.8,
0.055}, {255.8, 0.059}, {267.5,
0.063}, {280.5, 0.063}, {294.3, 0.066}, {307.7, 0.069}, {318.2,
0.069}}; errors = {{x2, 0.004}, {x1, 0.005}};
getX[x1_, x2_] := 1/x2^2
getY[x1_, x2_] := Evaluate[Simplify[ Sqrt[Sum[(D[getX[x1, x2], errors[[i]][[1]]]
errors[[i]][[2]])^2, {i, Length[errors]}]]]]
map[action_, list_] := action @@@ list
y = map[getY, data]; y
getY[2, 3]
UPDATE 2:
My math:
I intend to take the square root of the sum of the squares of all the partial derivatives of the getX
function. Thus the body of the getY
function. Then I want to evaluate that expression for different values of x1
and x2
. Thus I have the arguments for getY
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
__
,例如Use
__
, e.g.问题是,在第一个版本中,由于参数数量明确,您已使用 Evaluate 来计算右侧。当参数数量可变时,您不能执行此操作,因为评估器不知道要使用
getX
的哪个签名。因此,解决方案是将
getY
替换为以下内容:这将首先使用
errors
列表中的变量,其数量与您在getY
的参数中提供的变量完全相同。 code>,以符号方式计算导数,然后执行Dot
,而不是更快的Sum
。那么输出将是相同的。请注意,在代码的两个版本中,
errors
具有不同的值。或者,您可以像这样使用 Derivative:
使用它会产生相同的结果。
Well the issue is that in the first version, with explicit number of arguments, you have used Evaluate to evaluate the right hand side. You can not do this when the number of arguments is variable, because evaluator does not know which signature of
getX
to use.So the solution is to replace
getY
with the following:This would first use variables from
errors
list exactly as many as you have supplied in the arguments ofgetY
, compute the derivative symbolically, and then perform theDot
, instead ofSum
which is faster. Then the outputs will be the same.Notice that in your two versions of the code,
errors
have different values.Alternatively, you can use
Derivative
like so:Using it gives the same result.