Random index

I tryed to modify function "expand_nabla" from page about programming
to have random dummie index in Gamma, my version looks like

import random
def expand_nabla(ex):
indeces = 'a b c d e i k m n p q r s t u v w x y z'.split()
print(indeces)
for nabla in ex[r'\nabla']:
nabla.name=r'\partial'
dindex = nabla.indices().__next__()
upindex = random.choice(indeces)
print(upindex)
for arg in nabla.args():
ret:=0;
for index in arg.free_indices():
t2:= @(arg);
if index.parent_rel==sub:
t1:= -\Gamma^{@(upindex)}_{@(dindex) @(index)};
t2[index]:= _{@(upindex)};
else:
t1:=  \Gamma^{@(index)}_{@(dindex) @(upindex)};
t2[index]:= ^{@(upindex)};
ret += Ex(str(nabla.multiplier)) * t1 * t2
nabla += ret
return ex


The error message is RuntimeError: Python object 'upindex' does not exist
Maybe somebody knows how could it be realised?

+1 vote

Your upindex object is a string. In order to pull a symbol with a python name into a Cadabra maths expression with the @(...) operator, it needs to be a Cadabra Ex object. So convert your upindex to an Ex and it should work: instead of

upindex = random.choice(indeces)


use

upindex = Ex( random.choice(indeces) )


Ok, but now it's return something like \Gamma(s)_{a b}, and upindex now isn't index.

Ouch, that's a bug. I have just pushed a fix to github which corrects this. Let me know if you are using an installation from a binary package, then I can rebuild them (and let me know your OS/distribution).

Yes, I'm using cadabra2-2.3.2-ulyana.deb , my OS is linux mint ulyana 20.
Thank you very much!