Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often need to compute derivatives or integrals involving N-dimensional vectors (where the dimension could be equal to 2 or 3 but is not particularly relevant for the sake of the derivation). The only way I know to translate this into valid Mathematica input is to specialize to a particular N and define all quantities component-wise.

A simple example: computing the time derivative of a normalized quantity

V[t_] := {x[t], y[t], z[t]}
Simplify[D[V[t]/Sqrt[Dot[V[t], V[t]]], t]]

Time derivative of a normalized quantity

While correct, this can obviously get extremely messy fast. At that point, I find it easier to just do the computation by hand using generic vector calculus identities which work for vectors of any dimension.

I was wondering if there is a way of doing such computations using an abstract vector type? This post references a Vectors command that is potentially related, but it was not clear to me how to use it for calculus.

Edit: After some more experimentation with Vectors, I was able to get quite close to what I was looking for:

$Assumptions = V[t] \[Element] Vectors[3, Reals];
Simplify[D[V[t]/Sqrt[Dot[V[t], V[t]]], t]]

$$\frac{2 V(t).V(t) V'(t)-V(t) \left(V(t).V'(t)+V'(t).V(t)\right)}{2 (V(t).V(t))^{3/2}}$$

However, neither Simplify[] nor TensorReduce[] could simplify the commutative dot product in the expression above.

share|improve this question
There may be different levels of “abstract” in “abstract vectors”. I'd say they are not “abstract” if kept in coordinate-based form {x@t, y@t, z@t}. Things will probably always get messy unless you use coordinate-free approach, or at least abstract indices. To my knowledge, there is no Mathematica package for the latter (definitely no built-in support); as to the former, you might be interested in this: – Akater Jun 6 '14 at 20:57
Plus, post code, not images as it is easier to experiment with it. – Sektor Jun 6 '14 at 21:07
Take a look here How to declare a 3D vector variable? – Artes Jun 6 '14 at 21:13
@Artes: I have updated my question. – Wenzel Jakob Jun 6 '14 at 22:14
How about D[V[t]/Sqrt[Dot[V[t], V[t]]], t] /. Dot[a_, b_] :> Dot[b, a] /; LeafCount[a] < LeafCount[b] – chris Oct 10 '14 at 18:44
up vote 1 down vote accepted
SetAttributes[Dot, Orderless]

then try to evaluate this again

D[V[t]/Sqrt[Dot[V[t], V[t]]], t]

of course it would be better to define new function based on dot and set it attribute Orderless

share|improve this answer
that did it, thank you. I am curious about when Dot might ever be order-dependent? (it strikes me as an odd default) – Wenzel Jakob Jan 18 '15 at 20:51
the problem with dot starts if we try to fins the product of matrises. for this case we shoul define new function based on Dot but without Orderless attribute – k_v Jan 19 '15 at 5:13

It seems the simplest way to simplify such expression is to impose sorting based on CountLeaf

D[V[t]/Sqrt[Dot[V[t], V[t]]], t] /.   
     Dot[a_, b_] :>  Dot[b, a] /; LeafCount[a] < LeafCount[b]

Mathematica graphics

so that for instance the longest expression is always last. This is not bulletproof but it seems to do the trick on your example(?)

In order to deal with equal length cases you could do

(Dot[x, y] - Dot[y, x]) /. 
 Dot[a_, b_] :> Dot[b, a] /; LeafCount[a] < LeafCount[b] /. 
 Dot[a_, b_] :> Dot @@ Sort[{b, a}] /; LeafCount[a] == LeafCount[b]
share|improve this answer
Sorry for the delay. This seems fairly specific to this particular example. I was hoping for something more general to tell Mathematica that the dot product arguments commute. – Wenzel Jakob Nov 16 '14 at 15:59
The following e.g. does not work (maybe I am missing something?): (Dot[x, y] - Dot[y, x]) /. Dot[a_, b_] :> Dot[b, a] /; LeafCount[a] < LeafCount[b] – Wenzel Jakob Nov 17 '14 at 2:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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