7

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

tcapelle
  • 171
  • 1
  • 5

2 Answers2

9

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,

bill s
  • 68,936
  • 4
  • 101
  • 191
  • 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 09: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
6

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
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371