sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.45k stars 482 forks source link

Get integral of abs(sin(x)) and abs(cos(x)) right #17511

Open kcrisman opened 9 years ago

kcrisman commented 9 years ago

See this comment, discussion in #13364, the supposed fix at https://sourceforge.net/p/maxima/bugs/2520/, this ask.sagemath question, and so forth.

E.g. this is annoying (but apparently right)

(%i1) load(abs_integrate);
(%i3) display2d:false;

(%o3) false
(%i4) integrate(abs(sin(x)),x);

(%o4) 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)
        -(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(sin(x)^2/(cos(x)+1)^2+1)
         +signum(1/(cos(x)+1))*signum(sin(x))
                              *(2*sin(x)*atan(sin(x)/(cos(x)+1))/(cos(x)+1)
                               -log(2)))
         /4)

but it doesn't do the definite integral at all, even the easy ones where abs does nothing on the interval!

Upstream: Reported upstream. Developers acknowledge bug.

CC: @slel

Component: calculus

Keywords: abs_integrate

Stopgaps: #12731

Author: Frédéric Chapoton

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/17511

kcrisman commented 9 years ago
comment:1

Worse,

sage: integrate(abs(cos(x)),x,0,pi)
-1
kcrisman commented 9 years ago

Changed upstream from Not yet reported upstream; Will do shortly. to Reported upstream. Developers acknowledge bug.

kcrisman commented 9 years ago
comment:2

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

kcrisman commented 9 years ago

Description changed:

--- 
+++ 
@@ -1,6 +1,6 @@
 See [this comment](https://github.com/sagemath/sage/issues/11483#comment:12), discussion in #13364, the supposed fix at https://sourceforge.net/p/maxima/bugs/2520/, [this ask.sagemath question](http://ask.sagemath.org/question/25269/i-dont-know-why-sage-return-0-for-integratesin2xabsx0pi-when-its-value-is-2/), and so forth.

-E.g. apparently this is quite wrong
+E.g. this is annoying (but apparently right)

(%i1) load(abs_integrate); @@ -19,4 +19,4 @@ -log(2))) /4)

-and it doesn't do the definite integral at all, even the easy ones where abs does nothing on the interval!
+but it doesn't do the definite integral at all, even the easy ones where abs does nothing on the interval!
ea1d0bf8-c27a-4548-8cb7-de0b1d02441a commented 9 years ago

Stopgaps: #12731

kcrisman commented 9 years ago
comment:5

Reported again at https://groups.google.com/forum/#!topic/sage-support/1Hcv29UyqVo

fchapoton commented 5 years ago

Changed keywords from none to abs_integrate

fchapoton commented 5 years ago
comment:7

Everything works in 8.9.b7 after #27958. Some doctests were added there.

Maybe one could add one doctest for

sage: integrate(abs(sin(x)),x)
-cos(x)*sgn(sin(x)) + sgn(sin(x))
fchapoton commented 5 years ago
comment:8

I have added a doctest.


New commits:

4a8dff9trac 17511 adding a doctest
fchapoton commented 5 years ago

Commit: 4a8dff9

fchapoton commented 5 years ago

Author: Frédéric Chapoton

fchapoton commented 5 years ago

Branch: u/chapoton/17511

videlec commented 5 years ago
comment:9

This answer

sage: integrate(abs(sin(x)),x)
-cos(x)*sgn(sin(x)) + sgn(sin(x))

is very wrong (outside of [-pi, pi])! It takes negative values

sage: f = integrate(abs(sin(x)), x)
sage: (f(5) - f(0)).n()
-0.716337814536774

and the integral is supposed to go to +oo as x -> +oo/-oo...

videlec commented 5 years ago

Reviewer: Vincent Delecroix

videlec commented 5 years ago
comment:10

But this is correct

sage: integrate(abs(sin(x)), x, 0, 10)
cos(10) + 7
videlec commented 5 years ago
comment:11

And I like very much

sage: %time integrate(abs(sin(x)), x, algorithm='sympy')
CPU times: user 8.09 s, sys: 99 µs, total: 8.09 s
Wall time: 8.1 s
integrate(abs(sin(x)), x)
fchapoton commented 5 years ago
comment:12

