Wolfram mathematica 线程未正确应用于已定义的模块
我有一个模块,用于生成函数的Wolfram mathematica 线程未正确应用于已定义的模块,wolfram-mathematica,Wolfram Mathematica,我有一个模块,用于生成函数的nth泰勒多项式: taylor[n_, a_] := Module[{terms = {f[0]}}, Do[AppendTo[ terms, (D[f[x], {x, i}] /. x -> a)/Factorial[i]*(x - a)^i], {i, 1, n}]; Return[Plus @@ terms]] 现在,我想调用这个函数,让它生成一个多项式列表。这在使用单个参数时效果很好,将其映射到范围内。但是,在尝试线程时,
n
th泰勒多项式:
taylor[n_, a_] :=
Module[{terms = {f[0]}},
Do[AppendTo[
terms, (D[f[x], {x, i}] /. x -> a)/Factorial[i]*(x - a)^i], {i, 1,
n}]; Return[Plus @@ terms]]
现在,我想调用这个函数,让它生成一个多项式列表。这在使用单个参数时效果很好,将其映射到范围内。但是,在尝试线程时,模块如下所示:
Thread[taylor[Range[7], 0]]
我得到一个错误
迭代器{i,1,{1,2,3,4,5,6,7}}没有适当的界限
i、 例如,n
不像我想象的那样被评估,比如:
{taylor[1,0], taylor[2,0], ...}
我可以更改模块的实现,但我不知道为什么Thread
的行为不符合预期 试试这个,看看它是否符合你的要求
taylor[n_, a_] := Module[{terms = {f[0]}},
Do[AppendTo[terms, (D[f[x], {x, i}] /. x -> a)/Factorial[i]*(x - a)^i], {i, 1,n}];
Return[Plus @@ terms]]
Thread[g[{1,2,3},0]]/.g->taylor
返回
{f[0] + x*f'[0],
f[0] + x*f'[0] + x^2*f''[0]/2,
f[0] + x*f'[0] + x^2*f''[0]/2 + x^3*f'''[0]/6}
它所做的是所有线程,而不调用模块计算参数的方式,一旦完成,然后将其交给模块完成。在执行此操作之前,请确保您没有定义g
在依赖它之前,请仔细检查所有这些信息。这很有趣。非常感谢你。