MatrixArray:
Filter:
MatrixArray/Classes (extension) | Linear Algebra | Math

MatrixArray
ExtensionExtension

A two-dimensional array structure to facilitate matrix operations.

Description

A two-dimensional array structure to facilitate matrix operations. This is a partial refactoring of the Matrix class from the MathLib quark. The modifications enable much faster calculation of some matrix operations, namely the determinant (-det), which subsequently speeds up the inverse and others. Performance is further enhanced by storing the transpose , and doing in-place operations when possible.

NOTE: Because this class was designed around certain methods that involve a high amount of recursion, like the pseudoInverse, for efficiency, many of the underlying operations are done through Array's methods. As a consequence most methods will return a new Array rather than the MatrixArray object. So if you expect to call further methods on a result, the preferred pattern is to "cast" it into a new MatrixArray using *with.

NOTE: Certain error checks are omitted, for example in operations that require a square matrix, this shape is assumed.

Class Methods

.fill

Fill the matrix by evaluating function.

Arguments:

rows

The number of rows.

cols

The number of columns.

func

The function used to fill the matrix. The function is passed row and col as arguments.

Returns:

.with

Create a MatrixArray with values from a 2-D Array.

Arguments:

array

A 2-D Array — an array of rows.

Returns:

.newClear

Create a MatrixArray of a specified size, filled with nils.

Arguments:

rows

The number of rows.

cols

The number of columns.

Returns:

Instance Methods

Access

NOTE: The following access methods return the objects that comprise MatrixArray's state, e.g. the row Arrays, not a copy, so use care not to unintentionally change these objects once you access them.

.rowAt

Get the row at an index.

Arguments:

row

The row index.

Returns:

An Array, not a copy!

.colAt

Get the column at an index.

Arguments:

col

The column index.

Returns:

An Array, not a copy!

.at

Get the value at [row, col].

Arguments:

row

The row index.

col

The column index.

Returns:

The value stored at [row, col].

.matrix

Get the MatrixArray as a 2-D array. Synonymous with -asArray.

Returns:

An Array of Arrays (rows), not a copy!

.asArray

Get the MatrixArray as a 2-D array. Synonymous with -matrix.

Returns:

An Array of Arrays (rows), not a copy!

.transpose

Get the flopped matrix (transpose). This transpose is computed on first request and stored thereafter for internal use and quick access.

Returns:

An Array of Arrays (rows), not a copy!

.flopped

Get the flopped matrix (transpose). This transpose is computed on first request and stored thereafter for internal use and quick access.

Returns:

An Array of Arrays (rows), not a copy!

.rows

Get the number of rows in the matrix.

.cols

Get the number of columns in the matrix.

Filtering

The following filter methods will return new objects (Arrays).

.getSub

Get a sub-matrix from within matrix.

Arguments:

rowStart

Row index to begin copying.

colStart

Column index to begin copying.

rowLength

The number of elements to copy from each row.

colHeight

The number of elements to copy from each column.

Returns:

A new Array of Arrays (rows).

.withoutRow

Get a new matrix that is this one without a specified row.

Arguments:

row

The index of the row to omit.

Returns:

A new Array of Arrays (rows).

.withoutCol

Get a new matrix that is this one without a specified column.

Arguments:

col

The index of the column to omit.

Returns:

A new Array of Arrays (rows).

.withoutRowCol

A convenience method, for optimized use by -cofactor, which, unlike -withoutRow and -withoutCol, returns a MatrixArray that is this one without a specified row and column.

Arguments:

row

The index of the row to be removed.

col

The index of the column to be removed.

Returns:

A new MatrixArray.

Manipulation

.put

Set the value in the matrix at a location.

Arguments:

row

A row index.

col

A column index.

val

The value to set.

.removeRow

Remove a row from the matrix.

Arguments:

row

The index of the row to remove.

Returns:

This MatrixArray, which is now without row row.

.removeCol

Remove a column from the matrix.

Arguments:

col

The index of the column to remove.

Returns:

This MatrixArray, which is now without column col.

.zeroWithin

WARNING: Deprecated: use -thresh2 instead.

.thresh2

Bilateral thresholding in place.

Arguments:

thresh

When the input.abs < thresh, the output is forced to 0. Should be a positive value.

Default is -180.dbamp, or 1e-09.

Returns:

Characteristic Values of the Matrix

.cofactor

Get the cofactor to element [row, col]. This is the determinant of the sub-matrix up to [row, col] mutiplied with (-1)**(row+col).

NOTE: The matrix is assumed to be square (no check is performed).

Arguments:

row

The row index.

col

The column index.

Returns:

A Number, the cofactor.

.adjoint

Return the adjoint (adjugate) of a square matrix.

Returns:

A 2-D Array.

.det

Calculate the matrix determinant.

Returns:

A Number.

.gram

Compute the gram matrix (the transpose of this matrix multiplied with itself).

T^t * T

Returns:

A 2-D Array.

.inverse

Compute the inverse of the matrix.

Returns:

A 2-D Array.

.pseudoInverse

Compute the pseudoinverse of the matrix.

Returns:

A 2-D Array.

.isSquare

Return the answer to whether the matrix is square.

Returns:

Matrix Operations

*

Matrix multiplication by another matrix or a number. If multiplying by another matrix, this * that. this.cols must equal that.rows. See also -mulMatrix.

Arguments:

that

Can be a Number, a MatrixArray, or a (2D) Array.

Returns:

A 2-D Array.

.mulMatrix

Matrix multiplication: this * that. this.cols must equal that.rows.

A(m,n) * B(n,r) = AB(m,r)

Arguments:

mArray

Can be a MatrixArray, or a (2D) Array.

Returns:

A 2-D Array.

.mulNumber

Multiply the matrix by a Number.

Arguments:

aNumber

A Number.

Returns:

A 2-D Array.

.mixCoeffs

A convenience method for "mixing" one-dimensional coefficients with this matrix, i.e. a matrix multiplication. For example, mixing ambisonic signal coefficients with a transform matrix.

Arguments:

coeffs

An Array with a size equal to -cols.

Returns:

An Array with size this.rows.

Examples

Attribution

This is a partial refactoring of the Matrix class from the MathLib quark originally authored by sc.solar, Till Bovermann, Christofer Fraunberger, and Dan Stowell.

This refactoring also includes a much faster determinant caclulation based on LU Decomposition.

The algorithm is published in:

Intel Application Notes AP-931, Streaming SIMD Extensions - LU Decomposition