Skip to content

Conversation

gschintgen
Copy link
Member

References to other Issues or PRs

Fixes #17300

Brief description of what is fixed or changed

Before this commit, expressions of the form arg(exp()) are not special cased and the argument to arg(), including exp(), ends up being split apart by .as_real_imag(). The real and imaginary parts are then passed to atan2. This often leads to poor results. See issue #17300 for examples.

This commit adds a special case for arg(exp()) to better handle these expressions.

BEFORE:

>>> arg(exp(1 + 5*I))
atan(sin(5)/cos(5))
>>> arg(exp(1 + I*pi/8))
atan(sqrt(1/2 - sqrt(2)/4)/sqrt(sqrt(2)/4 + 1/2))

AFTER:

>>> arg(exp(1 + 5*I))
5 - 2⋅π
>>> arg(exp(1 + I*pi/8))
π
-
8

Other comments

Release Notes

  • functions
    • Improved automatic evaluation of arg(exp()).

Fixes sympy#17300.

Before this commit, expressions of the form arg(exp()) are not
special cased and the argument to arg(), including exp(), ends
up being split apart by .as_real_imag(). The real and imaginary
parts are then passed to atan2. This often leads to poor results.
See issue sympy#17300 for examples.

This commit adds a special case for arg(exp()) to better handle
these expressions.

BEFORE:

>>> arg(exp(1 + 5*I))
atan(sin(5)/cos(5))
>>> arg(exp(1 + I*pi/8))
atan(sqrt(1/2 - sqrt(2)/4)/sqrt(sqrt(2)/4 + 1/2))

AFTER:

>>> arg(exp(1 + 5*I))
5 - 2⋅π
>>> arg(exp(1 + I*pi/8))
π
-
8
@sympy-bot
Copy link

sympy-bot commented Dec 4, 2023

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.13.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes #17300

#### Brief description of what is fixed or changed

Before this commit, expressions of the form `arg(exp())` are not special cased and the argument to `arg()`, including `exp()`, ends up being split apart by `.as_real_imag()`. The real and imaginary parts are then passed to `atan2`. This often leads to poor results. See issue #17300 for examples.

This commit adds a special case for `arg(exp())` to better handle these expressions.

BEFORE:
```
>>> arg(exp(1 + 5*I))
atan(sin(5)/cos(5))
>>> arg(exp(1 + I*pi/8))
atan(sqrt(1/2 - sqrt(2)/4)/sqrt(sqrt(2)/4 + 1/2))
```
AFTER:
```
>>> arg(exp(1 + 5*I))
5 - 2⋅π
>>> arg(exp(1 + I*pi/8))
π
-
8
```

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* functions
  * Improved automatic evaluation of `arg(exp())`.

<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

Copy link

github-actions bot commented Dec 4, 2023

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

| Change   | Before [a00718ba]    | After [4adf8d24]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 67.5±0.7ms           | 43.5±0.3ms          |    0.64 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 67.3±1ms             | 44.1±0.4ms          |    0.66 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 18.0±0.2μs           | 29.1±0.4μs          |    1.62 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.18±0.03ms          | 2.83±0.03ms         |    0.55 | logic.LogicSuite.time_load_file                                      |
| -        | 73.6±0.2ms           | 28.1±0.1ms          |    0.38 | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 25.8±0.08ms          | 16.7±0.05ms         |    0.65 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 72.1±0.6ms           | 28.4±0.1ms          |    0.39 | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 254±1ms              | 123±0.9ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 255±2ms              | 126±1ms             |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 661±5ms              | 374±2ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 660±3ms              | 369±3ms             |    0.56 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 492±2μs              | 281±2μs             |    0.57 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.74±0.01ms          | 1.05±0ms            |    0.6  | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.71±0.02ms          | 3.06±0.02ms         |    0.54 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 449±2μs              | 233±2μs             |    0.52 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.46±0.01ms          | 658±4μs             |    0.45 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.83±0.02ms          | 1.62±0.01ms         |    0.33 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 372±2μs              | 202±2μs             |    0.54 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.43±0.02ms          | 1.21±0.01ms         |    0.5  | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 10.0±0.07ms          | 4.24±0.02ms         |    0.42 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 360±2μs              | 165±0.9μs           |    0.46 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.47±0.01ms          | 877±10μs            |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.39±0.07ms          | 2.59±0.02ms         |    0.28 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 999±10μs             | 419±2μs             |    0.42 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.71±0.02ms          | 499±3μs             |    0.29 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 5.68±0.05ms          | 1.76±0.01ms         |    0.31 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.27±0.04ms          | 1.50±0ms            |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 288±2μs              | 63.1±0.5μs          |    0.22 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.29±0.03ms          | 389±2μs             |    0.12 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 3.90±0.02ms          | 279±2μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 6.78±0.1ms           | 1.23±0.01ms         |    0.18 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.47±0.05ms          | 838±4μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.00±0.03ms          | 2.96±0.01ms         |    0.59 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 11.8±0.09ms          | 6.38±0.03ms         |    0.54 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.2±0.1ms           | 9.05±0.03ms         |    0.41 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.26±0.01ms          | 860±2μs             |    0.16 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.6±0.03ms          | 7.04±0.04ms         |    0.56 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 98.7±0.7ms           | 25.4±0.2ms          |    0.26 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 165±2ms              | 54.4±0.2ms          |    0.33 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 176±2μs              | 114±3μs             |    0.64 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 366±2μs              | 213±2μs             |    0.58 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.20±0.01ms          | 823±3μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.22±0.04ms          | 377±1μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 19.2±0.09ms          | 2.78±0.01ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 22.2±0.2ms           | 623±2μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 490±5μs              | 132±0.8μs           |    0.27 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.64±0.06ms          | 601±3μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.22±0.03ms          | 136±0.6μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 12.7±0.1ms           | 1.29±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.4±0.09ms          | 138±1μs             |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 131±0.3μs            | 74.1±0.4μs          |    0.57 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 251±2μs              | 87.5±1μs            |    0.35 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 24.1±0.06ms          | 10.2±0.03ms         |    0.42 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@smichr
Copy link
Member

smichr commented Dec 5, 2023

Looks ok to me. I don't prefer the i_ variable but understand that in context it is probably being distinguished from i (or I). Just a nit.

Will commit in 12 hours if there is no other argument. This is a nice clean up to automatic evaluation of such expressions!

@smichr smichr merged commit cec82f2 into sympy:master Dec 5, 2023
@gschintgen gschintgen deleted the fix-arg-exp branch December 5, 2023 19:09
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

Successfully merging this pull request may close these issues.

improve simplification of arg(exp(real*I))
4 participants