maspack.matrix
Class MatrixNd

java.lang.Object
  extended bymaspack.matrix.MatrixObject
      extended bymaspack.matrix.MatrixNd
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
SubMatrixNd

public class MatrixNd
extends MatrixObject
implements java.io.Serializable

Implements general m x n matrices, along with most the commonly used operations associated with such matrices.

Normally, these matrices can be resized, either explicitly through a call to setSize, or implicitly through operations that require the matrix size to be modified. The exception is when this matrix is either a submatrix, or is being referenced by a submatrix (see SubMatrixNd).

See Also:
Serialized Form

Constructor Summary
MatrixNd(double[][] values)
          Creates a matrix from a two dimensional array of doubles.
MatrixNd(int numRows, int numCols)
          Creates a matrix of a specific size, and initializes its elements to 0.
MatrixNd(int numRows, int numCols, double[] values)
          Creates a matrix of specific size and initializes its elements from an array of values.
MatrixNd(MatrixObject M)
          Creates a matrix whose size and elements are the same as an existing MatrixObject.
 
Method Summary
 void absolute()
          Sets the elements of this matrix to their absolute values.
 void add(MatrixNd M1)
          Adds this matrix to M1 and places the result in this matrix.
 void add(MatrixNd M1, MatrixNd M2)
          Adds matrix M1 to M2 and places the result in this matrix.
 int colSize()
          Returns the number of columns in this matrix.
 void copySubMatrix(int baseRowSrc, int baseColSrc, int numRows, int numCols, MatrixNd Msrc, int baseRowDest, int baseColDest)
          Copies a sub matrix from a specified source matrix Msrc to a region of this matrix.
 boolean epsilonEquals(MatrixNd M1, double epsilon)
          Returns true if the elements of this matrix equal those of matrix M1within a prescribed tolerance epsilon.
 boolean equals(MatrixNd M1)
          Returns true if the elements of this matrix exactly equal those of matrix M1.
 double frobeniusNorm()
          Returns the Frobenius norm of this matrix.
 void get(double[] values)
          Copies the elements of this matrix into an array of doubles.
 double get(int i, int j)
          Gets a single element of this matrix.
 double[] getBuffer()
          Returns the internal buffer used to store the elements in this matrix.
 int getBufferBase()
          Returns the internal buffer base index of this matrix, used for computing the index necessary to reference a particular element.
 int getBufferWidth()
          Returns the internal buffer width of this matrix, used for computing the index necessary to reference a particular element.
 void getColumn(int j, double[] values)
          Copies a column of this matrix into an array of doubles.
 void getColumn(int j, VectorObject v)
          Copies a column of this matrix into a VectorObject.
 void getRow(int i, double[] values)
          Copies a row of this matrix into an array of doubles.
 void getRow(int i, VectorObject v)
          Copies a row of this matrix into a VectorObject.
 void getSubMatrix(int[] rows, int[] cols, MatrixNd Mdest)
          Gets a submatrix of this matrix.
 void getSubMatrix(int baseRow, int baseCol, MatrixNd Mdest)
          Gets a submatrix of this matrix.
 double infinityNorm()
          Returns the infinity norm of this matrix.
 boolean invert()
          Inverts this matrix in place, returning false if the matrix is detected to be singular.
 boolean invert(MatrixNd M1)
          Inverts the matrix M1 and places the result in this matrix, returning false if the matrix is detected to be singular.
 boolean isFixedSize()
          Returns true if this matrix is of fixed size.
 boolean isSubMatrix()
          Returns true if the this matrix is a submatrix; i.e., is a member of the subclass SubMatrixNd.
