sagemath / sage

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

Sagemath-Fricas issue. RecursionError: maximum recursion depth exceeded calling integrate #37812

Open nasser1 opened 4 months ago

nasser1 commented 4 months ago

Steps To Reproduce

Using sagemath 10.3 with Fricas :


sage --version
SageMath version 10.3, Release Date: 2024-03-19

>fricas --version
FriCAS 1.3.10
based on sbcl 2.3.11
>

Sage gives RecursionError: maximum recursion depth exceeded on an integral which Fricas directly can do

>sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 10.3, Release Date: 2024-03-19                    │
│ Using Python 3.11.8. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: var('x a b B y A')
(x, a, b, B, y, A)
sage: integrate((1-x)^2014*x,x, algorithm="fricas")
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 integrate((Integer(1)-x)**Integer(2014)*x,x, algorithm="fricas")

File ~/TMP/sage-10.3/src/sage/misc/functional.py:788, in integral(x, *args, **kwds)
    655 """
    656 Return an indefinite or definite integral of an object ``x``.
    657 
   (...)
    785 
    786 """
    787 if hasattr(x, 'integral'):
--> 788     return x.integral(*args, **kwds)
    789 else:
    790     from sage.symbolic.ring import SR

File ~/TMP/sage-10.3/src/sage/symbolic/expression.pyx:13268, in sage.symbolic.expression.Expression.integral()
  13266                 R = SR
  13267         return R(integral(f, v, a, b, **kwds))
> 13268     return integral(self, *args, **kwds)
  13269 
  13270 integrate = integral

File ~/TMP/sage-10.3/src/sage/symbolic/integration/integral.py:1062, in integrate(expression, v, a, b, algorithm, hold)
   1060     if not integrator:
   1061         raise ValueError("Unknown algorithm: %s" % algorithm)
-> 1062     return integrator(expression, v, a, b)
   1063 if a is None:
   1064     return indefinite_integral(expression, v, hold=hold)

File ~/TMP/sage-10.3/src/sage/symbolic/integration/external.py:207, in fricas_integrator(expression, v, a, b, noPole)
    204     else:
    205         result = e_fricas.integrate(seg)
--> 207 result = result.sage()
    209 if result == "failed":
    210     result = expression.integrate(v, a, b, hold=True)

File ~/TMP/sage-10.3/src/sage/interfaces/interface.py:1123, in InterfaceElement.sage(self, *args, **kwds)
   1104 def sage(self, *args, **kwds):
   1105     """
   1106     Attempt to return a Sage version of this object.
   1107 
   (...)
   1121         [0 0]
   1122     """
-> 1123     return self._sage_(*args, **kwds)

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:2056, in FriCASElement._sage_(self)
   2051     return FriCASElement._sage_expression(P.get_InputForm(self._name))
   2053 if head == "Expression" or head == "Pi":
   2054     # we treat Expression Integer and Expression Complex
   2055     # Integer just the same
-> 2056     return FriCASElement._sage_expression(P.get_InputForm(self._name))
   2058 if head == 'DistributedMultivariatePolynomial':
   2059     base_ring = self._get_sage_type(domain[2])

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1737, in FriCASElement._sage_expression(fricas_InputForm)
   1733     return y
   1735 register_symbol(convert_rootOf, {'fricas': 'rootOf'}, 2)
-> 1737 ex, _ = FriCASElement._parse_and_eval(fricas_InputForm)
   1738 # postprocessing of rootOf
   1739 from sage.rings.qqbar import QQbar

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1319, in FriCASElement._parse_and_eval(s, start)
   1316     a += 1
   1318 if s[a] == FriCASElement._LEFTBRACKET:
-> 1319     return FriCASElement._parse_list(s, start=a)
   1320 elif s[a] == FriCASElement._STRINGMARKER:
   1321     return FriCASElement._parse_string(s, start=a)

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1362, in FriCASElement._parse_list(s, start)
   1360 args = []
   1361 while s[a] != FriCASElement._RIGHTBRACKET:
-> 1362     e, a = FriCASElement._parse_and_eval(s, start=a)
   1363     args.append(e)
   1364     a += 1

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1319, in FriCASElement._parse_and_eval(s, start)
   1316     a += 1
   1318 if s[a] == FriCASElement._LEFTBRACKET:
-> 1319     return FriCASElement._parse_list(s, start=a)
   1320 elif s[a] == FriCASElement._STRINGMARKER:
   1321     return FriCASElement._parse_string(s, start=a)

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1362, in FriCASElement._parse_list(s, start)
   1360 args = []
   1361 while s[a] != FriCASElement._RIGHTBRACKET:
-> 1362     e, a = FriCASElement._parse_and_eval(s, start=a)
   1363     args.append(e)
   1364     a += 1

    [... skipping similar frames: FriCASElement._parse_and_eval at line 1319 (1487 times), FriCASElement._parse_list at line 1362 (1486 times)]

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1362, in FriCASElement._parse_list(s, start)
   1360 args = []
   1361 while s[a] != FriCASElement._RIGHTBRACKET:
-> 1362     e, a = FriCASElement._parse_and_eval(s, start=a)
   1363     args.append(e)
   1364     a += 1

File ~/TMP/sage-10.3/src/sage/interfaces/fricas.py:1319, in FriCASElement._parse_and_eval(s, start)
   1316     a += 1
   1318 if s[a] == FriCASElement._LEFTBRACKET:
-> 1319     return FriCASElement._parse_list(s, start=a)
   1320 elif s[a] == FriCASElement._STRINGMARKER:
   1321     return FriCASElement._parse_string(s, start=a)

RecursionError: maximum recursion depth exceeded
sage: 

Using Fricas directly, no error is generated and anti-derivative is given (not shown as too long)

>fricas
FRICAS="/usr/local/lib/fricas/target/x86_64-linux-gnu"
spad-lib="/usr/local/lib/fricas/target/x86_64-linux-gnu/lib/libspad.so"
foreign routines found
openServer result 0
                       FriCAS Computer Algebra System 
                Version: FriCAS 1.3.10 built with sbcl 2.3.11
                 Timestamp: Wed Jan 10 09:37:52 PM CST 2024

(1) -> X connection to :0 broken (explicit kill or server shutdown).

(1) -> integrate((1-x)^2014*x,x)

%no error 

Expected Behavior

No exception is expected as this is what the external CAS does.

Actual Behavior

Exception is generated

Additional Information

No response

Environment

Linux on Virtual box

lsb_release -a
LSB Version:    n/a
Distributor ID: ManjaroLinux
Description:    Manjaro Linux
Release:    23.1.4
Codename:   Vulcan

sage --version
SageMath version 10.3, Release Date: 2024-03-19

Checklist

dimpase commented 4 months ago

does this happen with other integration backends on this example, too?

The problem is probably in expanding the integrand into a polynomial with 1000 monomials...

nasser1 commented 4 months ago

integrate((1-x)^2014*x,x, algorithm="fricas")

No error with maxima. No error with giac. Only with Fricas

sage:  print(maxima.version())
5.47.0

sage:  print(giac.version())
"giac 1.9.0-97, (c) B. Parisse and R. De Graeve, Institut Fourier, Universite Grenoble Alpes. Optimization, signalprocessing, graph theory (c) Luka Marohnić"

integrate((1-x)^2014*x,x, algorithm="giac")
integrate((1-x)^2014*x,x, algorithm="maxima")

No error. Result returned OK to sagemath