Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is the "same" answer, using a ring of polynomials. So the pythonic variable x below is representing the indeterminate of a polynomial ring $R$. We have to introduce this polynomial, in my case the ring of coefficients is $\Bbb Q$, the ring of rational numbers, but also other (exact) rings may be used. So $R=\Bbb Q[x]$. We ask sage to give us all roots of a sample polynomial $f$, these live in $\bar{\Bbb Q}$, check which ones are in $i\Bbb Q$ among them, and print them. (If $f$ has all roots in $i\Bbb Q$, we show them all.)

Our sample polynomial is: $$ f = 4 \, x^{12} + 4 \, x^{11} + 33 \, x^{10} + 49 \, x^{9} + 138 \, x^{8} + 254 \, x^{7} + 473 \, x^{6} + 689 \, x^{5} + 971 \, x^{4} + 912 \, x^{3} + 859 \, x^{2} + 432 \, x + 252 \ . $$ The code declares $R=\Bbb Q[x]$, and $f$ first,

R.<x> = PolynomialRing(QQ)    # or simply QQ[]
f = ( 4*x^12 + 4*x^11 + 33*x^10 + 49*x^9 + 138*x^8 + 254*x^7
             + 473*x^6 + 689*x^5 + 971*x^4 + 912*x^3 + 859*x^2 + 432*x + 252 )

and asks for the roots with real part zero, and imaginary part in QQ$=\Bbb Q$:

for r in f.roots(ring=QQbar, multiplicities=False):
    if r.real() == 0 and r.imag() in QQ:
        print(f'Found root r = {r} with rational imaginary part {QQ(r/i)}')

Results:

Found root r = -2*I with rational imaginary part -2
Found root r = -1.5000000000000000?*I with rational imaginary part -3/2
Found root r = -1*I with rational imaginary part -1
Found root r = 1*I with rational imaginary part 1
Found root r = 1.5000000000000000?*I with rational imaginary part 3/2
Found root r = 2*I with rational imaginary part 2

In this approach, the step of passing from $f$ to $g$ is no longer needed. But can be done. We could introduce $\Bbb Q(j)$, $j=\sqrt{-1}$, instead of $\Bbb Q$ as the base field for the coefficients. And while computing the roots we can ask directly for the roots in this ring. For instance:

K.<j> = QuadraticField(-1)    # so j is sqrt(-1)
RK.<X> = PolynomialRing(K)    # or simply K[]
g = f(j*X)

Then:

sage: [ r for r in g.roots(multiplicities=False) if r in QQ ]
[2, 3/2, 1, -1, -3/2, -2]
sage: [ r for r in f.roots(ring=K, multiplicities=False) if r*j in QQ ]
[2*j, 3/2*j, j, -j, -3/2*j, -2*j]

Here is the "same" answer, using a ring of polynomials. So the pythonic variable x below is representing the indeterminate of a polynomial ring $R$. We have to introduce this polynomial, in my case the ring of coefficients is $\Bbb Q$, the ring of rational numbers, but also other (exact) rings may be used. So $R=\Bbb Q[x]$. We ask sage to give us all roots of a sample polynomial $f$, these live in $\bar{\Bbb Q}$, check which ones are in $i\Bbb Q$ among them, and print them. (If $f$ has all roots in $i\Bbb Q$, we show them all.)

Our sample polynomial is: $$ \begin{aligned} f = &= 4 \, x^{12} + 4 \, x^{11} + 33 \, x^{10} + 49 \, x^{9} + 138 \, x^{8} + 254 \, x^{7} + \\ &+ 473 \, x^{6} + 689 \, x^{5} + 971 \, x^{4} + 912 \, x^{3} + 859 \, x^{2} + 432 \, x + 252 \ . \end{aligned} $$ The code declares $R=\Bbb Q[x]$, and $f$ first,

R.<x> = PolynomialRing(QQ)    # or simply QQ[]
f = ( 4*x^12 + 4*x^11 + 33*x^10 + 49*x^9 + 138*x^8 + 254*x^7
             + 473*x^6 + 689*x^5 + 971*x^4 + 912*x^3 + 859*x^2 + 432*x + 252 )

and asks for the roots with real part zero, and imaginary part in QQ$=\Bbb Q$:

for r in f.roots(ring=QQbar, multiplicities=False):
    if r.real() == 0 and r.imag() in QQ:
        print(f'Found root r = {r} with rational imaginary part {QQ(r/i)}')

Results:

Found root r = -2*I with rational imaginary part -2
Found root r = -1.5000000000000000?*I with rational imaginary part -3/2
Found root r = -1*I with rational imaginary part -1
Found root r = 1*I with rational imaginary part 1
Found root r = 1.5000000000000000?*I with rational imaginary part 3/2
Found root r = 2*I with rational imaginary part 2

In this approach, the step of passing from $f$ to $g$ is no longer needed. But can be done. We could introduce $\Bbb Q(j)$, $j=\sqrt{-1}$, instead of $\Bbb Q$ as the base field for the coefficients. And while computing the roots we can ask directly for the roots in this ring. For instance:

K.<j> = QuadraticField(-1)    # so j is sqrt(-1)
RK.<X> = PolynomialRing(K)    # or simply K[]
g = f(j*X)

Then:

sage: [ r for r in g.roots(multiplicities=False) if r in QQ ]
[2, 3/2, 1, -1, -3/2, -2]
sage: [ r for r in f.roots(ring=K, multiplicities=False) if r*j in QQ ]
[2*j, 3/2*j, j, -j, -3/2*j, -2*j]