sagemath / sage

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

Bug in FriCAS integration #25905

Closed nasser1 closed 3 years ago

nasser1 commented 6 years ago

FriCAS has a problem with the following integral:

(1) -> f := (%i*a*tan(d*x + c) + a)^3*tan(d*x + c)

   (1)
           3            4      3            3         3            2
     - %i a tan(d x + c)  - 3 a tan(d x + c)  + 3 %i a tan(d x + c)
   + 
      3
     a tan(d x + c)
                                           Type: Expression(Complex(Integer))
(2) -> integrate(f, x)

   >> System error:
   arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ -2 0).

Replacing the imaginary unit with a parameter makes the problem go away. This bug can only be fixed by the FriCAS team.

Old description:

There seems to be a problem with complex integrands in FriCAS. In any case, it would be good to have at least slightly better error handling.

Very old description:

I am not sure what is missing translation from FriCAS result to SageMath here, and do not know if this is known of not. Thought to report it just in case but feel free to close if this is known.

Large number of such integrals fail because of this. Here is an example

SageMath version 8.3.rc2, Release Date: 2018-07-22   

sage: var('a d x c')
(a, d, x, c)
sage: integrate((I*a*tan(d*x + c) + a)^3*tan(d*x + c),x, algorithm="fricas")

Which gives

NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-f02be8032da4> in <module>()
----> 1 integrate((I*a*tan(d*x + c) + a)**Integer(3)*tan(d*x + c),x, algorithm="fricas")

/usr/lib/python2.7/site-packages/sage/misc/functional.py in integral(x, *args, **kwds)
    751     """
    752     if hasattr(x, 'integral'):
--> 753         return x.integral(*args, **kwds)
    754     else:
    755         from sage.symbolic.ring import SR

/usr/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.integral (build/cythonized/sage/symbolic/expression.cpp:69759)()
  12364                     R = ring.SR
  12365             return R(integral(f, v, a, b, **kwds))
> 12366         return integral(self, *args, **kwds)
  12367 
  12368     integrate = integral

/usr/lib/python2.7/site-packages/sage/symbolic/integration/integral.py in integrate(expression, v, a, b, algorithm, hold)
    814         if not integrator:
    815             raise ValueError("Unknown algorithm: %s" % algorithm)
--> 816         return integrator(expression, v, a, b)
    817     if a is None:
    818         return indefinite_integral(expression, v, hold=hold)

/usr/lib/python2.7/site-packages/sage/symbolic/integration/external.py in fricas_integrator(expression, v, a, b, noPole)
    189                          " in the integration interval")
    190 
--> 191     return result.sage()
    192 
    193 

/usr/lib/python2.7/site-packages/sage/interfaces/interface.py in sage(self, *args, **kwds)
   1049             [0 0]
   1050         """
-> 1051         return self._sage_(*args, **kwds)
   1052 
   1053     def __repr__(self):

/usr/lib/python2.7/site-packages/sage/interfaces/fricas.py in _sage_(self)
   1443             return R(unparsed_InputForm)
   1444 
-> 1445         raise NotImplementedError("The translation of the FriCAS object %s to sage is not yet implemented." %(unparsed_InputForm))
   1446 
   1447 

NotImplementedError: The translation of the FriCAS object sage2 to sage is not yet implemented.
sage: 

The above works OK in friCAS itself:

>fricas
AXIOM="/usr/lib/fricas/target/x86_64-linux-gnu"
spad-lib="/usr/lib/fricas/target/x86_64-linux-gnu/lib/libspad.so"
                       FriCAS Computer Algebra System 
                            Version: FriCAS 1.3.4
                   Timestamp: Thu Jun 28 08:31:47 CDT 2018
(3) -> r:=integrate((I*a*tan(d*x + c) + a)^3*tan(d*x + c),x);
(4) -> unparse(r::InputForm) 

   (4)
  "((9*I^2+(-3))*a^3*log(1/(tan(d*x+c)^2+1))+(2*I^3*a^3*tan(d*x+c)^3+9*I^2*a^3*
  tan(d*x+c)^2+((-6)*I^3+18*I)*a^3*tan(d*x+c)+(6*I^3+(-18)*I)*a^3*d*x))/(6*d)"

Is it the complex number I which is causing the translation back to SageMath?

Upstream: Completely fixed; Fix reported upstream

CC: @mantepse

Component: symbolics

Keywords: fricas, integrate

Author: Martin Rubey

Branch/Commit: 6a81817

Reviewer: Frédéric Chapoton

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

mantepse commented 6 years ago
comment:2

It's a mix of two problems:

There is a strange problem in FriCAS:

(1) -> f := (%i*a*tan(d*x + c) + a)^3*tan(d*x + c)

   (1)
           3            4      3            3         3            2
     - %i a tan(d x + c)  - 3 a tan(d x + c)  + 3 %i a tan(d x + c)
   + 
      3
     a tan(d x + c)
                                           Type: Expression(Complex(Integer))
(2) -> integrate(f, x)

   >> System error:
   arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ -2 0).

and, of course, the error handling of the sage-FriCAS interface is less than perfect.

I'll report the bug on the FriCAS list.

mantepse commented 6 years ago

Description changed:

