sagemath / sage

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

Make sure integral(f(x) - f(x).expand(), (x, 0, 2*pi)) is zero #33034

Open 4c8eece5-96d5-4233-85f8-39c1bf9c5e17 opened 2 years ago

4c8eece5-96d5-4233-85f8-39c1bf9c5e17 commented 2 years ago

See this example in Sage 9.4 or Sage 9.5.beta7:

sage: f(x) = (x + sin(3*x)) * exp(-3*x*I)
sage: h(x) = f(x) - f(x).expand()
sage: integral(h(x), (x, 0, 2*pi))
I*pi

The answer should be 0 (the integrand is the constant zero function).

Many other examples (related to Fourier coefficients) give similar errors. For instance:

sage: f(x) = (x + cos(x)) * exp(-x*I)
sage: h(x) = f(x) - f(x).expand()
sage: integral(h(x), (x, 0, 2*pi))
-pi

It should again be 0.

The bug is in Maxima. Indeed, in each case:

sage: aa = ['giac', 'sympy', 'fricas']
sage: [integral(h(x), (x, 0, 2*pi), algorithm=a) for a in aa]
[0, 0, 0]

Update: See Maxima bug 3921.

Upstream: Fixed upstream, but not in a stable release.

CC: @slel @kcrisman

Component: calculus

Keywords: bug, integral, maxima

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

slel commented 2 years ago

Description changed:

--- 
+++ 
@@ -1,22 +1,29 @@
-See this example:
+See this example in Sage 9.4 or Sage 9.5.beta7:

-f(x)=(x+sin(3*x))*exp(-3*x*I)
+```
+sage: f(x) = (x + sin(3*x)) * exp(-3*x*I)
+sage: h(x) = f(x) - f(x).expand()
+sage: integral(h(x), (x, 0, 2*pi))
+I*pi
+```
+The answer should be 0 (the integrand
+is the constant zero function).

-g(x)=f(x).expand()
+Many other examples (related to Fourier coefficients)
+give similar errors. For instance:

-integral(f(x)-g(x),(x,0,2*pi))
+```
+sage: f(x) = (x + cos(x)) * exp(-x*I)
+sage: h(x) = f(x) - f(x).expand()
+sage: integral(h(x), (x, 0, 2*pi))
+-pi
+```
+It should again be 0.

-The answer is I*pi, but it should be 0 (we are integrating the constant function = 0).
+The bug is in Maxima. Indeed, in each case:

-Many other examples (related to Fourier coefficients) give similar errors. For instance:
-
-f(x)=(x+cos(x))*exp(-x*I)
-
-g(x)=f(x).expand()
-
-integral(f(x)-g(x),(x,0,2*pi))
-
-The answer is -pi, and it should be 0.
-
-It is clear that this is a bug "provided" by maxima.
-
+```
+sage: aa = ['giac', 'sympy', 'fricas']
+sage: [integral(h(x), (x, 0, 2*pi), algorithm=a) for a in aa]
+[0, 0, 0]
+```
fchapoton commented 2 years ago
comment:2

This should be reported upstream, please

kcrisman commented 2 years ago

Changed author from Juan Luis Varona to none

kcrisman commented 2 years ago
comment:3

This must be newish, as I can't reproduce it in my somewhat old Sage. (Note here for the domain business.)

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.0, Release Date: 2020-01-01                     │
│ Using Python 3.7.3. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
sage: f = (x + sin(3*x)) * exp(-3*x*I)
sage: h = f.expand()
sage: f-h
(x + sin(3*x))*e^(-3*I*x) - x*e^(-3*I*x) - e^(-3*I*x)*sin(3*x)
sage: integrate(f-h,x,0,2*pi)
0
sage: (f-h)._maxima_()
%e^-(3*%i*_SAGE_VAR_x)*(sin(3*_SAGE_VAR_x)+_SAGE_VAR_x)-%e^-(3*%i*_SAGE_VAR_x)*sin(3*_SAGE_VAR_x)-_SAGE_VAR_x*%e^-(3*%i*_SAGE_VAR_x)
sage: maxima_console()
;;; Loading #P"/Applications/SageMath-9.0.app/Contents/Resources/sage/local/lib/ecl/sb-bsd-sockets.fas"
;;; Loading #P"/Applications/SageMath-9.0.app/Contents/Resources/sage/local/lib/ecl/sockets.fas"
;;; Loading #P"/Applications/SageMath-9.0.app/Contents/Resources/sage/local/lib/ecl/defsystem.fas"
;;; Loading #P"/Applications/SageMath-9.0.app/Contents/Resources/sage/local/lib/ecl/cmp.fas"
Maxima 5.42.2 http://maxima.sourceforge.net
using Lisp ECL 16.1.2
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) domain;
(%o1)                                real
(%i2) domain:complex;
(%o2)                               complex
(%i3) f:%e^-(3*%i*x)*(sin(3*x)+x)-%e^-(3*%i*x)*sin(3*x)-x*%e^-(3*%i*x);
          - 3 %i x                    - 3 %i x                - 3 %i x
(%o3)   %e         (sin(3 x) + x) - %e         sin(3 x) - x %e
(%i5) integrate(f,x,0,2*%pi);
(%o5)                                  0

However, in my somewhat more recent Sage 9.4 beta:

Maxima 5.44.0 http://maxima.sourceforge.net
using Lisp ECL 21.2.1
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) domain:complex;
(%o1)                               complex
(%i2) f:%e^-(3*%i*x)*(sin(3*x)+x)-%e^-(3*%i*x)*sin(3*x)-x*%e^-(3*%i*x);
          - 3 %i x                    - 3 %i x                - 3 %i x
(%o2)   %e         (sin(3 x) + x) - %e         sin(3 x) - x %e
(%i3) integrate(f,x,0,2*%pi);
(%o3)                               %i %pi

This will help them track it down, I hope. Note that there is clearly a branch cut issue, or worse:

(%i3) integrate(f,x,0,2);        
(%o3)                                  0
(%i4) integrate(f,x,0,1);
(%o4)                                  0
(%i5) integrate(f,x,0,2*%pi);
(%o5)                               %i %pi
(%i6) integrate(f,x,0,-%pi);
                                     %i %pi
(%o6)                              - ------
                                       3
(%i7) integrate(f,x,0,3*%pi);
                                   4 %i %pi
(%o7)                              --------
                                      3
(%i8) integrate(f,x,-%pi,%pi);
(%o8)                                  0
(%i9) integrate(f,x,-%pi,2*%pi);
                                   4 %i %pi
(%o9)                              --------
(%i10) integrate(f,x,-1,1);
(%o10)                                 0
kcrisman commented 2 years ago

Upstream: Reported upstream. No feedback yet.

kcrisman commented 2 years ago

Description changed:

--- 
+++ 
@@ -27,3 +27,5 @@
 sage: [integral(h(x), (x, 0, 2*pi), algorithm=a) for a in aa]
 [0, 0, 0]

+ +Update: See Maxima bug 3921.

kcrisman commented 2 years ago

Changed upstream from Reported upstream. No feedback yet. to Reported upstream. Developers acknowledge bug.

kcrisman commented 2 years ago
comment:4

Looks like it can be narrowed down fairly well between a few Maxima releases.

kcrisman commented 2 years ago
comment:5

Quick work by Robert D :-)

kcrisman commented 2 years ago

Changed upstream from Reported upstream. Developers acknowledge bug. to Fixed upstream, but not in a stable release.

oldk1331 commented 10 months ago

Confirmed this is fixed in maxima-5.47.0.