Re: Re: Derivative of function with indexed variables
- To: mathgroup at smc.vnet.net
- Subject: [mg84026] Re: [mg83988] Re: Derivative of function with indexed variables
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Fri, 7 Dec 2007 03:02:45 -0500 (EST)
- References: <33430946.1196931596825.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
D[expr, y[i]] is zero here for several reasons:
1) y[i] is no specific symbol, since i is undefined.
2) expr is no specific Sum, since n, y, and p are undefined. (Especially
n.)
3) The i in y[i] doesn't have the same scope (isn't the same variable) as
the i in expr.
The last is especially important.
0
This is fairly general, but not too general to "work":
Clear[num, den, expr]
n = 5;
num[i_] = Exp[-(y[i] - y[j])^2];
den[y_] = Sum[Exp[-(y[k] - y[h])^2], {h, k + 1, n}, {k, 1, n}];
expr[y_, p_] = Sum[p[i, j] Log[p[i, j] den[y]/num[i]], {j, 1, n}];
dExpr[i_] := D[expr[y, p], y[i]]
dExpr[3]
2 p[3, 1] (-y[1] + y[3]) + 2 p[3, 2] (-y[2] + y[3]) +
2 p[3, 4] (y[3] - y[4]) + 2 p[3, 5] (y[3] - y[5])
Bobby
On Thu, 06 Dec 2007 01:44:18 -0600, Laurens van der Maaten
<l.vandermaaten at micc.unimaas.nl> wrote:
> Here is the same in proper Mathematica-style:
>
> num = Exp[- (y[i] - y[j])^2]
> den = Sum[Sum[Exp[-(y[k] - y[h])^2], {h, k + 1, n}], {k, 1, n}]
> expr = Sum[p[i, j]Log[p[i, j]/(num / den)], {j, 1, n}]
> D[expr, y[i]]
>
> The resulting derivative is incorrect, because Mathematica does not seem
> to notice that y[k] and y[h] are sometimes equal to y[i] (over which we
> compute the derivative). Any ideas?
>
>
--
DrMajorBob at bigfoot.com