Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+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()
        for nabla in ex[r'\nabla']:
            dindex = nabla.indices().__next__() 
            upindex = random.choice(indeces)
            for arg in nabla.args():             
                for index in arg.free_indices():
                    t2:= @(arg);
                    if index.parent_rel==sub:
                        t1:= -\Gamma^{@(upindex)}_{@(dindex) @(index)};
                        t2[index]:= _{@(upindex)};
                        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?

in General questions by (1.7k points)

1 Answer

+2 votes

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)


upindex = Ex( random.choice(indeces) )
by (82.0k points)

And admittedly, the error message is somewhat misleading.

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!

There is a available for download now, please test.

I tried to test this function, now it works. I will think about how to improve it so that it does not repeat the indices of the derivative argument. Thank you so much.