StarMath: Static Array Math Library

While there are already countless math libraries for performing common matrix/array functions, StarMath is distinguished by its simplicity and flexbility. There is no need to declare vectors and matrices in a specific library format. It works simliar to Math.function calls in C#.

Project Description

This project is small .NET managed dynamic library and fully compiles to a Portable Class Library (for all platforms).

Many matrix libraries like to offer short, concise matrix operations (e.g. A*B) like Matlab. However, this requires that you create vectors and matrices in the format of those libraries. Instead, StarMath is all about simple static methods that work with the fundamental Array objects (actually, many methods work for objects inherited from IList). This used to be rather clunky approach as one would have to write:

var z = StarMath.crossProduct2(v1, v2);

Now, with StarMath 2.0, which takes advantage of 'extensions', a more concise representation of many functions is possible. Now you simple have to write:

var z = v1.crossProduct2(v2);

The library is not going to do sophisticated sparse-matrix solving (yet!), but is intended for applications where common matrix operations are needed frequently and effortlessly within .NET applications.

But is StarMath fast?

Here are some examples that illustrate ways StarMath has been used to date:

The following are functions built into the library.

Basic Arithmetic

Example usage

multiply(integer/scalar/vector/matrix, integer/scalar/vector/matrix)

Multiplies all elements of a 1D array with the scalar.

A.multiply(x);

divide(vector/matrix, scalar)

Divides all elements of an array by the scalar.

A.divide(3.0);

add(vector/matrix, vector/matrix)

Adds arrays A and B

A.add(B);

subtract(vector/matrix, vector/matrix)

Subtracts one vector (B) from the other (A). C = A - B.

A.subtract(B);

Vector Multiply

 

dotProduct(vector, vector)

The dot product of the two 1D vectors A and B

v1.dotProduct(v2);

crossProduct(vector, vector)

The cross product of two -precision vectors, A and B,

v1.crossProduct(v2);

crossProduct2(vector, vector)

The cross product of two vectors, A and B, which are of length, 2. In actuality, there is no cross-product for 2D. This is shorthand for 2D systems that are really simplifications of 3D. The returned scalar is actually the scalar in the third (read: z) direction.

double z = StarMath.crossProduct2(v1, v2);

multiplyVectorsIntoAMatrix(vector, vector)

Multiply vector by transpose of another vector to create a matrix. Product of each element of array-1 (1D ) with each element of array-2 (1D ) C[i, j] = A[i] * B[j]

double[,] A = StarMath.multiplyVectorsIntoAMatrix(v1, v2);

EltMultiply(vector/matrix, vector/matrix)

The element-by-element (Hadamard) multiplication of vectors or matrices.

double[] v3 = v1.EltMultiply(v2);

Advanced Matrix Manipulation

 

SingularScalarDecomposition(matrix)

Computes the singular scalar decomposition of A.

double[] sigma = M.SingularValueDecomposition();
or
double[,] U, V;
double[] sigma = M.SingularValueDecomposition(out U, out V);

GetEigenScalars(matrix)

Gets the eigenvalues for matrix, A.

var eigenVectors = new double[size][];
var λ = A.GetEigenValuesAndVectors(out eigenVectors);

GetEigenScalarsAndVectors(matrix)

Gets the eigenscalars and eigenvectors for matrix, A.

inverse(matrix)

Inverses the matrix A only if the diagonal is all non-zero. A[i, i] != 0.0

double[,] B = A.inverse();

