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

edit retag close merge delete

Sort by ยป oldest newest most voted

In Sage 5.0, I'm not getting access to rectform as part of the object A here. The following works for me:

var('A x')
assume(A,"complex")
ar=real_part(A)+I*imag_part(A)
ar.subs(real_part(A)==x)


x + I*imag_part(A)

Also, the substitution with a dictionary works for me.

ar.subs({real_part(A):x})


x + I*imag_part(A)

In Sage 5.3, I'm getting access to rectform, and I have the difficulty you are having with realpart. I think it's got to be the difference between realpart and real_part.

Perhaps someone here has insight into the differences.

more