2 basic questions on sagemath programming with external CAS systems

61 views
Skip to first unread message

Nasser M. Abbasi

unread,
May 12, 2022, 6:32:26 AM5/12/22
to sage-devel

1)  I just want to confirm if this is what happens ( I could find information on this googling).

If from sagemath, one makes a call to an external CAS, does sagemath create a subprocess, starts the external CAS executable in it, makes the call, and when the call is over, that subprocess is then killed? Or does the subprocess remain running until sagemath itself is closed?

For example, when I do this first time:

sage: var('x')
sage: integrate(sin(x),x,algorithm="giac")
-cos(x)

At this point, is the giac process still running waiting for commands from sagemath? So that when I make another call

sage: integrate(cos(x),x,algorithm="giac")

Same process and any cashed data in it is used and sagemath does not need to start  new process again?

2) does sagemath 9.6 have now a way to timeout a call? For example, I'd like to do something like this (using Maple syntax for illustration)
 
     try
          timelimit(60, integrate(cos(x),x,algorithm="giac"))
     catch timeout:
          print("timed out");
    end try;

If not, are there any plans to one day add timelimit() to sagemath function calls? This is something very useful to have.

ps. I use sagemath on Linux only.

Thanks
--Nasser





Michael Orlitzky

unread,
May 12, 2022, 8:32:48 AM5/12/22
to sage-...@googlegroups.com
On Thu, 2022-05-12 at 03:32 -0700, 'Nasser M. Abbasi' via sage-devel
wrote:
> 1) I just want to confirm if this is what happens ( I could find
> information on this googling).
>
> If from sagemath, one makes a call to an external CAS, does sagemath create
> a subprocess, starts the external CAS executable in it, makes the call, and
> when the call is over, that subprocess is then killed? Or does the
> subprocess remain running until sagemath itself is closed?
>

There are a few different ways that sage interacts with other programs.
When the external program doesn't provide a library interface and we
have to interact with it by pretending to be a user, we typically use
pexpect for that. And in that case, the subprocess remains running.

You can confirm by running `ps aux` in another terminal.


>
> 2) does sagemath 9.6 have now a way to timeout a call? For example, I'd
> like to do something like this (using Maple syntax for illustration)
>
> try
> timelimit(60, integrate(cos(x),x,algorithm="giac"))
> catch timeout:
> print("timed out");
> end try;
>

This is a little tricky since the thing you want to timeout isn't
running within the sage process. The aforementioned pexpect interface
does support a timeout, but it isn't really exposed to the user. The
following seems to work:

sage: giac(2) # start the subprocess
2
sage: giac._expect.timeout = 0.0000001
sage: integrate(cos(x),x,algorithm="giac")
...
TIMEOUT: Giac with PID 15246 running /usr/bin/giac --sage
sage: giac.quit()

More generally, you can do anything in sage that you can do in python.
In situations where you don't have to ensure that some external
interface is consistent, whatever Google turns up for "python timeout"
should work fine.

Nasser M. Abbasi

unread,
May 12, 2022, 11:28:56 PM5/12/22
to sage-devel
Thanks, 

I've been testing this pexpect interface. But it does not seem to work for all CAS systems the same way.

Using sagemath 9.6, RC 4, it worked for fricas, but for giac and maxima it did not.

```
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.6.rc4, Release Date: 2022-05-12                 │
│ Using Python 3.10.4. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘

sage: x = var('x')
sage: integrand = cos(x)^3*(cos(2*x)-3*tan(x))/(sin(x)^2-sin(2*x))/sin(2*x)^(5/2)
sage: giac(2)
2
sage: giac._expect.timeout = 0.00000001 #set time out
sage: integrate(integrand,x,algorithm="giac")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: timeout must be a float or None
```


And with maxima it does not even time out. It hanged

```
>sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.6.rc4, Release Date: 2022-05-12                 │
│ Using Python 3.10.4. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: x = var('x')
sage: integrand = cos(x)^3*(cos(2*x)-3*tan(x))/(sin(x)^2-sin(2*x))/sin(2*x)^(5/2)
sage: maxima(2)
2
sage: maxima._expect.timeout = 0.00000001 #set time out
sage:  integrate(integrand,x,algorithm="maxima")
```
it hangs

