0

I am working with a bunch of functions of the form

fn[x1_,x2_,x3_,...,xn_] := x1x2+x2x3+...+xnx1

where $n$ changes. Is there a way to create these functions efficiently, without having to explicitly define each? I'm looking for a way of declaring, say, $n=5$ and getting back

f5[x1_,x2_,x3_,x4_,x5_]:=x1x2+x2x3+x3x4+x4x5+x5x1

Perhaps they could be defining iteratively? Since fn and fn-1 only differ by a few monomials. I appreciate any helpo that I can get! Thanks!

Fortunato
  • 103
  • 1

1 Answers1

1

edited to include comment from Kuba

From the manual: "BlankSequence is a pattern object that can stand for any sequence of one or more Mathematica expressions."

f[x__] := (Print["The number of parameters passed is: ", Length[{x}]]; {x}.RotateLeft[{x}])
f[a1, a2, a3]
(*The number of parameters passed is: 3*)
(*a1 a2 + a2 a3*)

Note that you must give a head to that sequence of parameters. In the example above, the calculation of the length of the parameters is not just Length[x] but Length[{x}]. The function you requested is:

f[x__] := {x}.RotateLeft[{x}]
Hector
  • 6,428
  • 15
  • 34