Ask Your Question

why giac integrate result fail to be parsed when using letter e?

asked 2021-06-04 05:10:48 +0200

Nasser gravatar image

updated 2021-06-04 05:28:21 +0200

Is this a known issue?

When integrand has the letter e, sagemath fails to parse its output. Even though the same integral works inside giac. Using sagemath 9.3 and giac 1.7

var('C B A e d x')
integrate((C*x^2+B*x+A)*(-e^2*x^2+d^2)^(1/2)/(e*x+d),x, algorithm="giac")
ValueError                                Traceback (most recent call last)
/usr/lib/python3.9/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._div_ (build/cythonized/sage/symbolic/expression.cpp:25839)()
   3894             else:
-> 3895                 x = left._gobj / _right._gobj
   3896             return new_Expression_from_GEx(left._parent, x)

ValueError: power::eval(): division by zero

During handling of the above exception, another exception occurred:

ZeroDivisionError                         Traceback (most recent call last)
/usr/lib/python3.9/site-packages/sage/interfaces/ in _sage_(self, locals)
   1131             try:
-> 1132                 return symbolic_expression_from_string(result, lsymbols,
   1133                     accept_sequence=True)

/usr/lib/python3.9/site-packages/sage/calculus/ in symbolic_expression_from_string(s, syms, accept_sequence)
   2407                                     if isinstance(v,Function)})
-> 2408     return parse_func(s)

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.parse_sequence (build/cythonized/sage/misc/parser.c:5837)()
--> 550     cpdef parse_sequence(self, s):
    551         """

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.parse_sequence (build/cythonized/sage/misc/parser.c:5702)()
    565         cdef Tokenizer tokens = Tokenizer(s)
--> 566         all = self.p_sequence(tokens)
    567         if != EOS:

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_sequence (build/cythonized/sage/misc/parser.c:6404)()
    627                     tokens.backtrack()
--> 628                     obj = self.p_eqn(tokens)
    629             elif token == '[':

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_eqn (build/cythonized/sage/misc/parser.c:7394)()
    728         """
--> 729         lhs = self.p_expr(tokens)
    730         cdef int op =

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_expr (build/cythonized/sage/misc/parser.c:7746)()
    768         cdef int op
--> 769         operand1 = self.p_term(tokens)
    770         op =

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_term (build/cythonized/sage/misc/parser.c:8105)()
    808         while op == '*' or op == '/':
--> 809             operand2 = self.p_factor(tokens)
    810             if op == '*':

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_factor (build/cythonized/sage/misc/parser.c:8461)()
    845             tokens.backtrack()
--> 846             return self.p_power(tokens)

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_power (build/cythonized/sage/misc/parser.c:8618)()
    873         """
--> 874         operand1 = self.p_atom(tokens)
    875         cdef int token =

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_atom (build/cythonized/sage/misc/parser.c:9141)()
    928                 func = self.callable_constructor(name)
--> 929                 args, kwds = self.p_args(tokens)
    930                 token =

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_args (build/cythonized/sage/misc/parser.c:9654)()
    965         while token == ',':
--> 966             arg = self.p_arg(tokens)
    967             if isinstance(arg, tuple):

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_arg (build/cythonized/sage/misc/parser.c:10081)()
   1015             tokens.backtrack()
-> 1016             return self.p_expr(tokens)

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_expr (build/cythonized/sage/misc/parser.c:7746)()
    768         cdef int op
--> 769         operand1 = self.p_term(tokens)
    770         op =

/usr/lib/python3.9/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.p_term (build/cythonized/sage/misc/parser.c:8150)()
    812             else:
--> 813                 operand1 = operand1 / operand2
    814             op =

/usr/lib/python3.9/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__truediv__ (build/cythonized/sage/structure/element.c:13162)()
   1734         if HAVE_SAME_PARENT(cl):
-> 1735             return (<Element>left)._div_(right)
   1736         if BOTH_ARE_ELEMENT(cl):

/usr/lib/python3.9/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._div_ (build/cythonized/sage/symbolic/expression.cpp:25919)()
   3900             if 'division by zero' in str(msg):
-> 3901                 raise ZeroDivisionError("symbolic division by zero")
   3902             else:

ZeroDivisionError: symbolic division by zero

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-8-35c8b048a377> in <module>
----> 1 integrate((C*x**Integer(2)+B*x+A)*(-e**Integer(2)*x**Integer(2)+d**Integer(2))**(Integer(1)/Integer(2))/(e*x+d),x, algorithm="giac")

/usr/lib/python3.9/site-packages/sage/misc/ in integral(x, *args, **kwds)
    757     """
    758     if hasattr(x, 'integral'):
--> 759         return x.integral(*args, **kwds)
    760     else:
    761         from sage.symbolic.ring import SR

/usr/lib/python3.9/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.integral (build/cythonized/sage/symbolic/expression.cpp:66867)()
  12645                     R = SR
  12646             return R(integral(f, v, a, b, **kwds))
> 12647         return integral(self, *args, **kwds)
  12649     integrate = integral

/usr/lib/python3.9/site-packages/sage/symbolic/integration/ in integrate(expression, v, a, b, algorithm, hold)
    988         if not integrator:
    989             raise ValueError("Unknown algorithm: %s" % algorithm)
--> 990         return integrator(expression, v, a, b)
    991     if a is None:
    992         return indefinite_integral(expression, v, hold=hold)

