To assume that x is real, define it like

x = Symbol('x', real=True)

Then you can use the functions re() and im(), or the method
.as_real_imag() to get the real and imaginary parts.

In [1]: x, y = symbols('x y', real=True)

In [2]: (x + I*y).as_real_imag()
Out[2]: (x, y)

In [3]: re(x + I*y)
Out[3]: x

In [4]: im(x + I*y)
Out[4]: y

Aaron Meurer

On Thu, Jun 9, 2011 at 4:31 PM, [email protected]
<[email protected]> wrote:
> Hi,
>
> Short version of the question: How to take the real/imaginary part of a
> symbolic expression? Can real(x + I*y) give me "x" with the proper
> assumptions in place?
>
>
> Here is the context:
> I'm implementing a small gaussian optics module that I am going to need
> during an internship. Here is my problem.
>
> There is a thing called complex beam parameter that is a complex number
> whose real part is a certain quantity describing the beam and the imaginary
> part is another such quantity. The formalism treats them together in this
> complex number.
>
> I want to have a class with the constructor:
>
>>>>a=Constructor(quantityA, quantityB)
>>>>a == quantityA + I*quantityB
> True
>
> with the assumptions that quantityA and quantityB are real.
>
> Then I want to have the selectors (@property decorators):
>
>>>>a.quantA
> quantityA
>>>>q.quantB
> quantityB
>
> My idea was just to define quantA as real(a) but as far as what the
> documentation or Google say there is no function "real" for a general
> symbolic expression in sympy. I suppose I have missed something. Can I take
> the real part of a general expression? Can real(x + I*y) give me "x" with
> the proper assumptions in place?
>
> Regars
> Stefan
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to