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

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?

asked in General questions by (170 points)

1 Answer

+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) )
answered by (57.8k 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.

...