I have an expression of the form

expr = $(A\cdot B)\;(C\cdot D)\;(F\cdot G)$

where $A, B,C, D, F, G$ are all vectors. I would like to use Einstein summation convention for each dot product and then simplify the expression. I attempt by writing a rule

rule = Dot[x_,y_]-> Times[Superscript[x,i],Superscript[y,i]]

But when I replace this rule in the expr all the dot products transform with similar indices. How can I achieve the following?

$(A^iB^i)\;(C^jD^j)\;(F^\ell G^\ell)$


2 Answers 2


Define a set of indices


and an indexpointer


Now you can replace series of different dot products

 rule = {ipt = 1; 
    (a_Symbol . b_Symbol) :> 
    (Subscript[a, ijk[[#]]]*Subscript[b, ijk[[#]]] &)[ipt++]}

For more than a small number of products there is no way to avoid subscripted indices.

Feynman, confronted with possibly infinite many indices simply used the integers discarding the superflous $i$ in

$$ a\cdot b * c\cdot d = a_1 b_1 c_2 d_2 $$

instead of

$$a_{i_1} b_{i_1} c_{i_2} d{i_2} \dots $$

  • $\begingroup$ Thank you for the answer. Your suggestion works. However for long expressions it seems that the indices from {i to q } will be run out and produce an error. How to modify it to include $(𝐴1⋅𝐡1)(𝐢1⋅𝐷1)(𝐹1⋅𝐺1) + (𝐴2⋅𝐡2)(𝐢2⋅𝐷2)(𝐹2⋅𝐺2)$ ? Or how to modify you suggestion such that after each Plus to start with again the same index "i"? $\endgroup$
    – Harken
    Oct 13, 2023 at 15:53
  • $\begingroup$ Generally you have to use different indices in all kinds of products. over sums the set may be identical for each . Another approach is using Unique (a_Symbol . b_Symbol) :> (Subscript[a, #]*Subscript[b, #] &)[Unique[j]]} $\endgroup$
    – Roland F
    Oct 13, 2023 at 22:38

By introducing a counter "c" into the rule you can achieve different indices. (Note you did not use "Dot" in your expression" but character 22C5D):

expr = (A . B) (C . D) (F . G)
c = 0;
rule := Dot[x_, y_] :> Times[Superscript[x, Symbol["i" <> ToString[++c]]], Superscript[y, Symbol["i" <> ToString[c]]]]
expr /. rule

enter image description here


Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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