Problems reproducing code on SageMath on a Jupyter NB with dependence on FriCAS

asked 2023-05-08 00:53:01 +0100

toni gravatar image

updated 2023-05-08 14:02:05 +0100

I am using SageMath on Ubuntu running on a Windows Subsystem for Lynux (WSL), and trying to reproduce the following code, which calls for the package friCAS as a dependency. As you can see in the reproduced error, SageMath does recognize friCAS:

x = var('x')
y = function('y')(x)
de = x^3*diff(y,x,3) - 3*x^2*diff(y,x,2) + 6*x*diff(y,x) -6*y == 0
h = desolve(de,y,ics=[1,7,17,32],algorithm="fricas")
show(expand(h))

This produces an output without glitches when run on SageMath Cell Server.

I have installed friCAS in two different ways:

sudo apt install fricas

And including it (appending it) in a long list of packages in https://sagemanifolds.obspm.fr/install_ubuntu.html (here).

I can run friCAS and start the program from the Ubuntu cmd line.

Versions: SageMath 9.5 and FriCAS 1.3.7

Why am I getting the error below when running this identical code on a Jupyter Notebook?

Short version of the error:

TypeError: An error occurred when FriCAS evaluated 'x':
   Function declaration sageprint : InputForm -> String has been added to 
      workspace.

The long version of the error is:

x = var('x')
y = function('y')(x)
de = x^3*diff(y,x,3) - 3*x^2*diff(y,x,2) + 6*x*diff(y,x) -6*y == 0
h = desolve(de,y,ics=[1,7,17,32],algorithm="fricas")
show(expand(h))
x = var('x')
y = function('y')(x)
de = x^3*diff(y,x,3) - 3*x^2*diff(y,x,2) + 6*x*diff(y,x) -6*y == 0
h = desolve(de,y,ics=[1,7,17,32],algorithm="fricas")
show(expand(h))
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/lib/python3/dist-packages/sage/interfaces/expect.py in __init__(self, parent, value, is_name, name)
   1469             try:
-> 1470                 self._name = parent._create(value, name=name)
   1471             # Convert ValueError and RuntimeError to TypeError for

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in _create(self, value, name)
    512         name = self._next_var_name() if name is None else name
--> 513         self.set(name, value)
    514         return name

/usr/lib/python3/dist-packages/sage/interfaces/fricas.py in set(self, var, value)
    674         output = self.eval(cmd, reformat=False)
--> 675         self._check_errors(value, output)
    676 

/usr/lib/python3/dist-packages/sage/interfaces/fricas.py in _check_errors(self, line, output)
    569                 output = output.replace(old, new)
--> 570             raise RuntimeError("An error occurred when FriCAS evaluated '%s':\n%s" % (line, output))
    571 

RuntimeError: An error occurred when FriCAS evaluated 'x':
   Function declaration sageprint : InputForm -> String has been added to 
      workspace.


During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_206935/3617173396.py in <module>
      2 y = function('y')(x)
      3 de = x**Integer(3)*diff(y,x,Integer(3)) - Integer(3)*x**Integer(2)*diff(y,x,Integer(2)) + Integer(6)*x*diff(y,x) -Integer(6)*y == Integer(0)
----> 4 h = desolve(de,y,ics=[Integer(1),Integer(7),Integer(17),Integer(32)],algorithm="fricas")
      5 show(expand(h))

/usr/lib/python3/dist-packages/sage/calculus/desolvers.py in desolve(de, dvar, ics, ivar, show_method, contrib_ode, algorithm)
    565 
    566     if algorithm == "fricas":
--> 567         return fricas_desolve(de, dvar, ics, ivar)
    568     elif algorithm != "maxima":
    569         raise ValueError("unknown algorithm %s" % algorithm)

/usr/lib/python3/dist-packages/sage/calculus/desolvers.py in fricas_desolve(de, dvar, ics, ivar)
    113         y = fricas(de).solve(dvar.operator(), ivar).sage()
    114     else:
--> 115         eq = fricas.equation(ivar, ics[0])
    116         y = fricas(de).solve(dvar.operator(), eq, ics[1:]).sage()
    117 

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in __call__(self, *args, **kwds)
    665 
    666     def __call__(self, *args, **kwds):