static void main(java.lang.String[] args)
           
 double maxElement()
          Returns the maximum element value of this matrix.
 double minElement()
          Returns the minimum element value of this matrix.
 void mul(MatrixNd M1)
          Multiplies this matrix by M1 and places the result in this matrix.
 void mul(MatrixNd M1, MatrixNd M2)
          Multiplies matrix M1 by M2 and places the result in this matrix.
 void mul(VectorNd vr, VectorNd v1)
          Multiplies this matrix by the column vector v1 and places the result in the vector vr.
 void mulDiagonalLeft(double[] diag)
          Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles.
 void mulDiagonalLeft(VectorNd diag)
          Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector.
 void mulDiagonalRight(double[] diag)
          Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles.
 void mulDiagonalRight(VectorNd diag)
          Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector.
 void mulTranspose(MatrixNd M1)
          Multiplies this matrix by the transpose of M1 and places the result in this matrix.
 void mulTranspose(VectorNd vr, VectorNd v1)
          Multiplies the transpose of this matrix by the vector v1 and places the result in vr.
 void mulTransposeBoth(MatrixNd M1, MatrixNd M2)
          Multiplies the transpose of matrix M1 by the transpose of M2 and places the result in this matrix.
 void mulTransposeLeft(MatrixNd M1, MatrixNd M2)
          Multiplies the transpose of matrix M1 by M2 and places the result in this matrix.
 void mulTransposeRight(MatrixNd M1, MatrixNd M2)
          Multiplies matrix M1 by the transpose of M2 and places the result in this matrix.
 void negate()
          Negates this matrix in place.
 void negate(MatrixNd M1)
          Sets this matrix to the negative of M1.
 double oneNorm()
          Returns the 1 norm of this matrix.
 void permuteColumns(int[] permutation)
          Rearrange the columns of this matrix according to the specified permutation, such that each column j is replaced by column permutation[j].
 void permuteRows(int[] permutation)
          Rearrange the rows of this matrix according to the specified permutation, such that each row i is replaced by row permutation[i].
 int rowSize()
          Returns the number of rows in this matrix.
 void scale(double s)
          Scales the elements of this matrix by s.
 void scale(double s, MatrixNd M1)
          Scales the elements of matrix M1 by s and places the results in this matrix.
 void set(double[] values)
          Sets the elements of this matrix from an array of doubles.
 void set(int i, int j, double value)
          Sets a single element of this matrix.
 void set(MatrixNd M1)
          Sets the size and values of this matrix to those of matrix M1.
 void setBuffer(int numRows, int numCols, double[] buffer, int bufWidth)
          Explicitly sets the size and internal buffer associated with this matrix.
 void setColumn(int j, double[] values)
          Sets a column of this matrix from an array of doubles.
 void setColumn(int j, VectorObject v)
          Sets a column of this matrix from a VectorObject.
 void setDiagonal(double[] diag)
          Sets this matrix to a diagonal matrix whose diagonal elements are specified by the leading elements of an array of doubles
 void setDiagonal(VectorNd diag)
          Sets this matrix to a diagonal matrix whose diagonal elements are specified by a vector.
 void setIdentity()
          Sets this matrix to the identity matrix.
 void setRandom()
          Sets the elements of this matrix to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).
 void setRandom(double lower, double upper)
          Sets the elements of this matrix to uniformly distributed random values in a specified range.
 void setRandom(double lower, double upper, java.util.Random generator)
          Sets the elements of this matrix to uniformly distributed random values in a specified range, using a supplied random number generator.
 void setRandomOrthogonal()
          Sets this matrix to a random orthogonal matrix.
 void setRandomOrthogonal(java.util.Random generator)
          Sets this matrix to a random orthogonal matrix, using a supplied random number generator.
 void setRandomSvd(double[] singularValues)
          Sets this matrix to a random matrix with specified singular values.
 void setRandomSvd(double[] singularValues, java.util.Random generator)
          Sets this matrix to a random matrix with specified singular values, using a supplied random number generator.
 void setRow(int i, double[] values)
          Set a row of this matrix from an array of doubles.
 void setRow(int i, VectorObject v)
          Sets a row of this matrix from a VectorObject.
 void setSize(int numRows, int numCols)
          Sets the size of this matrix.
 void setSubMatrix(int[] rows, int[] cols, MatrixNd Msrc)
          Sets a submatrix of this matrix.
 void setSubMatrix(int baseRow, int baseCol, MatrixNd Msrc)
          Sets a submatrix of this matrix.
 void setZero()
          Sets the elements of this matrix to zero.
 void sub(MatrixNd M1)
          Subtracts this matrix from M1 and places the result in this matrix.
 void sub(MatrixNd M1, MatrixNd M2)
          Subtracts matrix M1 from M2 and places the result in this matrix.
 void transpose()
          Replaces this matrix by its tranpose.
 void transpose(MatrixNd M1)
          Takes the transpose of matrix M1 and places the result in this matrix.
 void unsetBuffer()
          Removes an explicit buffer provided for this matrix and replaces it with a default implicit buffer.
 
