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

I am trying to write a function which approximates the exponential series of input up to a given exponent. I wrote something quick in Python, but the output is of string type instead of the input type that I can use later in Cadabra.

def factFun(i):     ## Define Factorial functio
    if i >= 2:
            return i * factFun(i-1)
            return 1

def strExpFun(*a,n):
    sumStr = [str(aX) for aX in list(a)]
    sumFun = '(' + '+'.join(sumStr) + ')'
    expOutputSteps = [sumFun + '**' + str(i) +
                      '/' + str(factFun(i)) for i in range(1,n+1)]
    expOutput = '1+' + '+'.join(expOutputSteps)
    return expOutput

How can I convert the output to something Cadabra understands? Of course if there's a better way, I'm all ears.

in General questions by (180 points)
edited by

1 Answer

+2 votes
Best answer

That's easy: you can create a Cadabra expression object from a string by creating an Ex object with the string as argument. So make your last line read

return Ex(expOutput)

and the result will be a Cadabra expression.

Should probably go into the reference guide, thanks for reminding me ;-)

by (76.6k points)
selected by

Very easy indeed! Thanks a lot!