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()
        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?

in General questions by (1.8k 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)

use

upindex = Ex( random.choice(indeces) )
by (83.1k 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 2.3.3.2 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.

...