--> 667         return self._parent.function_call(self._name, list(args), kwds)
    668 
    669     def _instancedoc_(self):

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in function_call(self, function, args, kwds)
    606             [0.5,5.5511151231257...e-15,21,0]
    607         """
--> 608         args, kwds = self._convert_args_kwds(args, kwds)
    609         self._check_valid_function_name(function)
    610         s = self._function_call_string(function,

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in _convert_args_kwds(self, args, kwds)
    567         for i, arg in enumerate(args):
    568             if not isinstance(arg, InterfaceElement) or arg.parent() is not self:
--> 569                 args[i] = self(arg)
    570         for key, value in kwds.items():
    571             if not isinstance(value, InterfaceElement) or value.parent() is not self:

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in __call__(self, x, name)
    300             # user-assigned name might change its value, so we return a
    301             # new element.
--> 302             result = self._coerce_from_special_method(x)
    303             return result if name is None else result.name(new_name=name)
    304         except TypeError:

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in _coerce_from_special_method(self, x)
    328             s = '_gp_'
    329         try:
--> 330             return (x.__getattribute__(s))(self)
    331         except AttributeError:
    332             return self(x._interface_init_())

/usr/lib/python3/dist-packages/sage/structure/sage_object.pyx in sage.structure.sage_object.SageObject._fricas_ (build/cythonized/sage/structure/sage_object.c:7704)()
    769             import sage.interfaces.fricas
    770             G = sage.interfaces.fricas.fricas
--> 771         return self._interface_(G)
    772 
    773     def _fricas_init_(self):

/usr/lib/python3/dist-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._interface_ (build/cythonized/sage/symbolic/expression.cpp:35189)()
   1202         if is_a_constant(self._gobj):
   1203             return self.pyobject()._interface_(I)
-> 1204         return super(Expression, self)._interface_(I)
   1205 
   1206     def _maxima_(self, session=None):

/usr/lib/python3/dist-packages/sage/structure/sage_object.pyx in sage.structure.sage_object.SageObject._interface_ (build/cythonized/sage/structure/sage_object.c:5852)()
    692             except Exception:
    693                 raise NotImplementedError("coercion of object %s to %s not implemented:\n%s\n%s" % (repr(self), I))
--> 694         X = I(s)
    695         if c:
    696             try:

/usr/lib/python3/dist-packages/sage/interfaces/interface.py in __call__(self, x, name)
    293 
    294         if isinstance(x, str):
--> 295             return cls(self, x, name=name)
    296         try:
    297             # Special methods do not and should not have an option to

/usr/lib/python3/dist-packages/sage/interfaces/expect.py in __init__(self, parent, value, is_name, name)
   1473             except (RuntimeError, ValueError) as x:
   1474                 self._session_number = -1
-> 1475                 raise TypeError(*x.args)
   1476             except BaseException:
   1477                 self._session_number = -1

TypeError: An error occurred when FriCAS evaluated 'x':
   Function declaration sageprint : InputForm -> String has been added to 
      workspace.
edit retag flag offensive close merge delete

Comments

What versions of Sagemath and Fricas are installed on your system ?

Note that the solution returned by desolve in this case is an expression. The solution you search is h.function(x).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-05-08 08:29:05 +0100 )edit

@Emmanuel Charpentier SageMath version 9.5 and Version: FriCAS 1.3.7. Thank you. I'm getting the same error with h.function(x).

toni gravatar imagetoni ( 2023-05-08 13:09:08 +0100 )edit

@toni :

  • 9.5 is an antique, by Sagemath standards.Try the latest you can install (installing the current development version isn't that hard on WSL Just a bit tedious...) and update your system packages.

  • You don't need to install fricas "systemwide (unless you want to use it ouside Sage...), installing it by adding --enable-fricas at the configure stage of installing from source. This way, your (mis-)chance of getting a mismatch between FRIcas and Sage versions are lowered.

  • I've tried your code, and iot WorksForMe(TM) on Debian testing + Sage 10.0.rc1. (I'd suggest some slight modifications to stay closer to the intended ise of the various Sage objects involved.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-05-08 20:10:17 +0100 )edit

@Emmanuel Charpentier I did try upgrading SageMath to, I believe, 9.8, as well as including --enable fricas, downloading the "tar ball" (?) into different directories as opposed to using the packages included in Ubuntu... I came close to having to reinstall Windows in my PC. All to now avail :-). I resigned myself (for now) to using CoCalc, which I don't particularly like, but has all the bells and whistles already installed...

toni gravatar imagetoni ( 2023-05-09 18:13:03 +0100 )edit

@Emmanuel Charpentier I installed Sage from source but I forgot the --enable-fricas you suggested. Any way of include it after the install?

toni gravatar imagetoni ( 2023-06-06 14:00:42 +0100 )edit