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

atan2 may evaluate incorrectly for symbolic expressions #1806

Open
cqc-alec opened this issue Jun 12, 2021 · 4 comments
Open

atan2 may evaluate incorrectly for symbolic expressions #1806

cqc-alec opened this issue Jun 12, 2021 · 4 comments

Comments

@cqc-alec
Copy link
Contributor

There is a (7-year-old) TODO comment in the implementation of atan2 which acknowledges that "ideally the answer should depend on the signs of num and dem". Currently, for example, if a is symbolic then atan2(a,a) evaluates to pi/4, whereas the correct value is dependent on the sign of a. So atan2(-1,-1) gives a different result from that obtained by substituting -1 for a in the expression.

The comment mentions is_positive and is_negative but perhaps sign is enough to fix this issue?

@isuruf
Copy link
Member

isuruf commented Jun 12, 2021

Yes, sign should be enough to fix this issue.

@rikardn
Copy link
Contributor

rikardn commented Jun 20, 2021

I think we need to do what sympy does, which is to simply return atan2(a, a). If a is real and non-zero we can refine it using the refine-function, but for general complex a including a=0 I don't think it is desired or even possible to auto-simplify.

@cqc-alec
Copy link
Contributor Author

I think returning atan2(a, a) would be fine.

But, is atan2(y,x) even defined for non-real arguments? I suppose it could be defined as the principal value of the argument of x + i y for complex x and y, though most implementations including std::atan2 restrict to the reals.

One simplification that can be made is to cancel common factors that are positive constants, like sqrt(2), but I don't know how easy this is to implement.

@isuruf
Copy link
Member

isuruf commented Jun 20, 2021

Here's a draft PR: #1808

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