Methods inherited from class maspack.matrix.MatrixObject
determinant, epsilonEquals, equals, equals, get, scan, set, set, toString, toString, toString
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MatrixNd

public MatrixNd(int numRows,
                int numCols)
Creates a matrix of a specific size, and initializes its elements to 0. It is legal to create a matrix with zero rows and columns.

Parameters:
numRows - number of rows
numCols - number of columns
Throws:
ImproperSizeException - if numRows or numCols are negative

MatrixNd

public MatrixNd(double[][] values)
Creates a matrix from a two dimensional array of doubles. The matrix size will be determined by the size of this array.

Parameters:
values - element values for the new matrix

MatrixNd

public MatrixNd(int numRows,
                int numCols,
                double[] values)
Creates a matrix of specific size and initializes its elements from an array of values.

Parameters:
numRows - number of rows
numCols - number of columns
values - element values for the matrix, with element (i,j) stored at location i*numCols+j

MatrixNd

public MatrixNd(MatrixObject M)
Creates a matrix whose size and elements are the same as an existing MatrixObject.

Parameters:
M - matrix object to be copied.
Method Detail

isFixedSize

public boolean isFixedSize()
Returns true if this matrix is of fixed size. This will be true if the matrix is either a submatrix or is being referenced by a submatrix (see SubMatrixNd). If the matrix is not of fixed size, then it can be resized dynamically, either explicitly using setSize, or implicitly when being used as a result for various matrix operations.

Overrides:
isFixedSize in class MatrixObject
Returns:
true if this matrix is of fixed size
See Also:
setSize(int, int)

isSubMatrix

public boolean isSubMatrix()
Returns true if the this matrix is a submatrix; i.e., is a member of the subclass SubMatrixNd.

Returns:
true if this matrix is a submatrix

getBuffer

public double[] getBuffer()
Returns the internal buffer used to store the elements in this matrix. When possible, applications should access the matrix elements using the various set and get methods. However, if efficiency requires it, this buffer can be used directly.

Roughly speaking, elements are stored in the buffer in row-column order. However, the buffer may be larger than the matrix, and there may be extra space at the beginning of the buffer. The buffer index which access the element (i,j) is computed as i*w + j + b, where w and b are the width and base values returned by getBufferWidth and getBufferBase, respectively.

Note also that if this matrix is a submatrix, the buffer will actually belong to the root matrix, and so modifying values in the buffer (including those outside the nominal range of this matrix) will change the root matrix as well.

If this matrix is resized, then the internal buffer may change and the buffer previously returned by this routine may no longer be valid.

Returns:
internal buffer for this matrix
See Also:
getBufferWidth(), getBufferBase(), setBuffer(int, int, double[], int)

getBufferWidth

public int getBufferWidth()
Returns the internal buffer width of this matrix, used for computing the index necessary to reference a particular element.

Returns:
internal buffer width
See Also:
getBuffer()

getBufferBase

public int getBufferBase()
Returns the internal buffer base index of this matrix, used for computing the index necessary to reference a particular element.

Returns:
internal buffer base index
See Also:
getBuffer()

setSize

public void setSize(int numRows,
                    int numCols)
Sets the size of this matrix. This operation is only supported if isFixedSize returns false. If necessary, this operation will enlarge the internal buffer associated with this matrix, invalidating buffers previously returned by getBuffer.

If a matrix is resized, then any previous element values which are still within the new matrix dimensions are preserved. Other (new) element values are undefined.

