Given the function \begin{align*} f \colon \mathbb{R}^n &\to \mathbb{R}^n\\ v&\mapsto \dfrac{v}{\|v\|}, \end{align*} I would like to compute the derivative of $f$, that is $df(v)$. It is possible to derive it by hand, which leads to

$$df(v)=\dfrac{1}{\| v\|}\Big(I_n - \dfrac{v}{\|v\|}\otimes \dfrac{v}{\|v\|}\Big)$$

where $I_n$ is the identity second-order matrix.

I believe Mathematica cannot find that using simple built-in functions (without explicitly defining v = {v1, v2, v3} if $n=3$ for instance). Some packages are dedicated to differential geometry (see Coordinate free differential forms package or Differential geometry add-ons for Mathematica) but I failed to achieve the above calculation. Any hint would be appreciated.

Edit For those of you who are interesting in how to find the above formula, you can define $g(t)=f(v(t))=\big(v(t)\cdot v(t)\big)^{1/2}v(t)$ and compute $g'(t)$ with the chain rule. $g'(t)$ is a linear function of $v'(t)$ because:

$$g'(t)=\dfrac{df}{dv}(v(t)) v'(t)$$

Taking the coefficient in front of $v'(t)$ gives the above expression.

Now, the naive implementation of this approach as follows fail because it does not capture the multi-dimensionality of the f:

f[v_] = v/Norm[v]
h[t_] = D[f[v[t]], t]/v'[t] // Simplify
h[t] /. Norm'[v[t]] -> v[t]/Norm[v[t]] // Simplify
(* (Norm[v[t]]^2 - v[t]^2)/Norm[v[t]]^3 *)
  • $\begingroup$ What is I_3? Can you clarify this? $\endgroup$ – CA Trevillian Sep 11 '20 at 12:57
  • 3
    $\begingroup$ @CATrevillian I believe it's IdentityMatrix[3] $\endgroup$ – chuy Sep 11 '20 at 13:50
  • 1
    $\begingroup$ @CATrevillian chuy is right, I clarified. $\endgroup$ – anderstood Sep 11 '20 at 19:55
  • $\begingroup$ @anderstood so your v, here for instance, is dependent upon t, and presumably v[t] is an n-dimensional vector? It will be interesting to see what solutions come about from this, as I’d expect somehow to either have to give the dimensionality, or be able to determine it from the inputs. $\endgroup$ – CA Trevillian Sep 11 '20 at 20:16
  • $\begingroup$ @CATrevillian v does not necessarily depend on t but that's a "trick" I used to derive the expression of $df(v)$ (the trick is to introduce artificially t and remove it afterwards). But that's right, v[t] would be a vector such as {v1[t], v2[t], ..., vn[t]}. $\endgroup$ – anderstood Sep 11 '20 at 21:46

A solution using FeynCalc would be to write

ex = CVD[v, i]/Sqrt[CSPD[v, v]]

which corresponds to $ \frac{v^i}{\sqrt{v^2}} $ (CVD denotes a $D-1$ dimensional Cartesian vector, CSPD is a Cartesian scalar product in $D-1$ dimensions). Then using the routine ThreeDivergence ($\nabla^j$)

ThreeDivergence[ex, CVD[v, j]]

we find $ \frac{\delta ^{i j}}{\sqrt{v^2}}-\frac{v^i v^j}{\left(v^2\right)^{3/2}}. $

Of course, FeynCalc is not a tool for doing differential geometry. The tensor routines only cover what one usually needs in Feynman diagram calculations. So I suppose that for more serious tasks the OP would still need to become familiar with dedicated tensor algebra packages.

  • 1
    $\begingroup$ Would you know how to do that with xTensor for example? $\endgroup$ – anderstood Sep 15 '20 at 11:34
  • $\begingroup$ Sorry, so far I never needed to use xTensor. For what I do for living, FeynCalc and FORM have always been sufficient. $\endgroup$ – vsht Sep 17 '20 at 21:39

You can abuse the variational derivative functionality in xTensor to do this:

<< xAct`xTensor`
DefManifold[M, dim, IndexRange[a, m]];
DefMetric[1, metric[-a, -b], PD, PrintAs -> "\[Delta]", 
  FlatMetric -> True, SymbolOfCovD -> {",", "\[PartialD]"}];

DefTensor[v[a], M]

The function ff here is a stand-in for "the inverse of the norm". It has to be typed as a scalar function of some scalar argument(s), so xTensor knows how to take its derivative. (Trying to do this directly using Sqrt throws errors; I'm not sure why.)

VarD[v[c], PD][v[a] ff[v[b] v[-b]]] // ScreenDollarIndices // ContractMetric
% /. ff -> (#^(-1/2) &)

enter image description here

Once the variational derivative is taken, you can then set ff to any function you please, including (in this case) the -1/2 power function.

Note that the dimensionality of the manifold dim remains unspecified in this code. It might be necessary to specify it in some related cases (for simplification or for calculating traces of quantities), but it doesn't appear to be needed here.


Maybe you could use the following approach:


VectorD[e_, v_] := ReplaceAll[
    D[e, VectorD, NonConstants->{v}],
    s_Dot:>TensorReduce[s,Assumptions->v ∈ Vectors[d]]

VectorD /: D[s_. v_,VectorD,NonConstants->{v_}] := s IdentityMatrix[d] + 
    TensorProduct[v, D[s, VectorD, NonConstants->{v}]]
VectorD /: D[Transpose[f_], VectorD, NonConstants->{x_}] := Transpose[
    D[f, VectorD, NonConstants->{x}]
VectorD /: D[a_Dot|a_Times|a_TensorProduct, VectorD, NonConstants->{x_}] := Sum[
    MapAt[D[#, VectorD, NonConstants->{x}]&, a, i],


VectorD[v/Sqrt[v.v], v]

IdentityMatrix[d]/Sqrt[v.v] - TensorProduct[v, v]/(v.v)^(3/2)

  • 1
    $\begingroup$ @anderstood See update $\endgroup$ – Carl Woll Sep 16 '20 at 16:00

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.