CoCalc Shared Filesmaxima-in-sagews.sagewsOpen in CoCalc with one click!
Author: Samuel Lelièvre
Views : 56
Description: Maxima in CoCalc's sagews worksheets

Using Maxima in CoCalc's .sagews worksheets

This discussion is prompted by CoCalc issue #54:

This file is shared at:

Use cell magic to set the worksheet to Maxima mode

The first thing to do is to use the %default_mode magic to set the whole worksheet to Maxima mode.

(Note: it's important to put this magic command in its own cell.)

%default_mode maxima

We can now use Maxima syntax.

cos(%pi/6)
sqrt(3)/2
trigsimp(2*cos(x)^2 + sin(x)^2)
cos(x)^2+1
integrate(log(x), x)
x*log(x)-x

How to deal with Maxima names that start with %

Problem

For example, the mathematical constants π\pi and ee are represented by %pi and %e in Maxima, but here starting a cell with % is interpreted as a cell magic.

Similarly, one would normally get numerical expression for π\pi and ee by evaluating the following in Maxima:

%pi, numer
%e, numer
%pi
<string>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details. Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1324, in execute_with_code_decorators code = code_decorator(code) File "sage/symbolic/expression.pyx", line 5447, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:33873) return self._parent._call_element_(self, *args, **kwds) File "sage/symbolic/ring.pyx", line 977, in sage.symbolic.ring.SymbolicRing._call_element_ (build/cythonized/sage/symbolic/ring.cpp:11660) raise ValueError("the number of arguments must be less than or equal to %s"%len(vars)) ValueError: the number of arguments must be less than or equal to 0
%e
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1324, in execute_with_code_decorators code = code_decorator(code) File "sage/symbolic/expression.pyx", line 5447, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:33873) return self._parent._call_element_(self, *args, **kwds) File "sage/symbolic/ring.pyx", line 977, in sage.symbolic.ring.SymbolicRing._call_element_ (build/cythonized/sage/symbolic/ring.cpp:11660) raise ValueError("the number of arguments must be less than or equal to %s"%len(vars)) ValueError: the number of arguments must be less than or equal to 0
%pi, numer
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1324, in execute_with_code_decorators code = code_decorator(code) TypeError: 'tuple' object is not callable
%e, numer
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1324, in execute_with_code_decorators code = code_decorator(code) TypeError: 'tuple' object is not callable

Workarounds

Working around the problem is easy: it is enough to put something before the %pi, %e, or other %-expression, so that % is no longer the first character in the cell, and thus does not get interpreted as a cell magic.

For example,

  • introduce an initial space in the compute cell before the % expression, or
  • introduce an initial blank line in the compute cell before the % expression, or
  • introduce an initial comment in the compute cell before the % expression.

These three are illustrated below.

%pi, numer /* notice the initial space in this cell */
3.141592653589793
%e,numer /* notice the initial space in this cell */
2.718281828459045
%pi, numer /* notice the initial blank line in this cell */
3.141592653589793
%e, numer /* notice the initial blank line in this cell */
2.718281828459045
/* The constant pi */ %pi, numer /* notice the initial comment */
3.141592653589793
/* The constant e */ %e, numer /* notice the initial comment */
2.718281828459045