--- 
+++ 
@@ -1,3 +1,7 @@
+There seems to be a problem with complex integrands in FriCAS.  In any case, it would be good to have at least slightly better error handling.
+
+Old description:
+
 I am not sure what is missing translation from FriCAS result to [SageMath](../wiki/SageMath) here, and do not know if this is known of not. Thought to report it just in case but feel free to close if this is known.

 Large number of such integrals fail because of this. Here is an example
mantepse commented 6 years ago
comment:4

See https://groups.google.com/forum/#!topic/fricas-devel/TaduS9mswqE

mantepse commented 6 years ago

Upstream: Reported upstream. Developers acknowledge bug.

mantepse commented 5 years ago

Description changed:

--- 
+++ 
@@ -1,6 +1,30 @@
+FriCAS has a problem with the following integral:
+> 
+> ```
+> (1) -> f := (%i*a*tan(d*x + c) + a)^3*tan(d*x + c)
+> 
+>    (1)
+>            3            4      3            3         3            2
+>      - %i a tan(d x + c)  - 3 a tan(d x + c)  + 3 %i a tan(d x + c)
+>    + 
+>       3
+>      a tan(d x + c)
+>                                            Type: Expression(Complex(Integer))
+> (2) -> integrate(f, x)
+>  
+>    >> System error:
+>    arithmetic error DIVISION-BY-ZERO signalled
+> Operation was (/ -2 0).
+> ```
+> 
+
+Replacing the imaginary unit with a parameter makes the problem go away.  This bug can only be fixed by the FriCAS team.
+
+Old description:
+
 There seems to be a problem with complex integrands in FriCAS.  In any case, it would be good to have at least slightly better error handling.

-Old description:
+Very old description:

 I am not sure what is missing translation from FriCAS result to [SageMath](../wiki/SageMath) here, and do not know if this is known of not. Thought to report it just in case but feel free to close if this is known.
fchapoton commented 3 years ago
comment:6

works now (9.2.b4)

sage: var('a d x c')                                                            
(a, d, x, c)
sage: integrate((I*a*tan(d*x + c) + a)^3*tan(d*x + c),x, algorithm="fricas")    
-2/3*(24*a^3*e^(4*I*d*x + 4*I*c) + 33*a^3*e^(2*I*d*x + 2*I*c) + 13*a^3 + 6*(a^3*e^(6*I*d*x + 6*I*c) + 3*a^3*e^(4*I*d*x + 4*I*c) + 3*a^3*e^(2*I*d*x + 2*I*c) + a^3)*log(e^(2*I*d*x + 2*I*c) + 1))/(d*e^(6*I*d*x + 6*I*c) + 3*d*e^(4*I*d*x + 4*I*c) + 3*d*e^(2*I*d*x + 2*I*c) + d)
nasser1 commented 3 years ago
comment:7

Replying to @fchapoton:

works now (9.2.b4)

sage: var('a d x c')                                                            
(a, d, x, c)
sage: integrate((I*a*tan(d*x + c) + a)^3*tan(d*x + c),x, algorithm="fricas")    
-2/3*(24*a^3*e^(4*I*d*x + 4*I*c) + 33*a^3*e^(2*I*d*x + 2*I*c) + 13*a^3 + 6*(a^3*e^(6*I*d*x + 6*I*c) + 3*a^3*e^(4*I*d*x + 4*I*c) + 3*a^3*e^(2*I*d*x + 2*I*c) + a^3)*log(e^(2*I*d*x + 2*I*c) + 1))/(d*e^(6*I*d*x + 6*I*c) + 3*d*e^(4*I*d*x + 4*I*c) + 3*d*e^(2*I*d*x + 2*I*c) + d)

Thanks. It is also working on V 9.2 on Linux:

>sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.2, Release Date: 2020-10-24                     │
│ Using Python 3.8.6. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
sage:  var('a d x c')
(a, d, x, c)
sage: integrate((I*a*tan(d*x + c) + a)^3*tan(d*x + c),x,algorithm="fricas")
-2/3*(24*a^3*e^(4*I*d*x + 4*I*c) + 33*a^3*e^(2*I*d*x + 2*I*c) + 13*a^3 + 6*(a^3*e^(6*I*d*x + 6*I*c) + 3*a^3*e^(4*I*d*x + 4*I*c) + 3*a^3*e^(2*I*d*x + 2*I*c) + a^3)*log(e^(2*I*d*x + 2*I*c) + 1))/(d*e^(6*I*d*x + 6*I*c) + 3*d*e^(4*I*d*x + 4*I*c) + 3*d*e^(2*I*d*x + 2*I*c) + d)
sage: 
mantepse commented 3 years ago

Changed upstream from Reported upstream. Developers acknowledge bug. to Completely fixed; Fix reported upstream

mantepse commented 3 years ago
comment:8

I don't know in which release of FriCAS this was fixed, though.

fchapoton commented 3 years ago
comment:9

this need a doctest

mantepse commented 3 years ago

Branch: u/mantepse/bug_in_fricas_integration

mantepse commented 3 years ago

Author: Martin Rubey

mantepse commented 3 years ago
comment:11

I was unable to find a simple way to verify the integral. I did some random checks, though.


New commits:

6a81817add doctest
mantepse commented 3 years ago

Commit: 6a81817

fchapoton commented 3 years ago

Reviewer: Frédéric Chapoton

fchapoton commented 3 years ago
comment:12

ok, thx

vbraun commented 3 years ago

Changed branch from u/mantepse/bug_in_fricas_integration to 6a81817