Repository navigation
Sum, Product with integer bounds: add the terms, do not go through SymPy - #2012
Merged
Merged
Conversation
With step 1, IterationFunction.eval_iter always converted Sum/Product to
SymPy: the summand was converted, the terms were evaluated by Mathics3,
the result converted to SymPy again, then Together, back to Mathics3 and
Cancel. For integer bounds all that is needed is to evaluate the terms
and add them, as Table does, and the round trip costs far more than the
sum itself when the terms are large symbolic expressions.
Now SymPy is used only when the bounds are not integers. A sum of
rational functions with integer bounds is no longer put over a common
denominator: Sum[1/(x + i), {i, 1, 2}] gives 1/(1 + x) + 1/(2 + x).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
LGTM |
Member
|
Thanks for profiling and fixing this! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SumandProductwith integer bounds now add (or multiply) the terms directly. SymPy is used only when the bounds are not integers.Before this change,
IterationFunction.eval_itersent everySum/Productwith step 1 through SymPy. The summand was converted to SymPy, the terms were evaluated by Mathics3, the result was converted to SymPy again, put throughTogether, converted back to Mathics3 and then put throughCancel. With integer bounds, all that is needed is to evaluate the terms and add them, asTabledoes. When the terms are large symbolic expressions, the round trip costs far more than the sum itself.The new form is, as far as I can tell, also what WMA returns: it does not put an explicit finite sum over a common denominator. Symbolic bounds (
Sum[i, {i, 1, n}]) and infinite sums go through SymPy as before.Tests:
test_sum_product_boundsintest/builtin/test_arithmetic.py. The full pytest suite and doctests pass.Context: we found this while profiling the lambda-algebra homology computation from the Toda-bracket project we are running on Mathics3 (see #2001). In that workload, SymPy conversion inside
Sumtook about 40% of the profile on small cases. Adding the terms is itself costly, though, so on the same core the end-to-end gain is a few percent.🤖 Generated with Claude Code