Overrides:
setSize in class MatrixObject
Parameters:
numRows - new row size
numCols - new column size
Throws:
ImproperSizeException - if this matrix has an explicit internal buffer and that buffer is too small to support the requested size
java.lang.UnsupportedOperationException - if this matrix has fixed size
See Also:
isFixedSize()

rowSize

public final int rowSize()
Returns the number of rows in this matrix.

Specified by:
rowSize in class MatrixObject
Returns:
number of rows

colSize

public final int colSize()
Returns the number of columns in this matrix.

Specified by:
colSize in class MatrixObject
Returns:
number of columns

setBuffer

public void setBuffer(int numRows,
                      int numCols,
                      double[] buffer,
                      int bufWidth)
Explicitly sets the size and internal buffer associated with this matrix. Any previous values will be discarded, and the matrix will assume new values with each element (i,j) given by the buffer contents at index [i*bufWidth+j]. In particular, bufWidth must satisfy bufWidth >= numCols, and the length of buffer must satisfy buffer.length >= numRows*bufWidth. The matrix can continue to be resized as long as requested sizes stay do not violate these bounds. The value of bufWidth will become the value returned by getBufferWidth, while the value returned by getBufferBase will be 0.

Parameters:
numRows - new row size
numCols - new column size
buffer - explicit buffer for this matrix
bufWidth - width for the explicit buffer
Throws:
java.lang.IllegalStateException - if this matrix has referring submatrices or is itself a submatrix
ImproperSizeException - if the specified buffer and/or width is too small for the requested size
See Also:
getBufferWidth(), getBufferBase(), unsetBuffer()

unsetBuffer

public void unsetBuffer()
Removes an explicit buffer provided for this matrix and replaces it with a default implicit buffer. The matrix retains its present size but all values are replaced with zero.

Throws:
java.lang.IllegalStateException - if this matrix does not have an explicit buffer given by setBuffer
java.lang.IllegalStateException - if this matrix has referring submatrices
See Also:
setBuffer(int, int, double[], int)

copySubMatrix

public void copySubMatrix(int baseRowSrc,
                          int baseColSrc,
                          int numRows,
                          int numCols,
                          MatrixNd Msrc,
                          int baseRowDest,
                          int baseColDest)
                   throws ImproperSizeException
Copies a sub matrix from a specified source matrix Msrc to a region of this matrix. The base row and column of the source and destination may be different.

Parameters:
baseRowSrc - starting submatrix row in the source matrix
baseColSrc - starting submatrix column in the source matrix
numRows - number of rows in the submatrix
numCols - number of columns in the submatrix
Msrc - source matrix
baseRowDest - starting submatrix row in this matrix
baseColDest - starting submatrix column in this matrix
Throws:
ImproperSizeException - if the specified submatrix dimensions are infeasible
See Also:
getSubMatrix(int,int,MatrixNd), setSubMatrix(int,int,MatrixNd)

get

public final double get(int i,
                        int j)
Gets a single element of this matrix.

Specified by:
get in class MatrixObject
Parameters:
i - element row index
j - element column index
Returns:
element value

get

public final void get(double[] values)
Copies the elements of this matrix into an array of doubles. The elements are stored using row-major order, so that element (i,j) is stored at location i*colSize()+j.

Overrides:
get in class MatrixObject
Parameters:
values - array into which values are copied

getColumn

public final void getColumn(int j,
                            double[] values)
Copies a column of this matrix into an array of doubles.

Overrides:
getColumn in class MatrixObject
Parameters:
j - column index
values - array into which the column is copied

getRow

public final void getRow(int i,
                         double[] values)
Copies a row of this matrix into an array of doubles.

Overrides:
getRow in class MatrixObject
Parameters:
i - row index
values - array into which the row is copied

getColumn

public final void getColumn(int j,
                            VectorObject v)
Copies a column of this matrix into a VectorObject.

Overrides:
getColumn in class MatrixObject
Parameters:
j - column index
v - vector into which the column is copied
Throws:
ImproperSizeException - vector's size not equal to the number of matrix rows and the vector cannot be resized

