Common calculations in Emacs/Calc

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.

Basic calculations

RPN style and stack operation

  • Enter number (use _ for negative sign) then press <RETURN> or <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: Q
  • Reciprocal: &
  • Change sign (negative, positive): n
  • Function: f followed by function name, for example: s for sine, c for cosine; prefix with H for hyperbolic.
  • Duplicate the top of stack: <RETURN> or <SPACE>
  • Edit top of stack in a buffer (can be used to fix error): `
  • Undo: U, Redo: D

Algebraic style

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.

Variables

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 q0 to q9.

  • Store to variable and leave TOS unchanged: s s followed by a name or a number. To store to a quick variable: s followed by a number.
  • Store to variable and remove TOS: s t and t for quick variables.
  • Recall a variable to TOS: s r and r.
  • Un-store a variable: s u

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 =>

If 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.

Symbolic computation

Prefixed by command a.

  • Expand: a x
  • Collect: a c
  • Simplified: a s
  • Derivative: a d
  • Integrate: a i (note that Calc can only integrate simple expressions, use numerical integration if possible).
  • Numerical integration: a I
  • Form an equation: with both sides on the stack, type a = to create an equation. For example: put 2 x + y and 3 on 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: a P

Vector and matrix

Prefixed by v or V.

  • Use algebraic input style and enter vectors and matrices as in Matlab.
  • Identity matrix: v i
  • All operations such as +, -, * perform on vectors/matrices, not on elements. So dimentions must match.
  • Invert matrix: &
  • Transpose: v t
  • Mapping: with vectors or matrices on stack, type v M followed 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 A x = b: put b on stack followed by A, then /.

Graphics

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s