discriminant of multivariate polynomial

If I do

R.<x,z,I>=QQ[]
f_str = 'x**3 + x*z +1'
f = eval(f_str)
f.discriminant(x)


this works out correctly, giving

-4*z^3 - 27


But if I instead replace the coefficient of any monomial with a rational, such as

R.<x,z,I>=QQ[]
f_str = '1.0*x**3 + x*z +1'
f = eval(f_str)
f.discriminant(x)


I get the following error:

AttributeError: 'MPolynomial_polydict' object has no attribute 'discriminant'


Can someone explain how to make it work?

edit retag close merge delete

Sort by ยป oldest newest most voted

Multiplying a string by 1.0 shouldn't work. What version of SageMath are you using? Try 9.0.

You probably want to do something like this:

sage: R.<x,z,I>=QQ[]
sage: f_str = '1.0*(x**3 + x*z +1)'
sage: f = R(sage_eval(f_str,locals={'x':x, 'z':z, 'I':I}))
sage: f.discriminant(x)
-4*z^3 - 27


If you do only sage_eval without converting into R, you will get a polynomial over RR (Real Field with 53 bits of precision), because that's what the parent of 1.0 is in SageMath.

Instead of specifying the dictionary of local variables you can also lazily write locals(), but that is less clean.

more

Thanks so much! Of course, I didn't attempt to multiply the whle string by 1.0, that was a typo in my question, which I'll correct now. Just wondering, what's wrong with the field RR for this purpose? Is it that the variables x,z,I are not included automatically?

( 2020-03-23 13:05:06 +0200 )edit

Trying to compute the discriminant over RR using SageMath 9.0 gives me a different error than the one you posted; I reported it as a bug; it is now trac ticket #29396.

( 2020-03-23 13:50:32 +0200 )edit