public class MatrixUtils extends Object
Modifier and Type | Method and Description |
---|---|
static Jama.Matrix |
abs(Jama.Matrix mat)
Return a copy of the input matrix with all elements set to their absolute
value.
|
static Jama.Matrix |
all(Jama.Matrix x) |
static Jama.Matrix |
and(Jama.Matrix... x) |
static boolean |
anyNaNorInf(Jama.Matrix matrix)
Are any values NaN or Inf?
|
static Jama.Matrix |
convert(no.uib.cipr.matrix.DenseMatrix mjt)
Convert a
DenseMatrix to a Matrix . |
static Jama.Matrix |
convert(no.uib.cipr.matrix.DenseMatrix mjt,
int nrows,
int ncols)
Convert a
DenseMatrix to a Matrix . |
static Jama.Matrix |
covariance(Jama.Matrix m)
Compute the covariance matrix of the given samples (assumed each sample is a
row).
|
static Jama.Matrix |
diag(double[] s)
Create a diagonal matrix
|
static Jama.Matrix |
diag(Jama.Matrix cv)
Extract the diagonal component from the given matrix
|
static double[] |
diagVector(Jama.Matrix cv)
Extract the diagonal component from the given matrix
|
static EigenValueVectorPair |
eig2x2(Jama.Matrix m)
An eigen decomposition that uses a deterministic method if the matrix is 2x2.
|
static boolean |
equals(Jama.Matrix m1,
Jama.Matrix m2,
double eps)
Check if two matrices are equal
|
static Jama.Matrix |
exp(Jama.Matrix m)
Return a copy of the input matrix where every value is the exponential of the
elements, e to the X.
|
static Jama.Matrix |
fill(Jama.Matrix m,
double v)
Fill a matrix with a constant value.
|
static Jama.Matrix |
fromRowPacked(double[] vector,
int ncols)
Construct a matrix from a row-packed (i.e.
|
static Jama.Matrix |
greaterThan(Jama.Matrix x,
double val) |
static Jama.Matrix |
greaterThanSet(Jama.Matrix x,
int val,
int toset)
for every value in x greater than val set toset
|
static Jama.Matrix |
hstack(Jama.Matrix... x)
horizontally stack all the matrices provided.
|
static Jama.Matrix |
incrColumn(Jama.Matrix m,
int col,
double value)
Increment values in a single column by a constant
|
static Jama.Matrix |
incrRow(Jama.Matrix m,
int row,
double value)
Increment values in a single column by a constant
|
static Jama.Matrix |
invSqrtSym(Jama.Matrix matrix)
Compute the inverse square root, X, of the symmetric matrix A; A^-(1/2)
|
static Jama.Matrix |
lessThan(Jama.Matrix x,
double val) |
static Jama.Matrix |
lessThanSet(Jama.Matrix x,
int val,
int toset)
for every value in x less than val set toset
|
static Jama.Matrix |
matrixFromFloat(float[][] data)
Construct a matrix from a 2D float array of data.
|
static double |
maxAbsDiag(Jama.Matrix matrix)
Get the maximum absolute value of the diagonal.
|
static Jama.Matrix |
min(Jama.Matrix A,
Jama.Matrix B)
min(A,B) returns an array the same size as A and B with the smallest elements
taken from A or B.
|
static double |
minAbsDiag(Jama.Matrix matrix)
Get the minimum absolute value of the diagonal.
|
static Jama.Matrix |
minus(Jama.Matrix m,
double v)
Subtract a constant from all values
|
static Jama.Matrix |
minusCol(Jama.Matrix in,
Jama.Matrix col)
Subtract the given col vector (held as a Matrix) from every col of the given
matrix, returning the result in a new matrix.
|
static Jama.Matrix |
minusRow(Jama.Matrix in,
double[] row)
Subtract the given row vector from every row of the given matrix, returning
the result in a new matrix.
|
static double[][] |
mtjToDoubleArray(no.uib.cipr.matrix.DenseMatrix mat)
Convert an mtj matrix into a 2d double array
|
static Jama.Matrix |
ntuples(Jama.Matrix A,
Jama.Matrix B)
Given two row vectors, construct the power set of rowvector combinations
|
static Jama.Matrix |
ones(int rowDimension,
int columnDimension) |
static Jama.Matrix |
plus(Jama.Matrix m,
double v)
Add a constant to all values
|
static Jama.Matrix |
plusEquals(Jama.Matrix result,
Jama.Matrix add)
Add a matrix to another inline.
|
static Jama.Matrix |
plusEqualsRow(Jama.Matrix mat,
Jama.Matrix rows,
int[] rowIndex)
Add the rows to the mat at rowIndex.
|
static Jama.Matrix |
pow(Jama.Matrix mat,
double exp)
Return a copy of the matrix with all the values raised to a power.
|
static Jama.Matrix |
pseudoInverse(Jama.Matrix matrix)
Computes the Moore-Penrose pseudoinverse.
|
static Jama.Matrix |
randGaussian(int rows,
int cols)
Generate a matrix with Gaussian distributed randoms
|
static Jama.Matrix |
range(double... range)
range is: - a single number (a) (0:1:a) - two numbers (a,b) (a:1:b) - three
numbers (a,b,c) (a:b:c)
|
static Jama.Matrix |
range(int... range)
range is: - a single number (a) (0:1:a) - two numbers (a,b) (a:1:b) - three
numbers (a,b,c) (a:b:c)
|
static Jama.Matrix |
rangePow(double d,
double... range)
d to the power of each value in range.
|
static Jama.Matrix |
reduceRank(Jama.Matrix m,
int rank)
Reduce the rank a matrix by estimating a the best (in a least-squares sense)
approximation using the thin SVD.
|
static Jama.Matrix |
repmat(Jama.Matrix x,
int i,
int j)
Given a matrix, repeat the matrix over i rows and j columns
|
static Jama.Matrix |
reshape(Jama.Matrix m,
int newRows)
Get a reshaped copy of the input matrix
|
static Jama.Matrix |
reshape(Jama.Matrix m,
int newRows,
boolean columnMajor)
Get a reshaped copy of the input matrix
|
static Jama.Matrix |
reverseColumns(Jama.Matrix m)
Create a copy of a matrix with the columns in reverse order.
|
static Jama.Matrix |
reverseColumnsInplace(Jama.Matrix m)
Reverse the column order of the input matrix inplace.
|
static Jama.Matrix |
reverseRows(Jama.Matrix m)
Create a copy of a matrix with the rows in reverse order.
|
static Jama.Matrix |
reverseRowsInplace(Jama.Matrix m)
Reverse the row order of the input matrix inplace.
|
static Jama.Matrix |
round(Jama.Matrix times)
round (using
Math.round(double) each value of the matrix |
static Jama.Matrix |
setColumn(Jama.Matrix m,
int c,
double v)
Set the values of the elements in a single column to a constant value.
|
static Jama.Matrix |
setRow(Jama.Matrix m,
int r,
double v)
Set the values of the elements in a single column to a constant value.
|
static Jama.Matrix |
sign(Jama.Matrix m)
For each element of X, sign(X) returns 1 if the element is greater than zero,
0 if it equals zero and -1 if it is less than zero.
|
static double[] |
solveHomogeneousSystem(no.uib.cipr.matrix.DenseMatrix A)
Solves the system
Ax = 0 , returning the vector x as an array. |
static double[] |
solveHomogeneousSystem(double[][] A)
Solves the system
Ax = 0 , returning the vector x as an array. |
static double[] |
solveHomogeneousSystem(Jama.Matrix A)
Solves the system
Ax = 0 , returning the vector x as an array. |
static double |
sparsity(SparseMatrix matrix)
Compute the sparsity (i.e.
|
static Jama.Matrix |
sqrt(Jama.Matrix matrix)
Compute the principle square root, X, of the matrix A such that A=X*X
|
static double |
sum(Jama.Matrix mat)
Compute the sum of all elements of the matrix.
|
static Jama.Matrix |
sumCols(Jama.Matrix m)
Compute the sum of values in all cols
|
static double |
sumColumn(Jama.Matrix m,
int col)
Compute the sum of values in a single column
|
static double |
sumRow(Jama.Matrix m,
int row)
Compute the sum of values in a single row
|
static Jama.Matrix |
sumRows(Jama.Matrix m)
Compute the sum of values in all rows
|
static EigenValueVectorPair |
symmetricEig2x2(Jama.Matrix m)
Compute the real Eigen decomposition of a symmetric 2x2 matrix.
|
static Jama.Matrix |
tanh(Jama.Matrix m)
Return a copy of the input matrix where every value is the hyperbolic tangent
of the elements.
|
static Jama.Matrix |
times(Jama.Matrix m,
double val)
Multiply a matrix by a constant inplace, returning the matrix.
|
static String |
toJavaString(Jama.Matrix mat)
Format a matrix as a single-line string suitable for using in java
|
static String |
toMatlabString(Jama.Matrix mat)
Format a matrix as a single-line string suitable for using in matlab or
octave
|
static String |
toPythonString(Jama.Matrix mat)
Format a matrix as a single-line string suitable for using in python
|
static String |
toString(Jama.Matrix mat)
Generate a
String representation of a matrix. |
static double |
trace(Jama.Matrix mat) |
static int[] |
valsToIndex(double[] vals) |
static void |
zero(Jama.Matrix m)
Zero the matrix
|
public static boolean anyNaNorInf(Jama.Matrix matrix)
matrix
- matrix to testpublic static double maxAbsDiag(Jama.Matrix matrix)
matrix
- the matrixpublic static double minAbsDiag(Jama.Matrix matrix)
matrix
- the matrixpublic static Jama.Matrix sqrt(Jama.Matrix matrix)
matrix
- the matrixpublic static Jama.Matrix pseudoInverse(Jama.Matrix matrix)
PseudoInverse.pseudoInverse(Matrix)
.matrix
- the matrix to invert.PseudoInverse.pseudoInverse(Matrix)
public static Jama.Matrix invSqrtSym(Jama.Matrix matrix)
matrix
- the symmetric matrixpublic static Jama.Matrix abs(Jama.Matrix mat)
mat
- the matrix.public static boolean equals(Jama.Matrix m1, Jama.Matrix m2, double eps)
m1
- first matrixm2
- second matrixeps
- epsilon for checking valuespublic static Jama.Matrix pow(Jama.Matrix mat, double exp)
mat
- the matrix.exp
- the power.public static String toString(Jama.Matrix mat)
String
representation of a matrix.mat
- the matrixpublic static double sum(Jama.Matrix mat)
mat
- the matrix.public static void zero(Jama.Matrix m)
m
- the matrixpublic static EigenValueVectorPair symmetricEig2x2(Jama.Matrix m)
m
- the matrixpublic static EigenValueVectorPair eig2x2(Jama.Matrix m)
EigenvalueDecomposition
i.e. the
largest eigen value is held in the [m.rows - 1,m.cols-1] (i.e. [1,1])
locationm
- public static Jama.Matrix matrixFromFloat(float[][] data)
data
- the data.public static Jama.Matrix reduceRank(Jama.Matrix m, int rank)
m
- the matrix to reduce.rank
- the desired rank.public static Jama.Matrix convert(no.uib.cipr.matrix.DenseMatrix mjt)
DenseMatrix
to a Matrix
.mjt
- DenseMatrix
to convertpublic static Jama.Matrix convert(no.uib.cipr.matrix.DenseMatrix mjt, int nrows, int ncols)
DenseMatrix
to a Matrix
.mjt
- DenseMatrix
to convertnrows
- number of rows to copyncols
- number of columns to copypublic static Jama.Matrix reverseColumns(Jama.Matrix m)
m
- the input matrixpublic static Jama.Matrix reverseColumnsInplace(Jama.Matrix m)
m
- the input matrixpublic static Jama.Matrix reverseRows(Jama.Matrix m)
m
- the input matrixpublic static Jama.Matrix reverseRowsInplace(Jama.Matrix m)
m
- the input matrixpublic static Jama.Matrix diag(double[] s)
s
- length diagonal numberspublic static Jama.Matrix setColumn(Jama.Matrix m, int c, double v)
m
- the matrixc
- the columnv
- the constant valuepublic static Jama.Matrix setRow(Jama.Matrix m, int r, double v)
m
- the matrixr
- the rowv
- the constant valuepublic static Jama.Matrix fill(Jama.Matrix m, double v)
m
- the matrixv
- the constant valuepublic static Jama.Matrix minus(Jama.Matrix m, double v)
m
- the matrixv
- the constant valuepublic static Jama.Matrix plus(Jama.Matrix m, double v)
m
- the matrixv
- the constant valuepublic static Jama.Matrix reshape(Jama.Matrix m, int newRows)
m
- the matrix to reshapenewRows
- the new number of rowspublic static Jama.Matrix reshape(Jama.Matrix m, int newRows, boolean columnMajor)
m
- the matrix to reshapenewRows
- the new number of rowscolumnMajor
- if true, values are drawn and placed down columns first. if false
values are drawn and placed across rows firstpublic static double sumColumn(Jama.Matrix m, int col)
m
- the matrixcol
- the columnpublic static double sumRow(Jama.Matrix m, int row)
m
- the matrixrow
- the rowpublic static Jama.Matrix incrColumn(Jama.Matrix m, int col, double value)
m
- the matrixcol
- the columnvalue
- the constantpublic static Jama.Matrix incrRow(Jama.Matrix m, int row, double value)
m
- the matrixrow
- the rowvalue
- the constantpublic static Jama.Matrix round(Jama.Matrix times)
Math.round(double)
each value of the matrixtimes
- public static Jama.Matrix min(Jama.Matrix A, Jama.Matrix B)
A
- B
- public static Jama.Matrix rangePow(double d, double... range)
range(double...)
range is: - a
single number (a) (0:1:a) - two numbers (a,b) (a:1:b) - three numbers (a,b,c)
(a:b:c)
any other amount of range results in a RuntimeException
d
- range
- public static Jama.Matrix range(double... range)
range
- public static Jama.Matrix range(int... range)
range
- public static Jama.Matrix ntuples(Jama.Matrix A, Jama.Matrix B)
A
- B
- public static Jama.Matrix repmat(Jama.Matrix x, int i, int j)
x
- i
- j
- public static Jama.Matrix hstack(Jama.Matrix... x)
x
- public static Jama.Matrix plusEqualsRow(Jama.Matrix mat, Jama.Matrix rows, int[] rowIndex)
mat
- rows
- rowIndex
- public static Jama.Matrix lessThan(Jama.Matrix x, double val)
x
- val
- public static Jama.Matrix greaterThan(Jama.Matrix x, double val)
x
- val
- public static Jama.Matrix and(Jama.Matrix... x)
x
- public static Jama.Matrix ones(int rowDimension, int columnDimension)
rowDimension
- columnDimension
- public static Jama.Matrix all(Jama.Matrix x)
x
- public static int[] valsToIndex(double[] vals)
vals
- public static Jama.Matrix greaterThanSet(Jama.Matrix x, int val, int toset)
x
- val
- toset
- public static Jama.Matrix lessThanSet(Jama.Matrix x, int val, int toset)
x
- val
- toset
- public static Jama.Matrix minusRow(Jama.Matrix in, double[] row)
in
- the matrixrow
- the row vectorpublic static Jama.Matrix minusCol(Jama.Matrix in, Jama.Matrix col)
in
- the matrixcol
- the col Matrix (Only the first column is used)public static Jama.Matrix plusEquals(Jama.Matrix result, Jama.Matrix add)
result
- the matrix to add toadd
- the matrix to addpublic static Jama.Matrix times(Jama.Matrix m, double val)
m
- the matrixval
- the value to multiply bypublic static double[][] mtjToDoubleArray(no.uib.cipr.matrix.DenseMatrix mat)
mat
- public static Jama.Matrix sumRows(Jama.Matrix m)
m
- the matrixpublic static Jama.Matrix sumCols(Jama.Matrix m)
m
- the matrixpublic static Jama.Matrix randGaussian(int rows, int cols)
rows
- the number of rowscols
- the number of columnspublic static double sparsity(SparseMatrix matrix)
matrix
- the matrixpublic static Jama.Matrix diag(Jama.Matrix cv)
cv
- the matrixpublic static double[] diagVector(Jama.Matrix cv)
cv
- the matrixpublic static String toMatlabString(Jama.Matrix mat)
mat
- the matrix to formatpublic static String toPythonString(Jama.Matrix mat)
mat
- the matrix to formatpublic static double trace(Jama.Matrix mat)
mat
- public static double[] solveHomogeneousSystem(Jama.Matrix A)
Ax = 0
, returning the vector x as an array.
Internally computes the least-squares solution using the SVD of
A
.A
- the matrix describing the systempublic static double[] solveHomogeneousSystem(double[][] A)
Ax = 0
, returning the vector x as an array.
Internally computes the least-squares solution using the SVD of
A
.A
- the matrix describing the systempublic static double[] solveHomogeneousSystem(no.uib.cipr.matrix.DenseMatrix A)
Ax = 0
, returning the vector x as an array.
Internally computes the least-squares solution using the SVD of
A
.A
- the matrix describing the systempublic static String toJavaString(Jama.Matrix mat)
mat
- the matrix to formatpublic static Jama.Matrix fromRowPacked(double[] vector, int ncols)
vector
- the row-packed vectorncols
- the number of columnspublic static Jama.Matrix covariance(Jama.Matrix m)
m
- the samples matrixpublic static Jama.Matrix sign(Jama.Matrix m)
m
- the matrixpublic static Jama.Matrix exp(Jama.Matrix m)
m
- the input matrixpublic static Jama.Matrix tanh(Jama.Matrix m)
m
- the input matrix