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

Fixed OverflowError in plot #20939

Merged
merged 1 commit into from Feb 19, 2021
Merged

Fixed OverflowError in plot #20939

merged 1 commit into from Feb 19, 2021

Conversation

techiepriyansh
Copy link
Contributor

@techiepriyansh techiepriyansh commented Feb 11, 2021

Changes

Added handling for OverflowError caused while plotting expressions involving exp(1/x).

The Bug

>>> from sympy import *
>>> x = Symbol('x')
>>> plot(exp(1/x))
F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py:1486: RuntimeWarning: invalid value encountered in double_scalars
  cos_theta = dot_product / (vector_a_norm * vector_b_norm)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1725, in plot
    plots.show()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 219, in show
    self._backend.show()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1401, in show
    self.process_series()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1398, in process_series
    self._process_series(series, ax, parent)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1226, in _process_series
    collection = self.LineCollection(s.get_segments())
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 697, in get_segments
    sample(np.array([self.start, f_start]),
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 668, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 668, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  [Previous line repeated 3 more times]
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 690, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 658, in sample
    ynew = f(xnew)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\experimental_lambdify.py", line 170, in __call__
    result = complex(self.lambda_func(args))
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\experimental_lambdify.py", line 267, in __call__
    return self.lambda_func(*args, **kwargs)
  File "<string>", line 1, in <lambda>
OverflowError: math range error

Resolution

During the sampling of the expression exp(1/x), a very large value is encountered which causes it to crash in OverflowError. This error is (kind of) similar to the ZeroDivisionError because in both the cases, the resultant value is arbitrarily large (at least for the computer), and so we handle it just like the ZeroDivisionError (that has already been implemented).

Release Notes

  • plotting
    • Added handling for OverflowError (when plotting functions like exp(1/x)).

@sympy-bot
Copy link

sympy-bot commented Feb 11, 2021

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

  • plotting

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

Click here to see the pull request description that was parsed.
#### Changes
Added handling for `OverflowError` caused while plotting expressions involving `exp(1/x)`.
#### The Bug
```python
>>> from sympy import *
>>> x = Symbol('x')
>>> plot(exp(1/x))
F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py:1486: RuntimeWarning: invalid value encountered in double_scalars
  cos_theta = dot_product / (vector_a_norm * vector_b_norm)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1725, in plot
    plots.show()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 219, in show
    self._backend.show()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1401, in show
    self.process_series()
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1398, in process_series
    self._process_series(series, ax, parent)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 1226, in _process_series
    collection = self.LineCollection(s.get_segments())
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 697, in get_segments
    sample(np.array([self.start, f_start]),
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 668, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 668, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 669, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 691, in sample
    sample(new_point, q, depth + 1)
  [Previous line repeated 3 more times]
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 690, in sample
    sample(p, new_point, depth + 1)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\plot.py", line 658, in sample
    ynew = f(xnew)
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\experimental_lambdify.py", line 170, in __call__
    result = complex(self.lambda_func(args))
  File "F:\MyPrograms\Python\lib\site-packages\sympy\plotting\experimental_lambdify.py", line 267, in __call__
    return self.lambda_func(*args, **kwargs)
  File "<string>", line 1, in <lambda>
OverflowError: math range error
```


#### Resolution
During the sampling of the expression `exp(1/x)`, a very large value is encountered which causes it to crash in `OverflowError`. This error is (kind of) similar to the `ZeroDivisionError` because in both the cases, the resultant value is arbitrarily large (at least for the computer), and so we handle it just like the `ZeroDivisionError` (that has already been implemented).

#### Release Notes
<!-- BEGIN RELEASE NOTES -->
* plotting
  * Added handling for `OverflowError` (when plotting functions like `exp(1/x)`).
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@techiepriyansh techiepriyansh changed the title Handling OverflowError in plot Fixed OverflowError in plot Feb 14, 2021
@techiepriyansh
Copy link
Contributor Author

Reopening the PR due to some bug in the doctests which caused them to fail earlier (Issue #20955).

@oscarbenjamin
Copy link
Contributor

I think this looks reasonable. @sylee957 does it look okay to you?

@sylee957 sylee957 merged commit d506702 into sympy:master Feb 19, 2021
@techiepriyansh techiepriyansh deleted the patch-2 branch February 19, 2021 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants