Whenever I need to perform some calculations or plotting (for example to verify some equality that I am not sure of, to solve an equation, to inspect a function), I often open Matlab. It is fine if Matlab is already running. However, if it is not, this practice takes quite some time on my old laptop. Furthermore, Matlab is not the perfect tool for every type of calculation.
Today, I rediscovered Calc in Emacs. I occasionally use Calc when writing LaTeX to convert between different mathematical representations (this post), but never beyond that. Calc is a very powerful calculator and symbolic computation engine. But learning and memorizing its commands can be overwhelming unless you use it everyday. So I make this short cheat sheet for my reference. Hopefully it will be useful for some people too.
Enter and exit Calc
- Start Calc: from everywhere in Emacs, type
C-x * *.
- Quit Calc:
q(this only hides the Calc buffer).
- Really quit Calc (remove everything):
C-x * *inside Calc buffer.
RPN style and stack operation
- Enter number (use
_for negative sign) then press
<SPACE>to push it to the stack.
- Delete top of stack:
<DEL>, delete all:
C-u 0 <DEL>.
- Type an operator (+, -, *, /, ^, etc) to perform calculation on the stack.
- Square root:
- Change sign (negative, positive):
ffollowed by function name, for example:
cfor cosine; prefix with
- Duplicate the top of stack:
- Edit top of stack in a buffer (can be used to fix error):
Enter an algebraic expression: ‘ (apostrophe) then enter the expression. Note that division has lower priority than multiplication. Some examples of expression:
2 + 3*(6-1) 2 x + 3 sqrt(y) sin(x+y)^2 2 x + 3 y*(x + 1)
Note that you can use space for multiplication, but beware if a variable is followed by parentheses. For example, the last expression would be wrong if it were written as
2 x + 3 y (x + 1) because
y will be interpreted as a function.
To refer to TOS:
$, just before TOS:
$$, and so on.
Current value at top of stack can be stored in a variable. Variables can have any valid name. There are 10 quick variables referred to by numbers, from
- Store to variable and leave TOS unchanged:
s sfollowed by a name or a number. To store to a quick variable:
sfollowed by a number.
- Store to variable and remove TOS:
tfor quick variables.
- Recall a variable to TOS:
- Un-store a variable:
Variables can be used in expressions. However, when a variable changes its value, all previous expressions on the stack do not change, unless they are postfixed by
=>. For example:
1: 2 x + 3 y => 2: x - y =>
x is assigned a value, both expressions will be evaluated and the results are appended after the
=> symbols. The update is dynamic, so if
x is un-stored, both expressions will change to their original form.
Prefixed by command
a i(note that Calc can only integrate simple expressions, use numerical integration if possible).
- Numerical integration:
- Form an equation: with both sides on the stack, type
a =to create an equation. For example: put
2 x + yand
3on stack and type
a =to create
2 x + y = 3.
- Solve equation:
a S(only give one solution of the form
x = 1.234). To extract the value only:
a .(a followed by a period).
- Solve all solutions:
Vector and matrix
- Use algebraic input style and enter vectors and matrices as in Matlab.
- Identity matrix:
- All operations such as +, -, * perform on vectors/matrices, not on elements. So dimentions must match.
- Invert matrix:
- Mapping: with vectors or matrices on stack, type
v Mfollowed by any operator will perform the operation element-wise.
- Mapping: with a vector or matrix on stack, followed by an expression at TOS, type
v M $will perform the expression on each element and return the results as a vector/matrix.
- Solve linear equations : put
bon stack followed by
Calc can plot data and functions using Gnuplot. In most cases, it is better to use Gnuplot (either in its console or in Emacs). I find it more convenient to plot functions in Gnuplot than in Matlab. Gnuplot can also plot data from CSV files. In case you have your functions and/or data already in Calc, it might be more convenient to tell Calc to plot them to Gnuplot for you. All graphing commands start with
g. The main commands are
g f for 2-D plotting and
g F for 3-D. Both take values on the stack (2 for 2-D and 3 for 3-D). The values can be vectors (x and y and z data), matrices for 3-D, functions (to plot functions) and so on. These are discussed in more details in the info page of Calc.