/usr/lib/python3.9/site-packages/sage/symbolic/integration/ in giac_integrator(expression, v, a, b)
    446         return expression.integrate(v, a, b, hold=True)
    447     else:
--> 448         return result._sage_()

/usr/lib/python3.9/site-packages/sage/interfaces/ in _sage_(self, locals)
   1135             except Exception:
-> 1136                 raise NotImplementedError("Unable to parse Giac output: %s" % result)
   1137         else:
   1138             return [entry.sage() for entry in self]

NotImplementedError: Unable to parse Giac output: 1/2*(4*A*d*exp(2)^3-4*A*d*exp(1)^4*exp(2)+4*B*d^2*exp(1)^3*exp(2)-4*B*d^2*exp(1)*exp(2)^2)*atan((-1/2*(-2*d*exp(1)-2*sqrt(d^2-x^2*exp(2))*exp(1))/x+exp(2))/sqrt(-exp(1)^4+exp(2)^2))/sqrt(-exp(1)^4+exp(2)^2)/exp(1)^4/exp(1)-1/4*(-2*C*d^3-4*A*d*exp(2)+2*B*d^2*exp(1))*sign(d)*asin(x*exp(2)/d/exp(1))/exp(1)/exp(2)+2*((16*exp(1)^4*C*1/96/exp(1)^5*x-(-24*exp(1)^4*B+24*exp(1)^3*C*d)*1/96/exp(1)^5)*x-(-48*exp(1)^4*A+48*exp(1)^3*d*B-32*exp(1)^2*C*d^2)*1/96/exp(1)^5)*sqrt(d^2-x^2*exp(2))

Using different letter than e, now it works.

sage: var('C B A z d x')
(C, B, A, z, d, x)
sage: integrate((C*x^2+B*x+A)*(-z^2*x^2+d^2)^(1/2)/(z*x+d),x, algorithm="giac")
1/6*sqrt(-x^2*z^2 + d^2)*(x*(2*C*x/z - 3*(C*d*z^3 - B*z^4)/z^5) + 2*(2*C*d^2*z^2 - 3*B*d*z^3 + 3*A*z^4)/z^5) + 1/2*(C*d^3 - B*d^2*z + 2*A*d*z^2)*arcsin(x*z/d)*sgn(d)*sgn(z)/(z^2*abs(z))

The original integral works as in inside giac

Welcome to giac readline interface, version 1.7.0
1>> integrate((C*x^2+B*x+A)*(-e^2*x^2+d^2)^(1/2)/(e*x+d),x)

// Time 0.06

Also, using e works when using different CAS than giac. giac uses e as exp. I should ask Author of Rubi to change the test files not to use e as letter in the integrand.

I can't now change letters in the input, as this is part of standard Rubi's test suite. But my question is: Since it works using e inside giac, should it not also work as is when passing it to giac via sagemath?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-06-04 08:51:50 +0200

Emmanuel Charpentier gravatar image

This is basically the same issue as the recent one with "D", which denotesfricas' derivative operator : "e" has special meaning for giac's parser.

Workaround :

sage: var('C B A variable_e d x')
(C, B, A, variable_e, d, x)
sage: Ex=SR("(C*x^2+B*x+A)*(-e^2*x^2+d^2)^(1/2)/(e*x+d)".replace("e","variable_e"))
sage: SR(repr(Ex.integral(x, algorithm="giac")).replace("variable_e","e"))
1/2*(C*d^3 - B*d^2*e + 2*A*d*e^2)*arcsin(x*e/d)*e^(-3)*sgn(d) + 1/6*sqrt(-x^2*e^2 + d^2)*((2*C*x*e^(-1) - 3*(C*d*e^3 - B*e^4)*e^(-5))*x + 2*(2*C*d^2*e^2 - 3*B*d*e^3 + 3*A*e^4)*e^(-5))

Such a sanitization, which can account for various parsesrs' quirks, can be applied by program to all your test database.

As far as I remember, Rubi's test database comes expressed in "Wolfram language" (a. k. a. Mathematica). This implies that this language's lexical and syntactic rules should be used to interpret both integrand and "reference functions". This can be problematic, since Mathematica implements many functions not directly known by other CASes : e. g. Hypergeometric0F1, Hypergeometric1F1 and Hypergeometric2F1 are all special cases of HypergeometricPFQ, whose translation to Sage is hypergeometric. See Trac#31902 for further input...

edit flag offensive delete link more


mwageringel gravatar imagemwageringel ( 2021-06-04 19:16:21 +0200 )edit

thanks for the workaround. Although I am still not sure I understand why sagemath gives exception, since it works as is in giac, May be this will be fixed in 9.4 so at least it does not crash.

Nasser gravatar imageNasser ( 2021-06-04 20:59:00 +0200 )edit

answered 2021-06-04 21:35:36 +0200

mwageringel gravatar image

Sage is not able to understand the output of Giac because the result returned by Giac contains the expression sqrt(-exp(1)^4+exp(2)^2) as a denominator. The problem is that this expression is zero, so Giac's result does not make much sense.

In this case, it looks like a feature that Sage catches this and returns an error.

edit flag offensive delete link more


If so, why doesn't this happen when ee is substituted to e ? Hmmm ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-06-07 21:51:03 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-06-04 05:10:48 +0200

Seen: 87 times

Last updated: Jun 04