Re: Functions of Arrays

*To*: mathgroup at smc.vnet.net*Subject*: [mg102643] Re: [mg102590] Functions of Arrays*From*: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>*Date*: Mon, 17 Aug 2009 04:07:04 -0400 (EDT)*References*: <200908161039.GAA01145@smc.vnet.net>

Hi, I'm not exactly sure whether I understood you right but say you want a function which calculates f[x1,x2,...] -> x1^2+x2^2+... and you don't know how many xs you will put into the function, then you could (beside many other *easier* possibilities) do something like f[xs__] := Module[{x}, Sum[x^2, {x, {xs}}]]; f[x1, x2] f[x[1], x[2], x[3]] f[a, b, c, d] with the BlankSequence (__) you collect all given parameters into a sequence of values. But why don't you give the list as a parameter? Then you would have just one parameter and you can handle lists of any length. g1[l_List] := Sum[v^2, {v, l}] or shorter g2[l_List] := l.l and call it with g1[{a, b, c, d}] g2[{a, b, c, d}] Cheers Patrick On Sun, 2009-08-16 at 06:39 -0400, zak wrote: > Functions can be defined via > > f[x1_, x2_] := x1^2 + x2^2 > > in Mathematica. > > > But how can I manage it to define a function depending on an array? > I would like to do something like > > Array[x,2] > f[x[1]_,x[2]_]:=x[1]^2+x[2]^2 > > because the length of the array varies in my application. >

**References**:**Functions of Arrays***From:*zak <u.gotzes@googlemail.com>