开发者

How to feed a list valued variable in the Outer command in mathematica

开发者 https://www.devze.com 2023-04-04 18:01 出处:网络
Consider the following piece of mathematica code: a := {1, 2, 3}; f[n_, a_] := Sum[a[[j]], {j, 1, n}]; Outer[f, {3}, (a)]

Consider the following piece of mathematica code:

a := {1, 2, 3};

f[n_, a_] := Sum[a[[j]], {j, 1, n}];

Outer[f, {3}, (a)]

The intention is to simply to evaluate f[3,a]. But I get the following error messages:

During evaluation of In[16]:= Part::partd: Part specification 1[[1]] is longer 
 than depth of object. >>

During evaluation of In[16]:= Part::partd: Part specification 1[[2]] is longer 
 than depth of object. >>

During evaluation of In[16]:= Part::partd: Part specification 1[[3]] is longer 
 than depth of object. >>

During evalua开发者_JAVA技巧tion of In[16]:= General::stop: Further output of Part::partd will 
be suppressed during this calculation. >>

Out[16]= {{1[[1]] + 1[[2]] + 1[[3]], 2[[1]] + 2[[2]] + 2[[3]], 
   3[[1]] + 3[[2]] + 3[[3]]}}

So apparently Outer takes the list variable input a apart and treat its components separately.

My question is, how can I bundle the components in a together in the Outer environment? Many thanks!


You can do this:

Outer[f, {3}, {a}, 1, 1]

(*    {{6}}   *)

Depending on the real problem you are solving, there may be more superior ways (w.r.t. Outer), using Map or similar.

0

精彩评论

暂无评论...
验证码 换一张
取 消