Opened 8 years ago

# integral of ln(1+4/5*sin(x)) from -3.1415 to 3.1415 crashes Maxima and Sage

Reported by: Owned by: kcrisman burcin major sage-duplicate/invalid/wontfix calculus integral benjaminfjones, dsm Fixed upstream, in a later stable release.

See extensive discussion at this sage-support thread.

Reported by Ben Jones:

```sage: integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415)

;;;
;;; Binding stack overflow.
;;; Jumping to the outermost toplevel prompt
;;;

...

;;;
;;; Binding stack overflow.
;;; Jumping to the outermost toplevel prompt
;;;

/Users/jonesbe/sage/sage-5.1.beta2/spkg/bin/sage: line 335: 86594 Illegal instruction     sage-ipython "\$@" -i
```

There don't seem to be any problems with Maxima. Although it's been traced down (thanks to Dan Drake) to 5.0.beta3, the only obvious candidate for a change in our interface is when we added `abs_integrate`, which doesn't seem to impact things.

```(%i2) load(abs_integrate);
(%o2) /Users/.../sage-5.0/local/share/maxima/5.26.0/s\
hare/contrib/integration/abs_integrate.mac
(%i4) keepfloat:true;
(%o4)                                true
(%i7) integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415);
3.1415
/
[            4 sin(x)
(%o7)                    I         ln(-------- + 1) dx
]               5
/
- 3.1415
```

And we have

```sage: maxima_calculus(" integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415);")
'integrate(ln(4*sin(x)/5+1),x,-3.1415,3.1415)
```

so something must indeed be wrong in how we're sending this to Maxima.

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

• Description modified (diff)

### comment:2 Changed 8 years ago by dimpase

Could it rather be something wrong in how we are receiving data from Maxima(lib)?

### comment:4 follow-ups: ↓ 6 ↓ 7 Changed 8 years ago by nbruin

I don't think maxima understands `ln`. If you do

```maxima_calculus(" integrate(log(1+4/5*sin(x)), x, -3.1415, 3.1415);")
```

you see the same thing happening. No problem in vanilla maxima. But when we load `abs_integrate`:

```(%i1) load(abs_integrate);
(%o1) /usr/local/sage/5.0/local/share/maxima/5.26.0/share/contrib/integration/abs_integrate.mac
(%i2) integrate(log(1+4/5*sin(x)), x, -3.1415, 3.1415);
...
Condition of type: STACK-OVERFLOW
BINDING-STACK overflow at size 8448. Stack can probably be resized.
```

FYI: with

```from sage.interfaces.maxima_lib import *
from sage.libs.ecl import *
args=(ln(1+4/5*sin(x)), x, -3.1415, 3.1415)
I=EclObject(([max_integrate],[sr_to_max(SR(a)) for a in args]))
I
```

you see what expression gets passed to maxima_lib. With

```EclObject('#\$integrate(log(1+4/5*sin(x)), x, -3.1415, 3.1415)\$')
```

you see what maxima's parser would produce. With

```maxima_eval(I)
```

you can trigger the same explosion. Note that with this trick, you can quickly see that

```EclObject('#\$integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415)\$')
```

creates an essentially different expression.

### comment:5 Changed 8 years ago by nbruin

• Report Upstream changed from N/A to Reported upstream. No feedback yet.

The problem can be simplified to

```integrate(log(sin(x)),x);
```

which maxima without `abs_integrate` can do (or at least it returns an answer).

This is now Maxima Artifact 3533723

### comment:6 in reply to: ↑ 4 Changed 8 years ago by kcrisman

I don't think maxima understands `ln`. If you do

Well, egg on my face! Sorry for that noise. I just don't use Maxima enough beyond trying to generate bug reports or find new functionality for Sage :)

And good diagnosis; I guess if I had tried `log` instead I would have gotten the same problem.

### comment:7 in reply to: ↑ 4 Changed 8 years ago by dimpase

I don't think maxima understands `ln`.

*facepalm*. Sorry for noise on `sage-support`

### comment:8 Changed 8 years ago by nbruin

• Report Upstream changed from Reported upstream. No feedback yet. to Reported upstream. Developers acknowledge bug.

We really need more options for "upstream" ;-). How about "Developers acknowledge bug, propose fix, but haven't gotten around to merging it because they got the fix with less than 24 hours round trip time"? See Maxima tracker. I have no doubt this will be fixed in some future Maxima release.

### comment:9 Changed 8 years ago by nbruin

We can monkey-patch Barton Willis's fix into sage's maxima:

