Ask Your Question

ximeg's profile - activity

2017-05-18 20:36:01 +0200 received badge  Nice Question (source)
2015-10-04 20:30:51 +0200 received badge  Famous Question (source)
2014-05-10 06:48:47 +0200 received badge  Notable Question (source)
2013-11-04 10:52:14 +0200 received badge  Popular Question (source)
2013-02-27 11:28:56 +0200 received badge  Student (source)
2013-02-27 11:00:23 +0200 asked a question How to substitute realpart and imagpart after conversion to rectform?

Hello, I need to split some complicated expression into real and imaginary part, and I use function "rectform()" to do it. Then I want to plot just the imaginary part of the expression, and it seems obvious to substitute zero in place of real part. However, the substitution does not work.

Here is a minimal example, which reproduces this behavior. I want to substitute $\Re [A]$ with "$x$":

sage: var("A, x")
-> (A, x)
sage: assume(A, "complex")
sage: Ar = A.rectform()
sage: print Ar
-> realpart(A) + I*imagpart(A)

Then we try to substitute $\Re[x] \to x$:

sage: Ar.subs({real(A): x})
-> realpart(A) + I*imagpart(A)    # No effect
sage: Ar.subs({real_part(A): x})
-> realpart(A) + I*imagpart(A)    # No effect
sage: Ar.subs({A.real(): x})
-> realpart(A) + I*imagpart(A)    # No effect
sage: Ar.subs({A.real_part(): x})
-> realpart(A) + I*imagpart(A)    # No effect
sage: Ar.subs({realpart(A): x})
-> NameError: name 'realpart' is not defined
sage: Ar.subs({A.realpart(): x})
-> AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'realpart'

Then I try to use interface to maxima:

sage: maxima.subst(x, realpart(A), Ar)
-> NameError: name 'realpart' is not defined
sage: maxima.subst(x, real(A), Ar)
-> 'realpart(x)+%i*'imagpart(x)    # Wrong substitution
sage: maxima.subst(x, A.realpart(), Ar)
-> AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'realpart'
sage: maxima.subst(x, A.real(), Ar)
-> 'realpart(x)+%i*'imagpart(x)    # Wrong substitution

As you can see, maxima replaces $A$ with $x$ in the whole expression. E.g., if $x=0$, then after the substitution the whole expression is zero, not just the real part. How to do the substitution? Are there any workarounds?

2013-02-23 17:40:26 +0200 received badge  Supporter (source)
2013-02-23 17:40:03 +0200 commented answer Bug with absolute value of a complex variable?

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 15:39:53 +0200 received badge  Editor (source)
2013-02-23 15:38:37 +0200 asked a question 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?