3
$\begingroup$

Is it posible to define a multivariate function where the number of indipendent variables is not fixed?

For simulation reasons I have to change the dimensionality (and also the definition) of my function making it to be:

$f(x_0,x_1, \ldots, x_k)$ where $k$ can change.

My function is:

$$f(x_0,x_1, \ldots, x_k)= \sum_{d=0}^{d=k} x_d\,d\,u(d)$$

$\endgroup$
1
  • $\begingroup$ Yes, it is, but it sounds like to want a list as input. $\endgroup$
    – Szabolcs
    May 13, 2017 at 9:56

2 Answers 2

4
$\begingroup$

In addition to the possible solution Sumit offers, you can also define functions to take variable numbers of arguments directly:

f[xs___] := Dot[{xs},{xs}]
{f[1,2,3], f[1], f[]}

{14, 1, 0}

Usually, I turn these into a list like this so that you can use functions like Length on them:

f[xsSeq___] := With[
  {xs = {xsSeq}},
  ...]

Here are a few pieces of relevant documentation:

$\endgroup$
3
$\begingroup$

with a[n] being a predefined function

f[x_List] := Module[{n}, n = Length[x];
             Sum[1/i a[i] x[[i]], {i, n}]]

f[{x1, x2, x3}]

x1 a[1] + 1/2 x2 a[2] + 1/3 x3 a[3]

f[{q1, q2, q3, q4}]

q1 a[1] + 1/2 q2 a[2] + 1/3 q3 a[3] + 1/4 q4 a[4]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.