pari/gp | mathics | excel | r | hp-11c

bc | shell | browser | emacs | postgresql | glpsol

*What are the best calculators?*

## pari/gp

Install and run `gp`:

```
$ brew install pari
$ gp
? (2/7)^20
%1 = 1048576/79792266297612001
? %1 * 1.
%2 = 1.3141323697825355135314451523953967139 E-11
```

The value for each expression evaluated is stored in a variable with a leading percent sign. Integers and rational numbers are arbitrary length. Multiply by `1.` or add `0.` to get a decimal approximation.

The number of digits in a decimal approximation can be set to an arbitrarily large value. To see the first 100,000 digits of π:

```
? \p 100000
? Pi
```

Use `?` to get online help, and `?\` to get a description of the "keyboard shortcuts", which are commands which start with a backslash.

`gp` has complex numbers. The real and imaginary parts can be rationals or decimal approximations:

```
? real(1 + 3 * I)
%3 = 1
? imag(1 + 3 * I)
%4 = 3
? abs(1 + 3 * I)
%5 = 3.1622776601683793319988935444327185337
? arg(1 + 3 * I)
%6 = 1.2490457723982544258299170772810901231
```

There are matrices and vectors:

```
? [1,2;3,4] * [4,3;2,1]
%1 =
[ 8 5]
[20 13]
? [1,2;3,4]~
%2 =
[1 3]
[2 4]
? [1,2,3] * [3,2,1]~
%3 = 10
```

The same operator `*` is used for multiplying two scalars, multiplying a scalar by a matrix, and multiplying two matrices.

Vectors are 1×n matrices. `*` can be used for the dot product, but the transpose postfix operator `~` must be used on the 2nd matrix.

*base conversion*

*number theory*

*combinatorics*

*ascii plots*

## mathics

Mathics is an implementation of the Mathematica language. The underlying engine is SymPy.

Use `pip` to install it. Run the command line version with `mathics`:

```
$ pip install mathics
$ mathics
```

Or use `mathicsserver` to run a webserver which supports a notebook interface.

Solving equations:

```
In[1]:= Solve[x^2 + 2x + 1 == 0, x]
Out[1]= {{x -> -1}}
In[2]:= Solve[{2x+3y==7, 4x-y==7}, {x, y}]
Out[2]= {{x -> 2, y -> 1}}
```

Calculus:

```
In[1]:= D[x^3 + x + 3, x]
Out[1]= 1 + 3 x ^ 2
In[2]:= D[x^3 + x + 3, x] /. x -> 2
Out[2]= 13
In[3]:= D[Log[x], {x, 3}]
Out[3]= 2 / x ^ 3
In[4]:= Integrate[x^3 + x + 3, x]
Out[4]= 3 x + x ^ 2 / 2 + x ^ 4 / 4
In[5]:= Integrate[x^3 + x + 3, {x, 0, 1}]
Out[5]= 15 / 4
```

- DSolve, RSolve

- Simplify
- Factor, Expand
- Together, Apart

## excel

- sort
- filter
- group
- graph

## r

Best for statistics and graphing.

R installs itself on the command line on Mac OS X at `r` and on Ubuntu at `R`. Note that `zsh` has a built-in called `r` for repeating the last command. Use `command r` to by-pass the built-in.

## hp-11c

Dedicated calculator hardware is obsolete. For that matter calculators which display one number at a time are obsolete.

That said, I like having an HP-11C style calculator app on my iPhone. Why waste cerebral cortex learning something else. I chose the most skeuomorphic HP-11C app that I could find. The best HP-11C apps run the original ROM on a hardware emulator.

I like reverse Polish notation calculators because there is no need for paren keys. One can do more calculations without storing values in registers.

One can install a more capable HP-15C app instead. However, the HP-11C was what I owned back in the day. The HP-15C can perform complex and matrix arithmetic, but since the screen cannot display an entire matrix or even an entire complex number, it is an exercise in frustration.

## bc

An arbitrary precision algebraic notation Unix command line calculator.

[[collapsible show="+ how to use" hide="- how to use"]]

```
$ bc
10 ^ 100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
```

Calculations are done with the same precision needed to express the most precise decimal in the expression. The precision in the variable `scale` will be used if it is greater than the most precise decimal in the expression. By default `scale` is zero:

```
$ bc
6.1 * 7.1
43.3
scale = 3
6.1 * 7.1
43.31
.01 ^ 50
0
scale = 1000
.01 ^ 50
.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
```

Invoke `bc` with the `-l` flag to get transcendental functions. `scale` will be set to 20 automatically:

```
$ bc -l
l(10) /* natural log */
2.30258509299404568401
e(1)
2.71828182845904523536
s(3.14) /* sine */
.00159265291648695254
c(3.14) /* cosine */
-.99999873172753954528
a(1) /* arc tangent */
.78539816339744830961
```

`bc` has user defined variables, comments, and the control statements `if`, `while`, and `for`.

[[/collapsible]]

## shell

Shells can perform arithmetic. The syntax is cumbersome, but it can save a context switch:

```
$ echo $(( 347 * 232 ))
80504
```

The `zsh` shell can perform floating point arithmetic but `bash` cannot and must resort to forking `bc`:

`$ echo $(echo '3.14 * 5' | bc)`

The `fish` shell has a built-in command `math` which takes a string as an argument:

`$ math '1.1 * 2.3'`

It is a wrapper to `bc`, so one may need to use `scale` to get decimal digits:

```
$ math '13 / 5'
2
$ math 'scale=3; 13 / 5'
2.600
```

## browser

One can perform arithmetic by searching on the arithmetic expression. The search engine will probably infer that you are trying to do a calculation and show the result. Google puts the result in the display of a working calculator implemented in JavaScript.

http://www.wolframalpha.com/ implements a portion of the Mathematica language. Try searching for

`Solve[3x + 2 == 9x - 17, {x}]`

Both of these techniques require opening a web page in a new tab. One can calculate without leaving the current page by dropping into the debugger:

mac | windows | |
---|---|---|

chrome | ⌥⌘J | Ctrl+Shift+J |

firefox | ⌥⌘K | Ctrl+Shift+K |

safari | ⌥⌘C |

Try this:

`> Math.log(10)`

In Safari, one must first enable the `Develop` menu in `Preferences...`

## emacs

Using Emacs to perform a calculation is another way to avoid a context switch.

One can use `M-:` to enter and evaluate an Lisp S-expression in the minibuffer.

One can put the current buffer into `M-x lisp-interaction-mode`. The `*scratch*` buffer starts out in Lisp interaction mode. In Lisp interaction mode one can type Lisp S-expressions in the buffer and evaluate them. `C-j` writes the result in the buffer and `C-x C-e` echos the result in the minibuffer.

Another way to invoke Lisp from Emacs is `M-x ielm`. This creates a buffer with a Lisp REPL. Lisp expressions are evaluated as they are entered. It has command line history, but it is implemented in the slightly disconcerting (to me) Emacs way.

Emacs has a `calc` mode which sounds impressive on paper. It can perform matrix calculations, symbolic algebra manipulations, and graphing. I don't use it because it has a bit of a learning curve.

## postgresql

Using the database can be regarded as a context switch saver:

`> select 3.14 * 5;`

Databases are good at statistics. `count`, `sum`, `min`, `max`, and `avg` are standard. PostgreSQL also has:

```
stddev_samp
stddev_pop
var_samp
var_pop
cor(X, Y)
cov_samp(X, Y)
cor_pop(X, Y)
regr_intercept(X, Y)
regr_slope(X, Y)
```

*window functions*

*import and export*

*example of importing data and doing linear regression, scatterplot*

Finding out what functions are available.

```
> select count(*) from pg_proc
> select count(*) from INFORMATION_SCHEMA.routines
```