7
$\begingroup$

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

$\endgroup$
9
$\begingroup$

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,

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – tcapelle Jul 18 '13 at 9:00
  • $\begingroup$ 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. $\endgroup$ – bill s Jul 18 '13 at 12:02
6
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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