getRow

public final void getRow(int i,
                         VectorObject v)
Copies a row of this matrix into a VectorObject.

Overrides:
getRow in class MatrixObject
Parameters:
i - row index
v - vector into which the row is copied
Throws:
ImproperSizeException - vector's size not equal to the number of matrix columns and the vector cannot be resized

getSubMatrix

public void getSubMatrix(int baseRow,
                         int baseCol,
                         MatrixNd Mdest)
                  throws ImproperSizeException
Gets a submatrix of this matrix. The first row and column of the submatrix are given by baseRow and baseCol, and the values are written into the matrix Mdest. The size of the submatrix is determined by the dimensions of Mdest.

Parameters:
baseRow - first row of the submatrix
baseCol - first column of the submatrix
Mdest - destination for submatrix values
Throws:
ImproperSizeException - if baseRow or baseCol are negative, or if the submatrix exceeds the current matrix bounds.
See Also:
copySubMatrix(int, int, int, int, maspack.matrix.MatrixNd, int, int)

getSubMatrix

public void getSubMatrix(int[] rows,
                         int[] cols,
                         MatrixNd Mdest)
                  throws ImproperSizeException
Gets a submatrix of this matrix. The row and column indices of the submatrix are specified by rows and the cols, respectively, and the values are written into the matrix Mdest. The size of the submatrix is determined by the existing dimensions of Mdest.

Parameters:
rows - row indices of the submatrix
cols - column indices of the submatrix
Mdest - destination for submatrix values
Throws:
ImproperSizeException - if the number of rows or columns exceed the dimensions of Mdest, or if any of the specified indices lie outside the bounds of this matrix.

set

public final void set(int i,
                      int j,
                      double value)
Sets a single element of this matrix.

Specified by:
set in class MatrixObject
Parameters:
i - element row index
j - element column index
value - element value

set

public final void set(double[] values)
Sets the elements of this matrix from an array of doubles. The elements in the array should be stored using row-major order, so that element (i,j) is stored at location i*colSize()+j.

Overrides:
set in class MatrixObject
Parameters:
values - array from which values are copied

setColumn

public final void setColumn(int j,
                            double[] values)
Sets a column of this matrix from an array of doubles.

Overrides:
setColumn in class MatrixObject
Parameters:
j - column index
values - array from which column values are copied

setRow

public final void setRow(int i,
                         double[] values)
Set a row of this matrix from an array of doubles.

Overrides:
setRow in class MatrixObject
Parameters:
i - row index
values - array from which the row is copied

setColumn

public void setColumn(int j,
                      VectorObject v)
Sets a column of this matrix from a VectorObject.

Overrides:
setColumn in class MatrixObject
Parameters:
j - column index
v - vector from which the column is copied
Throws:
ImproperSizeException - vector's size not equal to the number of matrix rows

setRow

public void setRow(int i,
                   VectorObject v)
Sets a row of this matrix from a VectorObject.

Overrides:
setRow in class MatrixObject
Parameters:
i - row index
v - vector from which the row is copied
Throws:
ImproperSizeException - vector's size not equal to the number of matrix columns

setSubMatrix

public void setSubMatrix(int baseRow,
                         int baseCol,
                         MatrixNd Msrc)
                  throws ImproperSizeException
Sets a submatrix of this matrix. The first row and column of the submatrix are given by baseRow and baseCol, and the new values are given by the matrix Msrc. The size of the submatrix is determined by the dimensions of Msrc.

Parameters:
baseRow - index of the first row of the submatrix
baseCol - index of the first column of the submatrix
Msrc - new values for the submatrix.
Throws:
ImproperSizeException - if baseRow or baseCol are negative, or if the submatrix exceeds the current matrix bounds
See Also:
copySubMatrix(int, int, int, int, maspack.matrix.MatrixNd, int, int)

setSubMatrix

public void setSubMatrix(int[] rows,
                         int[] cols,
                         MatrixNd Msrc)
                  throws ImproperSizeException
