Skip to content

Fixed infinite recursion in trig functions #40020

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

Merged
merged 13 commits into from
Jun 1, 2025

Conversation

CalebVL
Copy link
Contributor

@CalebVL CalebVL commented Apr 29, 2025

Fixes #35696

When the trig functions sin, cos, tan, csc, sec, or cot are provided with a symbolic input which expands to 0 they would suffer an infinite recursion error.

This occurred because of a bug in the corresponding sin_eval, cos_eval, tan_eval, csc_eval, sec_eval, and cot_eval functions defined in src/sage/symbolic/ginac/inifcns_trig.cpp. These functions perform a simplification when the real part of the expanded input is 0. This simplification led to infinite recursion when the expanded input was exactly equal to 0.

A check has been added to each of these 6 functions which ensures correct behaviour when the input expands to 0.
Tests have been added to src/sage/functions/trig.py to cover these changes.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

Copy link

Documentation preview for this PR (built with commit 49e3393; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@DaveWitteMorris
Copy link
Member

Thanks for doing this. It looks good, but in pynac you should use x_red_expanded.is_zero() instead of is_zero(x_red_expanded). (You can look elsewhere in the source code to see this. For example, line 743 of the cotan_eval method.) So please change that.

@DaveWitteMorris
Copy link
Member

Local doctesting did not show any unexpected errors.

vbraun pushed a commit to vbraun/sage that referenced this pull request May 28, 2025
sagemathgh-40020: Fixed infinite recursion in trig functions
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Fixes sagemath#35696

When the trig functions sin, cos, tan, csc, sec, or cot are provided
with a symbolic input which expands to 0 they would suffer an infinite
recursion error.

This occurred because of a bug in the corresponding sin_eval, cos_eval,
tan_eval, csc_eval, sec_eval, and cot_eval functions defined in
src/sage/symbolic/ginac/inifcns_trig.cpp. These functions perform a
simplification when the real part of the expanded input is 0. This
simplification led to infinite recursion when the expanded input was
exactly equal to 0.

A check has been added to each of these 6 functions which ensures
correct behaviour when the input expands to 0.
Tests have been added to src/sage/functions/trig.py to cover these
changes.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [X] The title is concise and informative.
- [X] The description explains in detail what this PR is about.
- [X] I have linked a relevant issue or discussion.
- [X] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40020
Reported by: Caleb Van't Land
Reviewer(s):
@vbraun vbraun merged commit 1e504e4 into sagemath:develop Jun 1, 2025
10 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sage crashes on the sine of an expression that expands to 0
3 participants