solve and numerical answers

51 views
Skip to first unread message

Fernando Gouvea

unread,
Sep 14, 2020, 5:23:49 PM9/14/20
to sage-support

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

Vincent Delecroix

unread,
Sep 15, 2020, 1:55:55 AM9/15/20
to sage-s...@googlegroups.com
One way is to use polynomials

sage: x = polygen(QQ)
sage: (x^4 - 2*x - 1).roots(RealField(32), multiplicities=False)
[-0.474626618, 1.39533699]
sage: (x^4 - 2*x - 1).roots(ComplexField(128), multiplicities=False)
[-0.47462661756260555032941320989493141267,
1.3953369944670730187931436130710553428,
-0.46035518845223373423186520158806196509 -
1.1393176803019225636193089912568155343*I,
-0.46035518845223373423186520158806196509 +
1.1393176803019225636193089912568155343*I]

The method "roots" has an optional ring argument that can be used
to control what kind of solution you are looking for (rational, real,
complex, tuning precision of the answer, ...). In my example above
I used "RealField(32)" and "ComplexField(128)" which correspond
respectively to floating point numbers with precision respectively
32 and 128 bits.

You can also use SR (symbolic ring) if you want the symbolic version

sage: (x^4 - 2*x - 1).roots(SR, multiplicities=False)


[-1/2*sqrt(1/3)*sqrt((3*(2/9*sqrt(43)*sqrt(3) + 2)^(2/3) - 4...]

Vincent

Le 14/09/2020 à 23:23, Fernando Gouvea a écrit :
> 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

kcrisman

unread,
Sep 15, 2020, 1:53:15 PM9/15/20
to sage-support

I still don't know my way around the Sage documentation... Sorry for the elementary question.

Yeah, we are sorry that it never has gotten more organized (though it is actually quite thorough!).   You may want to try the French (now in English) Sage book, or Greg Bard's AMS (but free online) Sage book.

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 should be, and if you give us the precise commands you used, we should be able to either log it as a bug report or something else.  For many equations we get something back like x^5-x+1==0 which we know there isn't a formula for, but if it is using the quartic formula or something similar it should, in principle, be able to be approximated.  We'd have to see your exact output to determine why that isn't happening.

Fernando Gouvea

unread,
Sep 15, 2020, 2:38:02 PM9/15/20
to sage-s...@googlegroups.com

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

Emmanuel Charpentier

unread,
Sep 15, 2020, 3:20:13 PM9/15/20
to sage-support

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,

kcrisman

unread,
Sep 16, 2020, 1:23:02 PM9/16/20
to sage-support
On Tuesday, September 15, 2020 at 3:20:13 PM UTC-4 Emmanuel Charpentier wrote:

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


In particular, L[1].rhs() is the thing you wanted.  rhs is "right-hand side".

In principle, we should be able to use gp.solve (though you are right that the syntax is not valid Python), but I couldn't figure out how to get it to work that way.  However, there is this alternate:

sage: gp.eval("solve(t=10,30,%s)"%PP)

'21.364877203764406090765850667689038180'

where we are sort of abusing the string manipulation to just do it directly in Sage's copy of gp.
Reply all
Reply to author
Forward
0 new messages