Package smile.math.matrix
Class JMatrix
- java.lang.Object
-
- smile.math.matrix.JMatrix
-
- All Implemented Interfaces:
java.io.Serializable,DenseMatrix,Matrix,MatrixMultiplication<DenseMatrix,DenseMatrix>
public class JMatrix extends java.lang.Object implements DenseMatrix
A pure Java implementation of DenseMatrix whose data is stored in a single 1D array of doubles in column major order.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description JMatrix(double[] A)Constructor of a column vector/matrix with given array as the internal storage.JMatrix(double[][] A)Constructor.JMatrix(int rows, int cols)Constructor of all-zero matrix.JMatrix(int rows, int cols, double value)Constructor.JMatrix(int rows, int cols, double[] value)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JMatrixaat()Returns A * A'JMatrixabmm(DenseMatrix B)Returns the result of matrix multiplication A * B.JMatrixabtmm(DenseMatrix B)Returns the result of matrix multiplication A * B'.JMatrixadd(double x)In place element-wise addition A = A + xDenseMatrixadd(double x, DenseMatrix c)Element-wise addition C = A + xJMatrixadd(double x, JMatrix c)doubleadd(int i, int j, double x)A[i][j] += xJMatrixadd(DenseMatrix b)In place addition A = A + BDenseMatrixadd(DenseMatrix b, DenseMatrix c)C = A + BJMatrixadd(JMatrix b)JMatrixadd(JMatrix b, JMatrix c)JMatrixata()Returns A' * AJMatrixatbmm(DenseMatrix B)Returns the result of matrix multiplication A' * B.double[]atx(double[] x, double[] y)y = A' * xdouble[]atxpy(double[] x, double[] y)y = A' * x + ydouble[]atxpy(double[] x, double[] y, double b)y = A' * x + b * ydouble[]ax(double[] x, double[] y)y = A * xdouble[]axpy(double[] x, double[] y)y = A * x + ydouble[]axpy(double[] x, double[] y, double b)y = A * x + b * yCholeskycholesky()Cholesky decomposition for symmetric and positive definite matrix.JMatrixcopy()Returns a copy of this matrix.double[]data()Returns the array of storing the matrix.JMatrixdiv(double x)In place element-wise division A = A / xDenseMatrixdiv(double x, DenseMatrix c)Element-wise addition C = A / xJMatrixdiv(double x, JMatrix c)doublediv(int i, int j, double x)A[i][j] /= xJMatrixdiv(DenseMatrix b)In place element-wise division A = A / B A = A - BDenseMatrixdiv(DenseMatrix b, DenseMatrix c)C = A / BJMatrixdiv(JMatrix b)JMatrixdiv(JMatrix b, JMatrix c)double[]eig()Returns the eigen values in an array of size 2N.EVDeigen()Returns the eigen value decomposition.doubleget(int i, int j)Returns the entry value at row i and column j.booleanisSymmetric()Returns true if the matrix is symmetric.intld()The LDA (and LDB, LDC, etc.) parameter in BLAS is effectively the stride of the matrix as it is laid out in linear memory.LUlu()LU decomposition is computed by a "left-looking", dot-product, Crout/Doolittle algorithm.JMatrixmul(double x)In place element-wise multiplication A = A * xDenseMatrixmul(double x, DenseMatrix c)Element-wise addition C = A * xJMatrixmul(double x, JMatrix c)doublemul(int i, int j, double x)A[i][j] *= xJMatrixmul(DenseMatrix b)In place element-wise multiplication A = A * BDenseMatrixmul(DenseMatrix b, DenseMatrix c)C = A * BJMatrixmul(JMatrix b)JMatrixmul(JMatrix b, JMatrix c)intncols()Returns the number of columns.intnrows()Returns the number of rows.QRqr()QR Decomposition is computed by Householder reflections.JMatrixreplaceNaN(double x)Replaces NaN's with given value.doubleset(int i, int j, double x)Set the entry value at row i and column j.voidsetSymmetric(boolean symmetric)Sets if the matrix is symmetric.protected static voidsort(double[] d, double[] e)Sort eigenvalues.protected static voidsort(double[] d, double[] e, DenseMatrix V)Sort eigenvalues and eigenvectors.JMatrixsub(double x)In place element-wise subtraction A = A - xDenseMatrixsub(double x, DenseMatrix c)Element-wise addition C = A - xJMatrixsub(double x, JMatrix c)doublesub(int i, int j, double x)A[i][j] -= xJMatrixsub(DenseMatrix b)In place subtraction A = A - BDenseMatrixsub(DenseMatrix b, DenseMatrix c)C = A - BJMatrixsub(JMatrix b)JMatrixsub(JMatrix b, JMatrix c)doublesum()Returns the sum of all elements in the matrix.SVDsvd()Returns the singular value decomposition.java.lang.StringtoString()JMatrixtranspose()Returns the matrix transpose.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Constructor Detail
-
JMatrix
public JMatrix(double[][] A)
Constructor.- Parameters:
A- the array of matrix.
-
JMatrix
public JMatrix(double[] A)
Constructor of a column vector/matrix with given array as the internal storage.- Parameters:
A- the array of column vector.
-
JMatrix
public JMatrix(int rows, int cols)Constructor of all-zero matrix.
-
JMatrix
public JMatrix(int rows, int cols, double value)Constructor. Fill the matrix with given value.
-
JMatrix
public JMatrix(int rows, int cols, double[] value)Constructor.- Parameters:
value- the array of matrix values arranged in column major format
-
-
Method Detail
-
isSymmetric
public boolean isSymmetric()
Description copied from interface:MatrixReturns true if the matrix is symmetric.- Specified by:
isSymmetricin interfaceMatrix
-
setSymmetric
public void setSymmetric(boolean symmetric)
Description copied from interface:MatrixSets if the matrix is symmetric. It is the caller's responability to make sure if the matrix symmetric. Also the matrix won't update this property if the matrix values are changed.- Specified by:
setSymmetricin interfaceMatrix
-
copy
public JMatrix copy()
Description copied from interface:DenseMatrixReturns a copy of this matrix.- Specified by:
copyin interfaceDenseMatrix
-
data
public double[] data()
Description copied from interface:DenseMatrixReturns the array of storing the matrix.- Specified by:
datain interfaceDenseMatrix
-
transpose
public JMatrix transpose()
Description copied from interface:DenseMatrixReturns the matrix transpose.- Specified by:
transposein interfaceDenseMatrix- Specified by:
transposein interfaceMatrix
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
nrows
public int nrows()
Description copied from interface:MatrixReturns the number of rows.
-
ncols
public int ncols()
Description copied from interface:MatrixReturns the number of columns.
-
ld
public int ld()
Description copied from interface:DenseMatrixThe LDA (and LDB, LDC, etc.) parameter in BLAS is effectively the stride of the matrix as it is laid out in linear memory. It is perfectly valid to have an LDA value which is larger than the leading dimension of the matrix which is being operated on. Typical cases where it is either useful or necessary to use a larger LDA value are when you are operating on a sub matrix from a larger dense matrix, and when hardware or algorithms offer performance advantages when storage is padded to round multiples of some optimal size (cache lines or GPU memory transaction size, or load balance in multiprocessor implementations, for example).- Specified by:
ldin interfaceDenseMatrix- Returns:
- the leading dimension
-
get
public double get(int i, int j)Description copied from interface:MatrixReturns the entry value at row i and column j.
-
set
public double set(int i, int j, double x)Description copied from interface:DenseMatrixSet the entry value at row i and column j.- Specified by:
setin interfaceDenseMatrix
-
add
public double add(int i, int j, double x)Description copied from interface:DenseMatrixA[i][j] += x- Specified by:
addin interfaceDenseMatrix
-
sub
public double sub(int i, int j, double x)Description copied from interface:DenseMatrixA[i][j] -= x- Specified by:
subin interfaceDenseMatrix
-
mul
public double mul(int i, int j, double x)Description copied from interface:DenseMatrixA[i][j] *= x- Specified by:
mulin interfaceDenseMatrix
-
div
public double div(int i, int j, double x)Description copied from interface:DenseMatrixA[i][j] /= x- Specified by:
divin interfaceDenseMatrix
-
add
public JMatrix add(DenseMatrix b)
Description copied from interface:DenseMatrixIn place addition A = A + B- Specified by:
addin interfaceDenseMatrix- Returns:
- this matrix
-
add
public DenseMatrix add(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrixC = A + B- Specified by:
addin interfaceDenseMatrix- Returns:
- the result matrix
-
sub
public JMatrix sub(DenseMatrix b)
Description copied from interface:DenseMatrixIn place subtraction A = A - B- Specified by:
subin interfaceDenseMatrix- Returns:
- this matrix
-
sub
public DenseMatrix sub(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrixC = A - B- Specified by:
subin interfaceDenseMatrix- Returns:
- the result matrix
-
mul
public JMatrix mul(DenseMatrix b)
Description copied from interface:DenseMatrixIn place element-wise multiplication A = A * B- Specified by:
mulin interfaceDenseMatrix- Returns:
- this matrix
-
mul
public DenseMatrix mul(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrixC = A * B- Specified by:
mulin interfaceDenseMatrix- Returns:
- the result matrix
-
div
public JMatrix div(DenseMatrix b)
Description copied from interface:DenseMatrixIn place element-wise division A = A / B A = A - B- Specified by:
divin interfaceDenseMatrix- Returns:
- this matrix
-
div
public DenseMatrix div(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrixC = A / B- Specified by:
divin interfaceDenseMatrix- Returns:
- the result matrix
-
add
public JMatrix add(double x)
Description copied from interface:DenseMatrixIn place element-wise addition A = A + x- Specified by:
addin interfaceDenseMatrix
-
add
public DenseMatrix add(double x, DenseMatrix c)
Description copied from interface:DenseMatrixElement-wise addition C = A + x- Specified by:
addin interfaceDenseMatrix
-
sub
public JMatrix sub(double x)
Description copied from interface:DenseMatrixIn place element-wise subtraction A = A - x- Specified by:
subin interfaceDenseMatrix
-
sub
public DenseMatrix sub(double x, DenseMatrix c)
Description copied from interface:DenseMatrixElement-wise addition C = A - x- Specified by:
subin interfaceDenseMatrix
-
mul
public JMatrix mul(double x)
Description copied from interface:DenseMatrixIn place element-wise multiplication A = A * x- Specified by:
mulin interfaceDenseMatrix
-
mul
public DenseMatrix mul(double x, DenseMatrix c)
Description copied from interface:DenseMatrixElement-wise addition C = A * x- Specified by:
mulin interfaceDenseMatrix
-
div
public JMatrix div(double x)
Description copied from interface:DenseMatrixIn place element-wise division A = A / x- Specified by:
divin interfaceDenseMatrix
-
div
public DenseMatrix div(double x, DenseMatrix c)
Description copied from interface:DenseMatrixElement-wise addition C = A / x- Specified by:
divin interfaceDenseMatrix
-
replaceNaN
public JMatrix replaceNaN(double x)
Description copied from interface:DenseMatrixReplaces NaN's with given value.- Specified by:
replaceNaNin interfaceDenseMatrix
-
sum
public double sum()
Description copied from interface:DenseMatrixReturns the sum of all elements in the matrix.- Specified by:
sumin interfaceDenseMatrix- Returns:
- the sum of all elements.
-
ata
public JMatrix ata()
Description copied from interface:MatrixReturns A' * A- Specified by:
atain interfaceDenseMatrix- Specified by:
atain interfaceMatrix
-
aat
public JMatrix aat()
Description copied from interface:MatrixReturns A * A'- Specified by:
aatin interfaceDenseMatrix- Specified by:
aatin interfaceMatrix
-
ax
public double[] ax(double[] x, double[] y)Description copied from interface:Matrixy = A * x
-
axpy
public double[] axpy(double[] x, double[] y)Description copied from interface:Matrixy = A * x + y
-
axpy
public double[] axpy(double[] x, double[] y, double b)Description copied from interface:Matrixy = A * x + b * y
-
atx
public double[] atx(double[] x, double[] y)Description copied from interface:Matrixy = A' * x
-
atxpy
public double[] atxpy(double[] x, double[] y)Description copied from interface:Matrixy = A' * x + y
-
atxpy
public double[] atxpy(double[] x, double[] y, double b)Description copied from interface:Matrixy = A' * x + b * y
-
abmm
public JMatrix abmm(DenseMatrix B)
Description copied from interface:MatrixMultiplicationReturns the result of matrix multiplication A * B.- Specified by:
abmmin interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
abtmm
public JMatrix abtmm(DenseMatrix B)
Description copied from interface:MatrixMultiplicationReturns the result of matrix multiplication A * B'.- Specified by:
abtmmin interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
atbmm
public JMatrix atbmm(DenseMatrix B)
Description copied from interface:MatrixMultiplicationReturns the result of matrix multiplication A' * B.- Specified by:
atbmmin interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
lu
public LU lu()
LU decomposition is computed by a "left-looking", dot-product, Crout/Doolittle algorithm.- Specified by:
luin interfaceDenseMatrix
-
cholesky
public Cholesky cholesky()
Cholesky decomposition for symmetric and positive definite matrix. Only the lower triangular part will be used in the decomposition.- Specified by:
choleskyin interfaceDenseMatrix- Throws:
java.lang.IllegalArgumentException- if the matrix is not positive definite.
-
qr
public QR qr()
QR Decomposition is computed by Householder reflections.- Specified by:
qrin interfaceDenseMatrix
-
svd
public SVD svd()
Description copied from interface:DenseMatrixReturns the singular value decomposition. Note that the input matrix will hold U on output.- Specified by:
svdin interfaceDenseMatrix
-
eig
public double[] eig()
Description copied from interface:DenseMatrixReturns the eigen values in an array of size 2N. The first half and second half of returned array contain the real and imaginary parts, respectively, of the computed eigenvalues.- Specified by:
eigin interfaceDenseMatrix
-
eigen
public EVD eigen()
Description copied from interface:DenseMatrixReturns the eigen value decomposition. Note that the input matrix will be overwritten on output.- Specified by:
eigenin interfaceDenseMatrix
-
sort
protected static void sort(double[] d, double[] e)Sort eigenvalues.
-
sort
protected static void sort(double[] d, double[] e, DenseMatrix V)Sort eigenvalues and eigenvectors.
-
-