# Random index

+1 vote

I tryed to modify function "expand_nabla" from page about programming https://cadabra.science/notebooks/ref_programming.html 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?

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) )
by (70.9k points)