Opened 4 months ago

Last modified 2 months ago

#33379 new defect

Changing one letter in integrand causes sagemath/giac crash

Reported by: gh-nasser1 Owned by:
Priority: major Milestone: sage-9.7
Component: interfaces Keywords: symbolic integrate giac
Cc: charpent, parisse Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

This is using sage 9.5 on Linux Arch.

FYI, I also posted question on this at https://ask.sagemath.org/question/61215/changing-one-letter-in-integrand-causes-sagemathgiac-crash/ but then I thought to add a ticket just in case.

I've been trying for a while to find why some integrals cause giac to crash. This is different from earlier question I asked.

This seem to be an interface issue between sagemath and giac.

Here is an integrand which causes giac to crash according to sagemath. Ok, may be due sagemath changing the form of the integrand. I do not know. (this takes about almost 10 minutes to crash, which I do not know why so long).

btw, is there a way to see what exactly sagemath sends to giac in the call if sagemath changes the form of the integrand? Is there a way to trace the call?

sage: var("x f e a b z")
sage: integrand = cot(f * x + e) * (a + b * sin(f * x + e) ^ 2) ^ (1 / 2)
sage: integrand
        sqrt(b*sin(f*x + e)^2 + a)*cot(f*x + e)
sage: integrate(integrand, x, algorithm="giac")
Giac crashed -- automatically restarting.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /usr/lib/python3.10/site-packages/sage/interfaces/interface.py:1216, in InterfaceElement.__getattr__(self, attrname)
   1215 try:
-> 1216     P = self._check_valid()
   1217 except ValueError:

File /usr/lib/python3.10/site-packages/sage/interfaces/expect.py:1500, in ExpectElement._check_valid(self)
   1498     if P is None or P._session_number == BAD_SESSION or self._session_number == -1 or \
   1499                   P._session_number != self._session_number:
-> 1500         raise ValueError("The %s session in which this object was defined is no longer running."%P.name())
   1501 except AttributeError:

ValueError: The giac session in which this object was defined is no longer running.

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Input In [9], in <module>
----> 1 integrate(integrand, x, algorithm="giac")

File /usr/lib/python3.10/site-packages/sage/misc/functional.py:764, in integral(x, *args, **kwds)
    639 """
    640 Return an indefinite or definite integral of an object ``x``.
    641 
   (...)
    761 
    762 """
    763 if hasattr(x, 'integral'):
--> 764     return x.integral(*args, **kwds)
    765 else:
    766     from sage.symbolic.ring import SR

File /usr/lib/python3.10/site-packages/sage/symbolic/expression.pyx:13138, in sage.symbolic.expression.Expression.integral (build/cythonized/sage/symbolic/expression.cpp:95254)()
  13136                 R = SR
  13137         return R(integral(f, v, a, b, **kwds))
> 13138     return integral(self, *args, **kwds)
  13139 
  13140 integrate = integral

File /usr/lib/python3.10/site-packages/sage/symbolic/integration/integral.py:1047, in integrate(expression, v, a, b, algorithm, hold)
   1045     if not integrator:
   1046         raise ValueError("Unknown algorithm: %s" % algorithm)
-> 1047     return integrator(expression, v, a, b)
   1048 if a is None:
   1049     return indefinite_integral(expression, v, hold=hold)

File /usr/lib/python3.10/site-packages/sage/symbolic/integration/external.py:258, in giac_integrator(expression, v, a, b)
    256     return expression.integrate(v, a, b, hold=True)
    257 else:
--> 258     return result._sage_()

File /usr/lib/python3.10/site-packages/sage/interfaces/giac.py:1135, in GiacElement._sage_(self, locals)
   1131 from sage.calculus.calculus import symbolic_expression_from_string, SR_parser_giac
   1133 result = repr(self) # string representation
-> 1135 if str(self.type()) not in ['DOM_LIST', 'vector', 'vecteur']:
   1136 
   1137     # Merge the user-specified locals dictionary and the symbol_table
   1138     # (locals takes priority)
   1139     lsymbols = symbol_table['giac'].copy()
   1140     lsymbols.update(locals)

