Calc

Table of Contents

Calc is an advanced desk calculator and mathematical tool written by Dave Gillespie that runs as part of the GNU Emacs environment.

1. Global Keybidings

  • <C-x> * c, <C-x> * *, <M-x> calc to open
    • <C-x> * o open another window
    • <C-x> * q quick calc. open in the minibuffer
  • <C-x> * e embedded calc on a block of continuous formula as a line, after evaluating it.
    • The separation of a block is detected by a two newlines. So the previous line must be empty.
    • <C-x> * j embed but on appropriate side of the => or :=. a:=10 becomes . .. 10
    • <C-x> * w embed on a single word if the expression is not parsable, otherwise same as just embedding.
    • The embed can be exited with any of the commands
  • <C-x> * b toggle full screen for the Calc
  • <C-x> * y y yank the top element to the most recent edit buffer
  • <C-x> * g grab, parse a line into the top element as vector
    • The number of line can be given as numeric prefix, and if only C-u is given the line is interpreted as a single entry.
    • Use Emacs region instead of evil region
  • <C-x> * r rectangle, parse a region as matrix
    • The column length can be given as numeric prefix
    • It works with evil region as well
  • <C-x> * : grab rectangle and sum the columns
    • <C-x> * _ grab rectangle and sum the rows
  • <C-x> * 0 reset
  • <C-x> * x turn calc off
  • <C-x> * m read in as a macro
  • <C-x> * z user invocation
    • start Calc with user defined macro by Z I

2. Local Keybindings

The key in the heading is the common prefix.

  • p set precision
    • The floating point precision default to 12
  • x run calc command <M-x> calc-
  • q quit

2.1. Help

  • i manual
  • h help
    • h all keybindings
    • s summary appendix
    • i manual
  • ? rotate through the brief help

2.2. Input

  • <, > scroll to the right/left
  • {, } scroll up/down

Edit

  • U undo, D redo
  • <TAB> rotate the top two
    • rotate the top N elements downward while wrapping if positive
    • move the entire stack N step downward if negative.
    • <M-...> rotate upwards
  • <DEL> remove the top
    • remove the top N elements if positive
    • Delete the entire stack if 0
    • N th element form the top is removed if negative
    • <M-...> remove from the bottom

