Opened 7 years ago

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

Reported by: Owned by: kcrisman major sage-9.7 calculus abs_integrate slelievre Frédéric Chapoton Vincent Delecroix Reported upstream. Developers acknowledge bug. #12731

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!

### comment:1 Changed 7 years ago by kcrisman

Worse,

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

### comment:2 Changed 7 years ago by kcrisman

• Report Upstream changed from Not yet reported upstream; Will do shortly. to Reported upstream. Developers acknowledge bug.

### comment:3 Changed 7 years ago by kcrisman

• Description modified (diff)

### comment:4 Changed 7 years ago by jakobkroeker

• Stopgaps set to #12731

### comment:7 Changed 3 years ago by chapoton

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))
```

### comment:8 Changed 3 years ago by chapoton

• Authors set to Frédéric Chapoton
• Branch set to u/chapoton/17511
• Commit set to 4a8dff966bffe1f792c9d2acbfa9d9c03b15a5e4
• Milestone changed from sage-6.5 to sage-8.9
• Status changed from new to needs_review

New commits:

 ​4a8dff9 `trac 17511 adding a doctest`

### comment:9 Changed 3 years ago by vdelecroix

• Reviewers set to Vincent Delecroix

```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`...

### comment:10 Changed 3 years ago by vdelecroix

But this is correct

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

### comment:11 Changed 3 years ago by vdelecroix

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)
```

### comment:12 Changed 3 years ago by chapoton

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))
```
Last edited 3 years ago by chapoton (previous) (diff)

### comment:13 follow-up: ↓ 14 Changed 3 years ago by chapoton

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
```

### comment:14 in reply to: ↑ 13 Changed 3 years ago by vdelecroix

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...

Last edited 3 years ago by vdelecroix (previous) (diff)

### comment:15 Changed 3 years ago by chapoton

• Branch u/chapoton/17511 deleted
• Commit 4a8dff966bffe1f792c9d2acbfa9d9c03b15a5e4 deleted
• Status changed from needs_review to needs_info

### comment:16 Changed 3 years ago by chapoton

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

### comment:17 Changed 3 years ago by slelievre

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).
```

### comment:18 Changed 2 years ago by embray

• Milestone changed from sage-8.9 to sage-9.1

Ticket retargeted after milestone closed

### comment:19 Changed 2 years ago by mkoeppe

• Milestone changed from sage-9.1 to sage-9.2

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

### comment:20 Changed 19 months ago by mkoeppe

• Milestone changed from sage-9.2 to sage-9.3

### comment:21 Changed 14 months ago by mkoeppe

• Milestone changed from sage-9.3 to sage-9.4

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

### comment:22 Changed 10 months ago by mkoeppe

• Milestone changed from sage-9.4 to sage-9.5

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

### comment:23 Changed 5 months ago by mkoeppe

• Milestone changed from sage-9.5 to sage-9.6

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

### comment:24 Changed 3 weeks ago by mkoeppe

• Milestone changed from sage-9.6 to sage-9.7
Note: See TracTickets for help on using tickets.