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

Hi everyone! I would like to replace the following list of statements


with a cycle which, given to each step the variable i = 0, ..., 20, iteratively evaluates the declaration.

Initially I tried to write the iterative declaration as a string and then to submit it to an eval(), but it doesn't work. Can anyone help me?

Thanks, Mattia

in General questions by (410 points)

1 Answer

+3 votes

You can't do this with a straightforward eval, because that function does not pre-parse the Cadabra input into Python input. But if you realise that the :: notation gets translated into a simple function call, then it's a relatively simple thing to do yourself:

for i in range(20):
    LaTeXForm(Ex(r'\a'+str(i)+r'{#}'), Ex(r'"\alpha_{'+str(i)+r'}"') )

The only tricky thing here is the quotes in the 2nd Ex: oyu need the single quote to generate a string, and then the double quote inside that so that Cadabra's LaTeXForm property gets fed a string too, not a maths expression.

Granted, this would be nice to have in a cleaner notation, but it gets the job done.

by (65.1k points)

Thank you Kasper!

Excellent! I was trying a similar approach, but never go it right! hahaha

Hi everyone!

how a similar approach can be used to cycle the following statements?


Thanks, Mattia