Restricting the field used when solving equations.

11 views
Skip to first unread message

jason.t.stein

unread,
Dec 10, 2009, 11:16:39 AM12/10/09
to sage-edu
I am a high school teacher in Turtleford, SK Canada and am thinking
about using sage in some of my teaching. The one problem I have right
now is all of the complex roots found when solving an equation. Is
there a way to restrict the returned solutions to the Reals?

jason...@creativetrax.com

unread,
Dec 10, 2009, 12:06:06 PM12/10/09
to sage...@googlegroups.com
You can take all of the returned solutions and test them to see if they
are real. Then you can just return the real ones.

sage: all_roots=solve((x^2+1)*(x-1)==0,x,solution_dict=True)
sage: all_roots
[{x: -I}, {x: I}, {x: 1}]
sage: real_roots=[root[x] for root in all_roots if imag(root[x])==0]
sage: real_roots
[1]

Jason


Vincent D

unread,
Dec 10, 2009, 6:49:23 PM12/10/09
to sage-edu
I prefer Polynomials than symbolic expression :

sage: R.<x> = PolynomialRing(QQ, 'x')
sage: P = (x^2 + 1) * (x^2 - x - 1)
sage: P.roots() # no root in QQ
[]
sage: P.roots(RR) # two roots in RR
[(-0.618033988749895, 1), (1.61803398874989, 1)]
sage: P.roots(CC) # four roots in CC
[(-0.618033988749895, 1), (1.61803398874989, 1),
(-8.79016911342623e-17 - 1.00000000000000*I, 1),
(-8.79016911342623e-17 + 1.00000000000000*I, 1)]


But it can also be done with symbolics

sage: x = var('x')
sage: P = (x^2 + 1) * (x^2 - x - 1)
sage: P.roots(x, ring=QQ)
[]
sage: P.roots(x, ring=RR) # two roots in RR
[(-0.618033988749895, 1), (1.61803398874989, 1)]
sage: P.roots(x, ring=CC) # four roots in CC
[(-0.618033988749895, 1), (1.61803398874989, 1),
(-8.79016911342623e-17 - 1.00000000000000*I, 1),
(-8.79016911342623e-17 + 1.00000000000000*I, 1)]
sage: P.roots(x, ring=SR) # the symbolic ring
[(-1/2*sqrt(5) + 1/2, 1), (1/2*sqrt(5) + 1/2, 1), (-I, 1), (I, 1)]

mhampton

unread,
Dec 11, 2009, 6:33:44 PM12/11/09
to sage-edu
It might be overkill for this use case, but you can also use the
extremely nice real_roots functions that give guaranteed intervals and
multiplicities:

sage: from sage.rings.polynomial.real_roots import *
sage: x = polygen(ZZ)
sage: real_roots((x^2 + 1) * (x^2 - x - 1))
[((-3/4, -1/8), 1), ((1/2, 7/4), 1)]

You can restrict to certain intervals, and go to arbitrary precision:

sage: real_roots((x^2 + 1) * (x^2 - x - 1),max_diameter=1/10^14,
bounds = [0,2])
[((295594915476877884107369019065608127654139919973/182687704666362864775460604089535377456991567872,
605378386896645906651894226927171110065727063886243/374144419156711147060143317175368453031918731001856),
1)]

Or converting the first end of the interval to a numeric value:

sage:[N(q[0][0],200) for q in real_roots((x^2 + 1)*(x^2 - x -
1),max_diameter=1/10^14, bounds = [0,2])]
6180339887498948482045824713181755587457888095347418966031

-Marshall Hampton
Reply all
Reply to author
Forward
0 new messages