I wanted to present an example of the use of Python
functions within Cadabra.
First I define my (Cadabra) variables, a rule of substitution, and a post_process
algorithm
{a,b}::NonCommuting.
{a,b}::Distributable.
{b}::SelfAntiCommuting.
rl := b * b = 0;
def post_process(ex):
substitute( distribute( expand_power(ex) ), rl)
Then, I proceed to calculate my expression
expr := (a + b) ** 7;
(I don't know how to show the output here! Sorry for that)
Say that I want to manipulate the terms in this expression as python
strings. I would like to know first if the expression can be translated to a python string, and
str(expr);
does the job... but not in a useful form.
Therefore, I would like to remove the asterisks (*) of multiplication, and separate the terms into monomials,
mylist = str(expr).replace("*", "").split('+')
mylist;
Well, then you can use it as you wish!
Until this point, we have used python functions, but we haven't defined our own. Say then that we want to compare terms on our list, and know if these are related through a circular
transformation (or cyclic transformation). We define our function
def isCircular(arr1, arr2):
return arr1 in arr2*2
and compare
isCircular( mylist[1], mylist[3]);
isCircular( mylist[0], mylist[1]);
Cheers.