Only with fricas it worked

```
>sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.6.rc4, Release Date: 2022-05-12                 │
│ Using Python 3.10.4. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: x = var('x')
sage: integrand = cos(x)^3*(cos(2*x)-3*tan(x))/(sin(x)^2-sin(2*x))/sin(2*x)^(5/2)
sage: fricas(2)
2
sage: fricas._expect.timeout = 0.00000001 #set time out
sage:  integrate(integrand,x,algorithm="fricas")
---------------------------------------------------------------------------
TIMEOUT                                   Traceback (most recent call last)
```

Now, fricas and maxima are the ONES installed by sagemath

```
>which sage
/home/me/TMP/sage-9.6.rc4/sage
>which maxima
/home/me/TMP/sage-9.6.rc4/local/bin/maxima
>which fricas
/home/me/TMP/sage-9.6.rc4/local/bin/fricas
```

But giac, since I had it already installed before building sagemath, and it is 
newer version, it used the one I had on the system.

```
>which giac
/usr/bin/giac
>giac --version
1.7.0
```

Does this makes a difference to pexpect? i.e. for pexpect timeout to work, does
the external CAS has to be the one build by sagemath and not the one
that is already installed in the system and build outside sagemath?  sagemath
comes with older CAS systems and I need to use ones I build myself
since they are newer (maxima for example is now at 5.46, but the one
that is build by sagemath is 5.45.

Note in the above, maxima was build by sagemath and the timeout still did not work.
Should it not have worked?

Thanks
--Nasser


Dima Pasechnik

unread,
May 13, 2022, 3:45:10 AM5/13/22
to sage-devel


On Thu, 12 May 2022, 11:32 'Nasser M. Abbasi' via sage-devel, <sage-...@googlegroups.com> wrote:

1)  I just want to confirm if this is what happens ( I could find information on this googling).

If from sagemath, one makes a call to an external CAS, does sagemath create a subprocess, starts the external CAS executable in it, makes the call, and when the call is over, that subprocess is then killed? Or does the subprocess remain running until sagemath itself is closed?

For example, when I do this first time:

sage: var('x')
sage: integrate(sin(x),x,algorithm="giac")
-cos(x)

At this point, is the giac process still running waiting for commands from sagemath? So that when I make another call

sage: integrate(cos(x),x,algorithm="giac")

Same process and any cashed data in it is used and sagemath does not need to start  new process again?

Indeed, repeated call to the same external CAS do not start new subprocesses.
(it would be prohibiltively expensive otherwise). Not only cashing in the external CAS is used,
Sage keeps handle objects which relate its Python side to these external objects. 

Note that some CASes have a dynamic library interface, and for them no subprocess is started at all. Such a dynamic library may either be a native Unix library itself, or a loadable module, say for a Lisp-based system such as Maxima.
We're transitioning from pexpect based interfaces to loadable library interfaces (so some system have at the moment both, e.g. Maxima, GAP, Pari/GP, Singular).


2) does sagemath 9.6 have now a way to timeout a call? For example, I'd like to do something like this (using Maple syntax for illustration)
 
     try
          timelimit(60, integrate(cos(x),x,algorithm="giac"))
     catch timeout:
          print("timed out");
    end try;

If not, are there any plans to one day add timelimit() to sagemath function calls? This is something very useful to have.
 

ps. I use sagemath on Linux only.

Thanks
--Nasser





--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/a6ec24ab-2217-4810-af21-2010a9062cebn%40googlegroups.com.

Michael Orlitzky

unread,
May 13, 2022, 4:44:38 AM5/13/22
to sage-...@googlegroups.com
On Thu, 2022-05-12 at 20:28 -0700, 'Nasser M. Abbasi' via sage-devel
wrote:
>
>
> And with maxima it does not even time out. It hanged
>

Integrating with maxima doesn't use the pexpect interface. There *is* a
pexpect interface to maxima, but there is also a library interface that
is faster. Some things have been ported to the faster interface while
others have not. Integration has.

Reply all
Reply to author
Forward
0 new messages