Possible problem with our interfaces to maxima

84 views
Skip to first unread message

Emmanuel Charpentier

unread,
Aug 22, 2024, 7:04:32 AM8/22/24
to sage-support

Inspired by this ask.sagemath.org question.

Compare this Maxima standalone execution :

(%i1) display2d:false; (%o1) false (%i2) g: 1/4*((w1^2*w3^2*x1^4*x2^6*x3^8 + 2*(w1^2*w3^2*x1^4*x2^6 + w1^2*w3*x1^3*x2^5)*x3^7 + (w1^2*w3^2*x1^4*x2^6 + 2*w1^2*w3*x1^3*x2^5 + 2*w1^2*x1^2*x2^4)*x3^6)*x4^6 + 2*((w1^2*w3^2*x1^4*x2^6 + (w1^2*w3^2*x1^4 + (2*w1^2*w3 + w1*w3^2)*x1^3)*x2^5)*x3^7 + 2*(w1^2*w3^2*x1^4*x2^6 + (w1^2*w3^2*x1^4 + (3*w1^2*w3 + w1*w3^2)*x1^3)*x2^5 + (w1^2*w3*x1^3 + (w1^2 + w1*w3)*x1^2)*x2^4)*x3^6 + (w1^2*w3^2*x1^4*x2^6 + (w1^2*w3^2*x1^4 + (4*w1^2*w3 + w1*w3^2)*x1^3)*x2^5 + 2*(w1^2*w3*x1^3 + (2*w1^2 + w1*w3)*x1^2)*x2^4 + 2*(w1^2*x1^2 + w1*x1)*x2^3)*x3^5)*x4^5 + ((w1^2*w3^2*x1^4*x2^6 + 2*(w1^2*w3^2*x1^4 + (3*w1^2*w3 + 2*w1*w3^2)*x1^3)*x2^5 + (w1^2*w3^2*x1^4 + 2*(3*w1^2*w3 + 2*w1*w3^2)*x1^3 + 2*(3*w1^2 + 6*w1*w3 + w3^2)*x1^2)*x2^4)*x3^6 + 2*(w1^2*w3^2*x1^4*x2^6 + (2*w1^2*w3^2*x1^4 + (7*w1^2*w3 + 4*w1*w3^2)*x1^3)*x2^5 + (w1^2*w3^2*x1^4 + 4*(2*w1^2*w3 + w1*w3^2)*x1^3 + (9*w1^2 + 16*w1*w3 + 2*w3^2)*x1^2)*x2^4 + (w1^2*w3*x1^3 + (3*w1^2 + 4*w1*w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5*x1*x2) + 3*(x2^3 + x2^2)*x3^4*e^(w5*x1*x2) + 3*(x2^3 + x2^2)*x3^3*e^(w5*x1*x2) + (x2^3 + x2^2)*x3^2*e^(w5*x1*x2))*x4^2*e^(w6*x1*x2*x3) + 3*((x2^3 + 2*x2^2 + x2)*x3^4*e^(w5*x1*x2) + 3*(x2^3 + 2*x2^2 + x2)*x3^3*e^(w5*x1*x2) + 3*(x2^3 + 2*x2^2 + x2)*x3^2*e^(w5*x1*x2) + (x2^3 + 2*x2^2 + x2)*x3*e^(w5*x1*x2))*x4*e^(w6*x1*x2*x3) + ((x2^3 + 3*x2^2 + 3*x2 + 1)*x3^3*e^(w5*x1*x2) + 3*(x2^3 + 3*x2^2 + 3*x2 + 1)*x3^2*e^(w5*x1*x2) + 3*(x2^3 + 3*x2^2 + 3*x2 + 1)*x3*e^(w5*x1*x2) + (x2^3 + 3*x2^2 + 3*x2 + 1)*e^(w5*x1*x2))*e^(w6*x1*x2*x3))$ (%i3) foo:coeff(taylor(g, x4, 0, 8), x4^7)$

which works okay, with these Sage snippets :

sage: g.taylor(x4, 0, 7).coefficient(x4, 7) --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) File /usr/local/sage-10/src/sage/interfaces/interface.py:750, in InterfaceElement.__init__(self, parent, value, is_name, name) 749 try: --> 750 self._name = parent._create(value, name=name) 751 except (TypeError, RuntimeError, ValueError) as x: File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:623, in MaximaLib._create(self, value, name) 622 else: --> 623 self.set(name, value) 624 except RuntimeError as error: File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:533, in MaximaLib.set(self, var, value) 532 cmd = '%s : %s$' % (var, value.rstrip(';')) --> 533 self.eval(cmd) File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:479, in MaximaLib._eval_line(self, line, locals, reformat, **kwds) 478 if statement: --> 479 maxima_eval("#$%s$" % statement) 480 if not reformat: File /usr/local/sage-10/src/sage/libs/ecl.pyx:830, in sage.libs.ecl.EclObject.__call__() 829 lispargs = EclObject(list(args)) --> 830 return ecl_wrap(ecl_safe_apply(self.obj, (<EclObject>lispargs).obj)) 831 File /usr/local/sage-10/src/sage/libs/ecl.pyx:358, in sage.libs.ecl.ecl_safe_apply() 357 else: --> 358 raise RuntimeError("ECL says: {}".format(message)) 359 else: RuntimeError: ECL says: THROW: The catch RAT-ERR is undefined. During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) Cell In[161], line 1 ----> 1 g.taylor(x4, Integer(0), Integer(7)).coefficient(x4, Integer(7)) File /usr/local/sage-10/src/sage/symbolic/expression.pyx:5044, in sage.symbolic.expression.Expression.taylor() 5042 except Exception: 5043 raise NotImplementedError("Wrong arguments passed to taylor. See taylor? for more details.") -> 5044 l = self._maxima_().taylor(B) 5045 return self.parent()(l) 5046 File /usr/local/sage-10/src/sage/interfaces/interface.py:698, in InterfaceFunctionElement.__call__(self, *args, **kwds) 697 def __call__(self, *args, **kwds): --> 698 return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds) File /usr/local/sage-10/src/sage/interfaces/interface.py:617, in Interface.function_call(self, function, args, kwds) 613 self._check_valid_function_name(function) 614 s = self._function_call_string(function, 615 [s.name() for s in args], 616 ['%s=%s' % (key, value.name()) for key, value in kwds.items()]) --> 617 return self.new(s) File /usr/local/sage-10/src/sage/interfaces/interface.py:386, in Interface.new(self, code) 385 def new(self, code): --> 386 return self(code) File /usr/local/sage-10/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name) 296 pass 298 if isinstance(x, str): --> 299 return cls(self, x, name=name) 300 try: 301 # Special methods do not and should not have an option to 302 # set the name directly, as the identifier assigned by the 303 # interface should stay consistent. An identifier with a 304 # user-assigned name might change its value, so we return a 305 # new element. 306 result = self._coerce_from_special_method(x) File /usr/local/sage-10/src/sage/interfaces/interface.py:752, in InterfaceElement.__init__(self, parent, value, is_name, name) 750 self._name = parent._create(value, name=name) 751 except (TypeError, RuntimeError, ValueError) as x: --> 752 raise TypeError(x) TypeError: ECL says: THROW: The catch RAT-ERR is undefined.

