Gev_2000
Gev_2000

Reputation: 59

Sympy simplification of polynomial with complex coefficients

Working on a problem with polynomials with complex coefficients, I am stuck with the following problem:

Let's say I have a polynomial P = λ^16*z + λ^15*z^2, where λ is complex. I want to simplify having the following constraint: λ^14 = 1. So, plugging in, we should get:

P = λ^2*z + λ*z^2.

I have tried P.subs(λ**14,1) but it doesn't work, because it assumes λ is real I guess. So it returns the original expression: P = λ^16*z + λ^15*z^2, without factoring out λ^14...

Upvotes: 0

Views: 771

Answers (4)

iacob
iacob

Reputation: 24231

I don't know any simple way to achieve what you want in sympy, but you could substitute each value explicitly:

p = (λ**16)*z + (λ**15)*(z**2)

p = p.subs(λ**16, λ**2).subs(λ**15, λ**1)
>>> z**2*λ + z*λ**2

Why subs fails to work here:

subs only substitutes an expression x**m in x**n when m is a factor of n, e.g.:

p.subs(λ, 1)
>>> z**2 + z
p.subs(λ**2, 1)
>>> z**2*λ**15 + z
p.subs(λ**3, 1)
>>> z**2 + z*λ**16
p.subs(λ**6, 1)
>>> z**2*λ**15 + z*λ**16

etc.

Upvotes: 1

Stelios
Stelios

Reputation: 5521

This works:

P.simplify().subs(λ**15,1).expand()

Upvotes: 0

asmeurer
asmeurer

Reputation: 91500

You can use the ratsimpmodprime() function to reduce a polynomial modulo a set of other polynomials. There is also the reduce() function, which does something similar.

>>> P = λ**16*z + λ**15*z**2
>>> ratsimpmodprime(P, [λ**14 - 1])
z**2*λ + z*λ**2

Upvotes: 0

butterwagon
butterwagon

Reputation: 499

If you assume that λ is real, this works:

lambda_, z = sym.symbols('lambda z', real=True)
print((lambda_**16*z + lambda_**15*z**2).subs(lambda_**14, 1))
z**2 + z

Edit:
It shouldn't actually work anyway because λ may be negative. What you want is only true if λ is a positive number.

Upvotes: 0

Related Questions