Skip to content
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

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

Open
kcrisman opened this issue Dec 15, 2014 · 28 comments
Open

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

kcrisman opened this issue Dec 15, 2014 · 28 comments

Comments

@kcrisman
Copy link
Member

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 kcrisman added this to the sage-6.5 milestone Dec 15, 2014
@kcrisman
Copy link
Member Author

comment:1

Worse,

sage: integrate(abs(cos(x)),x,0,pi)
-1

@kcrisman
Copy link
Member Author

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

@kcrisman
Copy link
Member Author

@kcrisman

This comment has been minimized.

@sagetrac-jakobkroeker
Copy link
Mannequin

sagetrac-jakobkroeker mannequin commented Feb 10, 2015

Stopgaps: #12731

@kcrisman
Copy link
Member Author

comment:5

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

@fchapoton
Copy link
Contributor

Changed keywords from none to abs_integrate

@fchapoton
Copy link
Contributor

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
Copy link
Contributor

comment:8

I have added a doctest.


New commits:

4a8dff9trac 17511 adding a doctest

@fchapoton
Copy link
Contributor

Commit: 4a8dff9

@fchapoton
Copy link
Contributor

Author: Frédéric Chapoton

@fchapoton
Copy link
Contributor

Branch: u/chapoton/17511

@videlec
Copy link
Contributor

videlec commented Sep 15, 2019

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
Copy link
Contributor

videlec commented Sep 15, 2019

Reviewer: Vincent Delecroix

@videlec
Copy link
Contributor

videlec commented Sep 15, 2019

comment:10

But this is correct

sage: integrate(abs(sin(x)), x, 0, 10)
cos(10) + 7

@videlec
Copy link
Contributor

videlec commented Sep 15, 2019

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

videlec commented Sep 15, 2019

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
Copy link
Contributor

Changed branch from u/chapoton/17511 to none

@fchapoton
Copy link
Contributor

Changed commit from 4a8dff9 to none

@fchapoton
Copy link
Contributor

comment:16

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

@slel
Copy link
Member

slel commented Oct 7, 2019

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
Copy link
Contributor

embray commented Dec 30, 2019

comment:18

Ticket retargeted after milestone closed

@embray embray modified the milestones: sage-8.9, sage-9.1 Dec 30, 2019
@mkoeppe
Copy link
Contributor

mkoeppe commented May 1, 2020

comment:19

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

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 1, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Oct 24, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 2, 2021

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 mkoeppe modified the milestones: sage-9.3, sage-9.4 Apr 2, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Jul 19, 2021

comment:22

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

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Dec 18, 2021

comment:23

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

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 May 3, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants