The Awesomeness of Calc
I have recently been playing with emacs a lot and its calculator, Calc, is my latest obsession.
Let me clarify first that Calc is not simply a calculator, but a full computer algebra system. It’s capable of matrix calculations for example. It uses RPN (reverse polish notation) by default, but has another normal (algebraic) calculator. Don’t worry about RPN, it’s easy to learn and just makes it fun to use.
One of the great features of emacs is the ability to transfer text between buffers easily. This enables you, for example, to transfer an expression from a file to to the calculator and back.
To start Calc, press C-x * *
.
For more information about Calc, see the calc card. In this post I will focus on some use cases:
- Grabbing text
- Evaluating expressions
Grabbing Text
Suppose you have data in textual format such as:
x y
--- ---
1.34 0.234
1.41 0.298
1.49 0.402
1.56 0.412
1.64 0.466
1.73 0.473
1.82 0.601
1.91 0.519
2.01 0.603
2.11 0.637
2.22 0.645
2.33 0.705
2.45 0.917
2.58 1.009
2.71 0.971
2.85 1.062
3.00 1.148
3.15 1.157
3.32 1.354
First highlight a region using
C-space
marks a continuous regionC-x space
marks a rectangular region
From here, you can do the following to send the text to the calculator
C-x * :
sums columnsC-x * -
sums rowsC-x * g
grabs text and sends it to the calculator as a 1D vector.C-x * r
grabs rows of text into vectors.
Now you are in the calculator, you can do the following:
v t
transposes a vectorv R
reduces a vector (into a scalar, by summing the values or multiplying them for example).v u
unpacks (gets rid of the brackets).y
to paste the result where the point (cursor) is.C-x * y
if you have moved from the calc minibuffer, you can enter this to paste the result where the point is.
Evaluating Expressions
Suppose you want to evaluate the following expresion 88+55+66/4
and put its result in the text.
You can place the cursor where the result will be then open the calculator with C-x * *
. Enter 88
, 55
, 66
, and 4
, then press the /
+
+
keys. Then paste as before with y
. The calculator can then be closed with q
.
I prefer to write the expression, highlight it, then use the kill command C-w
, then C-x * *
, then '
for the algebraic mode, evaluate the expression then paste it back like before.
Calc also provides Embedded Mode, activated with C-x * e
, which enables you to go to an equation that’s separated from the text by delimiters such as new lines, and then press RET
to evaluate them.