简体   繁体   中英

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?

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 .

float.is_integer(z.real)告诉您实部是否为整数

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM