org.openimaj.math.matrix

## Class MatlibMatrixUtils

• ```public class MatlibMatrixUtils
extends Object```
Some helpful operations on `Matrix` instances from Adrian Kuhn's library.
Author:
Sina Samangooei (ss@ecs.soton.ac.uk)
• ### Constructor Summary

Constructors
Constructor and Description
`MatlibMatrixUtils()`
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static boolean` `any(Vector v)`
`static <T extends Matrix>T` ```appendColumn(T m, Vector col)```
`static <T extends Matrix>T` ```appendRow(T m, Vector row)```
`static com.jmatio.types.MLDouble` `asMatlab(Matrix m)`
`static <T extends Matrix>T` `copy(T sparseMatrix)`
Copy a matrix
`static Matrix` ```dotProduct(Matrix X, Matrix W)```
Compute the dot product X.W
`static Matrix` ```dotProductTranspose(Matrix A, Matrix B)```
Compute Y = A .
`static <T extends Matrix>T` ```dotProductTranspose(Matrix A, Matrix B, T Y)```
Y = A .
`static Matrix` ```dotProductTransposeTranspose(Matrix A, Matrix B)```
Perform: A.T.dot(B.T) without performing the transpose.
`static Matrix` `fromCF(gov.sandia.cognition.math.matrix.Matrix init)`
Create a `Matrix` from the Cognitive Foundry equivalent
`static Matrix` `fromJama(Jama.Matrix sol)`
`static Matrix` `fromMatlab(com.jmatio.types.MLArray mlArray)`
Create a `Matrix` from a matlab `MLArray`
`static Vector` ```lessThan(Vector v, double d)```
`static double` `max(Matrix mat)`
uses the second value returned by `minmaxmean(Matrix)`
`static SparseMatrix` ```maxInplace(SparseMatrix A, SparseMatrix B)```
`static double` `mean(Matrix mat)`
uses the third value returned by `minmaxmean(Matrix)`
`static double` `min(Matrix mat)`
uses the first value returned by `minmaxmean(Matrix)`
`static SparseMatrix` ```minInplace(SparseMatrix A, SparseMatrix B)```
`static double[]` `minmaxmean(Matrix mat)`
`static <T extends Matrix>T` ```minus(double v, T l)```
`static Matrix` ```minus(Matrix A, Matrix B)```
Subtract matrices A-B
`static <T extends Matrix>T` ```minus(T l, double v)```
`static Vector` ```minus(Vector l, Vector v)```
`static <T extends Matrix>T` ```minusInplace(DiagonalMatrix D, T A)```
Subtract two matrices, storing the result in the second: `A = D - A`
`static Matrix` ```minusInplace(Matrix A, Matrix B)```
Subtract two matrices, storing the result in the first: `A = A - B`
`static <T extends Vector>T` ```minusInplace(T A, Vector D)```
Subtract a vector from another vector `A = A - D`
`static double` `norm2(Vector row)`
Compute the 2-norm (Euclidean norm) of the vector
`static double` `normF(Matrix A)`
Compute the Frobenius norm
`static <T extends Matrix>T` ```plusInplace(DiagonalMatrix D, T A)```
Add two matrices, storing the results in the second: `A = D + A`
`static SparseMatrix` ```plusInplace(SparseMatrix A, Matrix B)```
Add two matrices, storing the results in the first: `A = A + B`
`static <T extends Matrix>T` ```plusInplace(T A, double d)```
Add a constant inplace `A = A + d`
`static <T extends Matrix>T` ```plusInplace(T A, Matrix B)```
Add two matrices, storing the results in the first: `A = A + B`
`static <T extends Vector>T` ```plusInplace(T A, Vector D)```
Subtract a vector from another vector `A = A + D`
`static <T extends Matrix>T` ```powInplace(T matrix, double d)```
Raise each element to the power d, operating on the matrix itself
`static <T extends Matrix>T` ```scaleInplace(T A, double s)```
A = A .
`static void` ```setSubMatrix(Matrix to, int row, int col, Matrix from)```
Set a submatrix of a larger matrix
`static void` ```setSubMatrixCol(Matrix to, int row, int col, Vector v)```
Starting from a given row of a column, set the values of a matrix to the values of v
`static void` ```setSubMatrixRow(Matrix to, int row, int col, Vector v)```
Starting from a given column of a row, set the values of a matrix to the values of v
`static void` ```setSubVector(Vector to, int startindex, Vector from)```
`static SparseDoubleArray` `sparseVectorToSparseArray(SparseVector row)`
`static double` `sparsity(Matrix mat)`
Compute the matrix sparsity (i.e.
`static <T extends Matrix>T` ```subMatrix(T mat, int rowstart, int rowend, int colstart, int colend)```
Extract the submatrix of the same type of mat
`static <T extends Matrix>T` ```subMatrix(T mat, int rowstart, int rowend, gnu.trove.list.array.TIntArrayList cols)```
`static <T extends Matrix>T` ```subMatrix(T mat, gnu.trove.list.array.TIntArrayList rows, int colstart, int colend)```
`static <T extends Matrix>T` ```subMatrix(T mat, gnu.trove.TIntCollection rows, gnu.trove.TIntCollection cols)```
Extract a submatrix from the given rows and cols
`static double` `sum(DiagonalMatrix d)`
Sum the diagonal of the given matrix
`static SparseMatrix` ```threshold(SparseMatrix data, double thresh)```
Set values below the given threshold to zero in the output matrix.
`static SparseMatrix` ```times(DiagonalMatrix D, SparseMatrix A)```
Left multiply two matrices: `R = D .`
``` ```
`static SparseMatrix` ```times(SparseMatrix A, DiagonalMatrix D)```
Right multiply two matrices: `R = A .`
``` ```
`static Matrix` ```timesInplace(Matrix A, Matrix B)```
`static SparseMatrix` ```timesInplace(SparseMatrix A, SparseMatrix B)```
`static Jama.Matrix` `toColJama(Vector vector)`
`static Jama.Matrix` `toJama(Matrix laplacian)`
`static no.uib.cipr.matrix.Matrix` `toMTJ(Matrix sol)`
`static Jama.Matrix` `toRowJama(Vector vector)`
`static <T extends Matrix>T` `transpose(T mat)`
Transpose a matrix, returning a new matrix.
`static Matrix` ```transposeDotProduct(Matrix A, Matrix B)```
Compute A^T .
`static Matrix` `vstack(Matrix... matricies)`
Stack matrices vertically
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### MatlibMatrixUtils

`public MatlibMatrixUtils()`
• ### Method Detail

• #### sparsity

`public static double sparsity(Matrix mat)`
Compute the matrix sparsity (i.e. proportion of zero elements)
Parameters:
`mat` - the matrix
Returns:
the sparsity
`Matrix.density()`
• #### powInplace

```public static <T extends Matrix> T powInplace(T matrix,
double d)```
Raise each element to the power d, operating on the matrix itself
Parameters:
`matrix` - the matrix
`d` - the power
Returns:
the input matrix, with elements raised to the given power
• #### times

```public static SparseMatrix times(DiagonalMatrix D,
SparseMatrix A)```
Left multiply two matrices: `R = D . A`
Parameters:
`D` - first matrix
`A` - second matrix
Returns:
result of multiplication
• #### times

```public static SparseMatrix times(SparseMatrix A,
DiagonalMatrix D)```
Right multiply two matrices: `R = A . D`
Parameters:
`D` - first matrix
`A` - second matrix
Returns:
result of multiplication
• #### plusInplace

```public static SparseMatrix plusInplace(SparseMatrix A,
Matrix B)```
Add two matrices, storing the results in the first: `A = A + B`
Parameters:
`A` - first matrix
`B` - matrix to add
Returns:
A first matrix
• #### plusInplace

```public static <T extends Matrix> T plusInplace(T A,
Matrix B)```
Add two matrices, storing the results in the first: `A = A + B`
Parameters:
`A` - first matrix
`B` - matrix to add
Returns:
A first matrix
• #### plusInplace

```public static <T extends Matrix> T plusInplace(T A,
double d)```
Add a constant inplace `A = A + d`
Parameters:
`A` - first matrix
`d` - the constant to add
Returns:
A first matrix
• #### minusInplace

```public static <T extends Matrix> T minusInplace(DiagonalMatrix D,
T A)```
Subtract two matrices, storing the result in the second: `A = D - A`
Parameters:
`D` - first matrix
`A` - second matrix
Returns:
second matrix
• #### minusInplace

```public static Matrix minusInplace(Matrix A,
Matrix B)```
Subtract two matrices, storing the result in the first: `A = A - B`
Parameters:
`A` - first matrix
`B` - second matrix
Returns:
first matrix
• #### minusInplace

```public static <T extends Vector> T minusInplace(T A,
Vector D)```
Subtract a vector from another vector `A = A - D`
Parameters:
`A` - first matrix
`D` - second matrix
Returns:
second matrix
• #### plusInplace

```public static <T extends Vector> T plusInplace(T A,
Vector D)```
Subtract a vector from another vector `A = A + D`
Parameters:
`A` - first matrix
`D` - second matrix
Returns:
second matrix
• #### plusInplace

```public static <T extends Matrix> T plusInplace(DiagonalMatrix D,
T A)```
Add two matrices, storing the results in the second: `A = D + A`
Parameters:
`D` - first matrix
`A` - second matrix
Returns:
second matrix
• #### transposeDotProduct

```public static Matrix transposeDotProduct(Matrix A,
Matrix B)```
Compute A^T . B
Parameters:
`A` -
`B` -
Returns:
A^T . B
• #### dotProductTranspose

```public static Matrix dotProductTranspose(Matrix A,
Matrix B)```
Compute Y = A . B^T
Parameters:
`A` -
`B` -
Returns:
Y
• #### dotProductTransposeTranspose

```public static Matrix dotProductTransposeTranspose(Matrix A,
Matrix B)```
Perform: A.T.dot(B.T) without performing the transpose. This is fine for dense matricies but is very inefficient for sparse matrices, consider performing the transpose manually.
Parameters:
`A` -
`B` -
Returns:
A.T.dot(B.T)
• #### dotProductTranspose

```public static <T extends Matrix> T dotProductTranspose(Matrix A,
Matrix B,
T Y)```
Y = A . Bt
Parameters:
`A` -
`B` -
`Y` -
Returns:
Y
• #### scaleInplace

```public static <T extends Matrix> T scaleInplace(T A,
double s)```
A = A . s
Parameters:
`A` -
`s` -
Returns:
A
• #### toJama

`public static Jama.Matrix toJama(Matrix laplacian)`
Parameters:
`laplacian` -
Returns:
returns a dense jama matrix
• #### toColJama

`public static Jama.Matrix toColJama(Vector vector)`
Parameters:
`vector` -
Returns:
the vector as a column in a matrix
• #### toRowJama

`public static Jama.Matrix toRowJama(Vector vector)`
Parameters:
`vector` -
Returns:
the vector as a row in a matrix
• #### fromJama

`public static Matrix fromJama(Jama.Matrix sol)`
Parameters:
`sol` -
Returns:
Dense matrix from a `Matrix`
• #### toMTJ

`public static no.uib.cipr.matrix.Matrix toMTJ(Matrix sol)`
Parameters:
`sol` -
Returns:
Dense matrix from a `Matrix`
• #### subMatrix

```public static <T extends Matrix> T subMatrix(T mat,
int rowstart,
int rowend,
int colstart,
int colend)```
Extract the submatrix of the same type of mat
Parameters:
`mat` -
`rowstart` -
`rowend` -
`colstart` -
`colend` -
Returns:
new instance
• #### subMatrix

```public static <T extends Matrix> T subMatrix(T mat,
gnu.trove.TIntCollection rows,
gnu.trove.TIntCollection cols)```
Extract a submatrix from the given rows and cols
Parameters:
`mat` - the matrix to extract from
`rows` - the rows to extract
`cols` - the columns to extract
Returns:
the extracted matrix
• #### subMatrix

```public static <T extends Matrix> T subMatrix(T mat,
gnu.trove.list.array.TIntArrayList rows,
int colstart,
int colend)```
Parameters:
`mat` -
`rows` -
`colstart` -
`colend` -
Returns:
the submatrix
• #### subMatrix

```public static <T extends Matrix> T subMatrix(T mat,
int rowstart,
int rowend,
gnu.trove.list.array.TIntArrayList cols)```
Parameters:
`mat` -
`rowstart` -
`rowend` -
`cols` -
Returns:
the submatrix
• #### asMatlab

`public static com.jmatio.types.MLDouble asMatlab(Matrix m)`
Parameters:
`m` -
Returns:
a `MLDouble` for matlab
• #### min

`public static double min(Matrix mat)`
uses the first value returned by `minmaxmean(Matrix)`
Parameters:
`mat` -
Returns:
the min
• #### max

`public static double max(Matrix mat)`
uses the second value returned by `minmaxmean(Matrix)`
Parameters:
`mat` -
Returns:
the min
• #### mean

`public static double mean(Matrix mat)`
uses the third value returned by `minmaxmean(Matrix)`
Parameters:
`mat` -
Returns:
the min
• #### minus

```public static <T extends Matrix> T minus(T l,
double v)```
Parameters:
`l` -
`v` -
Returns:
performs l - v returning a matrix of type T
• #### minus

```public static Vector minus(Vector l,
Vector v)```
Parameters:
`l` -
`v` -
Returns:
performs l - v returning a matrix of type T
• #### minus

```public static <T extends Matrix> T minus(double v,
T l)```
Parameters:
`v` -
`l` -
Returns:
performs v - l returning a matrix of type T
• #### fromMatlab

`public static Matrix fromMatlab(com.jmatio.types.MLArray mlArray)`
Create a `Matrix` from a matlab `MLArray`
Parameters:
`mlArray` - the matlab array
Returns:
the matrix
• #### sum

`public static double sum(DiagonalMatrix d)`
Sum the diagonal of the given matrix
Parameters:
`d` - the matrix
Returns:
the sum along the diagonal
• #### setSubMatrix

```public static void setSubMatrix(Matrix to,
int row,
int col,
Matrix from)```
Set a submatrix of a larger matrix
Parameters:
`to` - the matrix to write into
`row` - the row to start inserting from
`col` - the column to start inserting from
`from` - the matrix to insert
• #### transpose

`public static <T extends Matrix> T transpose(T mat)`
Transpose a matrix, returning a new matrix.
Parameters:
`mat` - the matrix to transpose
Returns:
the transposed matrix
• #### maxInplace

```public static SparseMatrix maxInplace(SparseMatrix A,
SparseMatrix B)```
Parameters:
`A` -
`B` -
Returns:
A = MAX(A,B)
• #### minInplace

```public static SparseMatrix minInplace(SparseMatrix A,
SparseMatrix B)```
Parameters:
`A` -
`B` -
Returns:
A = MIN(A,B)
• #### timesInplace

```public static SparseMatrix timesInplace(SparseMatrix A,
SparseMatrix B)```
Parameters:
`A` -
`B` -
Returns:
A = A.*B
• #### timesInplace

```public static Matrix timesInplace(Matrix A,
Matrix B)```
Parameters:
`A` -
`B` -
Returns:
A = A.*B
• #### copy

`public static <T extends Matrix> T copy(T sparseMatrix)`
Copy a matrix
Parameters:
`sparseMatrix` - the matrix to copy
Returns:
the copy
• #### threshold

```public static SparseMatrix threshold(SparseMatrix data,
double thresh)```
Set values below the given threshold to zero in the output matrix.
Parameters:
`data` - the input matrix
`thresh` - the threshold
Returns:
a new matrix with values in the input matrix set to zero.
• #### sparseVectorToSparseArray

`public static SparseDoubleArray sparseVectorToSparseArray(SparseVector row)`
Parameters:
`row` - the vector to convert
Returns:
the converted vector
• #### setSubVector

```public static void setSubVector(Vector to,
int startindex,
Vector from)```
Parameters:
`to` - add items to this
`startindex` - starting index in to
`from` - add items from this
• #### setSubMatrixRow

```public static void setSubMatrixRow(Matrix to,
int row,
int col,
Vector v)```
Starting from a given column of a row, set the values of a matrix to the values of v
Parameters:
`to` -
`row` -
`col` -
`v` -
• #### setSubMatrixCol

```public static void setSubMatrixCol(Matrix to,
int row,
int col,
Vector v)```
Starting from a given row of a column, set the values of a matrix to the values of v
Parameters:
`to` -
`row` -
`col` -
`v` -
• #### lessThan

```public static Vector lessThan(Vector v,
double d)```
Parameters:
`v` - the value vector
`d` - the check value
Returns:
for each item in the vector, returns 1 if the value is less than the check value
• #### any

`public static boolean any(Vector v)`
Parameters:
`v` -
Returns:
any values of the vector are not-zero
• #### appendColumn

```public static <T extends Matrix> T appendColumn(T m,
Vector col)```
Parameters:
`m` -
`col` -
Returns:
• #### appendRow

```public static <T extends Matrix> T appendRow(T m,
Vector row)```
Parameters:
`m` -
`row` -
Returns:
• #### fromCF

`public static Matrix fromCF(gov.sandia.cognition.math.matrix.Matrix init)`
Create a `Matrix` from the Cognitive Foundry equivalent
Parameters:
`init` - the matrix
Returns:
the converted matrix
• #### dotProduct

```public static Matrix dotProduct(Matrix X,
Matrix W)```
Compute the dot product X.W
Parameters:
`X` -
`W` -
Returns:
dot product
• #### norm2

`public static double norm2(Vector row)`
Compute the 2-norm (Euclidean norm) of the vector
Parameters:
`row` - the vector
Returns:
the Euclidean norm
• #### minus

```public static Matrix minus(Matrix A,
Matrix B)```
Subtract matrices A-B
Parameters:
`A` -
`B` -
Returns:
A-B
• #### normF

`public static double normF(Matrix A)`
Compute the Frobenius norm
Parameters:
`A` - the matrix
Returns:
the F norm
• #### vstack

`public static Matrix vstack(Matrix... matricies)`
Stack matrices vertically
Parameters:
`matricies` - matrices to stack
Returns:
matrix created from the stacking