JuliaPy / SymPy.jl

Julia interface to SymPy via PyCall
http://juliapy.github.io/SymPy.jl/
MIT License
268 stars 62 forks source link

equality on complex symbols #230

Open timziebart opened 6 years ago

timziebart commented 6 years ago

Hi, and thanks for the SymPy package. I am trying to use complex numbers and just stumbled over a small problem that I am not sure how to solve:

julia> @syms u
(u,)

julia> real(u) + imag(u)*im == u
false

How would I manage for SymPy to identify this equality correctly? Thanks a lot, Tim

jverzani commented 6 years ago

Yeah, good question. This works (maybe due to a change in the version released today):

real(u) + imag(u)*im == complex(u)

This works:

@vars u real=true
real(u) + imag(u)*im == u

But, these two are not equal

@vars u
real(u) + imag(u)*im, u # (re(u) + I*im(u), u)

There is SymPy's is_complex that might help with your usecase:

is_complex(u) # true

In the most recent change, I noticed that complex is commented out in the assumption code, so this also fails

ask(Q.complex(u))

Whereas, I would have liked it to work like this:

@vars u real=true
ask(Q.real(u))  # true

I'll raise an issue about this so it can be addressed or at least I can be reminded of why.

mzaffalon commented 6 years ago

There are also the assumptions, although the following gives -i*u

u = symbols("u", imaginary=true)
imag(u) # => -i * u

but correctly real(u) returns 0.

EDIT: the result is correct as pointed out by @timkittel below.

jverzani commented 6 years ago

Hmm, this now throws an error! That isn't good. I'll put in a fix, but it seems like I can't fix the minus sign! gamma

timziebart commented 6 years ago

@mzaffalon

There are also the assumptions, although the following gives -i*u

u = symbols("u", imaginary=true) imag(u) # => -i * u and not u

When trying this line, I also get an error. But the result you show is correct, right? Assuming u = ia (with a real), then imag(u) = a = -iu.

Yeah, good question. This works (maybe due to a change in the version released today):

real(u) + imag(u)*im == complex(u)

Awesome, I will use that for now! Still, I would feel that this should either be handled by something like simplify or, and maybe more reasonable, by the actual equality comparison. One could even run complex on both sides of the == sign before actually comparing. Would that make sense?

PS: My use case is that I have some functions representing actual formulas, so I am trying to test their validity symbolically.