Ask Your Question
0

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

asked 2025-03-21 12:10:10 +0200

updated 2025-03-26 16:28:44 +0200

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2025-03-21 21:12:10 +0200

Max Alekseyev gravatar image

Like this:

mymod = 21
x = polygen(Zmod(mymod))
f = x^4 + x^2 + 1
print( f.roots(multiplicities=False) )
edit flag offensive delete link more

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 ( 2025-03-21 22:39:16 +0200 )edit

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 ( 2025-03-21 23:37:38 +0200 )edit

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 ( 2025-03-27 15:23:04 +0200 )edit

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 ( 2025-03-27 16:03:57 +0200 )edit

What is your example?

Max Alekseyev gravatar imageMax Alekseyev ( 2025-03-27 16:39:24 +0200 )edit

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: 2025-03-21 12:10:10 +0200

Seen: 100 times

Last updated: Mar 21