Re: Function to handle array with variable _number_ of dimensions?

*To*: mathgroup at smc.vnet.net*Subject*: [mg58544] Re: Function to handle array with variable _number_ of dimensions?*From*: "dkr" <dkrjeg at adelphia.net>*Date*: Wed, 6 Jul 2005 03:11:30 -0400 (EDT)*References*: <dad7t1$36$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Let h[k,m,{p,q}] denote the element km of the array, h[k,m,n,{p,q}] denote element kmn of the array and so forth, where the sublist simply allows the array element to depend on p and q. Let g denote the function you wish to apply to your array. In[37]:= fun[p_,q_]:= g[Table[h[Sequence@@Array[i,q],{p,q}], Evaluate[Sequence@@Table[{i[j],p},{j,q}]]]]; For example, setting p=2 and q=2 yields a 2x2 array to which g is applied: In[39]:= fun[2,2] Out[39]= g[{{h[1,1,{2,2}],h[1,2,{2,2}]},{h[2,1,{2,2}],h[2,2,{2,2}]}}] Setting p=2 and q=3 yields a 2x2x2 array (i.e., p x p x p (i.e., q factors)) to which g is applied: In[40]:= fun[2,3] Out[40]= g[{{{h[1,1,1,{2,3}],h[1,1,2,{2,3}]},{h[1,2,1,{2,3}], h[1,2,2,{2,3}]}},{{h[2,1,1,{2,3}],h[2,1,2,{2,3}]},{h[2,2,1,{2,3}], h[2,2,2,{2,3}]}}}]