Skip to content

Document known Maxima simplify_sum bug in symbolic_sum - #42981

Merged
vbraun merged 6 commits into
sagemath:developfrom
vivek41-glitch:fix-symbolic-sum-binomal
Oct 22, 2026
Merged

vbraun merged 6 commits into
sagemath:developfrom
vivek41-glitch:fix-symbolic-sum-binomal

Conversation

@vivek41-glitch

@vivek41-glitch vivek41-glitch commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #42894.

This adds a # known bug doctest to symbolic_sum that records the
incorrect output for sums with finite-support binomials, with a link to
the upstream Maxima ticket.

Change

A single TESTS:: block is added to the symbolic_sum docstring:

sage: k = var('k')
sage: n = 4
sage: sum((3*k)*binomial(n, 3*k-n-2), k, 0, oo)  # known bug
42
sage: n = 11
sage: sum((3*k)*binomial(n, 3*k-n-2), k, 0, oo)  # known bug
12630

The tests are skipped by default. Once the upstream bug is fixed, the
# known bug annotations can be removed.

Verification

./sage -t src/sage/calculus/calculus.py → 481 tests, all passed
(the two known-bug tests are skipped).

Upstream

https://sourceforge.net/p/maxima/bugs/5688/

Sage's default algorithm='maxima' delegates to Maxima's simplify_sum,
which incorrectly evaluates sums of the form
sum(f(k)*binomial(n, c*k+d), k, 0, oo) for concrete integer n.

For example:

    sage: n = 4
    sage: sum((3*k)*binomial(n, 3*k-n-2), k, 0, oo)
    36   # wrong, should be 42

The summand has finite support: binomial(n, 3*k-n-2) vanishes unless
0 <= 3*k-n-2 <= n, so the infinite sum is really a finite sum over a
small range of k.  Maxima's hypergeometric route ignores this and
produces a spurious contribution.  A similar issue was previously
reported for sum(binomial(1,n), n, 0, oo) returning 3 instead of 2.

Fix: add a _finite_binomial_sum helper that detects summands of the
form f(k)*binomial(n, c*k+d) with n a concrete nonnegative integer,
computes the finite support of the binomial, and evaluates the sum
directly.  This bypasses the backend for these cases.  The helper
returns None (falling back to the existing behaviour) when the
pattern is not matched: symbolic n, non-integer top, non-polynomial
bottom, symbolic bounds, float coefficients, or a singularity inside
the support.

The fix is placed before the algorithm dispatch, so it applies to
all backends (Maxima, SymPy, ...), since the finite-support result
is mathematically identical regardless of backend.  hold=True is
unaffected because symbolic_sum returns early in that case.

Regression doctests for n=4 (default and algorithm='sympy'), n=11,
hold=True, symbolic integer coefficients, and float rejection are
added.  The new helper has its own INPUT/OUTPUT/EXAMPLES/TESTS
documentation.

Fixes sagemath#42894.
…hitespace

The helper computed support bounds from the linear coefficient of the
binomial's bottom argument, but did not check that the bottom argument
was actually linear.  For a quadratic like k^2 - k, coefficient(v)
returns -1 (the coefficient of v after treating v^2 as a separate
term), so the helper silently computed the wrong support and returned
a wrong result (1 instead of 8).

Add an explicit degree check and a regression doctest.

Also strip a trailing-whitespace blank line flagged by the RST/ruff
linter (W293).
Substitution inside the support can produce non-finite values without
raising an exception (e.g. log(0) = -Infinity, tan(pi/2) = zoo).  The
helper previously returned such values, contradicting the documented
behaviour that singularities inside the support fall back to the
backend.

Add an explicit check using Expression.is_infinity() (the correct
method; Expression has no is_finite method) and a NaN check via
term != term.  Non-finite terms now cause the helper to return None
and the backend handles the sum.  Regression doctest:

    sage: _finite_binomial_sum(binomial(4, k)*log(k), k, 0, oo) is None
    True
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

@dimpase
dimpase requested a review from fchapoton October 16, 2026 17:09
@mantepse

Copy link
Copy Markdown
Contributor

This looks brittle to me, and I think it should really be fixed upstream. In particular, I guess that it cannot recognize summands of the form binomial(4, a) + binomial(2, b), can it?

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