File /usr/lib/python3.10/site-packages/sage/interfaces/interface.py:1218, in InterfaceElement.__getattr__(self, attrname)
   1216     P = self._check_valid()
   1217 except ValueError:
-> 1218     raise AttributeError(attrname)
   1219 if attrname[:1] == "_":
   1220     raise AttributeError

AttributeError: type
sage:

When I change e to say z the crash goes away! The answer returns immediately also.

sage: integrand = cot(f * x + z) * (a + b * sin(f * x + z) ^ 2) ^ (1 / 2)
sage: integrand
     sqrt(b*sin(f*x + z)^2 + a)*cot(f*x + z)

sage: integrate(integrand, x, algorithm="giac")
   (a*arctan(sqrt(b*sin(f*x + z)^2 + a)/sqrt(-a))/sqrt(-a) + sqrt(b*sin(f*x + z)^2 + a))/f

Note that the above used to work OK in 9.3. i.e. when using e, it used to work and return the same antiderivative shown above but with z replaced by e. I have report showing this worked before with giac. so something changed.

Notice that inside giac, e is the Euler constant. And I assume sagemath knows this ofcourse and it changes internally e to some made up VAR symbol before calling giac and in return reverse this VAR back to e in sagemath space.

Here is the same thing inside giac, both complete immediately.

2>> integrand:=sqrt(b*sin(f*x + e)^2 + a)*cot(f*x + e)
3>> integrate(integrand,x)
1/b/f*b*(sqrt(a+b*sin(f*x+exp(1))^2)+2*a/2/sqrt(-a)*atan(sqrt(a+b*sin(f*x+exp(1))^2)/sqrt(-a)))

4>> integrand:=sqrt(b*sin(f*x + z)^2 + a)*cot(f*x + z)
5>> integrate(integrand,x)
1/b/f*b*(sqrt(a+b*sin(f*x+z)^2)+2*a/2/sqrt(-a)*atan(sqrt(a+b*sin(f*x+z)^2)/sqrt(-a)))

No crash inside giac for same integrand using e or z

my question is: Why giac crashes when using e but not when using z? Surely the letter should not have made a difference? Is this a new interface problem? Why this same integral worked in 9.3 but not in 9.5? I am running the same test.

Change History (4)

comment:1 follow-up: Changed 4 months ago by charpent

giac definitely has a problem with some variables. With system's giac (= Debian testing's giac and xcas packages) :

charpent@zen-book-flip:~$ giac
// Using locale /usr/share/locale/
// fr_FR.UTF-8
// /usr/share/locale/
// giac
// UTF-8
// Maximum number of parallel threads 8
Added 168 synonyms
Welcome to giac readline interface, version 1.7.0
(c) 2001,2021 B. Parisse & others
Homepage http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
Released under the GPL license 3.0 or above
See http://www.gnu.org for license details
May contain BSD licensed software parts (lapack, atlas, tinymt)
-------------------------------------------------
Press CTRL and D simultaneously to finish session
Type ?commandname for help
0>> integrate(((((b)*((sin(((f)*(x))+(e)))^(2)))+(a))^(1/2))*(cot(((f)*(x))+(e))), x)
1/b/f*b*(sqrt(a+b*sin(f*x+exp(1))**2)+2*a/2/sqrt(-a)*atan(sqrt(a+b*sin(f*x+exp(1))**2)/sqrt(-a)))
// Time 0.04
1>> integrate(((((sageVARb)*((sin(((sageVARf)*(sageVARx))+(sageVARe)))^(2)))+(sageVARa))^(1/2))*(cot(((sageVARf)*(sageVARx))+(sageVARe))), sageVARx)

[ Very long wait... ]

