The VC calculator

This page contains a simple calculator Java applet, and links to documentation about it. The calculator is programmable and relatively versatile, and imitates the well known and beautifully designed (but pricey) calculators from Hewlett-Packard in that it follows the conventions of Reverse Polish Notation (RPN), although the language used to program it is far closer to PostScript. This calculator is called VC to stand for Vector Calculator.

Disclaimer: This calculator is not very efficient and indeed rather slow. It has in fact been designed intentionally to be used in an undergraduate computer laboratory where a large number of people are working, and where speed is less important than politeness. But in any event, this calculator is intended to demonstrate basic principles of mathematical algorithms, not to make complicated practical calculations. Have patience with it. Even a calculator as simple as this one can make manipulations with vectors much more pleasant and rapid than can an average hand-held calculator.

Warning! If you edit the calculator and then exit the page, reload it, or resize it, your text will be replaced by the original.

Please! I have tried very hard to make this calculator foolproof and bug-free, and to give an intuitive interface, but of course I cannot guarantee anything. If you encounter bizarre behaviour of any kind, please report it to me, explaining in as much detail as you can what the circumstances were.

The calculator

In the following display, type source code into the top window. Pressing Run will restart and run the program, and display output in the lower window. Pressing Stop will halt the program while it is running, pressing Reset will reinitialize it, and pressing Step will do one step of calculation. The text field in the middle displays the current instruction being processed. What that current instruction might not always be what you expect, since various instructions can change the flow of the calculation in non-intuitive ways.

Clicking in the program window will always reset the calculator, too - the calculator assumes that a click in this window means you are about to change the program. In a separate window labelled Stack the full stack is displayed (upside down). You can toggle the display of this window on and off by pressing the Stack button. Similarly, you can toggle the display of a graphics window with the Graphics button.

There is one extra quirk to be patient with, and that is that the cursor doesn't seem to appear regularly in the source window, even though it is still apparently functioning. Dunno what to do about this problem.

How to use the calculator: the basics

It is a stack-based calculator. The stack is an array of indefinite length into which data is fed, and on which all operations are performed. Generally, you have access only to what is called the top (or growing end) of the stack ( in this respect like a stack of dishes in a cafeteria). Commands are applied to items placed on the stack before the command is encountered. For example, 6 7 + puts first 6, then 7, on the stack and then replaces them by their sum. Roughly speaking, the data you can use in this calculator are integers, real numbers, complex numbers, vectors, matrices, and strings. You can apply built-in procedures or define your own. You can also use both global and local variables.

Normally, results are not displayed when calculated. If you want to display them, you can type =, which will display the item at the top of the stack without removing it. If you use ! instead you will both display and remove it. (So there are two ways to output the item at the top of the stack: ! which is destructive and = which is non-destructive.) Thus you would type 6 7 + = to calculate 6+7=13 and display the result, leaving it on the stack.

