# Scalar product of vectors using dummy indices

How can I rewrite the following expression

$$expr1= (A\cdot B)(C\cdot D)(E\cdot F) + (A^\prime\cdot B^\prime)(C^\prime\cdot D^\prime)(E^\prime\cdot F^\prime) +\cdots$$

in terms of the components as

$$expr2= A_iB_i\;C_jD_j\;E_\ell F_\ell + A^\prime_iB^\prime_i\;C^\prime_jD^\prime_j\;E^\prime_\ell F^\prime_\ell +\cdots$$

where $$A,A^\prime\cdots F, F^\prime$$ are 3D vectors. I start with the following rule

rule = {incr = 1; var = CharacterRange["i", "q"];
Dot[a_, b_] :> (Subscript[a, var[[#]]]*Subscript[b, var[[#]]] &)[
incr++]}


but at some point I ran out of indices. How to modify the rule such that it starts with the index $$i$$ in each some as in $$expr2$$?

• Large space algebraists use subscripted subscripts: rule = {incr = 1; Dot[a_, b_] :> (Subscript[a, Subscript[i,#] ] * Subscript[b, Subscript[i,#] ]&)[ incr++]} Oct 18, 2023 at 11:04
• You can use a rule like e.g.: a1 . b1 c1 . d1 e1 . f1 + a2 . b2 c2 . d2 e2 . f2 /. HoldPattern[a_ . b_ c_ . d_ e_ . f_ ] :> ( Subscript[a, i] Subscript[b, i] Subscript[c, j] Subscript[d, j] Subscript[e, k] Subscript[f, k])  but take care to put a space after each "_". Otherwise you will get some unwanted interpretations. Oct 18, 2023 at 19:00

If we take the requirements literally, with the number of vector groups being 4:

(# /: Format[#[i_Integer][j_]] :=
Subsuperscript[ToUpperCase[SymbolName[#]], j,
StringJoin[Table["\[Prime]", i - 1]]]) & /@ {a, b, c, d, e, f};
toComponents[v_] := Array[v, 3];

Sum[(a[j] . b[j]) (c[j] . d[j]) (e[j] . f[j]) /.
v : a | b | c | d | e | f :> (toComponents[v[#]] &), {j, 4}] //


I think the following will just give what I want:

For[
term = 1; finalexpr = 0,
term <= Length@(expr1/.Plus -> List),
incr = 1,
rule = # /. Dot[a_, b_] :>   (Superscript[a, Subscript[i, #]]*
Superscript[b, Subscript[i, #]] &)[incr++] &;
dottodummy = rule /@(expr1/.Plus -> List)[[term]];
finalexpr += dottodummy;
term++;
];

finalexpr


Now to test it let's define the input

$$(\text{A1}.\text{B1}) (\text{C1}.\text{D1}) (\text{E1}.\text{F1})+(\text{A2}.\text{B2}) (\text{C2}.\text{D2} )(\text{E2}.\text{F2})+(\text{A3}.\text{B3}) (\text{C3}.\text{D3}) (\text{E3}.\text{F3})$$

as

expr1 = Dot[A1, B1] Dot[C1, D1] Dot[E1, F1] + Dot[A2, B2] Dot[C2, D2] Dot[E2, F2] + Dot[A3, B3] Dot[C3, D3] Dot[E3, F3]


then running the code gives

finalexpr
Superscript[A1,Subscript[i, 1]] Superscript[B1,Subscript[i, 1]] Superscript[C1,Subscript[i, 2]] Superscript[D1,Subscript[i, 2]] Superscript[E1,Subscript[i, 3]] Superscript[F1,Subscript[i, 3]] +
Superscript[A2,Subscript[i, 1]] Superscript[B2,Subscript[i, 1]] Superscript[C2,Subscript[i, 2]] Superscript[D2,Subscript[i, 2]] Superscript[E2,Subscript[i, 3]] Superscript[F2,Subscript[i, 3]] +
Superscript[A3,Subscript[i, 1]] Superscript[B3,Subscript[i, 1]] Superscript[C3,Subscript[i, 2]] Superscript[D3,Subscript[i, 2]] Superscript[E3,Subscript[i, 3]] Superscript[F3,Subscript[i, 3]]


in plain text as

$$\text{A1}^{i_1} \text{B1}^{i_1} \text{C1}^{i_2} \text{D1}^{i_2} \text{E1}^{i_3} \text{F1}^{i_3}+\text{A2}^{i_1} \text{B2}^{i_1} \text{C2}^{i_2} \text{D2}^{i_2} \text{E2}^{i_3} \text{F2}^{i_3}+\text{A3}^{i_1} \text{B3}^{i_1} \text{C3}^{i_2} \text{D3}^{i_2} \text{E3}^{i_3} \text{F3}^{i_3}$$

• You could improve your answer by applying your solution to the expression given in the question.. Oct 28, 2023 at 21:28

Solution depends on what your actually want to do: to compute or just to rewrite expression in nice form. Below I define few solution using replacement rules from good to very bad style.

1. That is good for programming

dotRules = {Dot[a_, b_] :> With[{k = Unique[i]}, a[k] b[k]]};

expr1 /. dotRules

(*out1 *)

A1[i$$22465] B1[i$$22465] C1[i$$22466] D1[i$$22466] E1[i$$22467] F1[ i$$22467] +
A2[i$$22468] B2[i$$22468] C2[i$$22469] D2[i$$22469] E2[i$$22470] F2[ i$$22470]

1. That is not very bad, however intended not for further computation but mostly for representation

 dotRulesBadStyle = {Dot[a_, b_] :>
With[{k = Unique[i]}, Superscript[a, k] Superscript[b, k]]};


(*out2 *)

 Superscript[A1,i$$22473] Superscript[B1,i$$22473] Superscript[
C1,i$$22474] Superscript[D1,i$$22474] Superscript[
E1,i$$22475] Superscript[F1,i$$22475] +
Superscript[A2,i$$22476] Superscript[B2,i$$22476] Superscript[
C2,i$$22477] Superscript[D2,i$$22477] Superscript[
E2,i$$22478] Superscript[F2,i$$22478]

2. that is very bad, since it uses global counter and superscripts

  globalCounter = 0;
With[{k = Unique[i],
localIndex = "i" <> ToString[globalCounter++]},
Superscript[a, localIndex] Superscript[b, localIndex]]}


Superscript[A1,"i0"] Superscript[B1,"i0"] Superscript[