Furthermore,

sage: foo=maxima_calculus("coeff(taylor(%s, %s, %s, %s), %s);"%tuple(map(lambda u:u._maxima_lib_init_(), (g, x4, 0, 8, x4^7)))) --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) File /usr/local/sage-10/src/sage/interfaces/interface.py:750, in InterfaceElement.__init__(self, parent, value, is_name, name) 749 try: --> 750 self._name = parent._create(value, name=name) 751 except (TypeError, RuntimeError, ValueError) as x: File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:623, in MaximaLib._create(self, value, name) 622 else: --> 623 self.set(name, value) 624 except RuntimeError as error: File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:533, in MaximaLib.set(self, var, value) 532 cmd = '%s : %s$' % (var, value.rstrip(';')) --> 533 self.eval(cmd) File /usr/local/sage-10/src/sage/interfaces/maxima_lib.py:479, in MaximaLib._eval_line(self, line, locals, reformat, **kwds) 478 if statement: --> 479 maxima_eval("#$%s$" % statement) 480 if not reformat: File /usr/local/sage-10/src/sage/libs/ecl.pyx:830, in sage.libs.ecl.EclObject.__call__() 829 lispargs = EclObject(list(args)) --> 830 return ecl_wrap(ecl_safe_apply(self.obj, (<EclObject>lispargs).obj)) 831 File /usr/local/sage-10/src/sage/libs/ecl.pyx:358, in sage.libs.ecl.ecl_safe_apply() 357 else: --> 358 raise RuntimeError("ECL says: {}".format(message)) 359 else: RuntimeError: ECL says: THROW: The catch RAT-ERR is undefined. During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) Cell In[165], line 1 ----> 1 foo=maxima_calculus("coeff(taylor(%s, %s, %s, %s), %s);"%tuple(map(lambda u:u._maxima_lib_init_(), (g, x4, Integer(0), Integer(8), x4**Integer(7))))) File /usr/local/sage-10/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name) 296 pass 298 if isinstance(x, str): --> 299 return cls(self, x, name=name) 300 try: 301 # Special methods do not and should not have an option to 302 # set the name directly, as the identifier assigned by the 303 # interface should stay consistent. An identifier with a 304 # user-assigned name might change its value, so we return a 305 # new element. 306 result = self._coerce_from_special_method(x) File /usr/local/sage-10/src/sage/interfaces/interface.py:752, in InterfaceElement.__init__(self, parent, value, is_name, name) 750 self._name = parent._create(value, name=name) 751 except (TypeError, RuntimeError, ValueError) as x: --> 752 raise TypeError(x) TypeError: ECL says: THROW: The catch RAT-ERR is undefined.