Sets a submatrix of this matrix. The row and column indices of the submatrix are specified by rows and the cols, respectively, and the new values are given by the matrix Msrc. The size of the submatrix is determined by the existing dimensions of Msrc.

Parameters:
rows - row indices of the submatrix
cols - column indices of the submatrix
Msrc - new values for the submatrix
Throws:
ImproperSizeException - if the number of rows or columns exceed the dimensions of Msrc, or if any of the specified indices lie outside the bounds of this matrix.

setRandom

public void setRandom()
Sets the elements of this matrix to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).


setRandom

public void setRandom(double lower,
                      double upper)
Sets the elements of this matrix to uniformly distributed random values in a specified range.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)

setRandom

public void setRandom(double lower,
                      double upper,
                      java.util.Random generator)
Sets the elements of this matrix to uniformly distributed random values in a specified range, using a supplied random number generator.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)
generator - random number generator

setRandomOrthogonal

public void setRandomOrthogonal()
Sets this matrix to a random orthogonal matrix. This is a matrix whose rows and columns are randomly selected while still being orthonormal.


setRandomOrthogonal

public void setRandomOrthogonal(java.util.Random generator)
Sets this matrix to a random orthogonal matrix, using a supplied random number generator.

Parameters:
generator - random number generator
See Also:
setRandomOrthogonal()

setRandomSvd

public void setRandomSvd(double[] singularValues)
Sets this matrix to a random matrix with specified singular values. The size of the matrix is unchanged, and the number of supplied singular values must equal the minimum matrix dimension. This routine is useful for creating random matrices with a prescribed degeneracy.

Parameters:
singularValues - singular values for this matrix
Throws:
ImproperSizeException - if the length of singularValues is less than the minimum matrix dimension

setRandomSvd

public void setRandomSvd(double[] singularValues,
                         java.util.Random generator)
Sets this matrix to a random matrix with specified singular values, using a supplied random number generator.

Parameters:
singularValues - singular values for this matrix
generator - random number generator
Throws:
ImproperSizeException - if the length of singularValues is less than the minimum matrix dimension
See Also:
setRandomSvd(double[])

mul

public void mul(MatrixNd M1)
Multiplies this matrix by M1 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - right-hand matrix
Throws:
ImproperSizeException - if this matrix and M1 do not conform, or if this matrix needs resizing but is of fixed size

mul

public void mul(MatrixNd M1,
                MatrixNd M2)
         throws ImproperSizeException
Multiplies matrix M1 by M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if matrices M1 and M2 do not comform, or if this matrix needs resizing but is of fixed size

mulTranspose

public void mulTranspose(MatrixNd M1)
Multiplies this matrix by the transpose of M1 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - right-hand matrix
Throws:
ImproperSizeException - if this matrix and the transpose of M1 do not conform, or if this matrix needs resizing but is of fixed size

mulTransposeRight

public void mulTransposeRight(MatrixNd M1,
                              MatrixNd M2)
                       throws ImproperSizeException
Multiplies matrix M1 by the transpose of M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if M1 and the transpose of M2 do not comform, or if this matrix needs resizing but is of fixed size

mulTransposeLeft

public void mulTransposeLeft(MatrixNd M1,
                             MatrixNd M2)
                      throws ImproperSizeException
Multiplies the transpose of matrix M1 by M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if the transpose of M1 and M2 do not comform, or if this matrix needs resizing but is of fixed size

mulTransposeBoth

public void mulTransposeBoth(MatrixNd M1,
                             MatrixNd M2)
                      throws ImproperSizeException
Multiplies the transpose of matrix M1 by the transpose of M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if the transpose of M1 and the transpose of M2 do not comform, or if this matrix needs resizing but is of fixed size

mulDiagonalLeft

public void mulDiagonalLeft(VectorNd diag)
Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector. This is the same as multiplying the rows of this matrix by the elements of the vector.

Parameters:
diag - specifies the diagonal elements of the implied left-hand matrix
Throws:
ImproperSizeException - if the size of diag does not equal the row size of this matrix

mulDiagonalLeft

