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

units: fixed convert_to() bug and added some tests #26612

Merged
merged 2 commits into from
May 20, 2024

Conversation

JotacBravo
Copy link
Contributor

References to other Issues or PRs

Fixes #21463

Brief description of what is fixed or changed

Fixed a convert_to() function bug where the result for some inverse sums duplicated the expression units incorrectly.
I found that by using the together() function, these duplicates are prevented when the expression is a function.

Examples:

  • Before

In [1]: convert_to(1/(betameter+meter), 1/meter)
Out [1]: 1/(meter
(meter*beta + meter))

  • After

In [2]: convert_to(1/(betameter+meter), 1/meter)
Out [2]: 1/(meter
beta + meter)

Other comments

Release Notes

  • physics.units
    • Fixed a bug with convert_to function.

@sympy-bot
Copy link

sympy-bot commented May 20, 2024

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 #21463

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

Fixed a convert_to() function bug where the result for some inverse sums duplicated the expression units incorrectly.
I found that by using the together() function, these duplicates are prevented when the expression is a function.

Examples:

- Before

In [1]: convert_to(1/(beta*meter+meter), 1/meter)
Out [1]: 1/(meter*(meter*beta + meter))

- After

In [2]: convert_to(1/(beta*meter+meter), 1/meter) 
Out [2]: 1/(meter*beta + meter)

#### 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 -->
* physics.units
  * Fixed a bug with convert_to function.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

Fixes sympy#21463
Fixed a convert_to() function bug where the result for some inverse
sums duplicated the expression units incorrectly.
I found that by using the together() function, these duplicates are
prevented when the expression is a function.

Examples:

- Before

In [1]: convert_to(1/(beta*meter+meter), 1/meter)
Out [1]: 1/(meter*(meter*beta + meter))

- After

In [2]: convert_to(1/(beta*meter+meter), 1/meter)
Out [2]: 1/(meter*beta + meter)
Copy link

github-actions bot commented May 20, 2024

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 [2487dbb5]    | After [3cc42c49]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 70.1±2ms             | 46.6±2ms            |    0.66 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 68.3±0.7ms           | 44.7±2ms            |    0.65 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 18.3±0.1μs           | 30.6±0.5μs          |    1.67 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.35±0.07ms          | 2.96±0.08ms         |    0.55 | logic.LogicSuite.time_load_file                                      |
| -        | 72.8±0.3ms           | 29.0±0.3ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 72.9±0.3ms           | 29.0±0.2ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 255±0.6ms            | 126±0.6ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 256±0.6ms            | 127±0.7ms           |    0.5  | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 662±4ms              | 373±0.8ms           |    0.56 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 649±2ms              | 372±2ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 494±2μs              | 296±1μs             |    0.6  | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.79±0.02ms          | 1.07±0ms            |    0.6  | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.86±0.03ms          | 3.13±0.03ms         |    0.53 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 448±1μs              | 235±3μs             |    0.52 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.48±0.01ms          | 696±5μs             |    0.47 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.90±0.04ms          | 1.69±0.01ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 371±2μs              | 208±3μs             |    0.56 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.44±0.03ms          | 1.26±0.01ms         |    0.51 | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 10.0±0.07ms          | 4.38±0.01ms         |    0.44 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 362±2μs              | 174±2μs             |    0.48 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.53±0.01ms          | 916±6μs             |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.55±0.09ms          | 2.72±0.02ms         |    0.28 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.02±0.01ms          | 432±3μs             |    0.42 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.71±0.01ms          | 517±2μs             |    0.3  | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 6.06±0.03ms          | 1.80±0.02ms         |    0.3  | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.43±0.02ms          | 1.53±0.01ms         |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 284±1μs              | 67.5±0.2μs          |    0.24 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.43±0.03ms          | 394±2μs             |    0.11 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 4.05±0.01ms          | 287±1μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 6.90±0.09ms          | 1.25±0.01ms         |    0.18 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.81±0.02ms          | 865±4μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.03±0.01ms          | 3.15±0.02ms         |    0.63 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 12.2±0.09ms          | 6.70±0.04ms         |    0.55 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.4±0.08ms          | 9.92±0.02ms         |    0.44 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.26±0.02ms          | 894±10μs            |    0.17 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.6±0.03ms          | 7.21±0.02ms         |    0.57 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 103±0.7ms            | 26.6±0.09ms         |    0.26 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 168±1ms              | 54.5±0.2ms          |    0.32 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 172±1μs              | 114±0.5μs           |    0.66 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 360±4μs              | 223±2μs             |    0.62 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.27±0.02ms          | 855±3μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.31±0.03ms          | 388±0.8μs           |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 19.8±0.1ms           | 2.86±0.03ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 23.0±0.2ms           | 649±2μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 484±4μs              | 139±0.7μs           |    0.29 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.74±0.02ms          | 636±5μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.26±0.03ms          | 143±0.5μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 13.1±0.05ms          | 1.33±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 14.1±0.09ms          | 144±1μs             |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 134±0.5μs            | 76.5±0.3μs          |    0.57 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 249±4μs              | 90.0±0.5μs          |    0.36 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 24.2±0.2ms           | 10.2±0.03ms         |    0.42 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |
| -        | 28.8±0.3ms           | 16.0±0.07ms         |    0.55 | solve.TimeSparseSystem.time_linsolve_Aaug(20)                        |
| -        | 55.3±0.5ms           | 25.3±0.1ms          |    0.46 | solve.TimeSparseSystem.time_linsolve_Aaug(30)                        |
| -        | 28.3±0.2ms           | 15.4±0.09ms         |    0.54 | solve.TimeSparseSystem.time_linsolve_Ab(20)                          |
| -        | 54.9±0.2ms           | 24.9±0.2ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Ab(30)                          |

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 May 20, 2024

Just used the preferred format for creation of symbols in the test suite.

@smichr
Copy link
Member

smichr commented May 20, 2024

We can wait to see if there are any other comments.

Should cancel be used or is together sufficient?

@JotacBravo
Copy link
Contributor Author

We can wait to see if there are any other comments.

Should cancel be used or is together sufficient?

I did some more tests and for all of them, cancel and together gave the same results, so together seemed sufficient to me.

@smichr smichr merged commit 2eb826c into sympy:master May 20, 2024
48 checks passed
@smichr
Copy link
Member

smichr commented May 20, 2024

Nice to have this old issue fixed. Thanks!

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.

Unit conversion returns erroneous result for inverse of sum (bad Pow handling probably)
3 participants