```sage: L="""signum_int(q,x) :=
....: block([w : 1, acc : [], sgn, v, f, listconstvars : true],
....:   q : convert_to_signum(q),
....:   if freeof('signum, q) then
....:     (false)
....:   else (
....:     q : almost_everywhere_simp(q),
....:     v : listofvars(q),
....:     q : block([expandwrt_denom : true], expandwrt(q,'signum)),
....:     if (f : abs_int_extra(q,x)) # false then f
....:     else if (f : partition_as_signum(q,x)) # false then (
....:       w : first(f),
....:       acc : second(f),
....:       if every(lambda([s], linear_in_p(s,x)), acc) then (
....:       sgn : xreduce("*", map(lambda([s], diff(s,x)),acc)),
....:       acc : map(lambda([s], rhs(first(linsolve(s, x)))),acc),
....:       signum_int_helper(signum(sgn) * w, sort(acc), x)))
....:     else if (safe_op(q) = "+") then (
....:         f : map(lambda([s], signum_int(s,x)), args(q)),
....:         if member(false, f) then false else xreduce("+", f))
....:       else block([extra_integration_methods : [],
....:         extra_definite_integration_methods : []],integrate(q,x))
....:   )
....: );"""
sage:  integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415)
integrate(log(4/5*sin(x) + 1), x, -3.1415, 3.1415)
sage: integrate(ln(sin(x)),x)
1/2*I*x^2 - 1/2*x*log(sin(x)^2 + cos(x)^2 - 2*cos(x) + 1) - 1/2*x*log(sin(x)^2 + cos(x)^2 + 2*cos(x) + 1) + x*log(sin(x)) + I*x*arctan2(sin(x), -cos(x) + 1) - I*x*arctan2(sin(x), cos(x) + 1) + I*polylog(2, -e^(I*x)) + I*polylog(2, e^(I*x))

```

This just patches a routine defined in `abs_integrate.mac`. It's a *LOT* slower than in 4.7.1, though, so if performance is any concern, we might want to reconsider whether abs_integrate should be loaded by default. By manipulating the maxima variables `extra_integration_methods` and `extra_definite_integration_methods` it should be possible to control which integration methods maxima tries to use (this is what `abs_integrate` hooks into).

### comment:10 Changed 8 years ago by kcrisman

• Report Upstream changed from Reported upstream. Developers acknowledge bug. to Fixed upstream, but not in a stable release.

### comment:11 Changed 7 years ago by jdemeyer

• Milestone changed from sage-5.11 to sage-5.12

### comment:12 Changed 7 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

### comment:13 Changed 7 years ago by vbraun_spam

• Milestone changed from sage-6.2 to sage-6.3

### comment:14 Changed 6 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4

### comment:16 Changed 2 months ago by dimpase

• Milestone changed from sage-6.4 to sage-duplicate/invalid/wontfix
• Status changed from new to needs_review

I don't see crashes 8 years later on:

```┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.2.beta12, Release Date: 2020-09-06              │
│ Using Python 3.8.5. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
sage: integrate(ln(1+4/5*sin(x)), x, -3.1415, 3.1415)
-1.40205228301
sage: integrate(ln(1+4/5*sin(x)), x, -pi, pi)
integrate(log(4/5*sin(x) + 1), x, -pi, pi)
sage: integrate(log(sin(x)),x)
1/2*I*x^2 - I*x*arctan2(sin(x), cos(x) + 1) + I*x*arctan2(sin(x), -cos(x) + 1) - 1/2*x*log(cos(x)^2 + sin(x)^2 + 2*cos(x) + 1) - 1/2*x*log(cos(x)^2 + sin(x)^2 - 2*cos(x) + 1) + x*log(sin(x)) + I*dilog(-e^(I*x)) + I*dilog(e^(I*x))
```

propose to close as worksforme.

### comment:17 Changed 2 months ago by chapoton

• Resolution set to worksforme
• Status changed from needs_review to closed

ok, then

### comment:18 Changed 2 months ago by kcrisman

• Resolution worksforme deleted
• Status changed from closed to new

As always, doctest ...

### comment:19 Changed 2 months ago by kcrisman

• Report Upstream changed from Fixed upstream, but not in a stable release. to Fixed upstream, in a later stable release.

Also, it was indeed applied in Maxima.

### comment:20 Changed 2 months ago by chapoton

ok, you are right, but I will wait until #25119 is in sage

### comment:21 Changed 2 months ago by kcrisman

obviously no rush whatsoever since it's fixed

Note: See TracTickets for help on using tickets.