Indeed. Giac is responsible for the wrong answer. This should be reported upstream.

sage: integrate(abs(sin(x)),x,algorithm='maxima')
integrate(abs(sin(x)), x)
sage: integrate(abs(sin(x)),x,algorithm='sympy')
integrate(abs(sin(x)), x)
sage: integrate(abs(sin(x)),x,algorithm='giac')
-cos(x)*sgn(sin(x)) + sgn(sin(x))
sage: integrate(abs(sin(x)),x,algorithm='fricas')
integral(abs(sin(x)), x)
sage: integrate(abs(sin(x)),x,algorithm='mathematica_free')
-cos(x)*sgn(sin(x))
fchapoton commented 5 years ago
comment:13

But giac is also providing the correct answer for the definite integral, where sympy fails to deliver:

sage: integrate(abs(cos(x)),x,0,44,algorithm='maxima')
integrate(abs(cos(x)), x, 0, 44)
sage: integrate(abs(cos(x)),x,0,44,algorithm='sympy')
-sin(44) + 4
sage: integrate(abs(cos(x)),x,0,44,algorithm='giac')
sin(44) + 28
sage: integrate(abs(cos(x)),x,0,44,algorithm='fricas')
integrate(abs(cos(x)), x, 0, 44)
sage: integrate(abs(cos(x)),x,0,44,algorithm='mathematica_free')
sin(44) + 28
videlec commented 5 years ago
comment:14

Replying to @fchapoton:

But giac is also providing the correct answer for the definite integral, where sympy fails to deliver:

[...]

which should also be reported upstream I guess...

fchapoton commented 5 years ago

Changed branch from u/chapoton/17511 to none

fchapoton commented 5 years ago

Changed commit from 4a8dff9 to none

fchapoton commented 5 years ago
comment:16

Samuel, would you please report to giac the failure of comment:12 ?

slel commented 5 years ago
comment:17

I emailed Bernard Parisse, here is his reply:

Ce n'est pas une erreur, au sens ou un avertissement est renvoyé
0>> integrate(abs(sin(x))
Warning adding 1 ) at end of input
Attention, l'intégration de abs() ou sign() suppose un signe constant
par intervalles (vérifié si l'argument est réel):
  Verifiez [abs(sin(x))]
Discontinuités aux zeros de sin(x) were not checked
sign(sin(x))-cos(x)*sign(sin(x))
// Time 0.04

C'est à l'utilisateur de prendre garde aux discontinuites de la
primitive pour calculer une intégrale définie s'il utilise la primitive
renvoyée. Lorsqu'on demande à giac une intégrale définie, il essaie de
tenir compte des discontinuités, et il fait le calcul numérique en
parallèle pour verifier. En cas de non concordance, les 2 valeurs sont
renvoyées (la plupart du temps c'est la valeur numérique qui est la bonne).

which roughly translates as

This is not an error, in the sense that a warning is issued:
0>> integrate(abs(sin(x))
Warning adding 1 ) at end of input
Caution, integrating abs() or sign() assumes piecewise constant sign
(check if the argument is real):
Check [abs(sin(x))]
Discontinuities at zeros of sin(x) were not checked
sign(sin(x))-cos(x)*sign(sin(x))
// Time 0.04

It is up to the user to check for discontinuities of the antiderivative
to compute an integral using the antiderivative returned by giac.
When asking giac for an integral on an interval, it tries to take
discontinuities into account, and does the numerical integration in parallel
to check. In case of mismatch, both values are returned (most of the time
the numerical value is the one that is correct).
embray commented 4 years ago
comment:18

Ticket retargeted after milestone closed

mkoeppe commented 4 years ago
comment:19

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

mkoeppe commented 3 years ago
comment:21

Moving this ticket to 9.4, as it seems unlikely that it will be merged in 9.3, which is in the release candidate stage

mkoeppe commented 3 years ago
comment:22

Setting a new milestone for this ticket based on a cursory review.

mkoeppe commented 2 years ago
comment:23

Stalled in needs_review or needs_info; likely won't make it into Sage 9.5.