Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
0 votes


Can I change indeces in a cycle? For example (it does not work).

for i in [1,2,3]:
   for j in [1,2,3]:
      for k in [1,2,3]:
         substitute(canonicalise($e_{i j k} s_{k}$), $e_{1 2 3} = 1$);

Thank you.

asked in General questions by

1 Answer

0 votes
Best answer

This does not work (as you wrote it) because the mathematical symbols i, j, k in the mathematical expression e_{i j k} s_{k} are not the same as the python symbols of the same name over which you do the loops. In Cadabra, symbols appearing in mathematical expressions are not Python objects (unlike, for example, in SymPy). If this sounds weird, think about the object e_{i j k}. That is clearly not a Python object. Cadabra's maths input language is not Python.

Of course you can still do what you want, using the much more powerful evaluate algorithm. You would write

\epsilon_{i j k}::EpsilonTensor();
ex:=\epsilon_{i j k} s_{k};

The evaluate does the index loops for you; the output will show that e.g. the i=1, j=2 component of your expression equals s_3.

If you had wanted to do other component value substitutions too, you can add them as rules to the evaluate function. For instance, if $s=(a,b,c)$, then use (instead of the last line above)

evaluate(ex, $s_{1}=a, s_{2}=b, s_{3}=c$);

Hope this helps.

answered by (57.2k points)

Thank you. It helped.