The goog.math.Matrix Class

goog.math.Matrix(m, opt_n)

Class for representing and manipulating matrices. The entry that lies in the i-th row and the j-th column of a matrix is typically referred to as the i,j entry of the matrix. The m-by-n matrix A would have its entries referred to as: [ a0,0 a0,1 a0,2 ... a0,j ... a0,n ] [ a1,0 a1,1 a1,2 ... a1,j ... a1,n ] [ a2,0 a2,1 a2,2 ... a2,j ... a2,n ] [ . . . . . ] [ . . . . . ] [ . . . . . ] [ ai,0 ai,1 ai,2 ... ai,j ... ai,n ] [ . . . . . ] [ . . . . . ] [ . . . . . ] [ am,0 am,1 am,2 ... am,j ... am,n ]

m {goog.math.Matrix|Array.<Array.<number>>|goog.math.Size|number}
A matrix to copy, a 2D-array to take as a template, a size object for dimensions, or the number of rows.
opt_n {number=}
Number of columns of the matrix (only applicable if the first argument is also numeric).

.add(m)

Returns a new matrix that is the sum of this and the provided matrix.

m {goog.math.Matrix}
The matrix to add to this one.
returns {!goog.math.Matrix}
Resultant sum.

.appendColumns(m)

Appends the given matrix to the right side of this matrix.

m {goog.math.Matrix}
The matrix to augment this matrix with.
returns {!goog.math.Matrix}
A new matrix with additional columns on the right.

.appendRows(m)

Appends the given matrix to the bottom of this matrix.

m {goog.math.Matrix}
The matrix to augment this matrix with.
returns {!goog.math.Matrix}
A new matrix with added columns on the bottom.

.createIdentityMatrix(n)

Creates a square identity matrix. i.e. for n = 3:

[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
n {number}
The size of the square identity matrix.
returns {!goog.math.Matrix}
Identity matrix of width and height {@code n}.

.equals(m, opt_tolerance)

Returns whether the given matrix equals this matrix.

m {goog.math.Matrix}
The matrix to compare to this one.
opt_tolerance {number=}
The tolerance when comparing array entries.
returns {boolean}
Whether the given matrix equals this matrix.

.forEach(matrix, fn, opt_obj)

Calls a function for each cell in a matrix.

matrix {goog.math.Matrix}
The matrix to iterate over.
fn {Function}
The function to call for every element. This function takes 4 arguments (value, i, j, and the matrix) and the return value is irrelevant.
opt_obj {Object=}
The object to be used as the value of 'this' within {@code fn}.

.getDeterminant()

Returns the determinant of this matrix. The determinant of a matrix A is often denoted as |A| and can only be applied to a square matrix.

returns {number}
The determinant of this matrix.

.getInverse()

Returns the inverse of this matrix if it exists or null if the matrix is not invertible.

returns {goog.math.Matrix}
A new matrix which is the inverse of this matrix.

.getReducedRowEchelonForm()

Transforms this matrix into reduced row echelon form.

returns {!goog.math.Matrix}
A new matrix reduced row echelon form.

.getSize()

returns {!goog.math.Size}
The dimensions of the matrix.

.getTranspose()

Return the transpose of this matrix. For an m-by-n matrix, the transpose is the n-by-m matrix which results from turning rows into columns and columns into rows

returns {!goog.math.Matrix}
A new matrix A^T.

.getValueAt(i, j)

Retrieves the value of a particular coordinate in the matrix or null if the requested coordinates are out of range.

i {number}
The i index of the coordinate.
j {number}
The j index of the coordinate.
returns {?number}
The value at the specified coordinate.

.isSquare()

returns {boolean}
Whether the horizontal and vertical dimensions of this matrix are the same.

.isValidArray(arr)

Tests whether an array is a valid matrix. A valid array is an array of arrays where all arrays are of the same length and all elements are numbers.

arr {Array}
An array to test.
returns {boolean}
Whether the array is a valid matrix.

.map(matrix, fn, opt_obj)

Calls a function for every cell in a matrix and inserts the result into a new matrix of equal dimensions.

matrix {goog.math.Matrix}
The matrix to iterate over.
fn {Function}
The function to call for every element. This function takes 4 arguments (value, i, j and the matrix) and should return something. The result will be inserted into a new matrix.
opt_obj {Object=}
The object to be used as the value of 'this' within {@code fn}.
returns {!goog.math.Matrix}
A new matrix with the results from {@code fn}.

.multiply(m)

Performs matrix or scalar multiplication on a matrix and returns the resultant matrix. Matrix multiplication is defined between two matrices only if the number of columns of the first matrix is the same as the number of rows of the second matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their product AB is an m-by-p matrix Scalar multiplication returns a matrix of the same size as the original, each value multiplied by the given value.

m {goog.math.Matrix|number}
Matrix/number to multiply the matrix by.
returns {!goog.math.Matrix}
Resultant product.

.setValueAt(i, j, value)

Sets the value at a particular coordinate (if the coordinate is within the bounds of the matrix).

i {number}
The i index of the coordinate.
j {number}
The j index of the coordinate.
value {number}
The new value for the coordinate.

.subtract(m)

Returns a new matrix that is the difference of this and the provided matrix.

m {goog.math.Matrix}
The matrix to subtract from this one.
returns {!goog.math.Matrix}
Resultant difference.

.toArray()

returns {!Array.<!Array.<number>>}
A 2D internal array representing this matrix. Not a clone.

.toString()

Returns a string representation of the matrix. e.g.

 [ 12  5  9  1 ]
 [  4 16  0 17 ]
 [ 12  5  1 23 ]
 
returns {string}
A string representation of this matrix.