sagemath / sage

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

Symbolic integration fails for polynomials with huge coefficients #37946

Open rburing opened 5 months ago

rburing commented 5 months ago

Steps To Reproduce

var('x,t')
y = 2.66778786276411e493*t^3*x - 1.93258971534823e247*x
integrate(y, t, 0, 1, algorithm='libgiac')

It fails with algorithm=a for each a in ['maxima', 'sympy', 'mathematica_free', 'fricas', 'giac', 'libgiac'].

Expected Behavior

Get the integral.

Actual Behavior

For algorithm='giac' or algorithm='libgiac': returns undef.

For algorithm='sympy':

...
TypeError: cannot create mpf from mpz(1)

For algorithm='fricas':

...
TypeError: An error occurred when FriCAS evaluated 'integrate(sage0,sage3,sage2)':
   There are 4 exposed and 1 unexposed library operations named integrate 
      having 3 argument(s) but none was determined to be applicable. Use 
      HyperDoc Browse, or issue
                             )display op integrate
      to learn more about the available operations. Perhaps 
      package-calling the operation or using coercions on the arguments 
      will allow you to apply the operation.

   Cannot find a definition or applicable library operation named 
      integrate with argument type(s) 
                                Variable(sage0)
                                PositiveInteger
                                     String

      Perhaps you should use "@" to indicate the required return type, or 
      "$" to specify which version of the function you need.

For algorithm='maxima':

...
RuntimeError: ECL says: #<a FLOATING-POINT-INVALID-OPERATION 0x7289d766bd00>

Additional Information

Originally reported at https://ask.sagemath.org/question/77253/what-does-undef-mean/.

Workaround for polynomials:

Y = SR(y.polynomial(RR).integral(t))
Y.subs(t=1) - Y.subs(t=0))

Environment

Checklist

rburing commented 5 months ago

The following code works in python but not in sage -python:

from sympy import symbols, Float, integrate
x, t = symbols('x t')
y = Float('2.66778786276411e493')*t**3*x - Float('1.93258971534823e247')*x
integrate(y, (t, Float('0'), Float('1')))

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/integrals/integrals.py", line 1567, in integrate
    return integral.doit(**doit_flags)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/integrals/integrals.py", line 499, in doit
    did = self.xreplace(reps).doit(**hints)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/integrals/integrals.py", line 612, in doit
    antideriv = self._eval_integral(
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/integrals/integrals.py", line 951, in _eval_integral
    return poly.integrate().as_expr()
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/polytools.py", line 2355, in integrate
    return f.per(f.rep.integrate(m=1))
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/polyclasses.py", line 606, in integrate
    return f.per(dmp_integrate_in(f.rep, m, j, f.lev, f.dom))
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/densetools.py", line 135, in dmp_integrate_in
    return _rec_integrate_in(f, m, u, 0, j, K)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/densetools.py", line 109, in _rec_integrate_in
    return dmp_integrate(g, m, v, K)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/densetools.py", line 88, in dmp_integrate
    return dup_integrate(f, m, K)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/densetools.py", line 66, in dup_integrate
    g.insert(0, K.exquo(c, K(n)))
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/domains/domain.py", line 381, in __call__
    return self.new(*args)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/domains/fractionfield.py", line 38, in new
    return self.field.field_new(element)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/fields.py", line 230, in field_new
    return self.ground_new(element)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/fields.py", line 181, in ground_new
    return self.new(self.ring.ground_new(element))
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/rings.py", line 320, in ground_new
    return self.term_new(self.zero_monom, coeff)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/rings.py", line 323, in term_new
    coeff = self.domain_new(coeff)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/rings.py", line 317, in domain_new
    return self.domain.convert(element, orig_domain)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/domains/domain.py", line 423, in convert
    return self.convert_from(ZZ(element), ZZ)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/domains/domain.py", line 396, in convert_from
    result = _convert(element, base)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/sympy/polys/domains/realfield.py", line 76, in from_ZZ
    return self.dtype(element)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/mpmath/ctx_mp_python.py", line 79, in __new__
    v._mpf_ = mpf_pos(cls.mpf_convert_arg(val, prec, rounding), prec, rounding)
  File "/home/rburing/mambaforge/envs/sage/lib/python3.10/site-packages/mpmath/ctx_mp_python.py", line 98, in mpf_convert_arg
    raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from mpz(1)