3
$\begingroup$
$Assumptions = 
 x \[Element] Matrices[{2*M, 2*M}, Reals, Antisymmetric[{1, 2}]]

x[arg__] /; ! OrderedQ@{arg} := Signature@{arg} x @@ Sort@{arg} 
Format[x[arg__]] := Subscript[x, arg]

p[arg__] /; ! OrderedQ@{arg} := Signature@{arg} p @@ Sort@{arg}
p[___, j_, j_, ___] = 0;

Format[p[arg__]] := Subscript[p, arg]
test1 = (4 *p[4, i, k, l] *x[2, i] *x[k, 2] *x[l, 3]) + (4* 
    p[1, j, k, l] *x[2, j]* x[k, 2]* x[l, 2]) + (p[1, 2, 3, i]* 
    x[1, i])

I want to implement the above in such a way that repeated indices only summed up just as in Einstein summation convention. Any way to do this? When I perform summation as:

Sum[test1, {i, 4}] // Expand

I am getting:

Subscript[p, 1, 2, 3, 4] Subscript[x, 1, 4] + 
 16 Subscript[p, 1, j, k, l] Subscript[x, 2, j] Subscript[x, 2, k]
   Subscript[x, 2, l] + 
 4 Subscript[p, 1, 4, k, l] Subscript[x, 1, 2] Subscript[x, 2, k]
   Subscript[x, 3, l] - 
 4 Subscript[p, 3, 4, k, l] Subscript[x, 2, 3] Subscript[x, 2, k]
   Subscript[x, 3, l]

But I want to use Einstein summation convention in which only repeated indices sums up. Is there a way that Mathematica to perform summation only for repeated indices only,not for constants.

$\endgroup$
5
  • $\begingroup$ good question! minor typo: capital I in the last term of test1 (which I'm guessing should be lowercase?) $\endgroup$
    – thorimur
    Commented Jul 13, 2021 at 5:48
  • $\begingroup$ @thorimur its lower case $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 5:49
  • $\begingroup$ @Jasmine Please see this mathematica.stackexchange.com/questions/14654/… $\endgroup$
    – qahtah
    Commented Jul 13, 2021 at 5:52
  • $\begingroup$ @qahtah It's applicable only for single variable $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 5:53
  • $\begingroup$ @Jasmine Have a look at this package arxiv.org/abs/1309.2561 $\endgroup$
    – qahtah
    Commented Jul 13, 2021 at 5:54

1 Answer 1

5
$\begingroup$

You are admittedly probably better off using a package such as the one @qahtah mentioned in the comments or the package xAct, but just for fun, here is a way of defining a function EinsteinSum which does what you're looking for—unless I've misunderstood. If you have any questions about why this works the way it does, or if I've misunderstood what you're looking for, let me know! :)

Clear[EinsteinSum]

EinsteinSum[Times[a___, (p_Symbol)[x___, i_Symbol, y___], b___, (q_Symbol)[w___, i_Symbol, z___], c___]] := 
 EinsteinSum[
  a b c With[{ii = Unique["ii"]}, Sum[p[x, ii, y] q[w, ii, z], {ii, 4}]]]

EinsteinSum[Times[a_, b__]] := EinsteinSum[Expand[a b]] /; ! SameQ[a b, Expand[a b]]

EinsteinSum[Plus[a_, b__]] := Plus @@ (EinsteinSum /@ {a, b})

EinsteinSum[x_] := x

Test:

test1 = (4 *p[4, i, k, l] *x[2, i] *x[k, 2] *x[l, 3]) + (4* 
    p[1, j, k, l] *x[2, j]* x[k, 2]* x[l, 2]) + (p[1, 2, 3, i]* 
    x[1, i])

EinsteinSum[test1]

(* Output: a huge expanded form *)
$\endgroup$
7
  • $\begingroup$ There is a small issue. In my case $Assumptions = x [Element] Matrices[{2*M, 2*M}, Reals, Antisymmetric[{1, 2}]]; x[arg__] /; ! OrderedQ@{arg} := Signature@{arg} x @@ Sort@{arg} Format[x[arg__]] := Subscript[x, arg]; p[arg__] /; ! OrderedQ@{arg} := Signature@{arg} p @@ Sort@{arg} p[, j, j_, ___] = 0; Format[p[arg]] := Subscript[p, arg] So I define them as test1 = (4 p[4, i, k, l] *x[2, i] *x[k, 2] *x[l, 3]) + (4 p[1, j, k, l] x[2, j] x[k, 2]* x[l, 2]) + (p[1, 2, 3, i]* x[1, I]) So your code is not working! $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 6:27
  • $\begingroup$ Your code is great. But can you please let me know how it works in this case $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 6:29
  • $\begingroup$ I have edited my question $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 6:36
  • 1
    $\begingroup$ @Jasmine glad it helps! :) I think we might be able to leverage TensorContract and mathematica's usage of $Assumptions to make it a bit more general. if I have time I'd like to come back and improve it tomorrow! $\endgroup$
    – thorimur
    Commented Jul 13, 2021 at 7:00
  • 1
    $\begingroup$ That's great. Mainly it would be of great help if we can sum only desired indices at a time. For example at first if I want to sum repeated 'i' only that would be great. Also what about a summation with restriction. ie, summing indices with restriction. eg: How to implement i<j summations. $\endgroup$
    – Jasmine
    Commented Jul 13, 2021 at 7:03

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.