3
$\begingroup$

I want to symbolically represent a function $p: \mathbb R^n \to \mathbb R^n$, where the eventual goal is to compute an exact partial derivative. The function in question is given by $$ p_i(z) = \frac{\exp(z_i)}{\sum\limits_{j=1}^n \exp(z_j)}. $$ I tried representing the function in Mathematica as

p[z_] := Normalize[Map[Exp,z],Norm[#,1]&]

However, then I get:

In[3]:= p[a]

Out[3]= a/Norm[a, 1]

That is, symbolically, Mathematica has forgotten the Exp. Can I fix this?

$\endgroup$
  • $\begingroup$ Replace Map[Exp, z] with Exp[z] ? $\endgroup$ – kglr Jan 23 '18 at 14:08
  • $\begingroup$ @kglr, but I don't mean $\exp(z)$; I mean the vector $(\exp(z_i))_i$. I want to treat $z$ as a vector. $\endgroup$ – Mees de Vries Jan 23 '18 at 14:37
  • 3
    $\begingroup$ Mees, Exp threads over lists; that is Exp[{z1, z2, z3}] is {E^z1, E^z2, E^z3} $\endgroup$ – kglr Jan 23 '18 at 15:03
  • $\begingroup$ you can just do p[z_List] := Exp[z]/Total[Exp[z]], then for example p[z] remains symbolic until you give it an actual vector argument. $\endgroup$ – george2079 Jan 23 '18 at 19:01
1
$\begingroup$

Mathematica now has some limited support for differentiation with respect to indexed components inside of sums. It still needs a little help, but I think the following does what you want. First a definition of your $p$ function:

Subscript[p, i_][z_] := Exp[Subscript[z, i]]/Sum[Exp[Subscript[z, j]], {j, n}]

Differentiation of indexed components outside of sums doesn't work, so we need to teach this to Mathematica:

SetOptions[D, NonConstants->{Subscript}];
Subscript /: D[Subscript[z, i_], Subscript[z, j_], NonConstants->{Subscript}] := KroneckerDelta[i,j]

Then, differentiate:

Assuming[
    (i|k) ∈ Integers && 1<=k<=n,
    Simplify @ D[Subscript[p, i][z], Subscript[z, k]]
];
% //TeXForm

$\frac{e^{z_i} \left(\delta _{i,k} \sum _j^n e^{z_j}-e^{z_k}\right)}{\left(\sum _j^n e^{z_j}\right){}^2}$

$\endgroup$
0
$\begingroup$
ClearAll[p]
p[z_] := Normalize[Exp @ z, Norm[{#}, 1] &]

Examples:

p[a]

E^(a - Re[a])

Simplify[p[a], Element[a, Reals]]

1

p[{a, b, c}]

{E^a/Max[E^Re[a], E^Re[b], E^Re[c]],
E^b/Max[E^Re[a], E^Re[b], E^Re[c]],
E^c/Max[E^Re[a], E^Re[b], E^Re[c]]}

Simplify[p[{a, b, c}], Element[{a, b, c}, Reals]]

{E^a/Max[E^a, E^b, E^c], E^b/Max[E^a, E^b, E^c], E^c/Max[E^a, E^b, E^c]}

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