# Symbolic representation of vector function

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?

• Replace Map[Exp, z] with Exp[z] ?
– kglr
Jan 23, 2018 at 14:08
• @kglr, but I don't mean $\exp(z)$; I mean the vector $(\exp(z_i))_i$. I want to treat $z$ as a vector. Jan 23, 2018 at 14:37
• Mees, Exp threads over lists; that is Exp[{z1, z2, z3}] is {E^z1, E^z2, E^z3}
– kglr
Jan 23, 2018 at 15:03
• 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. Jan 23, 2018 at 19:01

## 2 Answers

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}$

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]}