# Vector calculator routines in calc.inc

## The data types

Vectors are arrays

[ number number number ... ]

like

[ 2 3 4 ]

Matrices are arrays of rows

[ vector vector vector ... ]

like

[
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
]

## The instructions

In each case I have tried to show how the stack should look before and after an operation. Occasionally I include some further comments.

## Utilities

/print

displays the top of the stack without destroying it

## Vector algebra

x+y

where x and y are vectors.

/vectorsub

x y vectorsub
x-y

where x and y are vectors.

/vectorscale

x c vectorscale
cx

## The dot product

x y dotproduct
x.y

where x and y are vectors.

length of vector v

/normalize

v normalize
v/|v|

## The cross product

u v crossprod
u x v

where u and v are 3D vectors

## Matrix algebra

x+y

/matrixsub

x y matrixsub
x - y

/matrixscale

x c matrixscale
cx

where x is a matrix, c a scalar.

x transpose
transpose of x

x y matrixmul
xy

m v matrixvector
mv

## Row operations

where m is a matrix, v a vector.

m i elementswap

swaps items i and j of array m

/rowscale

m i c rowscale

replaces row m[i] of m by cm[i]

/rowsubscale

m i j c rowsubscale

replaces row m[j] of m by m[j] - cm[i]

## Rotations

n identity
n x n
identity matrix

/tworotation

tworotation
2 x 2
matrix representing rotation by x

/threerotation

axis angle v threerotation
v
rotated through (scalar) angle around (vector) axis

## Solving systems

x gauss
w l u

replaces matrix x by its three matrix factors in WLU decomposition.

/triangular-solve

u y triangular-solve
x

where ux = y

## Determinants

a twodet
det(a)

where a is a 2 x 2 matrix

/threedet

a threedet
det(a)

where a is a 3 x 3 matrix

## Inverses

a twoinverse
matrix inverse of a

where a is a 2 x 2 matrix

/threeinverse

a threeinverse
matrix inverse of a

where a is a 3 x 3 matrix