rtoy / maxima

A Clone of Maxima's repo
Other
0 stars 0 forks source link

abs_integrate fails on abs(sin(x)) and abs(cos(x)) #1102

Closed rtoy closed 2 months ago

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:01 Created by dimpase on 2012-12-17 10:22:44 Original: https://sourceforge.net/p/maxima/bugs/2520


Summary: integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) are computed incorrectly. integrate(abs(sin(x)),x,0,2*%pi) causes crashes, and integrate(abs(cos(x)),x,0,2*%pi) returns nonsense.

In more detail: Maxima 5.28.0 (SBCL/OSX 10.6.8 - downloaded from SF version), Maxima 5.26.0 (ECL 11.1 / Sage), Maxima 5.29.1 (ECL 12.12 and CLISP) all compute integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) incorrectly.

On Maxima 5.26.0 integrate(abs(sin(x)),x,0,2*%pi) returns the unevaluated integral, on Maxima 5.29.1 it causes segfaults, on Maxima 5.28.0 one gets "Control stack exhausted (no more space for function call frames)." On Maxima 5.28.0 integrate(abs(cos(x)),x,0,2*%pi) returns 0, after a long wait and many messages "log: encountered log(0)."

See more details on http://trac.sagemath.org/sage_trac/ticket/13364 (this is where we stumbled upon this problem).

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:03 Created by rswarbrick on 2012-12-17 11:02:43 Original: https://sourceforge.net/p/maxima/bugs/2520/#927e


Confusingly, on my machine, both the current git version and 2.28.0 give noun forms:

rupert@skate /s/n/maxima> ./maxima-local
Maxima 5.28.0_130_g925e3f2_dirty http://maxima.sourceforge.net
using Lisp SBCL 1.0.57.0.debian
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) integrate(abs(sin(x)),x,0,2*%pi);
                              2 %pi
                             /
                             [
(%o1)                        I      abs(sin(x)) dx
                             ]
                             /
                              0
(%i2) integrate(abs(cos(x)),x,0,2*%pi);
                              2 %pi
                             /
                             [
(%o2)                        I      abs(cos(x)) dx
                             ]
                             /
                              0
(%i3) rupert@skate /s/n/maxima> maxima

Maxima 5.28.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) integrate(abs(cos(x)),x,0,2*%pi);
                              2 %pi
                             /
                             [
(%o1)                        I      abs(cos(x)) dx
                             ]
                             /
                              0
(%i2) integrate(abs(sin(x)),x,0,2*%pi);
                              2 %pi
                             /
                             [
(%o2)                        I      abs(sin(x)) dx
                             ]
                             /
                              0
rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:06 Created by dimpase on 2012-12-17 11:40:00 Original: https://sourceforge.net/p/maxima/bugs/2520/#637b


one needs to explicitly load(abs_integrate);

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:10 Created by rswarbrick on 2012-12-17 17:07:50 Original: https://sourceforge.net/p/maxima/bugs/2520/#e063


Some more poking shows that, with abs(sin(x)), everything dies because Maxima is trying to find the limit of some truly hideous expression, returned by

(ANTIDERIV '((MABS SIMP) ((%SIN SIMP) $X)))

namely:

4*((signum(1/(cos(x)+1))*signum(sin(x))*log(abs(sin(x)/(cos(x)+1)+%i))
  +signum(1/(cos(x)+1))*signum(sin(x))*log(abs(sin(x)/(cos(x)+1)-%i))
  -signum(1/(cos(x)+1))*signum(sin(x))*log(2*sin(x)^2/(cos(x)+1)^2+2)
  +signum(1/(cos(x)+1))*signum(sin(x))
                       *(log(2)-2*sin(x)*atan(sin(x)/(cos(x)+1))/(cos(x)+1)))
  /4
  +abs(sin(x))*(atan(sin(x)/(cos(x)+1))/2
               +sin(x)/((cos(x)+1)*(2*sin(x)^2/(cos(x)+1)^2+2)))
   /abs(cos(x)+1))

It looks plausibly correct when you check it between, say, 0.1 and 0.5, but then when you plot it you see that it's not even continuous! (I think that at every point it is continuous, the function is smooth and its derivative is probably equal to abs(sin(x)), but that's not particularly helpful!)

A picture: [[img src=antideriv.png alt=graph of antiderivative]]

Attachments:

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:13 Created by rswarbrick on 2012-12-17 19:49:37 Original: https://sourceforge.net/p/maxima/bugs/2520/#4873


And a bit further... We're dying in a call to $taylor. A related call that blows the stack is

taylor (abs(sin(x))*atan(sin(x)/(cos(x)+1)), x, 0, 0);

Removing abs(sin(x)) stops things going haywire.

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:17 Created by willisbl on 2012-12-17 20:50:22 Original: https://sourceforge.net/p/maxima/bugs/2520/#96cd


Using GCL and Clozure CL, I'm unable to reproduce segfaults or crashes. Maybe these errors have something to do with ECL.

In neighborhoods of zero, integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) are both correct. Unfortunately these antiderivaties are not valid on the reals. Maxima incorrectly assumes that these antiderivaties are valid on larger sets, and thus it incorrectly computes some definite integrals.

Most all of this happens outside of the abs_integrate code. Maxima makes some fairly dubious substitutions, and the abs_integrate code (as far as I can tell) correctly integrates an expression that is mostly rational, but involves some absolute values.

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:20 Created by rswarbrick on 2012-12-17 21:16:24 Original: https://sourceforge.net/p/maxima/bugs/2520/#3328


Barton: That's surprising. I only have git master compiled with sbcl, but on ECL with 5.28.0, I get:

rupert@skate ~> maxima
Maxima 5.28.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) load("abs_integrate");
(%o1) /usr/share/maxima/5.28.0/share/contrib/integration/abs_integrate.mac
(%i2) integrate (abs(sin(x)), x, 0, 2*%pi);

Maxima encountered a Lisp error:

 Error in MACSYMA-TOP-LEVEL [or a callee]: Bind stack overflow.

Automatically continuing.
To enable the Lisp debugger set *debugger-hook* to nil.
(%i3) 

which seems to be what the bug reporter described. As far as I can tell, it's to do with Taylor going off the deep end on the expression in my previous note. I plan to trace and debug it further this evening, but I'd be very surprised if the behaviour varied between lisps.

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:24 Created by robert_dodier on 2012-12-17 21:34:31 Original: https://sourceforge.net/p/maxima/bugs/2520/#3c38


Diff:


--- old
+++ new
@@ -1,10 +1,8 @@
-Summary: integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) are computed incorrectly. integrate(abs(sin(x)),x,0,2*%pi) causes crashes, and integrate(abs(cos(x)),x,0,2*%pi) returns nonsense.
+Summary: integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) are computed incorrectly. integrate(abs(sin(x)),x,0,2\*%pi) causes crashes, and integrate(abs(cos(x)),x,0,2\*%pi) returns nonsense.

 In more detail: Maxima 5.28.0 (SBCL/OSX 10.6.8 - downloaded from SF version), Maxima 5.26.0 (ECL 11.1 / Sage), Maxima 5.29.1 (ECL 12.12 and CLISP) all compute integrate(abs(sin(x)),x) and integrate(abs(cos(x)),x) incorrectly.

-On Maxima 5.26.0 integrate(abs(sin(x)),x,0,2*%pi) returns the unevaluated integral, on Maxima 5.29.1 it causes segfaults, on Maxima 5.28.0 one gets "Control stack exhausted (no more space for function call frames)."
-On Maxima 5.28.0 integrate(abs(cos(x)),x,0,2*%pi) returns 0, after a long wait and many messages "log: encountered log(0)."
+On Maxima 5.26.0 integrate(abs(sin(x)),x,0,2\*%pi) returns the unevaluated integral, on Maxima 5.29.1 it causes segfaults, on Maxima 5.28.0 one gets "Control stack exhausted (no more space for function call frames)."
+On Maxima 5.28.0 integrate(abs(cos(x)),x,0,2\*%pi) returns 0, after a long wait and many messages "log: encountered log(0)."

 See more details on http://trac.sagemath.org/sage_trac/ticket/13364 (this is where we stumbled upon this problem).
-
-
rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 00:07:28 Created by robert_dodier on 2012-12-17 21:34:31 Original: https://sourceforge.net/p/maxima/bugs/2520/#cc98


Fix up formatting: throw in backslashes to make literal asterisks.