# 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()
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?

edit retag close merge delete

Thanks for asking this question. I had a similar problem in the latest release and this problem persists. Fortunately the workaround suggested by calc314 works.

( 2015-02-26 15:45:56 -0500 )edit

Sort by » oldest newest most voted

The following seems to work correctly.

var('a')
assume(a, 'complex')
b=a*a.conjugate()-a*a
simplify(b)

more

Thank 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?

( 2013-02-23 10:40:03 -0500 )edit

The 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 :)

( 2013-02-23 14:05:31 -0500 )edit

This 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
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
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 is essentially this issue.

more