LUDecomposition(matrix

Returns the LU decomposition of A in a new matrix.

double[,] L, U;
StarMath.LUDecomposition(A, out L, out U);

transpose(matrix)

Transposes the matrix, A.

double[,] At = A.transpose();

determinant(matrix)

Returns the determinant of matrix, A.

double detA = A.determinant();

solve(matrix, vector, vector)

Solves for x given matrix, A, vector, b & perhaps an initial guess for x in the formulation: Ax = b

double[] x = StarMath.solve(A, b);

Sizing and Finding

 

Max(vector/matrix)

Finds the maximum scalar in the given array and optionally returns the row and column indices along with it.

int rowIndex, colIndex;
double max = A.Max(out rowIndex, out colIndex);

Min(vector/matrix)

Finds the minimum scalar in the given array and optionally returns the row and column indices along with it.

var min = v1.Min(out index);

find(vector/matrix, scalar)

Finds all the indices for the specified find scalar.

List<int> indices = v1.find(100.0);

norm1(matrix)

Returns to 1-norm (sum of absolute scalars of all terms) of the vector or matrix. Or with two arguments, find the norm2 of their difference.

double diff = StarMath.norm1(v1, v2);

norm2(vector/matrix)

Returns to 2-norm (square root of the sum of squares of all terms) of the vector or matrix (optionally can choose to not do the square root). Or with two arguments, find the norm2 of their difference.

double[,] A;
...
A.norm2();

normP(vector/matrix)

Returns to p-norm (p-root of the sum of each term raised to the p power)

double[,] A;
...
A.normP(10.0);

normalize(vector)

Returns the normalized vector (has length (or 2-norm) of 1) of the vector, x.

double[] v1;
...
v1 = v1.normalize();

normalizeInPlace(vector)

Destructively normalizes the vector x.

double[] v1;
...
v1.normalizeInPlace();

sum(vector/matrix)

Sum up all the elements of a given vector or matrix

var v1 = new[] { 1, 2, 3, 4, 5 };
var avg = v1.sum() / 5.0;

standardDeviation(vector/matrix)

Calculates the standard deviation assuming the whole population is provided (not sample st. dev.).

var v1 = new[] { 1, 2, 3, 4, 5 };
var stdev = v1.standardDeviation();

3D Coordinate Transforms

 

Translate(scalar, scalar, scalar)

Creates a translated coordinate frame.

double[,] t = StarMath.Translate(1.2, 3.4, 5.6);

RotationX(scalar)

Creates a rotation matrix about the X-axis.

double[,] rx45 = StarMath.RotationX(45.0);

RotationY(scalar)

Creates a rotation matrix about the Y-axis.

double[,] ry90 = StarMath.RotationY(90.0);

RotationZ(scalar)

Creates a rotation matrix about the Z-axis.

double[,] rz30 = StarMath.RotationZ(30.0);

Make/Get/Set Methods

 

makeZero(integer, integer)

Makes the zero vector/matrix of the given dimensions.

double[] g = StarMath.makeZeroVector(3);
double[,] H = StarMath.makeZero(4,3);

makeIdentity(integer)

Makes an identity matrix of size p by p.

var I = StarMath.makeIdentity(4);

makeLinearProgression(scalar, scalar, scalar)

Makes a linear progression from start to, but not including, the end.

double[] timeSteps = StarMath.makeLinearProgression(100, 0.01);

SetRow(integer, matrix, vector)

Sets/Replaces the given row of matrix A with the vector v.

A.SetRow(0, new[] { 1.0, 2.0, 3.0 });

SetColumn(integer, matrix, vector)

Sets/Replaces the given column of matrix A with the vector v.

A.SetColumn(0, new[] { 1.0, 2.0, 3.0 });

GetColumn(matrix, integer)

Gets the column of matrix, A.

double[] trans = A.GetColumn(4);

GetColumns(vector, matrix)

Get more than one column from a given 2D array.

double[,] rotation = A.GetColumns(new[] { 0, 1, 2 });

GetRow(integer, matrix)

Gets a row of matrix, A.

double[] scale = A.GetRow(4);

GetRows(vector, matrix)

Get more than one row from a given 2D array.

double[,] rotation = A.GetRows(new[] { 0, 1, 2 });

GetPartialVector(vector, vector)

Get some portion of a vector and put in a new vector.

double[] rx = col.GetPartialVector(new[] { 0, 1, 2 });

JoinCol(matrix, matrix)

Joins two 2D arrays side by side and returns the results. The given variables remain unchanged

double[,] AB = A.JoinCol(B);

JoinRow(matrix, matrix)

Joins two 2D arrays one under the other and returns the results. The given variables remain unchanged

double[,] AB = A.JoinRow(B);

JoinVectors(vector, vector)

Concatenates two 1D arrays and returns the result. The given variables remain unchanged

double[,] v12 = v1.JoinVectors(v2);

JoinMatrixColumnsIntoVector(matrix)

Joins the matrix columns into vector.

double[,] A;
...
double[] Avect = A.JoinMatrixColumnsIntoVector();

JoinMatrixRowsIntoVector(matrix)

Joins the matrix rows into vector.

double[] Avect = A.JoinMatrixRowsIntoVector();

MakePrintString(matrix, vector)

Makes the string printing the matrix or vector

Console.WriteLine("A = "+ A.MakePrintString());

Authors and Contributors

This is a collaboration of the Design Engineering Lab at Oregon State University (@micampbell).