sagemath / sage

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

latex() command makes sage 9.5 coredump when using it on Maxima output #33356

Open nasser1 opened 2 years ago

nasser1 commented 2 years ago

This seems like a new problem I have not seen before. If already known or reported, please delete this ticket.

fyi, I also asked about it here

https://ask.sagemath.org/question/61120/latex-command-makes-sage-95-coredump-any-workaround/

This seems to happen on some output and so far see it happen when trying to convert maxima output to Latex on some results. But could also when using other algorithms. I need to check more. Here is an example

>sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.5, Release Date: 2022-01-30                     │
│ Using Python 3.10.2. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: var("x")
x
sage: anti = integrate(1 / (1 + cos(x) ^ 5), x, algorithm="maxima")
sage: latex(anti)
// Giac share root-directory:/usr/share/giac/
// Giac share root-directory:/usr/share/giac/
Added 0 synonyms
Precision problem choosing root in common_EXT, current precision 14
Precision problem choosing root in common_EXT, current precision 28
Precision problem choosing root in common_EXT, current precision 56
Precision problem choosing root in common_EXT, current precision 112
Precision problem choosing root in common_EXT, current precision 224
Precision problem choosing root in common_EXT, current precision 448
Precision problem choosing root in common_EXT, current precision 896
Segmentation fault (core dumped)
>

Doing the same, but using "fricas" instead of "maxima" shows no issues and the latex is generated with no problem. This is on Arch Linux running inside Virtual box.

>which sage
/bin/sage
>which maxima
/bin/maxima
>sage --version
SageMath version 9.5, Release Date: 2022-01-30
>maxima --version
;;; Loading #P"/usr/lib/ecl-21.2.1/sb-bsd-sockets.fas"
;;; Loading #P"/usr/lib/ecl-21.2.1/sockets.fas"
Maxima 5.45.1
>

Thanks. --Nasser

Component: symbolics

Keywords: latex maxima giac

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

nasser1 commented 2 years ago

Description changed:

--- 
+++ 
@@ -1,10 +1,10 @@
-This seems like a new problem I have no seen before. If already known or reported, please delete this ticket.
+This seems like a new problem I have not seen before. If already known or reported, please delete this ticket.

-I also asked about it here
+fyi, I also asked about it here

 https://ask.sagemath.org/question/61120/latex-command-makes-sage-95-coredump-any-workaround/ 

-This seems to happen on some output and so far see it from maxima only but could also when using other algorithms. I need to check more. Here is an example
+This seems to happen on some output and so far see it happen when trying to convert maxima output to Latex on some results. But could also when using other algorithms. I need to check more. Here is an example
jhpalmieri commented 2 years ago
comment:2

It doesn't work for me on OS X, either. I see:

sage: s = latex(anti)
Help file /Applications/usr/share/giac/doc/fr/aide_cas not found
Added 0 synonyms

and then it hangs. If I hit ctrl-C to interrupt it, it kicks me out to the shell, not to the Sage prompt. This is with Sage 9.6.beta1.

I'm guessing that's it's a giac bug, trying to convert a symbolic expression to LaTeX.

jhpalmieri commented 2 years ago

Changed keywords from latex maxima to latex maxima giac

DaveWitteMorris commented 2 years ago
comment:3

Testing on Ubuntu 20.04 (CoCalc) reproduces the bug on 9.4, but not on 9.3. With 9.3, it takes about 15 seconds to produce the latex. On 9.4, it usually crashed, but once it ran for more than 5 minutes until I stopped it, with memory usage over 4GB.

Here's what I think is happening. If you look at anti, you will see it is an expression that contains an unevaluated integral, and this integral is much worse than the one we started with. Calling latex(anti) means "evaluate/simplify anti, and then return latex code of the result". So sagemath tries to evaluate the integral. In 9.3 (and earlier), sagemath realizes this is hopeless, and produces latex that includes an unevaluated integral. In 9.4 (and later), giac (or some other backend) does not give up, so keeps trying to evaluate the integral until it runs out of memory.

To more-or-less confirm this diagnosis, I asked sagemath to evaluate the unevaluated integral. In 9.3, it returns after about 15 seconds, with essentially the same integral that we started with. In 9.4, it hangs or crashes.

I don't know what we should do to fix this.

nasser1 commented 2 years ago
comment:4

Yes, this is a problem if latex() command tries to evaluate the math it is trying to convert to Latex. In this case I will modify my code to avoid calling latex() on result which contains integrate in it to bypass this problem.

But does not sagemath has a way to prevent evaluation? For example, in Mathematica one can do the following

TeXForm[HoldForm[Integrate[Sin[x], x]]]

   \int \sin (x) \, dx

The command HoldForm tells it not to evaluate what is inside. Without this, it will evaluate first.

TeXForm[Integrate[Sin[x], x]]

    -\cos (x)
DaveWitteMorris commented 2 years ago
comment:5

To prevent evaluation of an integral, use the keyword argument hold=True:

sage: integral(sin(x), x)
-cos(x)
sage: integral(sin(x), x, hold=True)
integrate(sin(x), x)
sage: latex(integral(sin(x), x, hold=True))
\int \sin\left(x\right)\,{d x}

It should also be possible to use a hold context, but that doesn't seem to work for the complicated integral, though I don't know why.

nasser1 commented 2 years ago
comment:6

Yes, this is not the same as Mathematica's HoldForm. The result returned is from maxima with integrate already in it without hold=True.

So one would then need to first parse the input first, look for any integrate and modify the integrate command to add hold=true into it at the correct position, and only then try latex() command again on the updated result.

There should be a way to tell sage not to evaluate any expression regardless what it is.

Similar to Mathematica's Hold commands. But for now, I will bypass calling latex() in sage on any result that contains integrate in it. This is much easier solution for me at this moment.

DaveWitteMorris commented 2 years ago
comment:7

A hold context is supposed to prevent evaluation, but it is buggy (see #31607, for example) and it does not seem to work for your example.

oldk1331 commented 10 months ago

This no longer coredumps in sage-10.2. But as discussed above, the output of latex is different than its input -- the argument anti is evaluated.