Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate(..., algorithm="giac"): Incorrect conversion to Sage #28913

Closed
EmmanuelCharpentier mannequin opened this issue Dec 26, 2019 · 20 comments
Closed

integrate(..., algorithm="giac"): Incorrect conversion to Sage #28913

EmmanuelCharpentier mannequin opened this issue Dec 26, 2019 · 20 comments

Comments

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented Dec 26, 2019

Seen in this ask.sagemath question:

sage: Ex=(1-2*x^(1/3))^(3/4)/x
sage: integrate(Ex, x, algorithm="giac")
---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/interfaces/giac.py in _sage_(self, locals)
   1100                 return symbolic_expression_from_string(result, lsymbols,
-> 1101                     accept_sequence=True)
   1102 

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/calculus/calculus.py in symbolic_expression_from_string(s, syms, accept_sequence)
   2384             _augmented_syms = syms
-> 2385             return parse_func(s)
   2386         finally:

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.parse_sequence (build/cythonized/sage/misc/parser.c:5479)()
    538 
--> 539     cpdef parse_sequence(self, s):
    540         """

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.parse_sequence (build/cythonized/sage/misc/parser.c:5369)()
    556         if tokens.next() != EOS:
--> 557             self.parse_error(tokens)
    558         if len(all) == 1 and isinstance(all, list):

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/misc/parser.pyx in sage.misc.parser.Parser.parse_error (build/cythonized/sage/misc/parser.c:9742)()
   1007     cdef parse_error(self, Tokenizer tokens, msg="Malformed expression"):
-> 1008         raise SyntaxError(msg, tokens.s, tokens.pos)
   1009 

SyntaxError: Malformed expression

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-22-617e71e52902> in <module>()
----> 1 integrate(Ex, x, algorithm="giac")

/usr/local/sage-P3-2/local/lib/python3.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/local/sage-P3-2/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.integral (build/cythonized/sage/symbolic/expression.cpp:64575)()
  12389                     R = ring.SR
  12390             return R(integral(f, v, a, b, **kwds))
> 12391         return integral(self, *args, **kwds)
  12392 
  12393     integrate = integral

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/symbolic/integration/integral.py in integrate(expression, v, a, b, algorithm, hold)
    925         if not integrator:
    926             raise ValueError("Unknown algorithm: %s" % algorithm)
--> 927         return integrator(expression, v, a, b)
    928     if a is None:
    929         return indefinite_integral(expression, v, hold=hold)

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/symbolic/integration/external.py in giac_integrator(expression, v, a, b)
    430         return expression.integrate(v, a, b, hold=True)
    431     else:
--> 432         return result._sage_()

/usr/local/sage-P3-2/local/lib/python3.7/site-packages/sage/interfaces/giac.py in _sage_(self, locals)
   1102 
   1103             except Exception:
-> 1104                 raise NotImplementedError("Unable to parse Giac output: %s" % result)
   1105         else:
   1106             return [entry.sage() for entry in self]

NotImplementedError: Unable to parse Giac output: Evaluation time: 2.82
12*(1/4*ln(abs((-2*x^(1/3)+1)^(1/4)-1))-1/4*ln((-2*x^(1/3)+1)^(1/4)+1)+1/2*atan((-2*x^(1/3)+1)^(1/4))+1/3*((-2*x^(1/3)+1)^(1/4))^3)

However, the following workaround works:

sage: from giacpy_sage import *
// Giac share root-directory:/usr/local/sage-P3-2/local/share/giac/
// Giac share root-directory:/usr/local/sage-P3-2/local/share/giac/
Help file /usr/local/sage-P3-2/local/share/giac/doc/fr/aide_cas not found
Added 0 synonyms
sage: libgiac.integrate(Ex,x).sage()
4*(-2*x^(1/3) + 1)^(3/4) + 6*arctan((-2*x^(1/3) + 1)^(1/4)) - 3*log((-2*x^(1/3) + 1)^(1/4) + 1) + 3*log(abs((-2*x^(1/3) + 1)^(1/4) - 1))

This seems to point to the (unwelcome) timing information added by Giac before its results... It's probably more of a booboo than a (real|hard) bug, but might defeat a whole problem if this error is raised before other algorithms has had a chance to report their results.

I'm not sure that this should be reported upstream (it should if there is no way to cleanly suppress the timing information).

Ccing the participants to #27958, which introduced this particular wolf in the general Sage integration sheepshed...

CC: @fchapoton @mwageringel @sagetrac-jakobkroeker @kcrisman @pjbruin @rwst @seblabbe @slel @sagetrac-tmonteil @videlec @vinklein @zimmermann6 @mforets @mezzarobba

Component: calculus

Keywords: symbolics giac integration

Author: Frédéric Chapoton

Branch: 297033a

Reviewer: Thierry Monteil

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

@EmmanuelCharpentier EmmanuelCharpentier mannequin added this to the sage-9.0 milestone Dec 26, 2019
@EmmanuelCharpentier
Copy link
Mannequin Author

EmmanuelCharpentier mannequin commented Dec 26, 2019

comment:1

Forgot Frederic Chapoton in the Cc list...

@EmmanuelCharpentier
Copy link
Mannequin Author

EmmanuelCharpentier mannequin commented Dec 26, 2019

Changed keywords from none to symbolics giac integration

@mwageringel
Copy link

comment:3

I think it would make sense to use giacpy_sage for this in general, rather than the pexpect interface. Perhaps giacpy_sage could be turned into a standard package.

@slel
Copy link
Member

slel commented Dec 28, 2019

comment:4

Replying to @mwageringel:

I think it would make sense to use giacpy_sage for this in general, rather than the pexpect interface. Perhaps giacpy_sage could be turned into a standard package.

I opened #28918 for that. See also #16688.

@embray
Copy link
Contributor

embray commented Jan 6, 2020

comment:5

Ticket retargeted after milestone closed

@embray embray modified the milestones: sage-9.0, sage-9.1 Jan 6, 2020
@seblabbe
Copy link
Contributor

comment:6

Is the following

sage: integrate(sqrt(x + sqrt(x)), x, algorithm='giac')

an other instance of the problem described in this ticket? See https://ask.sagemath.org/question/50885/is-there-a-way-to-integrate-sqrtxsqrtx-in-sage/

The result is:

Traceback (most recent call last):
...
AttributeError: 
...
During handling of the above exception, another exception occurred:
...
RuntimeError: An error occurred running a Giac command:
INPUT:
sage2
OUTPUT:
Warning, choosing root of [1,0,0,%%%{4,[1]%%%},%%%{4,[2]%%%}+%%%{-1,[1]%%%}] at parameters values [-97]
Warning, need to choose a branch for the root of a polynomial with parameters. This might be wrong.
The choice was done assuming [x]=[9]
Warning, need to choose a branch for the root of a polynomial with parameters. This might be wrong.
The choice was done assuming [x]=[54]
  ***   bug in PARI/GP (Bus Error), please report.sym2poly exception caught Error in PARI subsystem Error: Bad Argument Value
Warning, need to choose a branch for the root of a polynomial with parameters. This might be wrong.
The choice was done assuming [x]=[64]

  ***   bug in PARI/GP (Segmentation Fault), please report.sym2poly exception caught Error in PARI subsystem Error: Bad Argument Value

  ***   bug in PARI/GP (Segmentation Fault), please report.sym2poly exception caught Error in PARI subsystem Error: Bad Argument Value

  ***   Warning: normalizing a polynomial with 0 leading term.
  ***   Warning: normalizing a polynomial with 0 leading term.
Warning, choosing root of [1,0,0,%%%{4,[1]%%%},%%%{4,[2]%%%}+%%%{-1,[1]%%%}] at parameters values [6.38357630698]
  ***   Warning: normalizing a polynomial with 0 leading term.
  ***   Warning: normalizing a polynomial with 0 leading term.
Warning, choosing root of [1,0,0,%%%{4,[1]%%%},%%%{4,[2]%%%}+%%%{-1,[1]%%%}] at parameters values [82.1195442914]
2*(2*((1/6*sqrt(x)+1/24)*sqrt(x)-1/16)*sqrt(x+sqrt(x))-1/16*ln(sqrt(4*sqrt(x)+1-4*sqrt(x)*cos((pi*sign(im(sqrt(x)))*sign(x+re(sqrt(x)))-pi*sign(im(sqrt(x)))-2*atan(im(sqrt(x))/(x+re(sqrt(x)))))/2)+rootof([[-4,-4,0],[1,0,0,4*x,4*x^2-x]])*cos(1/2*(atan(im(sqrt(x))/(x+re(sqrt(x))))+(1-sign(x+re(sqrt(x))))*sign(im(sqrt(x)))*pi/2)))))

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 7, 2020
@fchapoton
Copy link
Contributor

Branch: u/chapoton/28913

@fchapoton
Copy link
Contributor

comment:8

here is a bandaid. A better would be to go through #28918


New commits:

203f9ebbandaid for giac pexpect unwanted timing display

@fchapoton
Copy link
Contributor

Commit: 203f9eb

@fchapoton
Copy link
Contributor

Author: Frédéric Chapoton

@fchapoton
Copy link
Contributor

comment:9

green bot, please review

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented May 16, 2020

comment:10

The fix looks good. How wbout adding a direct doctest in SAGE_ROOT/src/sage/symbolic/integration/integral.py pointing to this ticket ?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 17, 2020

Changed commit from 203f9eb to 297033a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 17, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

297033atrac 28913 adding yet another doctest

@fchapoton
Copy link
Contributor

comment:12

Doctest ajouté

@fchapoton
Copy link
Contributor

comment:13

bot is morally green, please review

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented May 17, 2020

Reviewer: Thierry Monteil

@vbraun
Copy link
Member

vbraun commented May 26, 2020

Changed branch from u/chapoton/28913 to 297033a

@vbraun vbraun closed this as completed in 58f004f May 26, 2020
@seblabbe
Copy link
Contributor

comment:16

Replying to @seblabbe:

Is the following

sage: integrate(sqrt(x + sqrt(x)), x, algorithm='giac')

an other instance of the problem described in this ticket?

Answer is no. I created #29966 for the other issue mentioned in comment 6.

@seblabbe
Copy link
Contributor

Changed commit from 297033a to none

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants