Suppose I have a function that depends on variables $x_1, ... , x_n$. For concreteness I may have $f(x) = v \cdot x$, with $v$ a certain vector.
Now I want to do certain operation with this function, for example to compute its gradient.
I would like to write a mathematica code that works for any n. Thus, I cannot write something like
f[x1_,...,xn_]:= (any clever way to write v . x);
because that would not work for another n: I have to manually write the new simbols x1,...,xn'.
Also, the following would not work:
f[x_]:= v . x;
because then I would not be able to use Derivative, as f is now a function of a single argument (a list), and it is impossible to get the derivative with respect to x[[2]] say. (Right?)
What is the best design to work with this kind of problems?
ClearAll[f]; f[x__] := v.{x}? – kglr Sep 09 '17 at 12:30f[list_List]:=Total[list]. Using the multi-argument notation, this would bef[x___]:=Total[{x}]. Here I putxbetween curly braces again to make a list out of it. So it's really the same thing as a function taking a list-argument. The difference is only notation, nothing else. This should make it easier for you to think about the problem. – Szabolcs Sep 09 '17 at 12:56Dwas enhanced in V11.1 to help with this sort of problem. Take a look at this Wolfram Blog article – m_goldberg Sep 09 '17 at 18:13