public void mulDiagonalLeft(double[] diag)
Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles. This is the same as multiplying the rows of this matrix by the elements of the array.

Parameters:
diag - specifies the diagonal elements of the implied left-hand matrix
Throws:
ImproperSizeException - if the length of diag is less than the row size of this matrix

mulDiagonalRight

public void mulDiagonalRight(VectorNd diag)
Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector. This is the same as multiplying the columns of this matrix by the elements of the vector.

Parameters:
diag - specifies the diagonal elements of the implied right-hand matrix
Throws:
ImproperSizeException - if the size of diag does not equal the column size of this matrix

mulDiagonalRight

public void mulDiagonalRight(double[] diag)
Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles. This is the same as multiplying the columns of this matrix by the elements of the array.

Parameters:
diag - specifies the diagonal elements of the implied right-hand matrix
Throws:
ImproperSizeException - if the length of diag is less than the column size of this matrix

set

public void set(MatrixNd M1)
         throws ImproperSizeException
Sets the size and values of this matrix to those of matrix M1.

Parameters:
M1 - matrix whose size and values are copied
Throws:
ImproperSizeException - if matrices have different sizes and this matrix cannot be resized accordingly

absolute

public void absolute()
Sets the elements of this matrix to their absolute values.


maxElement

public double maxElement()
Returns the maximum element value of this matrix.

Returns:
maximum element value

minElement

public double minElement()
Returns the minimum element value of this matrix.

Returns:
minimum element value

add

public void add(MatrixNd M1)
         throws ImproperSizeException
Adds this matrix to M1 and places the result in this matrix.

Parameters:
M1 - right-hand matrix
Throws:
ImproperSizeException - if this matrix and M1 have different sizes

add

public void add(MatrixNd M1,
                MatrixNd M2)
         throws ImproperSizeException
Adds matrix M1 to M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if matrices M1 and M2 have different sizes, or if this matrix needs resizing but is of fixed size

sub

public void sub(MatrixNd M1)
         throws ImproperSizeException
Subtracts this matrix from M1 and places the result in this matrix.

Parameters:
M1 - right-hand matrix
Throws:
ImproperSizeException - if this matrix and M1 have different sizes

sub

public void sub(MatrixNd M1,
                MatrixNd M2)
         throws ImproperSizeException
Subtracts matrix M1 from M2 and places the result in this matrix. This matrix is resized if necessary.

Parameters:
M1 - left-hand matrix
M2 - right-hand matrix
Throws:
ImproperSizeException - if matrices M1 and M2 have different sizes, or if this matrix needs resizing but is of fixed size

negate

public void negate(MatrixNd M1)
            throws ImproperSizeException
Sets this matrix to the negative of M1. This matrix is resized if necessary.

Parameters:
M1 - matrix to negate
Throws:
ImproperSizeException - if this matrix needs resizing but is of fixed size

negate

public void negate()
Negates this matrix in place.


scale

public void scale(double s,
                  MatrixNd M1)
           throws ImproperSizeException
Scales the elements of matrix M1 by s and places the results in this matrix. This matrix is resized if necessary.

Parameters:
s - scaling factor
M1 - matrix to be scaled
Throws:
ImproperSizeException - if this matrix needs resizing but is of fixed size

scale

public void scale(double s)
Scales the elements of this matrix by s.

Parameters:
s - scaling factor

transpose

public void transpose()
Replaces this matrix by its tranpose. The matrix is resized if necessary.

Throws:
ImproperSizeException - if this matrix needs resizing but is of fixed size

transpose

public void transpose(MatrixNd M1)
               throws ImproperSizeException
Takes the transpose of matrix M1 and places the result in this matrix. The matrix is resized if necessary.

Parameters:
M1 - matrix to take the transpose of
Throws:
ImproperSizeException - if this matrix needs resizing but is of fixed size

setIdentity

public void setIdentity()
Sets this matrix to the identity matrix. If this matrix matrix is not square, then element (i,j) is set to 1 if i and j are equal, and 0 otherwise.


setZero

public void setZero()
Sets the elements of this matrix to zero.


setDiagonal

