ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 22 Dec 2015 20:04:37 -0600Discriminant of multivariate polynomials with complex coefficientshttps://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/I would like to compute the discriminant of some polynomial in two variables, x and z, the discriminant should be computed with respect to x.
I am able to do so in certain cases, but not when the coefficients are complex numbers written in a certain way.
For example, this works:
R.<z> = QQ[]
S.<x> = R[]
p = x**2 - z**2
p.discriminant()
gives:
4*z^2
But when I try to use a complex coefficient, discriminant fails because the expression is regarded as a symbolic expression, instead of a polynomial
q = x**2 - z**2 * I
q.discriminant()
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'discriminant'
Curiously, if I use instead
q = x**2 - z**2 * 1j
q.discriminant()
this gives the correct answer.
Now for particular reasons I really need to deal with 'I' as the complex unit, and cannot use 'j'. What's wrong with 'I'?
Tue, 22 Dec 2015 13:31:47 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/Answer by vdelecroix for <p>I would like to compute the discriminant of some polynomial in two variables, x and z, the discriminant should be computed with respect to x.
I am able to do so in certain cases, but not when the coefficients are complex numbers written in a certain way.
For example, this works:</p>
<pre><code>R.<z> = QQ[]
S.<x> = R[]
p = x**2 - z**2
p.discriminant()
</code></pre>
<p>gives:</p>
<pre><code>4*z^2
</code></pre>
<p>But when I try to use a complex coefficient, discriminant fails because the expression is regarded as a symbolic expression, instead of a polynomial</p>
<pre><code>q = x**2 - z**2 * I
q.discriminant()
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'discriminant'
</code></pre>
<p>Curiously, if I use instead </p>
<pre><code>q = x**2 - z**2 * 1j
q.discriminant()
</code></pre>
<p>this gives the correct answer.
Now for particular reasons I really need to deal with 'I' as the complex unit, and cannot use 'j'. What's wrong with 'I'?</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?answer=31763#post-id-31763One solution
sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
The default I in Sage is (for now) a symbolic expression.Tue, 22 Dec 2015 14:31:24 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?answer=31763#post-id-31763Comment by vdelecroix for <p>One solution</p>
<pre><code>sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
</code></pre>
<p>The default I in Sage is (for now) a symbolic expression.</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31773#post-id-31773No no. CC does not use the symbolic I it is just a matter of notation. If you want to know "where" your object lives you can use the magic .parent() method
sage: I.parent()
Symbolic Ring
sage: I = CC(0,1)
sage: R.<z> = CC[]
sage: p = I*z
sage: p.parent()
Univariate Polynomial Ring in z over Complex Field with 53 bits of precisionTue, 22 Dec 2015 20:04:37 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31773#post-id-31773Comment by physicist for <p>One solution</p>
<pre><code>sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
</code></pre>
<p>The default I in Sage is (for now) a symbolic expression.</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31772#post-id-31772Indeed, it is. It seems that CC(0,1) = 1.000000 I, still employs 'I'.
So why is it so much faster? I guess previously sage was trying to solve exactly, since coefficients were integers, while now the rational coefficient triggers approximate solution?
Thanks again!Tue, 22 Dec 2015 19:16:29 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31772#post-id-31772Comment by vdelecroix for <p>One solution</p>
<pre><code>sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
</code></pre>
<p>The default I in Sage is (for now) a symbolic expression.</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31771#post-id-31771If you only want some approximate value of the discriminant, you can also do
sage: I = CC(0,1)
sage: R.<z> = CC[]
sage: S.<x> = R[]
Might be faster than what I proposed.Tue, 22 Dec 2015 17:55:09 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31771#post-id-31771Comment by physicist for <p>One solution</p>
<pre><code>sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
</code></pre>
<p>The default I in Sage is (for now) a symbolic expression.</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31768#post-id-31768On further inspection, while this definitely works, it seems that sticking to 'I' does have some drawback apparently.
For instance, computing the discriminant of a slightly more complicated polynomial like
-108*x**27*z**26 - 540*I*x**15*z**15 + 540*I*x**15*z**13 - x**3*z**4 + 2*x**3*z**2 - x**3
is much slower than computing the same discriminant with
-108*x**27*z**26 - 540*1j*x**15*z**15 + 540*1j*x**15*z**13 - x**3*z**4 + 2*x**3*z**2 - x**3
So it seems that I should really find a way of converting I to 1j, in the end. Any trick for this?Tue, 22 Dec 2015 16:58:28 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31768#post-id-31768Comment by physicist for <p>One solution</p>
<pre><code>sage: K.<I> = QuadraticField(-1)
sage: R.<z> = K[]
sage: S.<x> = R[]
sage: q = x**2 - z**2 * I
sage: q.discriminant()
4*I*z^2
</code></pre>
<p>The default I in Sage is (for now) a symbolic expression.</p>
https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31766#post-id-31766Thanks! Especially for explaining what is the issue with 'I'.
Also nice simple solutionTue, 22 Dec 2015 16:35:20 -0600https://ask.sagemath.org/question/31761/discriminant-of-multivariate-polynomials-with-complex-coefficients/?comment=31766#post-id-31766