Here, the maxima_lib interface does not hint at the source of problem.

sage: foo=maxima("coeff(taylor(%s, %s, %s, %s), %s);"%tuple(map(lambda u:u._maxima_init_(), (g, x4, 0, 8, x4^7)))) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[168], line 1 ----> 1 foo=maxima("coeff(taylor(%s, %s, %s, %s), %s);"%tuple(map(lambda u:u._maxima_init_(), (g, x4, Integer(0), Integer(8), x4**Integer(7))))) File /usr/local/sage-10/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name) 296 pass 298 if isinstance(x, str): --> 299 return cls(self, x, name=name) 300 try: 301 # Special methods do not and should not have an option to 302 # set the name directly, as the identifier assigned by the 303 # interface should stay consistent. An identifier with a 304 # user-assigned name might change its value, so we return a 305 # new element. 306 result = self._coerce_from_special_method(x) File /usr/local/sage-10/src/sage/interfaces/maxima.py:1176, in MaximaElement.__init__(self, parent, value, is_name, name) 1155 def __init__(self, parent, value, is_name=False, name=None): 1156 """ 1157 Create a Maxima element. 1158 See ``MaximaElement`` for full documentation. (...) 1174 True 1175 """ -> 1176 ExpectElement.__init__(self, parent, value, is_name=False, name=None) File /usr/local/sage-10/src/sage/interfaces/expect.py:1518, in ExpectElement.__init__(self, parent, value, is_name, name) 1516 else: 1517 try: -> 1518 self._name = parent._create(value, name=name) 1519 # Convert ValueError and RuntimeError to TypeError for 1520 # coercion to work properly. 1521 except (RuntimeError, ValueError) as x: File /usr/local/sage-10/src/sage/interfaces/interface.py:517, in Interface._create(self, value, name) 515 def _create(self, value, name=None): 516 name = self._next_var_name() if name is None else name --> 517 self.set(name, value) 518 return name File /usr/local/sage-10/src/sage/interfaces/maxima.py:1015, in Maxima.set(self, var, value) 1013 cmd = '%s : %s$' % (var, value.rstrip(';')) 1014 if len(cmd) > self.__eval_using_file_cutoff: -> 1015 self._batch(cmd, batchload=True) 1016 else: 1017 self._eval_line(cmd) File /usr/local/sage-10/src/sage/interfaces/maxima.py:905, in Maxima._batch(self, s, batchload) 903 self._expect_expr(s) 904 out = self._before() --> 905 self._error_check(cmd, out) 906 os.unlink(filename) 907 return out File /usr/local/sage-10/src/sage/interfaces/maxima.py:949, in Maxima._error_check(self, cmd, out) 947 m = r.search(out) 948 if m is not None: --> 949 self._error_msg(cmd, out) File /usr/local/sage-10/src/sage/interfaces/maxima.py:966, in Maxima._error_msg(self, cmd, out) 951 def _error_msg(self, cmd, out): 952 """ 953 Raise error with formatted description. 954 (...) 964 Principal Value 965 """ --> 966 raise TypeError("Error executing code in Maxima\nCODE:\n\t%s\nMaxima ERROR:\n\t%s" % (cmd, out.replace('-- an error. To debug this try debugmode(true);', ''))) TypeError: Error executing code in Maxima CODE: batchload("/tmp/tmp2d0k4lw6.input-1538064300");1+915520876; Maxima ERROR: kill(sage6)$batchload("/tmp/tmp2d0k4lw6.input-1538064300");1+915520876; PQUOTIENT: Quotient by a polynomial of higher degree (case 2a) -- an error. To debug this try: debugmode(true); (%o47)

Here, an external maxima process, launched by Sage, gives us an hint to the error.

