\magnification=1000
\input epsf
\input coursepages
\input verbatim
\input psfont.defs

\def\runningtitle{{\bigbold Calculator commands}}
\newcount\secno
\secno=0
\def\section#1{
\advance\secno by 1
\bigskip
{\the\secno. \bold #1}
\bigskip
\nobreak}

{\bigbold Mathematics 152 --- Spring 1999}

These notes are brief.  Experimenting
with the calculator will fill them out.

{\bigbold Calculator data types}

The calculator can deal with integers, real numbers, 
strings, vectors, and matrices.  It cannot yet read real 
numbers in scientific notation, but it can read them as fractions
in which there must be no spaces.  Thus @-1/3@ is a fraction
but @-1 / 3@ will be read differently.
Vectors are arrays of real numbers in square brackets
@[@ and @]@, and matrices are arrays of vectors,
which are interpreted as the rows of the matrix.

{\bigbold Calculator commands}

\epsfbox{labcopy.eps}

These are grouped by the sort of thing they do.

\section{Basic arithmetic}
%
{$\bullet$} {\bold +} replaces the previous two items 
on the stack by their sum.
Can add integers, real numbers, vectors, or matrices.
Thus 
\medbreak

@7 6 +@ 

calculates 7+6=13.

{\bold +} can also be used to build {\bold strings}.
A string is a phrase inside quotes.  The sum of a string and
any item tacks on a string representation of the item
to the original string.  Thus
\medbreak

@
"x = " 3 +
@

produces the string "x = 3".  
Using this feature is good for explaining in output
exactly what displayed data means.

{$\bullet$} {\bold --} replaces the previous two items 
on the stack by their difference.
Can subtract integers, real numbers, vectors, or matrices.
Thus 
\medbreak

@7 6 -@ 

calculates 7-6=1.

{$\bullet$} {\bold *} replaces the previous two items 
on the stack by their product.
Can multiply integers or real numbers.
Also calculates
the dot product of two vectors;
the scalar product of
a vector and a scalar; the product of a matrix and a vector;
and the product of two matrices.
Thus 
\medbreak

@7 6 *@ 

calculates 7*6=42.

{$\bullet$} {\bold /} replaces the previous 
two items on the stack by their quotient.
Can divide integers or real numbers.
Thus 
\medbreak

@14 2 /@ 

calculates 14/2 = 7.

{$\bullet$}
{\bold cross} replaces the previous two items by their
cross product, if they are both three dimensional vectors.

\section{Mathematical functions}
%
{$\bullet$}
{\bold exp} replaces the previous item $x$ by $e^x$.
Similarly for {\bold cos}, {\bold acos}, {\bold sin},
{\bold log} (which is the natural log).

{$\bullet$}
{\bold atan2} has two arguments $y$ and $x$ {\bold in that order},
and returns the angle coordinate of the point (x, y).
(This odd and unfortunate choice of the order 
in which x and y are written conforms with
that of most programming languages.)

{$\bullet$}
{\bold \char94} has two arguments $x$ and $y$,
and returns $x^y$.
Here either $x > 0$ or $y$ must be an integer.

{$\bullet$}
{\bold pi} is a constant equal to $3.14159 \ldots$

{$\bullet$}
{\bold floor} replaces a number by the largest integer 
less than or equal to it.  Thus 6.7 gets replaced by
6, while -6.7 gets replaced by -7.

{$\bullet$}
{\bold sqrt} replaces the previous item by its square root,
if it is a non-negative number.

\section{General}
 
{$\bullet$} 
Integers inside parentheses {\bold ( )} are indices into an array.
Thus if @v@ is a vector then @v(0)@ is the $0$-th element
of @v@.

{$\bullet$} 
{\bold def} defines the previous item to be the item below it.
The previous item must be a {\bold variable name} such as
\AT@x@\ or \AT@longVariableName@.
A variable name is what you get by putting {\AT} before the
variable itself.
Thus @x@\ is a variable and \AT@x@\ is its name.
(We have to distinguish between the variable and its name because 
the results of putting them in a program are very different.  
When the calculator comes across the variable, it attempts to make
a substitution.
This is similar to the difference between a variable and a pointer to
the variable in some programming languages.)
Thus
\medbreak

@5 @\AT@x0 def@

defines the variable @x0@ to be 5.  
Subsequent occurrences of @x0@ 
(with some exceptions to be explained some other time) will
be replaced by 5.

If $v$ is a vector then @4 @\AT@v(i) def@ defines $v(i)$ to be $5$.

{$\bullet$}
{\bold dim} replaces a vector by its dimension.

{$\bullet$} 
{\bold fix} requires a non-negative integer on the stack.
It sets the number of decimal
figures displayed, and does not leave anything on the stack.
Thus 
\medbreak

@5 fix 
4.0 =@ 

displays @4.00000@.

{$\bullet$} 
{\bold sci} is similar to @fix@, 
and produces output in scientific notation.

\section{Stack manipulations}
%
{$\bullet$}
{\bold dup} makes an extra copy of the item at the top
of the stack.

{$\bullet$}
{\bold pop} just removes the item at the top
of the stack.

{$\bullet$}
{\bold exch} swaps the top two items on the stack.

\section{Matrix operations}

{$\bullet$}
{\bold m i j rowswap} swaps rows $i$ and $j$ of the matrix $m$,
leaving $m$ on the stack.

{$\bullet$}
{\bold m i j c rowsub} replaces $m(i)$ by $m(i) - c*m(j)$,
leaving $m$ on the stack.

{$\bullet$}
{\bold rowscale} replaces $m(i)$ by $c*m(i)$,
leaving $m$ on the stack.

\section{Conditionals}
%
{$\bullet$}
{\bold lt}, {\bold le}, {\bold gt}, {\bold ge}, {\bold eq}
are tests on the previous two items,
which should be numbers.
The names stand for {\bold less than}, {\bold less than or equals to},
etc.  The effect is to place either a {\bold true} or
a {\bold false} on the stack.

{$\bullet$}
{\bold ifelse} uses the top three items on the stack,
which should be {\bold true/false} and two procedures.
If {\bold true}, it executes the first procedure,
while if {\bold false} it executes the second.
Either procedure can be empty.

{$\bullet$}
{\bold not}, @&@, @|@ are {\bold boolean}
operators.  They combine true/false data.
The notation for the last two ('and' and 'or')
is close to that of other programming languages.
Thus
\medbreak

@5 0 gt 
7 0 gt 
&@

returns true. 

\section{Loops}
%
{$\bullet$}
{\bold repeat} can be used to perform loops.
It requires an integer and a {\bold procedure} immediately
preceding it.  A procedure is a sequence of instructions
inside brackets @{@ and @}@.
Thus
\medbreak

@
1000 
10 { 1 - = } repeat
@


will output
\medbreak

@
999
998
997
996
995
994
993
992
991
990
@

{$\bullet$}
{\bold break} will break out of an enclosing loop.
This should be used together with conditionals 
in order to halt a @repeat@\ loop.  
Thus the following program will
print out only the numbers 10, 9, 8, 7, 6.
\medbreak

@
10 @\AT@x def
10 {
x 5 eq { break } { x = x 1 - @\AT@x def } ifelse
} repeat
@

{$\bullet$}
{\bold stop} halts the calculator temporarily, 
allowing you to view the stack,
step through, or rerun the program.

{$\bullet$}
Any error will be signalled by displaying an error message.
You should never ignore one of these messages.
It is possible that it is caused by a bug
in the program, in which case you should make a bug report.

\bye
