# Can I define a function for vectors of arbitrary dimension?

Is it possible to do analytic calculations with Mathematica? For example, I want to compute:

$$\partial \frac{\sum_{j=1}^n G_{j} \prod_{k=1}^{j-1} (1 - G_{k})}{\partial G_l}=-\prod_{k\neq l} (G_k-1)$$ Is it possible to define such analytic functions for an arbitrary parameter $n$?

Right now, I do it by hand by defining a function like this

f[a_, b_, c_, d_] = a + b (1 - a) + c (1 - a) (1 - b) + d (1 - a) (1 - b) (1 - c)


but I want to do it generally for an $n$-vector, not a 4-vector

-
Related: (21281) –  Mr.Wizard Jul 17 '13 at 14:34
Going by the title, this is a duplicate of How to create functions of arbitrary number of variables? –  Jens Jul 17 '13 at 17:25
$n$ is the upper limit of the sum over j? $l$ is also not defined. –  Sjoerd C. de Vries Jul 17 '13 at 18:34

## 2 Answers

Define a function (with thanks to Mr. Wizard for the more compact form)

g[a_] := Sum[a[[i]] Product[(1 - a[[j]]), {j, i - 1}], {i, Length[a]}]


In this function, a is a list like {a[1], a[2], a[3], a[4]} and so a[1] corresponds to your variable a, a[2] corresponds to your b, etc.

To use this, observe that

g[{a[1], a[2], a[3], a[4]}]


gives

a[1] + (1 - a[1]) a[2] + (1 - a[1]) (1 - a[2]) a[3] + (1 - a[1]) (1 - a[2]) (1 - a[3]) a[4]


which is the equivalent of your f. More generally, let

aVec = Array[a, 7];
g[aVec]


and you get the 7-term case,

-
I like your solution, i think is way cleaner than the ones that referenced from the duplicate answers. But can I compute derivatives from a vector coordinate? –  tcapelle Jul 18 '13 at 9:00
They're just symbols. Here's an example: D[a[1]^2 + a[2]^3 + a[1] a[2], a[1]]. It gives 2 a[1] + a[2] as you would expect. –  bill s Jul 18 '13 at 12:02

Here is another option that I like:

f = #.FoldList[# (1 - #2) &, 1, Most@#] &;

f[{a, b, c, d}]

a + (1 - a) b + (1 - a) (1 - b) c + (1 - a) (1 - b) (1 - c) d


If you need input in the multi-argument syntax you may write:

g = {##}.FoldList[# (1 - #2) &, 1, Most@{##}] &;

g[a, b, c, d]

a + (1 - a) b + (1 - a) (1 - b) c + (1 - a) (1 - b) (1 - c) d

-