Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equality on complex symbols #230

Open
timziebart opened this issue Jul 18, 2018 · 4 comments
Open

equality on complex symbols #230

timziebart opened this issue Jul 18, 2018 · 4 comments

Comments

@timziebart
Copy link

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
Copy link
Collaborator

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.

@jverzani jverzani mentioned this issue Jul 18, 2018
@mzaffalon
Copy link
Contributor

mzaffalon commented Jul 18, 2018

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
Copy link
Collaborator

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

@jverzani jverzani mentioned this issue Jul 18, 2018
@timziebart
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants