Skip to content

Re-enable abs_integrate for Maxima integration #40574

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions build/pkgs/maxima/SPKG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,5 @@ Special Update/Build Instructions
All patch files in the patches/ directory are applied. Descriptions of
these patches are either in the patch files themselves or below.

- 0001-taylor2-Avoid-blowing-the-stack-when-diff-expand-isn.patch:
Fix for Maxima bug #2520 (abs_integrate fails on abs(sin(x)) and
abs(cos(x))). Introduced in Issue #13364 (Upgrade Maxima to
5.29.1).

- build-fasl.patch: Build a fasl library for ecl in addition to an
executable program. Introduced in Issue #16178 (Build maxima fasl
without asdf).

- infodir.patch: Correct the path to the Info directory. Introduced
in Issue #11348 (maxima test fails when install tree is moved).

- matrixexp.patch: Fix matrixexp(matrix([%i*%pi])), which broke after
Maxima 5.29.1. Introduced in Issue #13973.

- maxima.system.patch: Set ``c::*compile-in-constants*`` to t.
Introduced in Issue #11966 (OS X 10.7 Lion: Maxima fails to build).

- undoing_true_false_printing_patch.patch: Revert an upstream change
causing '?' to be printed around some words. Introduced in Trac
#13364 (Upgrade Maxima to 5.29.1).
2 changes: 1 addition & 1 deletion src/sage/calculus/desolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def desolve(de, dvar, ics=None, ivar=None, show_method=False, contrib_ode=False,
sage: forget()
sage: y = function('y')(x)
sage: desolve(diff(y, x) == sqrt(abs(y)), dvar=y, ivar=x)
integrate(1/sqrt(abs(y(x))), y(x)) == _C + x
sqrt(-y(x))*(sgn(y(x)) - 1) + (sgn(y(x)) + 1)*sqrt(y(x)) == _C + x

AUTHORS:

Expand Down
11 changes: 8 additions & 3 deletions src/sage/functions/piecewise.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,16 +799,21 @@ def integral(self, parameters, variable, x=None, a=None, b=None, definite=False,
y|-->1/2*y^2 + 3*y + 9/2 on (-3, 0),
y|-->3*y + 9/2 on (0, +oo); y)

::
The output from this can change a bit depending on the
version of Maxima used, so we test for equality with a
known result on the sole piece::

sage: # long time
sage: f1(x) = e^(-abs(x))
sage: f = piecewise([[(-infinity, infinity), f1]])
sage: result = f.integral(definite=True)
...
sage: result
2
sage: f.integral()
piecewise(x|-->-integrate(e^(-abs(x)), x, x, +Infinity) on (-oo, +oo); x)
sage: actual = f.integral().expression_at(0) # only one piece
sage: expected = -1/2*e^(-x)*sgn(x) - 1/2*e^x*sgn(x) - 1/2*e^(-x) + 1/2*e^x + sgn(x) - 1
sage: bool(actual == expected)
True

::

Expand Down
90 changes: 46 additions & 44 deletions src/sage/interfaces/maxima_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@

init_code = ['besselexpand : true', 'display2d : false', 'domain : complex', 'keepfloat : true',
'load(to_poly_solve)', 'load(simplify_sum)',
'load(diag)']
'load(diag)', 'load(abs_integrate)']


# Turn off the prompt labels, since computing them *very
Expand Down Expand Up @@ -726,58 +726,21 @@ def sr_integral(self, *args):
sage: assumptions() # Check the assumptions really were forgotten
[]

Make sure the abs_integrate package is being used,
:issue:`11483`. The following are examples from the Maxima
abs_integrate documentation::
An example from the maxima documentation involving the
absolute value::

sage: integrate(abs(x), x)
1/2*x*abs(x)

::

sage: integrate(sgn(x) - sgn(1-x), x) # known bug
sage: integrate(sgn(x) - sgn(1-x), x)
abs(x - 1) + abs(x)

This is a known bug in Sage symbolic limits code, see
This is a fixed bug in Sage symbolic limits code, see
:issue:`17892` and https://sourceforge.net/p/maxima/bugs/3237/ ::

sage: integrate(1 / (1 + abs(x-5)), x, -5, 6) # not tested -- known bug
sage: integrate(1 / (1 + abs(x-5)), x, -5, 6)
log(11) + log(2)

::

sage: integrate(1/(1 + abs(x)), x) # known bug
1/2*(log(x + 1) + log(-x + 1))*sgn(x) + 1/2*log(x + 1) - 1/2*log(-x + 1)

::

sage: integrate(cos(x + abs(x)), x) # known bug
-1/2*x*sgn(x) + 1/4*(sgn(x) + 1)*sin(2*x) + 1/2*x

The last example relies on the following simplification::

sage: maxima("realpart(signum(x))")
signum(x)

An example from sage-support thread e641001f8b8d1129::

sage: f = e^(-x^2/2)/sqrt(2*pi) * sgn(x-1)
sage: integrate(f, x, -Infinity, Infinity) # known bug
-erf(1/2*sqrt(2))

From :issue:`8624`::

sage: integral(abs(cos(x))*sin(x),(x,pi/2,pi))
1/2

::

sage: integrate(sqrt(x + sqrt(x)), x).canonicalize_radical() # known bug
1/12*((8*x - 3)*x^(1/4) + 2*x^(3/4))*sqrt(sqrt(x) + 1) + 1/8*log(sqrt(sqrt(x) + 1) + x^(1/4)) - 1/8*log(sqrt(sqrt(x) + 1) - x^(1/4))

And :issue:`11594`::

sage: integrate(abs(x^2 - 1), x, -2, 2) # known bug
sage: integrate(abs(x^2 - 1), x, -2, 2)
4

This definite integral returned zero (incorrectly) in at least
Expand All @@ -795,6 +758,45 @@ def sr_integral(self, *args):
0.124756040961038
sage: a.imag().abs() < 3e-17
True

The following examples require Maxima's ``abs_integrate``
package. Enabling ``abs_integrate`` globally caused several
bugs (catalogued in :issue:`12731`) but most of these have
been fixed, and ``abs_integrate`` has been re-enabled::

sage: integrate(1/(abs(x) + 1), x, algorithm="maxima")
1/2*(log(x + 1) + log(-x + 1))*sgn(x) + 1/2*log(x + 1) - 1/2*log(-x + 1)
sage: integrate(cos(x + abs(x)), x, algorithm="maxima")
-1/4*(2*x - sin(2*x))*sgn(x) + 1/2*x + 1/4*sin(2*x)

Several examples where ``abs_integrate`` previously lead to
incorrect results. This was once reported to be divergent in
:issue:`13733`, and only in maxima-5.48 does it give the
correct answer::

sage: # long time, not tested until maxima-5.48 is widespread
sage: integral(log(cot(x)-1), x, 0, pi/4, algorithm="maxima")
catalan + 1/2*I*dilog(1/2*I + 1/2) - 1/2*I*dilog(-1/2*I + 1/2)

This used to return ``1/2`` in :issue:`11590`::

sage: integrate(x * sgn(x^2 - 1/4), x, -1, 0, algorithm="maxima")
-1/4

In :issue:`14591`, this incorrectly simplified to ``cosh(x)``::

sage: integrate(sqrt(1-1/4*cosh(x)^2), x, algorithm="maxima")
integrate(sqrt(-1/4*cosh(x)^2 + 1), x)

In :issue:`17468`, this integral hangs::

sage: integral(log(abs(2*sin(x))), x, 0, pi/3, algorithm="maxima")
1/36*I*pi^2 + I*dilog(1/2*I*sqrt(3) + 1/2) + I*dilog(-1/2*I*sqrt(3) - 1/2)

This used to return a *negative* answer in :issue:`17511`::

sage: integrate(abs(cos(x)), x, 0, pi, algorithm="maxima")
2
"""
try:
return max_to_sr(maxima_eval(([max_integrate],
Expand Down
Loading