# MATH

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

Syntax:
MATH xxx

Description:
The math command performs many simple mathematical calculations that can be programmed in BASIC but there are speed advantages to coding looping structures in the firmware and there is the advantage that once debugged they are there for everyone without re-inventing the wheel.
Note: 2 dimensional maths matrices are always specified DIM matrix(n_columns, n_rows) and of course the dimensions respect OPTION BASE.
Quaternions are stored as a 5 element array w, x, y, z, magnitude.

Also see the MATH() function and MATH FFT command

Simple array arithmetic

MATH SET nbr, array()
Sets all elements in array() to the value nbr.
Note this is the fastest way of clearing an array by setting it to zero.

MATH ADD in(), num, out()
This adds the value 'num' to every element of the matrix in() and puts the answer in out(). Works for arrays of any dimensionality of both integer and float and can convert between.
Setting 'num' to 0 is optimised and is the fastest way of copying an entire array. in() and out() can be the same array.

MATH INTERPOLATE in1(), in2(), ratio, out()
This command This implements the following equation on every array element:
out = (in2 - in1) * ratio + in1
Arrays can have any number of dimensions and must be distinct and have the same number of total elements. The command works with both integer and floating point arrays in any mixture

MATH SCALE in(), scale ,out()
This scales the matrix in() by the scalar scale and puts the answer in out(). Works for arrays of any dimensionality of both integer and float and can convert between.
Setting 'scale' to 1 is optimised and is the fastest way of copying an entire array.

MATH SLICE sourcearray(), [d1] [,d2] [,d3] [,d4] [,d5] , destinationarray()
This command copies a specified set of values from a multi-dimensional array into a single dimensional array.
It is much faster than using a FOR loop.
The slice is specified by giving a value for all but one of the source array indicies and there should be as many indicies in the command, including the blank one, as there are dimensions in the source array
e.g.
OPTION BASE 1
DIM a(3,4,5)
DIM b(4)
MATH SLICE a(), 2, , 3, b()
Will copy the elements 2,1,3 and 2,2,3 and 2,3,3 and 2,4,3 into array b()

MATH INSERT targetarray(), [d1] [,d2] [,d3] [,d4] [,d5] , sourcearray()
This is the opposite of MATH SLICE and allows you, for example, to substitute a single vector into an array of vectors with a single instruction
e.g.
OPTION BASE 1
DIM target(3,4,5)
DIM source(4)=(1,2,3,4)
MATH INSERT target(), 2, , 3, source()
Will set elements 2,1,3 = 1 and 2,2,3 = 2 and 2,3,3 = 3 and 2,4,3 = 4

Matrix arithmetic

MATH M_PRINT array()
Quick mechanism to print a 2D matrix one row per line.

MATH M_TRANSPOSE in(), out()
Transpose matrix in() and put the answer in matrix out(), both arrays must be 2D but need not be square.
If not square then the arrays must be dimensioned in(m,n) out(n,m)

MATH M_MULT in1(), in2(), out()
Multiply the arrays in1() and in2() and put the answer in out()c. All arrays must be 2D but need not be square.
If not square then the arrays must be dimensioned in1(m,n) in2(p,m) ,out(p,n)

MATH INTERPOLATE in1(), in2(), ratio, out()
This command This implements the following equation on every array element: out = (in2 - in1) * ratio + in1
Arrays can have any number of dimensions and must be distinct and have the same number of total elements.
The command works with both integer and floating point arrays in any mixture

Vector arithmetic

MATH V_PRINT array()
Quick mechanism to print a small array on a single line

MATH V_NORMALISE inV(), outV()
Converts a vector inV() to unit scale and puts the answer in outV() (sqr(x*x + y*y +.......)=1
There is no limit on number of elements in the vector

MATH V_MULT matrix(), inV(), outV()
Multiplies matrix() and vector inV() returning vector outV().
The vectors and the 2D matrix can be any size but must have the same cardinality.

MATH V_CROSS inV1(), inV2(), outV()
Calculates the cross product of two three element vectors inV1() and inV2() and puts the answer in outV()

Quaternion arithmetic

MATH Q_CREATE theta, x, y, z, outRQ()
Generates a normalised rotation quaternion outRQ() to rotate quaternion vectors around axis x,y,z by an angle of theta.
Theta is specified in radians but respects the setting of OPTION ANGLE

MATH Q_EULER yaw, pitch, roll, q()
Generates a normalised rotation quaternion q() to rotate quaternion vectors as defined by the yaw, pitch and roll angles
With the vector in front of the “viewer” yaw is looking from the top of the ector and rotates clockwise, pitch rotates the top away from the camera and roll rotates around the z-axis clockwise.
The yaw, pitch and roll angles default to radians but respect the setting of OPTION ANGLE

MATH Q_INVERT inQ(), outQ()
Invert the quaternion in inQ() and put the answer in outQ()

MATH Q_MULT inQ1(), inQ2(), outQ()
Multiplies two quaternions inQ1() and inQ2() and puts the answer in outQ()

MATH Q_ROTATE , RQ(), inVQ(), outVQ()
Rotates the source quaternion vector inVQ() by the rotate quaternion RQ() and puts the answer in outVQ()

MATH Q_VECTOR x, y, z, outVQ()
Converts a vector specified by x , y, and z to a normalised quaternion vector outVQ() with the original magnitude stored

Last edited: 25 February, 2021