Input

  • <RET> duplicate the top element
  • <M-RET> push the last arguments
    • recover the deleted elements by a function
  • [2-32]#NN input number in a given radix
  • NNeNN scientific notation
  • _ negative sign
  • : enters the fraction
  • [ .. , .. ; .. ] vector and matrix, [ .. ] [ .. ) interval, representing all number between the bounds
    • [ ( pushes the incomplete object and complete it later.
    • { .. , .. } can also be used. comma is required
  • ( .. , .. ) ( .. ; .. ) complex number (in polar),
  • NN@ NN' NN" hms time
  • " string
  • Operation can be put right after number
  • $ can be used in place of the top stack element, the output will be replace the top
    • $$, $$$ repeatedly using dollar sign access N th element from the top
  • ' algebraic input
    • Insert an expression containing symbols, equations, and various others
    • <RET> evaluate and the expression and insert
      • <M-RET> evaluate with the variables resolved, and insert
      • <C-j> or <LFD> (Line Feed) does not evaluate it, = to evaluate it later.
    • / has lower precedence than *.
    • a mod b modulo arithmetic
    • +/- to include the uncertainty
  • < , : > inline function
    • The arguments are given in a vector.
    • It cannot be used on its own. It needs to be evaluated by another function, such as, call(f, a, b), v A, apply(f, v)

2.3. Basic Operations

  • They take the elements from the stack and perform certain operations
  • Number prefix can be given
    • The binary operation is going to be reduce the top N elements if positive, or operate on each N next-to-top element by the top element if negative.
  • +, -, *, /, ^
  • \ integer division, % modulo
  • : fraction
  • ~ can be used to use the top element as the prefix number
  • K keep the arguments in the stack by prefixing this
  • Evaluation
    • = evaluate and substitute the stored variables
    • N expand the irrational values
    • a v evaluate algebra
  • Simplification
    • a s algebraic simplification
    • u s unit simplification
      • Automatically applied when in UnitSymp mode

2.4. Array Operations v (V)

  • p, pack(a, ...) pack first few top element into another object depending on the mode specified by numeric prefix N, or the value of the top element.
    • Vector of length N if N is nonnegative
    • -1 complex number
    • -2 polar complex number
    • -3 HMS form
    • -4 error form
    • -5 modulo form
    • -6 closed interval, -7 upper half-open interval, -8 lower half-open interval, -9 open interval
    • -10 fraction
    • -11 mantissa and exponent into floating point number
  • u, unpack(a, ...) unpack the top element
    • negative number specifies the mode
    • It can unpack any formula as well, including equations
      • It unpack the structure given by the underlying lisp representation.
  • (Without the prefix) |, vconcat(a,b) concatenate top two element into a vector, I | in reverse order, H | not recursively.
  • E convert an interval into a vector of integers
  • : vector into an interval
  • i push an identity matrix
  • d diagonal matrix out of the top
  • x push the index vector
  • r, c, mrow(m, i), mcol(m, i) extract the row/column of a matrix at the index of the top element or numeric prefix. s, subvec(m, i1, i2) extract a subvector from the first index up to the second index excluding it.
    • The index is counted from 1
      • A row/column is removed if negative number
    • m_i, subscr can be used for the algebraic case.
  • f search for an element in a vector
  • a arrange a vector or a matrix into a matrix of a given number of column
  • v reverse the vector
  • h, head(v) take the head, the first element of a vector.

2.4.1. Linear Algebra

  • The regular multiplication * does the matrix multiplication.
  • t transpose
  • J conjugate transpose
  • C cross product
  • (no prefix) A Euclidean norm or Frobenius norm
  • n Infinity norm
  • N one-norm
  • & inverse of a square matrix
  • D determinant
  • T trace
  • L LU decomposition
  • K Kronecker product

2.4.2. Element-wise Operations

  • A Apply
    • provide each element as each argument to a function.
  • M map, R reduce, U accumulate(scan)
    • Map can take multiple vectors as the inputs
    • :, _ map or reduce each column/row of matrices
    • N specify the number of arguments.
    • The element-wise operation can be any Calc binding such as +, v M, a ].
    • x select a lisp function
    • ' specify a custom function
      • Write the expression inline, for example, sin(x) + y.
        • The arguments of the function will be selected manually after you press RET.
      • The inline expression can contain $, $$ in which case you don't need to specify the arguments.
        • $ for the top, $$ for the second to top, and so on.
      • The anonymous function can contain #1 or # (one variable) in which case the arguments does not need to be specified.
      • A lambda expression can be passed as well, for example, lambda(a, b, a+b).
  • O, I generalized outer/inner product
    • Inner product requires specifying two operator

2.4.3. Set Operations

  • + remove duplicates, # set cardinality, V union, ^ intersection, - set difference, X set XOR, ~ set complement with respect to real number

2.5. Algebra a

  • v evaluate the algebra, " expand into the definition
  • s simplify
    • x expand: divide fractions, distribute multiplication
    • f factor a polynomial
      • H a f return the list of factors with its multiplicity.
    • c, collect express the polynomial in a given variable.
    • n, nrat(a) normalize rational (symbolic fractions into a one rational expression), a partial fraction
    • \ polynomial division, % polynomial remainder, g polynomial gcd
    • I a s trigonometric function simplification
    • H a s hyperbolic function simplification
    • a e extended (unsafe) simplification
  • d derivative, i (integ(f), integ(f(x), x, a, b)) integrate
    • I (ninteg) numerical integration
  • M map equation
    • Map both side of the equation
  • P find the root of polynomial
  • S solve the equations
    • It can also take a vector of equations and a vector of variables.
  • R root finding
    • A formula or an equation is solved numerically, starting from the top element
    • It can also take a vector of equations, and a vector of initial values, a vector of variables.
    • Assigned variable can be used, _ dose not constitute a variable.
    • The formulas have to be differentiable
  • F curve fitting
    • It act on a list of list containing each variables consecutively.
    • The number specify the detail
    • 1 linear, 2 - 9 multilinear
    • e, x, E, X polynomial, l, L logarithmic
    • q quadratic
    • g Gaussian function
    • s Logistic
  • + sum over a given range
  • * product over a given range

2.6. Builtin Functions

  • Inverse modifier I, and hyperbolic modifier H are often used
  • Q, sqrt(a)
  • F, floor(a), I F, ceil(a)
  • R, round(a), I R, trunc(a)
  • J, conj(a) complex conjugate, G, arg(z) argument
  • L, ln(z), E, exp(z), H L, log10(z), B, log(b x)
  • S, sin(z), C, cos(z), T, tan(z), H ...
  • ! factorial
  • f
  • k Combinatorial/Statistical

2.7. Random

  • r random()
    • It generate a random integer between 0 and the numeric prefix, or the top element
      • a generate a number reusing the recent value
      • h shuffle
    • It shuffles the top vector, may be truncated to a desired length

2.8. Units u

  • Units are just unassigned variables that are in the unit table
  • Number with unit can be entered as a algebraic expression. Most common units and constants are predefined
    • eps0 ɛ0, mu0 μ0, c, G, h, hbar, e ech, k, …
  • s simplify for the unit expression, a autorange unit prefix the unit with appropriate one.
  • c unit convert, n convert if exact, b convert to base unit, equivalent to s base
  • d, u define/undefine unit, r remove unit, u p make it permanent in the calc.el
  • g get unit definition, v show unit table
  • s Store
  • inf, e, pi, phi, gamma, uinf, nan are preassigned variables
    • These are only evaluated by N, not =

2.9. Store s

  • If the variable name is a number N it is stored in special variable qN. The quick variables can be directly specified without s
  • If the top element is an assign statement: =, :=, it will perform it
  • r recall, push the value
  • u unstore
  • t store into, pop the top element and store
  • x exchange with the top of the stack
  • c copy to another variable
  • +, - , … perform the operation and assign, equivalent to +=
  • = evaluate to, enter the evaluation statement of the top element
    • => The expression on the left is untact, the value on the right is recomputed by = (evaluate) whenever something changes.
  • d declare the type of a variable or a function
  • l temporarily store the top element and evaluate the second-top element with it.
  • i (calc-insert-variables) insert all variables in current calc to a buffer.
    • variable can be defined any other buffer than calc
    • literal string is interpreted automatically by calc
  • p (calc-permanent-variable) specify variable to store into the calc-settings-file (.emacs.d/calc.el by default)
    • edit the file directly when deleting variable

2.10. Select j

  • Select within a entry and perform operation on them
  • s, u select/unselect
  • a add, b break selection, m l select more/less, 1, …, 9 select =N=th part
  • n, p select next/previous
  • d highlight selected part within the whole formula
  • c clear all selection
  • e enable/disable selection. the * marker disappears and the operation works on the entire formula anyway. It is enables by default.
  • ' replace selection through minibuffer input
  • ` edit selection through a temporary buffer
  • R, L commute right/left, C commute within a selection
  • D, M distribute/merge a level
  • J jump equals, move a term on the other side of the equation
  • +, -, *, /, … Operate on both side of a equation, or numerator and denominator of a fraction.

2.11. Convert c

  • f convert fraction to float, F float to fraction
  • p between polar and rectangular complex numbers
  • d, r, h degree, radian, hms

2.12. Display d

  • t Truncate. It cuts the stack at the cursor, and stashes the ones "above" it.
  • r change the radix
  • c, i, j, complex number format
  • Language
    • N normal, B big (pretty print), U unformatted(raw Lisp), C, L LaTeX, T TeX
    • The complex number format does not apply in B and C

2.13. Mode m

  • The current mode is displayed in the mode line
    • a partly algebraic mode, ( and [ also starts algebraic input.
    • t full algebraic mode, <ESC> to not use algebraic input.
  • S ignore cases, the prefix key becomes case insensitive.
    • d degree, r radian
      • The default unit of the output of functions that produce angles
    • p polar mode, prefer complex number
    • f fraction mode, prefer fraction
      • c f can be used to evaluate the fractions
    • i infinite mode, 1/0 produce inf
    • s symbolic mode, the irrational result will be left symbolic
      • N can be used evaluate into numeric result
    • v matrix mode, every object is assumed to be matrices unless it cannot be
    • C toggle automatic recomputation, =>
    • Simplification
      • I basic simplification
      • U unit simplification
      • A algebraic simplification (Default)
      • N number simplification, only number operations are simplified
      • E extended simplification
      • O no simplification
    • m save modes, including display/language/precision/definition/calculation
    • g get modes, push the mode numbers
    • R record mode change directly into the file

2.14. Trail t

  • d toggle trail
  • i, o focus into/outof trail
  • n, p, [, ] move the trail marker > (to the start/end), y push trail marker to the stack
  • s, r search (in reverse)

2.15. Time t

  • N push current datetime
  • D date into a number measured since Jan 1, 1 AD
  • J convert date into Julian day count
  • U convert date into unix time value
  • C convert time zone of date

2.16. Graphing g

  • It stores the values in a temporary buffer and let the Gnuplot plot it.
  • f Fast plot. graph the top two element using . top element is the \(y\).
    • \(x\), \(y\) can be a vector of same length
    • \(x\) can be an interval, then \(y\) needs to be an algebraic expression in one unassigned variable
    • The top element can be a formula of the form xy(x(t), y(t)).
  • p plot
  • a, d, c add/delete/clear the plot
  • g, b, k… grid, border, key
  • N select the number of datapoints
  • q quit the plot

2.17. Define Function and Macro z, Z

  • Z D bind the function to a key
  • C-x ( , C-x ) the global kmacro can include the following calc specific functionalities.

    • Branch
      • <COND> Z [ start if, Z | else if, Z : else, Z ] end if
    • Loop

      • <repeat count> Z < <BODY> Z > repeats a command in between, the top value times.
        • <repeat count> is the top element
          • <init> <final> Z ( <BODY> <step> Z ) for loop, repeat with

      counter

      • <init>, <final> are the top two elements of the stack
      • <step> is a number
        • <COND> Z / break out of macro loop
      • Breaks when the top element <COND> evaluates to true.
        • Z {, Z } loop forever
      • Scope
        • Z `
        • Z '
      • Z K set macro name for the last recorded macro, which can

    be executed by z

    • Z E edit macro
    • <C-x> e or X execute macro
      • <C-x> e create a undo history for each command, X create a single undo history
  • Z I define user invocation
    • m m saves this
  • Z F define a formula
    • The formula at the top element is stored into specified key, command and function.
    • The anonymous function can also be used.
    • Z G push the formula to the stack
  • Z P define a command permanently in the calc.el
  • Z U undefine
  • z is prefixed to use the user defined keys and functions
  • Lisp
    • defmath can be used
    • (interactive NUM TAG)
      • Takes NUM arguments from the stack.
    • (calc-eval "expr") is available

2.18. Register r

  • s copy the region as calc stack to register
  • i insert calc stack, or parse the string and insert

3. Configurations

Configurations can be stored in:

  • .emacs: Configure manually or via Custom
  • the file set by calc-settings-file ( .emacs.d/calc.el by default).
    • This file is loaded on the first launch of Calc.

The global variables can be accessed directly, but the internal functions and variables needs to be imported via (require 'calc-*).

  • The variables that controls the initialization can be set.
  • The detail can be directly found in the repo

4. References

Author: Jeemin Kim

Created: 2026-07-12 Sun 14:28