First time here? Check out the FAQ!

Ask Your Question
0

Finding the roots of a polynomial mod a composite number in Python

asked 0 years ago

updated 0 years ago

FrédéricC gravatar image

I am looking for a way to solve 3rd and 4th degree polynomials from Python (actually Python 3) using Sage. The polynomials are not square-free and the modulo can be a composite number. How do I do that? Help would be appreciated.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 0 years ago

Max Alekseyev gravatar image

Like this:

mymod = 21
x = polygen(Zmod(mymod))
f = x^4 + x^2 + 1
print( f.roots(multiplicities=False) )
Preview: (hide)
link

Comments

Hey, awesome. I can't believe you answered so quickly. Thanks. One more question. What if I know the factorization, e.g. 7*3, can I pass it in to speed up the calculation? I guess it can be just precomputed once.

The Big Lebowski gravatar imageThe Big Lebowski ( 0 years ago )

I believe it will be factored once if you create a single Zmod (and x) object and reuse it in all polynomials.

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

You can get the multiplicity of root r as valuation(f, x-r). Note however that modulo a composite number polynomial roots may exhibit quite an unusual behavior - e.g., the total multiplicity of all roots may be larger than the polynomial degree. Even in my example x^4+x^2+1modulo 21 has 12 distinct roots. See also https://math.stackexchange.com/q/2823339

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

I got TypeError: Polynomial_dense_modn_ntl_ZZ.valuation() takes no arguments (1 given) for my example. It works fine with yours.

The Big Lebowski gravatar imageThe Big Lebowski ( 0 years ago )

What is your example?

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 0 years ago

Seen: 77 times

Last updated: Mar 21