Repository navigation
Document known Maxima simplify_sum bug in symbolic_sum - #42981
Conversation
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
|
Documentation preview for this PR (built with commit 0d1eae5; changes) is ready! 🎉 |
|
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 |
|
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. |
|
I have no idea. In any case, I think it would be great if you could report it upstream! |
|
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 : |
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. |
|
Okay! @antonio-rojas — that reasoning makes sense, and I don't want to add a workaround that outlives the upstream fix. |
|
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/.
|
@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 |
dimpase
left a comment
There was a problem hiding this comment.
you remove quite a lot from calculus.py - what is the reason?
Are you looking at the last commit only? |
dimpase
left a comment
There was a problem hiding this comment.
ah, OK - indeed I was looking at the wrong place
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
Fixes #42894.
This adds a
# known bugdoctest tosymbolic_sumthat records theincorrect output for sums with finite-support binomials, with a link to
the upstream Maxima ticket.
Change
A single
TESTS::block is added to thesymbolic_sumdocstring:The tests are skipped by default. Once the upstream bug is fixed, the
# known bugannotations 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/