Kevin Johnson
Kevin Johnson

Reputation: 840

How to check if complex number is a whole number

Given a value numerical value x, you can just do this float(x).is_integer() to check if it's an integer. Is there a way to do this for complex numbers?

I'm trying to use list comprehension to take only the integer roots of a polynomial over a finite field which are integers.

[r for r in solve(f,domain=FiniteField(p)) if float(r).is_integer()]

but if the solve function returns complex roots, this doesn't work.

Does anyone know how to either: check if a given (possibly complex number) is an integer OR know if there's a SymPy function to get the roots of a polynomial over a finite field which are integers?

Upvotes: 0

Views: 1690

Answers (4)

leogama
leogama

Reputation: 1059

Using a list comprehension as in your original question, one could do:

roots = [complex(r) for r in solve(f, domain=FiniteField(p))]
int_roots = [z.real for z in roots if z.real.is_integer() and not z.imag]

Upvotes: 0

Mark Dickinson
Mark Dickinson

Reputation: 30561

Use the ground_roots function along with the modulus keyword argument. This returns the roots modulo p, with multiplicities. Here's an example:

>>> from sympy import Symbol, ground_roots
>>> x = Symbol('x')
>>> f = x**5 + 7*x + 1
>>> p = 13
>>> ground_roots(f, modulus=p)
{-5: 1, 4: 2}

That says that the roots of poly modulo 13 are -5 and 4, with the root 4 having multiplicity 2.

By the way, it looks to me as though complex numbers are a red herring here: the roots of an integral polynomial over a finite field can't naturally be regarded as complex numbers. The call to solve in your original post is ignoring the domain argument and is simply giving algebraic numbers (which can reasonably be interpreted as complex numbers) as its results, which is probably why you ended up looking at complex numbers. But these aren't going to help when trying to find the roots modulo a prime p.

Upvotes: 2

Marcin
Marcin

Reputation: 238259

If you want to check if imaginary part is zero, you can do this:

In [17]: a=2 + 2j

In [18]: bool(a.imag)
Out[18]: True

In [19]: b=2 + 0j

In [20]: bool(b.imag)
Out[20]: False

Upvotes: 0

smci
smci

Reputation: 33950

float.is_integer(z.real) tells you if the real part is integer

Upvotes: 1

Related Questions