Class Matrix


class Matrix
Note that a Matrix object is an Array, where each component is a Vector, representing a row of the matrix.
Defined in Matrix.js

Variable Summary
an Array whose elements are Vectors, the vectors being rows of the matrix

Constructor Summary
Matrix (Number[] arr)
Matrix (Vector vec)
Constructor function.

Function Summary
public static Matrix I (int n)
Creates the Identity matrix of dimension N.
public Matrix add (Matrix matrix)
Adds a matrix to another one of the same size.
public Matrix augment (Matrix matrix)
Augments a matrix to the right of a matrix.
public boolean canMultiplyFromLeft (Object matrix)
Tests to see whether the supplied matrix can be multiplied by this matrix in form: this times matrix.
public Vector col (int j)
Returns the j'th column as a vector.
public Number det()
Shorthand for determinant()
public Number determinant()
Calculates the determinant of a square matrix (returns null if matrix not a square).
public static Matrix diagonal (Object elements)
Creates a diagonal matrix (off-diagonal elements are 0) from a given array of elements
public Object dimensions()
Returns the dimensions of the matrix as an Object.
public boolean equalTo (Matrix matrix)
Checks two matrices for equality.
public Number get (int i, int j)
Returns the i,j'th element of the matrix.
public Vector getDiagonal()
Gets and returns the diagonal entries of a square matrix.
public Object indexOf (Number val)
Returns the indices of the FIRST occurence of a given value.
public Matrix invert()
Inverts an invertible matrix and returns the inverse.
public boolean invertible()
Tests to see if a matrix is or is not invertible.
public boolean isSingular()
Tests a matrix for singularity.
public boolean isSquare()
Tests to see if a matrix is square or not.
public Matrix map (Function func)
Maps a matrix onto a new one according to a supplied function.
public Number max()
Returns the absolute maximum component of the matrix.
public Matrix minor (int row, int col, int numrows, int numcols)
Creates a sub-matrix from the original matrix.
public Object multiply (Object matrix)
If possible (i.e.
public Matrix multiplyBy (Number k)
Multiplies a matrix by a scalar value.
public Matrix negative()
Returns the negative version of the current matrix.
public int numCols()
Get the number of columns in the matrix.
public int numRows()
Get the number of rows in the matrix.
public static Matrix random (int rows, int cols, [int max])
Creates a rows x cols matrix with random elements between 0 and max or [0,1) if max not supplied
public Number rank()
Calculates the rank of a matrix.
public Matrix round()
Rounds all the elements of a matrix into a new one.
public Vector row (int i)
Returns a specific row of the matrix as a Vector (which it is already)
public boolean sameSizeAs (Matrix matrix)
Tests to see if two matrices are the same size.
public Matrix setElement (int i, int j, Number val)
Sets the i,j'th element to val.
public Matrix snapTo (Number val)
Sets all values within range of zero (set in mathnetics.js) to val.
public Matrix subtract (Matrix matrix)
Subtracts another matrix of the same size from this one.
public String toString()
Creates and returns a string representation of the matrix.
Uses a modified version of Gaussian elimination to create and return a new, upper triangular version of the original matrix while preserving determinant and rank.
public Vector toVector()
Makes a Vector out of a 1-D (row or column) Matrix.
public Number tr()
Shorthand for trace() function.
public Number trace()
Computes the trace of a square matrix (returns null if matrix is not a square).
public Matrix transpose()
Creates a new matrix that is the transpose of the original matrix.
public static Matrix zero (int rows, int cols)
Creates a rows x cols matrix with all 0 elements.

Variable Details

variable Vector[] components

an Array whose elements are Vectors, the vectors being rows of the matrix

Constructor Details

constructor Matrix

Matrix(Number[] arr)
Matrix(Vector vec)
Matrix(Matrix m)
Constructor function.
Parameters:
fromArray
arr - an array representation of the matrix; can be a 1D, 2D, or array of Vectors
fromVector
vec - the vector which will become the single row of this matrix
clone
m - the matrix of which to create a clone

Function Details

function I

public static Matrix I(int n)
Creates the Identity matrix of dimension N.
Parameters:
n - the size of the identity matrix to be created
Returns:
an identity matrix of size n x n

function add

public Matrix add(Matrix matrix)
Adds a matrix to another one of the same size.
Parameters:
matrix - the matrix to be added to the original one
Returns:
a new matrix that is the sum of the two or null if they aren't same size

function augment

public Matrix augment(Matrix matrix)
Augments a matrix to the right of a matrix.
Parameters:
matrix - the matrix to augment
Returns:
a new matrix that is the augmentation of the two (or the original matrix if the supplied matrix has a different number of rows)

function canMultiplyFromLeft

public boolean canMultiplyFromLeft(Object matrix)
Tests to see whether the supplied matrix can be multiplied by this matrix in form: this times matrix. This is called as a test from multiply.
Parameters:
matrix - Matrix, Vector, or scalar to be tested
Returns:
true if matrix/vector can be multiplied from left or if matrix is a number; false otherwise
See also:

function col

public Vector col(int j)
Returns the j'th column as a vector.
Parameters:
j - the number of the column you want
Returns:
the j'th column as a vector

function det

public Number det()
Shorthand for determinant()
See also:

function determinant

public Number determinant()
Calculates the determinant of a square matrix (returns null if matrix not a square).
Returns:
the determinant

function diagonal

public static Matrix diagonal(Object elements)
Creates a diagonal matrix (off-diagonal elements are 0) from a given array of elements
Parameters:
elements - either a Vector or 1-D array that will become the diagonal elements of a matrix that has elements.length rows and columns
Returns:
a matrix with diagonal elements as specified

function dimensions

public Object dimensions()
Returns the dimensions of the matrix as an Object. Called as thisMatrix.dimensions().rows or .columns (see sameSize method).
Returns:
a new object with the two values rows = numRows, columns = numCols

function equalTo

public boolean equalTo(Matrix matrix)
Checks two matrices for equality.
Parameters:
matrix - the matrix (or Vector) to compare to the original
Returns:
true iff both are equal, false otherwise

function get

public Number get(int i, int j)
Returns the i,j'th element of the matrix. Because (i,j) element is this.components[i-1].components[j-1], this method is especially uesful as shorthand.
Parameters:
i - the row number of the element
j - the column number of the element
Returns:
the element at i,j

function getDiagonal

public Vector getDiagonal()
Gets and returns the diagonal entries of a square matrix.
Returns:
a new vector that is the diagonal elements of the matrix

function indexOf

public Object indexOf(Number val)
Returns the indices of the FIRST occurence of a given value.
Parameters:
val - the component for which to find the indices
Returns:
an object with properties i = row, j = column; if you want the row of the first occurence of val, matrix.indexOf(val).i

function invert

public Matrix invert()
Inverts an invertible matrix and returns the inverse.
Returns:
a new matrix that is the inverse of the original (null if original not invertible)

function invertible

public boolean invertible()
Tests to see if a matrix is or is not invertible.
Returns:
true iff matrix is invertible, false if not

function isSingular

public boolean isSingular()
Tests a matrix for singularity.
Returns:
true iff matrix is singular (square and 0 determinant), false otherwise

function isSquare

public boolean isSquare()
Tests to see if a matrix is square or not.
Returns:
true if matrix is a square, false otherwise

function map

public Matrix map(Function func)
Maps a matrix onto a new one according to a supplied function.
Parameters:
func - the mapping function
Returns:
the new, mapped matrix

function max

public Number max()
Returns the absolute maximum component of the matrix.
Returns:
the value of the component with the largest absolute value

function minor

public Matrix minor(int row, int col, int numrows, int numcols)
Creates a sub-matrix from the original matrix. Element selection wraps, so this method can be used to do row or column cycling and copy augmenting
Parameters:
row - the row to start at
col - the column to start at
numrows - the number of rows for the submatrix
numcols - the number of columns
Returns:
a new matrix that is the minor matrix from rows row to row+numrows and col to col+numcols

function multiply

public Object multiply(Object matrix)
If possible (i.e. canMultiplyFromLeft(matrix) == true), multiplies the supplied matrix by this matrix.
Parameters:
matrix the matrix, vector, or number (will do scalar multiplication) to be multiplied
Returns:
new matrix that is the matrix product if matrix is Matrix or Number; new Vector if parameter is a vector

function multiplyBy

public Matrix multiplyBy(Number k)
Multiplies a matrix by a scalar value.
Parameters:
k - the scalar by which to multiply the matrix
Returns:
a new matrix that is the scalar multiple of the original

function negative

public Matrix negative()
Returns the negative version of the current matrix.
Returns:
a new, negative copy of the original matrix.

function numCols

public int numCols()
Get the number of columns in the matrix.
Returns:
the number of columns

function numRows

public int numRows()
Get the number of rows in the matrix.
Returns:
the number of rows

function random

public static Matrix random(int rows, int cols, [int max])
Creates a rows x cols matrix with random elements between 0 and max or [0,1) if max not supplied
Parameters:
rows - the number of rows
cols - the number of columns
[max] - the maximum value (included) that any element can have; if supplied, all elements will be integers

function rank

public Number rank()
Calculates the rank of a matrix.
Returns:
the rank (calculated without complete row reduction)

function round

public Matrix round()
Rounds all the elements of a matrix into a new one.
Returns:
the new matrix that is the rounded version of the original

function row

public Vector row(int i)
Returns a specific row of the matrix as a Vector (which it is already)
Parameters:
i - the row you want
Returns:
a new copy of the i'th row of the matrix;

function sameSizeAs

public boolean sameSizeAs(Matrix matrix)
Tests to see if two matrices are the same size.
Parameters:
matrix - the matrix with which size is to be compared
Returns:
true iff same size, false otherwise

function setElement

public Matrix setElement(int i, int j, Number val)
Sets the i,j'th element to val. Note: this modifies current matrix, does not return a new one.
Parameters:
i - the row of the element to set
j - the column of the element to set
val - the value to set element (i,j) to
Returns:
this matrix, updated

function snapTo

public Matrix snapTo(Number val)
Sets all values within range of zero (set in mathnetics.js) to val.
Parameters:
val - the number to snap the matrix to
Returns:
a new matrix that is this one snapped to val

function subtract

public Matrix subtract(Matrix matrix)
Subtracts another matrix of the same size from this one.
Parameters:
matrix - the matrix to be subtracted the original one
Returns:
a new matrix that is the difference of the two or null if not same size

function toString

public String toString()
Creates and returns a string representation of the matrix.
Returns:
the string representation of the matrix

function toUpperTriangular

public Matrix toUpperTriangular()
Uses a modified version of Gaussian elimination to create and return a new, upper triangular version of the original matrix while preserving determinant and rank.
Returns:
the upper triangular version of the matrix as a new matrix object

function toVector

public Vector toVector()
Makes a Vector out of a 1-D (row or column) Matrix.
Returns:
the new Vector, or null if Matrix cannot become a Vector.

function tr

public Number tr()
Shorthand for trace() function.
See also:

function trace

public Number trace()
Computes the trace of a square matrix (returns null if matrix is not a square).
Returns:
the trace (sum of diagonal elements) of the matrix

function transpose

public Matrix transpose()
Creates a new matrix that is the transpose of the original matrix.
Returns:
the transpose of the original matrix, as a new matrix

function zero

public static Matrix zero(int rows, int cols)
Creates a rows x cols matrix with all 0 elements.
Parameters:
rows - the number of rows for the 0 matrix
cols - the number of columns
Returns:
a rows x cols 0 matrix