public void setDiagonal(VectorNd diag)
Sets this matrix to a diagonal matrix whose diagonal elements are specified by a vector.

Parameters:
diag - diagonal elements for this matrix
Throws:
ImproperSizeException - if the size of diag does not equal the minimum matrix dimension

setDiagonal

public void setDiagonal(double[] diag)
Sets this matrix to a diagonal matrix whose diagonal elements are specified by the leading elements of an array of doubles

Parameters:
diag - diagonal elements for this matrix
Throws:
ImproperSizeException - if the length of diag is less than the minimum matrix dimension

epsilonEquals

public boolean epsilonEquals(MatrixNd M1,
                             double epsilon)
Returns true if the elements of this matrix equal those of matrix M1within a prescribed tolerance epsilon.

Parameters:
M1 - matrix to compare with
epsilon - comparison tolerance
Returns:
false if the matrices are not equal within the specified tolerance, or have different sizes

equals

public boolean equals(MatrixNd M1)
Returns true if the elements of this matrix exactly equal those of matrix M1.

Parameters:
M1 - matrix to compare with
Returns:
false if the matrices are not equal or have different sizes

infinityNorm

public double infinityNorm()
Returns the infinity norm of this matrix. This is equal to the maximum of the vector 1-norm of each row.

Overrides:
infinityNorm in class MatrixObject
Returns:
infinity norm of this matrix

oneNorm

public double oneNorm()
Returns the 1 norm of this matrix. This is equal to the maximum of the vector 1-norm of each column.

Overrides:
oneNorm in class MatrixObject
Returns:
1 norm of this matrix

frobeniusNorm

public double frobeniusNorm()
Returns the Frobenius norm of this matrix. This is equal to the square root of the sum of the squares of each element.

Overrides:
frobeniusNorm in class MatrixObject
Returns:
Frobenius norm of this matrix

invert

public boolean invert()
               throws ImproperSizeException
Inverts this matrix in place, returning false if the matrix is detected to be singular. The inverse is computed using LU decomposition with partial pivoting.

Throws:
ImproperSizeException - if this matrix is not square

invert

public boolean invert(MatrixNd M1)
               throws ImproperSizeException
Inverts the matrix M1 and places the result in this matrix, returning false if the matrix is detected to be singular. This matrix is resized if necessary. The inverse is computed using LU decomposition with partial pivoting.

Parameters:
M1 - matrix to take the inverse of
Throws:
ImproperSizeException - if matrix M1 is not square, or if this matrix needs resizing but is of fixed size

permuteColumns

public void permuteColumns(int[] permutation)
Rearrange the columns of this matrix according to the specified permutation, such that each column j is replaced by column permutation[j].

Parameters:
permutation - describes the column exchanges
Throws:
ImproperSizeException - if the length of permutation is less than the column size of this matrix.

permuteRows

public void permuteRows(int[] permutation)
Rearrange the rows of this matrix according to the specified permutation, such that each row i is replaced by row permutation[i].

Parameters:
permutation - describes the row exchanges
Throws:
ImproperSizeException - if the length of permutation is less than the row size of this matrix.

mul

public void mul(VectorNd vr,
                VectorNd v1)
Multiplies this matrix by the column vector v1 and places the result in the vector vr. If M represents this matrix, this is equivalent to computing
  vr = M v1
 
The vector vr is resized if necessary.

Parameters:
vr - result vector
v1 - vector to multiply by
Throws:
ImproperSizeException - if the size of v1 does not equal the number of columns of this matrix, or if vr needs resizing but is of fixed size

mulTranspose

public void mulTranspose(VectorNd vr,
                         VectorNd v1)
Multiplies the transpose of this matrix by the vector v1 and places the result in vr. If M represents this matrix, this is equivalent to computing
  vr = v1 M
 
The vector vr is resized if necessary.

Parameters:
vr - result vector
v1 - vector to multiply by
Throws:
ImproperSizeException - if the size of v1 does not equal the number of rows of this matrix, or if vr needs resizing but is of fixed size

main

public static void main(java.lang.String[] args)