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

series - Fixes series expansion around float failing with NotImplementedError #23456

Merged
merged 1 commit into from
May 4, 2022

Conversation

praneethratna
Copy link
Contributor

@praneethratna praneethratna commented May 2, 2022

References to other Issues or PRs

Brief description of what is fixed or changed

Fixes #23432 and now series(1/sqrt(1-x**2), x, 0.5) returns

0.769800358919501 + 1.539600717839*(x - 0.5)**2 + 2.39493444997178*(x - 0.5)**3 + 4.33369090947275*(x - 0.5)**4 + 7.75502583800386*(x - 0.5)**5 + 0.769800358919501*x + O((x - 1/2)**6, (x, 1/2))

instead of NotImplementedError.

Other comments

Release Notes

  • series
    • fixes bug in series and now series expansion with float does not give NotImplementedError.

@sympy-bot
Copy link

sympy-bot commented May 2, 2022

Hi, I am the SymPy bot (v163). 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:

  • series
    • fixes bug in series and now series expansion with float does not give NotImplementedError. (#23456 by @praneethratna)

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

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


#### Brief description of what is fixed or changed
Fixes #23432 and now `series(1/sqrt(1-x**2), x, 0.5)` returns 
```
0.769800358919501 + 1.539600717839*(x - 0.5)**2 + 2.39493444997178*(x - 0.5)**3 + 4.33369090947275*(x - 0.5)**4 + 7.75502583800386*(x - 0.5)**5 + 0.769800358919501*x + O((x - 1/2)**6, (x, 1/2))
```
instead of `NotImplementedError`.

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

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 -->
* series
  * fixes bug in `series` and now series expansion with float does not give `NotImplementedError`.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@praneethratna
Copy link
Contributor Author

CC @jksuom

@github-actions
Copy link

github-actions bot commented May 2, 2022

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)

       before           after         ratio
     [77f1d79c]       [ed4e06ef]
     <sympy-1.10.1^0>                 
+       114±0.8ms          202±3ms     1.77  sum.TimeSum.time_doit

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

sympy/core/power.py Outdated Show resolved Hide resolved
sympy/core/power.py Outdated Show resolved Hide resolved
@praneethratna
Copy link
Contributor Author

When I'm trying to add a test case, with the following change - Even though the answer is correct, I'm getting an AssertionError. @jksuom could you help me out here?

diff --git a/sympy/series/tests/test_series.py b/sympy/series/tests/test_series.py
index 34b598a415..f526b3bf0d 100644
--- a/sympy/series/tests/test_series.py
+++ b/sympy/series/tests/test_series.py
@@ -362,3 +362,11 @@ def test_issue_21245():
 def test_issue_21938():
     expr = sin(1/x + exp(-x)) - sin(1/x)
     assert expr.series(x, oo) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
+
+
+def test_issue_23432():
+    expr = 1/sqrt(1 - x**2)
+    assert expr.series(x, 0.5) == \
+        0.769800358919501 + 1.539600717839*(x - 0.5)**2 +\
+        2.39493444997178*(x - 0.5)**3 + 4.33369090947275*(x - 0.5)**4 +\
+        7.75502583800386*(x - 0.5)**5 + 0.769800358919501*x + O((x - 1/2)**6, (x, 1/2))

sympy/core/power.py Outdated Show resolved Hide resolved
@jksuom
Copy link
Member

jksuom commented May 2, 2022

sympy/sympy/core/power.py

Lines 1735 to 1739 in a189657

if not d.is_positive:
g = g.simplify()
_, d = g.leadterm(x)
if not d.is_positive:
raise NotImplementedError()

Two changes: First, two lines between lines 1736 and 1737:

              if g.is_zero:
                  return f**e

Second, 4 lines in place of 1739:

                  g = ((b - f)/f).expand()
                  _, d = g.leadterm(x)
                  if not d.is_positive:
                      raise NotImplementedError()

sympy/core/power.py Outdated Show resolved Hide resolved
sympy/core/power.py Outdated Show resolved Hide resolved
@praneethratna praneethratna requested a review from jksuom May 3, 2022 07:11
@jksuom
Copy link
Member

jksuom commented May 3, 2022

Even though the answer is correct, I'm getting an AssertionError.

Comparisons of expressions containing Floats are notoriously hard to implement. It seems that when a Float is printed, it is truncated, and when the printed expression is input and sympified, it will again be slightly modified so it will often not be exactly the same as the original Float. Comparison of printed strings is sometimes used as a work-around, but that should probably be avoided.

It is possible to use almosteq but that would make the test fairly complicated as all the Floats should be compared pairwise. In this case, I think that it would suffice to devise a test that would show that no error will be raised; for example, that expr.series(x, 0.5) is an Add.

@praneethratna
Copy link
Contributor Author

@jksuom I have added the test case - assert expr.series(x, 0.5) != NotImplementedError , but I'm not sure whether it is the right way or not.

@jksuom
Copy link
Member

jksuom commented May 3, 2022

The purpose of the tests is to give an indication of possible regressions when new code is added. assert expr.series(x, 0.5) != NotImplementedError would allow almost any changes to the series expansion; for example, it would pass even if an error of a different type were raised.
I think that a test of the following type would suffice in most cases:

res = expr.series(x, 0.5)
assert res.is_Add and len(res.args) == 7

@jksuom
Copy link
Member

jksuom commented May 4, 2022

Can you squash the commits? A single commit with a message would suffice.

@jksuom
Copy link
Member

jksuom commented May 4, 2022

Thanks, this is ready to go.

@jksuom jksuom merged commit 21b3279 into sympy:master May 4, 2022
@praneethratna praneethratna deleted the series branch May 4, 2022 09:44
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.

Series expansion around float fails with NotImplementedError
3 participants