The `backwards' behaviour of the calculator may seem peculiar at first, but it is extremely efficient in a chain of complicated calculations, and you should get comfortable with it in time.

Examples

Task: Calculate 6 + (7 * 8). It is in fact an interesting problem to figure out how to evaluate a general algebraic expression in RPN terms. The general rule is to lay down the data left to right, but apply operators from the inside out, or from the lowest levels up. Here the inner expression is (7 * 8), so multiplication is the first operator we apply.

Input:

6 7 8 * + !

Output:

62

Remark: What is going on here inside the stack? First we enter 6, 7, and 8. At this point the stack has three items on it. Then we replace 7 and 8 by 7*8=56, leaving 6 and 56 on the stack. Finally, we replace 6 and 56 by 6+56 = 62 and display the result destructively. At the end, the stack is empty. Here is a sequence of pictures of the stack as the calculation proceeds:

6
6 7
6 7 8
6 56
62

Task: Calculate the sum of vectors [1 2] and [1 -3].

Input:

[ 1 2 ][ 1 -3 ] + =

Output:

[ 2 -1 ]

Remark: Here the result is left on the stack.

Task: Define variables x = 6, y = 7, set z = x + y, and display z.

Input:

6 @x def
7 @y def
x y + @z def
z !

Output:

13

Task: Calculate and display the sum of the first 10 squares 1 + 4 + 9 + ...

Input:

0 @n def
0 @s def
10 {
1 n + @n def
n dup * s + @s def
} repeat
s !

Output:

385

Task: Construct a procedure called average which has just one argument, a vector, and returns the average of its coordinates.

Input:

{
  @v def
  v dim @n def
  0 @s def
  0 @i def
  n {
    v(i) s + @s def
    i 1 + @i def
  } repeat
  s n / 
} @average def

Remark: This is not as efficient as it might be. Cleverer stack manipulations could do better. Note that v(i) is the i-th coordinate of v if v is a vector.

Task: Calculate numerically the integral of y = exp(-x^2) from 0 to 1 by applying the trapezoidal rule with 10 intervals.

Input:

# Define the function to be integrated
# Here f(x) = exp(-x*x)
{
   dup * -1 * exp
} @f def

# Do the sums for the trapezoidal rule
# Each term = (f(x) + f(x+h))*0.5*h
10 @N def
0 @x def
1 N / @h def
0 @s def

N {
  x f
  x h + @x def
  x f + 0.5 *
  h *
  s + @s def
} repeat

# display the result
s !

Output:

 0.7462

Remark: This is more complicated than other examples. First we define the variable f to be the procedure or function which takes the variable x off the stack and then places exp(-x*x) on the stack. Just to be sure you get the point, I'll repeat it: you can define variables to be equal to procedures as well as ordinary constants. And almost always functions defined in the calculator will do something like this one - remove some items on the stack as its arguments, and place something on the stack as its return value. Incidentally, the command dup used here just makes an extra copy of what is on top of the stack. Also, this function is not as efficient as it might be. With a little care you can get away with only one function evaluation in each loop.

Task: Construct a function which takes a single argument which is a vector, and returns its length.

Left as an exercise.

Task: Construct a function which takes two arguments which are vectors and returns the angle between them.

I'll leave this as an exercise, too. It will use * to calculate the dot product of two vectors, the length function from the previous exercise, and the function acos (inverse cosine). You'll have to recall a formula from linear algebra relating the dot product to angles.

Task: Evaluate the polynomial z^{2} + z + 1 for 10 values of the complex variable z distributed uniformly on the circle |z| = 1, and display the values you get.

{ local 
@z def 
z z * z + 1 + 
endlocal } @poly def

0 @T def
2 pi * 10 / @dT def

10 {
< T cos T sin > poly !
T dT + @T def
} repeat

Remark: The nested pair local, endlocal arranges things so that all variables in between are local, which means in this case that all assignments to z within the procedure poly do not affect the values of any other variable z used outside the procedure. Complex numbers are expressed as a pair of real numbers betwen angle brackets like < 1 2 > for 1 + 2i. The variable i is defined by default to be the square root of -1, so you can also enter 1 + 2i as 1 2 i * +.

More documentation

Available commands

An implementation of Newton's method

Graphics

A printable version of this file

A dictionary of calculator commands

Obtaining a version for yourself

If you want to use the calculator for your own work, it will be more efficient if you have a local copy of it. Download the file rpn.zip and unzip it. It will unpack everything, including a copy of this file ca.html and some printable copies of documentation, into a directory rpn (plus a few subdirectories). You can then access the calculator inside your browser by loading the file rpn/docs/ca.html instead of loading this page over the Internet. If you do download your own copy, you should be aware that the calculator is modified (improved, of course) frequently, and without notification.

A more efficient version can be run on any computer with a Java interpreter installed. If you install your own copy, and you have installed Java on your computer (which you can do without cost through Sun Microsystems' home page) then you have another option. If java is in your execution path and the directory above rpn is in your Java class path, you can run the calculator through standard input in any UNIX terminal or MSDOS window by typing java rpn.vc.vc. Typing java rpn.vc.vc x will run the calculator with the file x as input. You can also run the file ca.html under the program appletviewer.


The calculator applet and this page were constructed by Bill Casselman.