However, as initialized by Sage, this process triggers an error not occurring in a “standalone” `maxima process.

I did not find a relevant ticket in Github. Suggestions for filing a new issue ?

HTH,

Dima Pasechnik

unread,
Aug 22, 2024, 7:58:30 AM8/22/24
to sage-s...@googlegroups.com
To see exactly how it fails in Maxima, it has to be initialised with the same options as passed to maxima_calculus. It loads several non-default Maxima packages.
I suppose one of the latter triggers the error in question.

Nils Bruin

unread,
Aug 22, 2024, 11:42:06 AM8/22/24
to sage-support
By the looks of it, the place of failure is the same in both cases. In the case of using maxima_lib, the maxima error generation doesn't quite work though: as you can see, a condition RAT-ERR is not defined. That means there is code that "throws" the condition, assuming there's a "catch" set up around it. maxima_lib was originally designed to provide lower overhead for maxima access as well as provide a more robust interface by avoiding string parsing as much as possible. So it tries to run maxima code in a lisp environment that is as cheaply set up as possible. There is some catching, but I did not trace through all of maxima to see what catches can occur. Note that maxima sets up its evaluation loop once with all its catches around it, so set up is amortized. For maxima_lib, every command will exit back completely, so the stack frames holding the condition catches all need to be built up and torn down for every access to maxima_lib.

That explains why you see the message about RAT-ERR rather than the PQUOTIENT message (presumably, the RAT-ERR condition has some extra data hanging off it that the corresponding catch can use to generate a message with more data)

As to why something fails when called via sage and succeeds when executed in maxima directly: that will require some drilling down. There are non-standard settings that sage makes for its maxima-calculus instance, but that's unlikely to make a difference for polynomial division. There may be a small inequivalence between the code that is actually generated (note that maxima_lib will be entering and exiting the maxima environment for every method call, leaving the return value on ECL's heap to serve as input for the next call, whereas the maxima evaluation loop has its own way of chaining things together)

It looks to me it could be made into a ticket/issue.

Emmanuel Charpentier

unread,
Aug 25, 2024, 4:52:20 AM8/25/24
to sage-support

Correction :

  • /usr/bin/maxima, the “standalone” version of maxima provided by Debian testing, runs without error :
(%i3) t:taylor(g, x4, 0, 8)$ (%i4)
  • /usr/local/sage-10/local/bin/maxima, “our” version of Maxima, fails to compute the Taylor series :
(%i4) t:taylor(g, x4, 0, 8)$ PQUOTIENT: Quotient by a polynomial of higher degree (case 2a) -- an error. To debug this try: debugmode(true); (%i5)

Ticket underway (slowly).

HTH,

Dima Pasechnik

unread,
Aug 25, 2024, 5:25:12 AM8/25/24
to sage-s...@googlegroups.com


On 25 August 2024 09:52:20 BST, Emmanuel Charpentier <emanuel.c...@gmail.com> wrote:
>
>
>*Correction :*
>
> - /usr/bin/maxima, the “standalone” version of maxima provided by Debian
> testing, runs without error :
>
>(%i3) t:taylor(g, x4, 0, 8)$ (%i4)
>
> - /usr/local/sage-10/local/bin/maxima, “our” version of Maxima, fails to
> compute the Taylor series :
>
>(%i4) t:taylor(g, x4, 0, 8)$ PQUOTIENT: Quotient by a polynomial of higher
>degree (case 2a) -- an error. To debug this try: debugmode(true); (%i5)


our Maxima version is older by one or two versions. (5.45 vs 5.47)

Dima
>>> <https://ask.sagemath.org/question/78686/taylor-expansion-in-sagemath-returns-error/>
>>> .
>>> Here, an external maxima process, *launched by Sage*, gives us an hint
>>> to the error.
>>>
>>> However, *as initialized by Sage,* this process triggers an error *not
>>> occurring in a “standalone” `maxima process.*

Jan Groenewald

unread,
Aug 25, 2024, 5:49:43 AM8/25/24
to sage-s...@googlegroups.com
Hi

Debian Stable's maxima 46 also shows the error:

0 jan@jan-latitude:~$dpkg -l maxima|grep ii
ii  maxima         5.46.0-11    amd64        Computer algebra system -- base system
0 jan@jan-latitude:~$maxima -q<test-taylor.mac

(%i1)
(%i2)
PQUOTIENT: Quotient by a polynomial of higher degree (case 2a)
 -- an error. To debug this try: debugmode(true);

Regards,
Jan



--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/8CB1F994-36C2-454A-8CFC-229E3E9FACB2%40gmail.com.

Emmanuel Charpentier

unread,
Sep 1, 2024, 11:26:18 AM9/1/24
to sage-support

Ticket filed. This boils down to “upgrade to Maxima 5.47.0”.

HTH,

Reply all
Reply to author
Forward
0 new messages