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.Thu, 26 Feb 2015 15:45:56 -0600Bug with absolute value of a complex variable?https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/I perform some analytic calculations involving complex number, in particular complex electric field amplitude. I was quite shocked when I discovered how SAGE handles complex variables.
So, I define a new variable "A" and explicitly say that it is complex. Then I want to find the absolute value of this variable, which is $AA^* = |A|^2$.
sage: var("A", domain="complex")
sage: A*A.conjugate()
A*conjugate(A) # not bad
sage: _.simplify()
A^2 # THIS IS WRONG!
Furthermore we check, if $AA = |A|^2 = AA^*$, and it does!
sage: A*A.conjugate() - A*A # Substract squared A from absolute value of A
-A^2 + A*conjugate(A)
sage: _.simplify()
0 # So SAGE assumes that they are equal
But this is obviously WRONG, since if I assign some number to $A$, then the last test does not result in zero:
sage: A=3+4*i
sage: A*A.conjugate() - A*A
-24*I + 32 # It's not ZERO anymore!
Am I understanding/doing something wrong?Sat, 23 Feb 2013 08:38:37 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/Comment by NahsiN for <p>I perform some analytic calculations involving complex number, in particular complex electric field amplitude. I was quite shocked when I discovered how SAGE handles complex variables.
So, I define a new variable "A" and explicitly say that it is complex. Then I want to find the absolute value of this variable, which is $AA^* = |A|^2$.</p>
<pre><code>sage: var("A", domain="complex")
sage: A*A.conjugate()
A*conjugate(A) # not bad
sage: _.simplify()
A^2 # THIS IS WRONG!
</code></pre>
<p>Furthermore we check, if $AA = |A|^2 = AA^*$, and it does!</p>
<pre><code>sage: A*A.conjugate() - A*A # Substract squared A from absolute value of A
-A^2 + A*conjugate(A)
sage: _.simplify()
0 # So SAGE assumes that they are equal
</code></pre>
<p>But this is obviously WRONG, since if I assign some number to $A$, then the last test does not result in zero:</p>
<pre><code>sage: A=3+4*i
sage: A*A.conjugate() - A*A
-24*I + 32 # It's not ZERO anymore!
</code></pre>
<p>Am I understanding/doing something wrong?</p>
https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=25944#post-id-25944Thanks for asking this question. I had a similar problem in the latest release and this problem persists. Fortunately the workaround suggested by calc314 works.Thu, 26 Feb 2015 15:45:56 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=25944#post-id-25944Answer by kcrisman for <p>I perform some analytic calculations involving complex number, in particular complex electric field amplitude. I was quite shocked when I discovered how SAGE handles complex variables.
So, I define a new variable "A" and explicitly say that it is complex. Then I want to find the absolute value of this variable, which is $AA^* = |A|^2$.</p>
<pre><code>sage: var("A", domain="complex")
sage: A*A.conjugate()
A*conjugate(A) # not bad
sage: _.simplify()
A^2 # THIS IS WRONG!
</code></pre>
<p>Furthermore we check, if $AA = |A|^2 = AA^*$, and it does!</p>
<pre><code>sage: A*A.conjugate() - A*A # Substract squared A from absolute value of A
-A^2 + A*conjugate(A)
sage: _.simplify()
0 # So SAGE assumes that they are equal
</code></pre>
<p>But this is obviously WRONG, since if I assign some number to $A$, then the last test does not result in zero:</p>
<pre><code>sage: A=3+4*i
sage: A*A.conjugate() - A*A
-24*I + 32 # It's not ZERO anymore!
</code></pre>
<p>Am I understanding/doing something wrong?</p>
https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?answer=14587#post-id-14587This is really a long comment to calc314's answer.
This has nothing to do with asking for complex domain specially, because we automatically do that.
sage: var('a')
a
sage: b=a*a.conjugate()-a*a
sage: b
-a^2 + a*conjugate(a)
sage: simplify(b)
0
`simplify` just sends things to Maxima and back. Apparently we don't send the information about the domain to Maxima (nor is it immediately obvious to me if that would be easy).
Maxima 5.26.0 http://maxima.sourceforge.net
using Lisp ECL 12.12.1
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) -a^2+a*conjugate(a);
(%o1) 0
(%i2) declare(a,complex);
(%o2) done
(%i3) -a^2+a*conjugate(a);
2
(%o3) a conjugate(a) - a
Now this is a little odd, because we *explicitly* initialize Maxima with `'domain : complex'`. But apparently that only fixes some of these type of things.
Maxima 5.26.0 http://maxima.sourceforge.net
using Lisp ECL 12.12.1
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) domain:complex;
(%o1) complex
(%i2) -a^2+a*conjugate(a);
(%o2) 0
Edit: according to the Maxima manual, the *only* thing this does is
Option variable: domain
Default value: real
When domain is set to complex, sqrt (x^2) will remain sqrt (x^2) instead of returning abs(x).
Sigh.
Anyway, [Trac 6862](http://trac.sagemath.org/sage_trac/ticket/6862) is essentially this issue.Sat, 23 Feb 2013 14:15:45 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?answer=14587#post-id-14587Answer by calc314 for <p>I perform some analytic calculations involving complex number, in particular complex electric field amplitude. I was quite shocked when I discovered how SAGE handles complex variables.
So, I define a new variable "A" and explicitly say that it is complex. Then I want to find the absolute value of this variable, which is $AA^* = |A|^2$.</p>
<pre><code>sage: var("A", domain="complex")
sage: A*A.conjugate()
A*conjugate(A) # not bad
sage: _.simplify()
A^2 # THIS IS WRONG!
</code></pre>
<p>Furthermore we check, if $AA = |A|^2 = AA^*$, and it does!</p>
<pre><code>sage: A*A.conjugate() - A*A # Substract squared A from absolute value of A
-A^2 + A*conjugate(A)
sage: _.simplify()
0 # So SAGE assumes that they are equal
</code></pre>
<p>But this is obviously WRONG, since if I assign some number to $A$, then the last test does not result in zero:</p>
<pre><code>sage: A=3+4*i
sage: A*A.conjugate() - A*A
-24*I + 32 # It's not ZERO anymore!
</code></pre>
<p>Am I understanding/doing something wrong?</p>
https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?answer=14573#post-id-14573The following seems to work correctly.
var('a')
assume(a, 'complex')
b=a*a.conjugate()-a*a
simplify(b)
Sat, 23 Feb 2013 09:52:01 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?answer=14573#post-id-14573Comment by kcrisman for <p>The following seems to work correctly.</p>
<pre><code>var('a')
assume(a, 'complex')
b=a*a.conjugate()-a*a
simplify(b)
</code></pre>
https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=18179#post-id-18179The reason for this is that `simplify` sends things to Maxima and back (nothing else, really). See my "answer", which however should not be up voted :)Sat, 23 Feb 2013 14:05:31 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=18179#post-id-18179Comment by ximeg for <p>The following seems to work correctly.</p>
<pre><code>var('a')
assume(a, 'complex')
b=a*a.conjugate()-a*a
simplify(b)
</code></pre>
https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=18181#post-id-18181Thank you, it works. As I see, the only difference here is that you used function "assume", and it had effect. But if you specify domain at the definition of variable, it doesn't work. Is it present on the bug tracker? Sat, 23 Feb 2013 10:40:03 -0600https://ask.sagemath.org/question/9838/bug-with-absolute-value-of-a-complex-variable/?comment=18181#post-id-18181