Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)
sage: Ar.subs({real(A): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({real_part(A): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real(): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real_part(): x})
realpart(A) + I*imagpart(A)
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)
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'realpart'
sage: maxima.subst(x, A.real(), Ar)
'realpart(x)+%i*'imagpart(x)

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?

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

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

realpart(A) + I*imagpart(A)
sage: Ar.subs({real(A): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({real_part(A): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real(): x})
realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real_part(): x})
realpart(A) + I*imagpart(A)
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)
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'realpart'
sage: maxima.subst(x, A.real(), Ar)
'realpart(x)+%i*'imagpart(x)

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?

click to hide/show revision 3
correction of the wrong code

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$:

realpart(A) + I*imagpart(A)
sage: Ar.subs({real(A): x})
-> realpart(A) + I*imagpart(A)
sage: Ar.subs({real_part(A): x})
-> realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real(): x})
-> realpart(A) + I*imagpart(A)
sage: Ar.subs({A.real_part(): x})
-> realpart(A) + I*imagpart(A)
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)
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)

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?

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)
I*imagpart(A)    # No effect
sage: Ar.subs({real_part(A): x})
-> realpart(A) + I*imagpart(A)
I*imagpart(A)    # No effect
sage: Ar.subs({A.real(): x})
-> realpart(A) + I*imagpart(A)
I*imagpart(A)    # No effect
sage: Ar.subs({A.real_part(): x})
-> realpart(A) + I*imagpart(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)
'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)
'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?