Temps mis pour l'évaluation: 286.57
integration(-sqrt(sageVARb*sin(sageVARe)**2*cos(sageVARf*sageVARx)**2+sageVARb*sin(sageVARf*sageVARx)**2*cos(sageVARe)**2+2*sageVARb*sin(sageVARe)*sin(sageVARf*sageVARx)*cos(sageVARe)*cos(sageVARf*sageVARx)+sageVARa)*sin(sageVARe)*sin(sageVARf*sageVARx)/(sin(sageVARe)*cos(sageVARf*sageVARx)+sin(sageVARf*sageVARx)*cos(sageVARe))+sqrt(sageVARb*sin(sageVARe)**2*cos(sageVARf*sageVARx)**2+sageVARb*sin(sageVARf*sageVARx)**2*cos(sageVARe)**2+2*sageVARb*sin(sageVARe)*sin(sageVARf*sageVARx)*cos(sageVARe)*cos(sageVARf*sageVARx)+sageVARa)*cos(sageVARe)*cos(sageVARf*sageVARx)/(sin(sageVARe)*cos(sageVARf*sageVARx)+sin(sageVARf*sageVARx)*cos(sageVARe)),sageVARx)
// Time 286.57

Aaaarghh !

With sage' giac_console() :

sage: giac_console()
// Using locale /usr/share/locale/
// fr_FR.UTF-8
// /usr/share/locale/
// giac
// UTF-8
// Maximum number of parallel threads 8
Added 168 synonyms
Welcome to giac readline interface, version 1.7.0
(c) 2001,2021 B. Parisse & others
Homepage http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
Released under the GPL license 3.0 or above
See http://www.gnu.org for license details
May contain BSD licensed software parts (lapack, atlas, tinymt)
-------------------------------------------------
Press CTRL and D simultaneously to finish session
Type ?commandname for help
0>> integrate(((((sageVARb)*((sin(((sageVARf)*(sageVARx))+(sageVARe)))^(2)))+(sageVARa))^(1/2))*(cot(((sageVARf)*(sageVARx))+(sageVARe))), sageVARx)

Temps mis pour l'évaluation: 191.71
"Non inversible Erreur: Valeur Argument Incorrecte"
// Time 191.72
1>> integrate(((((b)*((sin(((f)*(x))+(e)))^(2)))+(a))^(1/2))*(cot(((f)*(x))+(e))), x)
1/b/f*b*(sqrt(a+b*sin(f*x+exp(1))**2)+2*a/2/sqrt(-a)*atan(sqrt(a+b*sin(f*x+exp(1))**2)/sqrt(-a)))
// Time 0.02
2>> integrate(((((sageVARb)*((sin(((sageVARf)*(sageVARx))+(sageVARe)))^(2)))+(sageVARa))^(1/2))*(cot(((sageVARf)*(sageVARx))+(sageVARe))), sageVARx)
terminate called after throwing an instance of 'std::runtime_error'
  what():  Non inversible Erreur: Valeur Argument Incorrecte
Aborted

Grrrrr....

I'm at loss.

comment:2 Changed 4 months ago by charpent

  • Cc charpent added

comment:3 in reply to: ↑ 1 Changed 4 months ago by gh-mwageringel

  • Cc parisse added

Replying to charpent:

2>> integrate(((((sageVARb)*((sin(((sageVARf)*(sageVARx))+(sageVARe)))^(2)))+(sageVARa))^(1/2))*(cot(((sageVARf)*(sageVARx))+(sageVARe))), sageVARx)
terminate called after throwing an instance of 'std::runtime_error'
  what():  Non inversible Erreur: Valeur Argument Incorrecte
Aborted

As this replicates the problem in Giac, this does not seem to be an issue of the interface, but a bug in Giac itself.

Note that the internal variable names like sageVARe were added in #30133 to avoid conflicts with the Euler e, so that is why in Sage 9.3 the problem was not noticeable with the example in the description.

Also, one can call integrand._giac_init_() to see which string is passed to Giac.

comment:4 Changed 2 months ago by mkoeppe

  • Milestone changed from sage-9.6 to sage-9.7
Note: See TracTickets for help on using tickets.