ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 23 Mar 2020 13:50:32 +0100discriminant of multivariate polynomialhttps://ask.sagemath.org/question/50317/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?Sat, 21 Mar 2020 09:50:29 +0100https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/Answer by rburing for <p>If I do</p>
<pre><code>R.<x,z,I>=QQ[]
f_str = 'x**3 + x*z +1'
f = eval(f_str)
f.discriminant(x)
</code></pre>
<p>this works out correctly, giving </p>
<pre><code>-4*z^3 - 27
</code></pre>
<p>But if I instead replace the coefficient of any monomial with a rational, such as</p>
<pre><code>R.<x,z,I>=QQ[]
f_str = '1.0*x**3 + x*z +1'
f = eval(f_str)
f.discriminant(x)
</code></pre>
<p>I get the following error:</p>
<pre><code>AttributeError: 'MPolynomial_polydict' object has no attribute 'discriminant'
</code></pre>
<p>Can someone explain how to make it work?</p>
https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?answer=50318#post-id-50318Multiplying 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.Sat, 21 Mar 2020 10:46:08 +0100https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?answer=50318#post-id-50318Comment by physicist for <p>Multiplying a string by 1.0 shouldn't work. What version of SageMath are you using? Try 9.0.</p>
<p>You probably want to do something like this:</p>
<pre><code>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
</code></pre>
<p>If you do only <code>sage_eval</code> without converting into <code>R</code>, you will get a polynomial over <code>RR</code> (Real Field with 53 bits of precision), because that's what the parent of <code>1.0</code> is in SageMath.</p>
<p>Instead of specifying the dictionary of local variables you can also lazily write <code>locals()</code>, but that is less clean.</p>
https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?comment=50327#post-id-50327Thanks 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?Mon, 23 Mar 2020 13:05:06 +0100https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?comment=50327#post-id-50327Comment by rburing for <p>Multiplying a string by 1.0 shouldn't work. What version of SageMath are you using? Try 9.0.</p>
<p>You probably want to do something like this:</p>
<pre><code>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
</code></pre>
<p>If you do only <code>sage_eval</code> without converting into <code>R</code>, you will get a polynomial over <code>RR</code> (Real Field with 53 bits of precision), because that's what the parent of <code>1.0</code> is in SageMath.</p>
<p>Instead of specifying the dictionary of local variables you can also lazily write <code>locals()</code>, but that is less clean.</p>
https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?comment=50329#post-id-50329Trying 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](https://trac.sagemath.org/ticket/29396).Mon, 23 Mar 2020 13:50:32 +0100https://ask.sagemath.org/question/50317/discriminant-of-multivariate-polynomial/?comment=50329#post-id-50329