# 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

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, a, a, a} and so a corresponds to your variable a, a corresponds to your b, etc.

To use this, observe that

g[{a, a, a, a}]


gives

a + (1 - a) a + (1 - a) (1 - a) a + (1 - a) (1 - a) (1 - a) a


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^2 + a^3 + a a, a]. It gives 2 a + a 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