Ask Your Question
2

Bug with absolute value of a complex variable?

asked 2013-02-23 15:38:37 +0100

ximeg gravatar image

updated 2023-01-09 23:59:31 +0100

tmonteil gravatar image

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?

edit retag flag offensive close merge delete

Comments

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.

NahsiN gravatar imageNahsiN ( 2015-02-26 22:45:56 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-02-23 16:52:01 +0100

calc314 gravatar image

The following seems to work correctly.

var('a')
assume(a, 'complex')
b=a*a.conjugate()-a*a
simplify(b)
edit flag offensive delete link more

Comments

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?

ximeg gravatar imageximeg ( 2013-02-23 17:40:03 +0100 )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 :)

kcrisman gravatar imagekcrisman ( 2013-02-23 21:05:31 +0100 )edit
1

answered 2013-02-23 21:15:45 +0100

kcrisman gravatar image

updated 2013-02-23 21:21:03 +0100

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2013-02-23 15:38:37 +0100

Seen: 2,774 times

Last updated: Feb 23 '13