I still don't know my way around the Sage documentation... Sorry
for the elementary question.
I just tried to use the solve command to find the roots of a polynomial of degree 4 with real coefficients. The result is a list of solutions expressed in (complicated) symbolic form. When I attempted to find the numerical value of the solutions, I got an error:
TypeError: cannot evaluate symbolic expression numerically
There must be a way to do this, analogous to the "solve" command in gp. (I tried gp.solve(t=10,30,P(t)==0), but that gives an error too.)
Fernando
-- ============================================================= Fernando Q. Gouvea http://www.colby.edu/~fqgouvea Carter Professor of Mathematics Dept. of Mathematics and Statistics Colby College 5836 Mayflower Hill Waterville, ME 04901 The vermine is a small black and white relative of the lemming, found in the cold Hublandish regions. Its skin is rare and highly valued, especially by the vermine itself; the selfish little bastard will do anything rather than let go of it. -- Discworld wildlife Terry Pratchett, Sourcery
I still don't know my way around the Sage documentation... Sorry for the elementary question.
I just tried to use the solve command to find the roots of a polynomial of degree 4 with real coefficients. The result is a list of solutions expressed in (complicated) symbolic form. When I attempted to find the numerical value of the solutions, I got an error:
TypeError: cannot evaluate symbolic expression numerically
Thanks! I've already learned more.
What I first did was this:
sage: PP
-0.625000000000000*t^4 + 23.5500000000000*t^3 -
264.051000000000*t^2 + 1026.90000000000*t - 853.800000000000
sage: L=solve(PP==0,t)
sage: L[1]
t ==
-1/1250*sqrt((390625*(4/1953125*I*sqrt(37468876945450884598)*sqrt(5)
- 3986170531587/244140625)^(2/3) +
28629375*(4/1953125*I*sqrt(37468876945450884598)*sqrt(5) -
3986170531587/244140625)^(1/3) +
397327289)/(4/1953125*I*sqrt(37468876945450884598)*sqrt(5) -
3986170531587/244140625)^(1/3)) +
1/2*sqrt(-(4/1953125*I*sqrt(37468876945450884598)*sqrt(5) -
3986170531587/244140625)^(1/3) -
397327289/390625/(4/1953125*I*sqrt(37468876945450884598)*sqrt(5)
- 3986170531587/244140625)^(1/3) -
2316636/5/sqrt((390625*(4/1953125*I*sqrt(37468876945450884598)*sqrt(5)
- 3986170531587/244140625)^(2/3) +
28629375*(4/1953125*I*sqrt(37468876945450884598)*sqrt(5) -
3986170531587/244140625)^(1/3) +
397327289)/(4/1953125*I*sqrt(37468876945450884598)*sqrt(5) -
3986170531587/244140625)^(1/3)) + 91614/625) + 471/50
sage: L[1].n()
(errors)
TypeError: cannot evaluate symbolic expression
numerically
I then tried to use the gp version of "solve":
sage: gp.solve(t=10,30,PP)
File "<ipython-input-20-236b1d016f63>", line 1
gp.solve(t=Integer(10),Integer(30),PP)
^
SyntaxError: positional argument follows keyword
argument
So Sage doesn't like the gp syntax.
I now know that I could have done this by creating a polynomial ring over R and asking for roots, or with PP.find_root(0,30), which only finds one of the roots (there are four in that interval).
Fernando
--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/292f795f-80af-488f-835c-344fa08782d8n%40googlegroups.com.
-- ============================================================= Fernando Q. Gouvea http://www.colby.edu/~fqgouvea
Carter Professor of Mathematics Dept. of Mathematics and Statistics Colby College 5836 Mayflower Hill Waterville, ME 04901 The first prerequisite for leading any satisfactory kind of personal life in a technological society is the ability to resist distraction. -- W. H. Auden
sage: L[1].n()
fails because L1 is an equation, i. e a symbolic expression whose operator is the built-in “eq”, which has no n() method.
However,
sage: PP=-625/1000*t^4 + 2355/100*t^3 - 264051/1000*t^2 + 10269/10*t - 8538/10
sage: PP.parent()
Symbolic Ring
sage: L=solve(PP,t)
sage: L[1].rhs().n()
6.66465694043241 + 1.07289603917368e-15*I
That’s still a numerical (“inexact”) answer. But you can get an “exact” answer without explicitly going to the relevant polynomial ring :
6.66465694043241 + 1.07289603917368e-15*I
sage: PP.roots(ring=QQbar, multiplicities=False)
[1.125379936402974?, 6.664656940432404?, 8.52508591940022?, 21.36487720376441?]
In this case, this exact answer tells you that this second root is real, which was not evident from the numerical output…
HTH,
sage: L[1].n()fails because L1 is an equation, i. e a symbolic expression whose operator is the built-in “eq”, which has no
n()method.However,
sage: PP=-625/1000*t^4 + 2355/100*t^3 - 264051/1000*t^2 + 10269/10*t - 8538/10 sage: PP.parent() Symbolic Ring sage: L=solve(PP,t) sage: L[1].rhs().n() 6.66465694043241 + 1.07289603917368e-15*I
'21.364877203764406090765850667689038180'
where we are sort of abusing the string manipulation to just do it directly in Sage's copy of gp.