porphyrin
porphyrin

Reputation: 103

Algebraic recursion using Sympy

I want to obtain the algebraic terms of a polynomial that can be defined recursively and by using SymPy.

As an example using Maple the Hermite polynomials be defined as

H(0,x):=1;
H(1,x):=2*x;
for n from 1 to 6 do
    H(n_1,x):= 2*x*H(n,x) - 2*n*H(n-1,x);
    print( simplify(%) );
end do:

which produces the required algebraic terms, however, I can find no way in SymPy to do the same thing. Please can you help?

Upvotes: 0

Views: 466

Answers (1)

mike
mike

Reputation: 837

Like in Maple you can loop, but it seems reasonable to use a function here. You can use standard python functions:

def coefs(x):
    m = Matrix([1, 2*x, 0 ,0, 0, 0])
    for k in range(3):
        m[k+2] = -2*(k+1)*m[k]+2*x*m[k+1]
        print(k)
    return m

You can, of course, just simplify within the function if you should desire that. However, since this can lead to expressions which don't cancel, I'd simplify only once at the bottom of your script.

Upvotes: 1

Related Questions