Thanks @mantepse — fair point. The helper is deliberately narrow: it matches a single binomial(n, c*k+d) directly or inside a product, so binomial(4, a) + binomial(2, b) does fall through, as you say. I kept the scope minimal to avoid touching sums Maxima already handles, and to fix the reported case (#42894) without a broader change.

I agree the root cause is in Maxima's simplify_sum, and this is a stopgap rather than a complete fix. Would you prefer to merge it as a stopgap with an upstream Maxima issue filed in parallel, or close it and pursue the upstream fix first? Happy to do either.

@mantepse

Copy link
Copy Markdown
Contributor

I have no idea. In any case, I think it would be great if you could report it upstream!

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

Will do — I'll file an upstream Maxima issue for simplify_sum on finite-support binomial sums and link it here.

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

Will do — I'll file an upstream Maxima issue for simplify_sum on finite-support binomial sums and link it here.

i reported hear is link :
https://sourceforge.net/p/maxima/bugs/5688/

@antonio-rojas

Copy link
Copy Markdown
Member

I agree the root cause is in Maxima's simplify_sum, and this is a stopgap rather than a complete fix. Would you prefer to merge it as a stopgap with an upstream Maxima issue filed in parallel, or close it and pursue the upstream fix first? Happy to do either.

I don't think we should be reimplementing specific cases like this to workaround upstream issues. It breaks consistency, it would make the code more complex and I'm pretty sure would stay forever even after the issue is fixed upstream.

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

Okay! @antonio-rojas — that reasoning makes sense, and I don't want to add a workaround that outlives the upstream fix.
@dimpase @fchapoton @mantepse do you agree we should close this and leave it to the Maxima fix (https://sourceforge.net/p/maxima/bugs/5688/)? If that's the consensus I'll close the PR and keep the branch as a reference patch.

@dimpase

dimpase commented Oct 17, 2026

Copy link
Copy Markdown
Member

what one can do is to add a doctest to show the problem, and tag it as a known error. And put next to it a comment citing the upstream bug report

Instead of a Sage-side workaround for issue sagemath#42894, record the
upstream bug with a known-bug doctest plus a link to the Maxima
ticket.  See https://sourceforge.net/p/maxima/bugs/5688/.
@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@dimpase got it! i have now replaced the workaround with a # known bug doctest that shows the expected output (42 and 12630) and links to the upstream Maxima ticket https://sourceforge.net/p/maxima/bugs/5688/. When Maxima is fixed, the annotations can be removed.

@antonio-rojas this keeps the code simple and doesn't add a permanent workaround just documentation

@vivek41-glitch vivek41-glitch changed the title Fix symbolic summation of binomial expressions with finite support Document known Maxima simplify_sum bug in symbolic_sum Oct 17, 2026

@dimpase dimpase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you remove quite a lot from calculus.py - what is the reason?

@antonio-rojas

Copy link
Copy Markdown
Member

you remove quite a lot from calculus.py - what is the reason?

Are you looking at the last commit only?

@dimpase dimpase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, OK - indeed I was looking at the wrong place

vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 18, 2026
Release Manager
sagemathgh-42981: Document known Maxima simplify_sum bug in symbolic_sum
    
Fixes sagemath#42894.

This adds a `# known bug` doctest to `symbolic_sum` that records the
incorrect output for sums with finite-support binomials, with a link to
the upstream Maxima ticket.

## Change

A single `TESTS::` block is added to the `symbolic_sum` docstring:

    sage: k = var('k')
    sage: n = 4
    sage: sum((3*k)*binomial(n, 3*k-n-2), k, 0, oo)  # known bug
    42
    sage: n = 11
    sage: sum((3*k)*binomial(n, 3*k-n-2), k, 0, oo)  # known bug
    12630

The tests are skipped by default. Once the upstream bug is fixed, the
`# known bug` annotations can be removed.

## Verification

`./sage -t src/sage/calculus/calculus.py` → 481 tests, all passed
(the two known-bug tests are skipped).

## Upstream

https://sourceforge.net/p/maxima/bugs/5688/
    
URL: sagemath#42981
Reported by: Vivek Dubey
Reviewer(s): Dima Pasechnik
@vbraun
vbraun merged commit 840d952 into sagemath:develop Oct 22, 2026
26 checks passed
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.

A bug in symbolic summation

5 participants