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 JMatrix
aat()
Returns A * A'JMatrix
abmm(DenseMatrix B)
Returns the result of matrix multiplication A * B.JMatrix
abtmm(DenseMatrix B)
Returns the result of matrix multiplication A * B'.JMatrix
add(double x)
In place element-wise addition A = A + xDenseMatrix
add(double x, DenseMatrix c)
Element-wise addition C = A + xJMatrix
add(double x, JMatrix c)
double
add(int i, int j, double x)
A[i][j] += xJMatrix
add(DenseMatrix b)
In place addition A = A + BDenseMatrix
add(DenseMatrix b, DenseMatrix c)
C = A + BJMatrix
add(JMatrix b)
JMatrix
add(JMatrix b, JMatrix c)
JMatrix
ata()
Returns A' * AJMatrix
atbmm(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 * yCholesky
cholesky()
Cholesky decomposition for symmetric and positive definite matrix.JMatrix
copy()
Returns a copy of this matrix.double[]
data()
Returns the array of storing the matrix.JMatrix
div(double x)
In place element-wise division A = A / xDenseMatrix
div(double x, DenseMatrix c)
Element-wise addition C = A / xJMatrix
div(double x, JMatrix c)
double
div(int i, int j, double x)
A[i][j] /= xJMatrix
div(DenseMatrix b)
In place element-wise division A = A / B A = A - BDenseMatrix
div(DenseMatrix b, DenseMatrix c)
C = A / BJMatrix
div(JMatrix b)
JMatrix
div(JMatrix b, JMatrix c)
double[]
eig()
Returns the eigen values in an array of size 2N.EVD
eigen()
Returns the eigen value decomposition.double
get(int i, int j)
Returns the entry value at row i and column j.boolean
isSymmetric()
Returns true if the matrix is symmetric.int
ld()
The LDA (and LDB, LDC, etc.) parameter in BLAS is effectively the stride of the matrix as it is laid out in linear memory.LU
lu()
LU decomposition is computed by a "left-looking", dot-product, Crout/Doolittle algorithm.JMatrix
mul(double x)
In place element-wise multiplication A = A * xDenseMatrix
mul(double x, DenseMatrix c)
Element-wise addition C = A * xJMatrix
mul(double x, JMatrix c)
double
mul(int i, int j, double x)
A[i][j] *= xJMatrix
mul(DenseMatrix b)
In place element-wise multiplication A = A * BDenseMatrix
mul(DenseMatrix b, DenseMatrix c)
C = A * BJMatrix
mul(JMatrix b)
JMatrix
mul(JMatrix b, JMatrix c)
int
ncols()
Returns the number of columns.int
nrows()
Returns the number of rows.QR
qr()
QR Decomposition is computed by Householder reflections.JMatrix
replaceNaN(double x)
Replaces NaN's with given value.double
set(int i, int j, double x)
Set the entry value at row i and column j.void
setSymmetric(boolean symmetric)
Sets if the matrix is symmetric.protected static void
sort(double[] d, double[] e)
Sort eigenvalues.protected static void
sort(double[] d, double[] e, DenseMatrix V)
Sort eigenvalues and eigenvectors.JMatrix
sub(double x)
In place element-wise subtraction A = A - xDenseMatrix
sub(double x, DenseMatrix c)
Element-wise addition C = A - xJMatrix
sub(double x, JMatrix c)
double
sub(int i, int j, double x)
A[i][j] -= xJMatrix
sub(DenseMatrix b)
In place subtraction A = A - BDenseMatrix
sub(DenseMatrix b, DenseMatrix c)
C = A - BJMatrix
sub(JMatrix b)
JMatrix
sub(JMatrix b, JMatrix c)
double
sum()
Returns the sum of all elements in the matrix.SVD
svd()
Returns the singular value decomposition.java.lang.String
toString()
JMatrix
transpose()
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:Matrix
Returns true if the matrix is symmetric.- Specified by:
isSymmetric
in interfaceMatrix
-
setSymmetric
public void setSymmetric(boolean symmetric)
Description copied from interface:Matrix
Sets 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:
setSymmetric
in interfaceMatrix
-
copy
public JMatrix copy()
Description copied from interface:DenseMatrix
Returns a copy of this matrix.- Specified by:
copy
in interfaceDenseMatrix
-
data
public double[] data()
Description copied from interface:DenseMatrix
Returns the array of storing the matrix.- Specified by:
data
in interfaceDenseMatrix
-
transpose
public JMatrix transpose()
Description copied from interface:DenseMatrix
Returns the matrix transpose.- Specified by:
transpose
in interfaceDenseMatrix
- Specified by:
transpose
in interfaceMatrix
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
nrows
public int nrows()
Description copied from interface:Matrix
Returns the number of rows.
-
ncols
public int ncols()
Description copied from interface:Matrix
Returns the number of columns.
-
ld
public int ld()
Description copied from interface:DenseMatrix
The 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:
ld
in interfaceDenseMatrix
- Returns:
- the leading dimension
-
get
public double get(int i, int j)
Description copied from interface:Matrix
Returns the entry value at row i and column j.
-
set
public double set(int i, int j, double x)
Description copied from interface:DenseMatrix
Set the entry value at row i and column j.- Specified by:
set
in interfaceDenseMatrix
-
add
public double add(int i, int j, double x)
Description copied from interface:DenseMatrix
A[i][j] += x- Specified by:
add
in interfaceDenseMatrix
-
sub
public double sub(int i, int j, double x)
Description copied from interface:DenseMatrix
A[i][j] -= x- Specified by:
sub
in interfaceDenseMatrix
-
mul
public double mul(int i, int j, double x)
Description copied from interface:DenseMatrix
A[i][j] *= x- Specified by:
mul
in interfaceDenseMatrix
-
div
public double div(int i, int j, double x)
Description copied from interface:DenseMatrix
A[i][j] /= x- Specified by:
div
in interfaceDenseMatrix
-
add
public JMatrix add(DenseMatrix b)
Description copied from interface:DenseMatrix
In place addition A = A + B- Specified by:
add
in interfaceDenseMatrix
- Returns:
- this matrix
-
add
public DenseMatrix add(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrix
C = A + B- Specified by:
add
in interfaceDenseMatrix
- Returns:
- the result matrix
-
sub
public JMatrix sub(DenseMatrix b)
Description copied from interface:DenseMatrix
In place subtraction A = A - B- Specified by:
sub
in interfaceDenseMatrix
- Returns:
- this matrix
-
sub
public DenseMatrix sub(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrix
C = A - B- Specified by:
sub
in interfaceDenseMatrix
- Returns:
- the result matrix
-
mul
public JMatrix mul(DenseMatrix b)
Description copied from interface:DenseMatrix
In place element-wise multiplication A = A * B- Specified by:
mul
in interfaceDenseMatrix
- Returns:
- this matrix
-
mul
public DenseMatrix mul(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrix
C = A * B- Specified by:
mul
in interfaceDenseMatrix
- Returns:
- the result matrix
-
div
public JMatrix div(DenseMatrix b)
Description copied from interface:DenseMatrix
In place element-wise division A = A / B A = A - B- Specified by:
div
in interfaceDenseMatrix
- Returns:
- this matrix
-
div
public DenseMatrix div(DenseMatrix b, DenseMatrix c)
Description copied from interface:DenseMatrix
C = A / B- Specified by:
div
in interfaceDenseMatrix
- Returns:
- the result matrix
-
add
public JMatrix add(double x)
Description copied from interface:DenseMatrix
In place element-wise addition A = A + x- Specified by:
add
in interfaceDenseMatrix
-
add
public DenseMatrix add(double x, DenseMatrix c)
Description copied from interface:DenseMatrix
Element-wise addition C = A + x- Specified by:
add
in interfaceDenseMatrix
-
sub
public JMatrix sub(double x)
Description copied from interface:DenseMatrix
In place element-wise subtraction A = A - x- Specified by:
sub
in interfaceDenseMatrix
-
sub
public DenseMatrix sub(double x, DenseMatrix c)
Description copied from interface:DenseMatrix
Element-wise addition C = A - x- Specified by:
sub
in interfaceDenseMatrix
-
mul
public JMatrix mul(double x)
Description copied from interface:DenseMatrix
In place element-wise multiplication A = A * x- Specified by:
mul
in interfaceDenseMatrix
-
mul
public DenseMatrix mul(double x, DenseMatrix c)
Description copied from interface:DenseMatrix
Element-wise addition C = A * x- Specified by:
mul
in interfaceDenseMatrix
-
div
public JMatrix div(double x)
Description copied from interface:DenseMatrix
In place element-wise division A = A / x- Specified by:
div
in interfaceDenseMatrix
-
div
public DenseMatrix div(double x, DenseMatrix c)
Description copied from interface:DenseMatrix
Element-wise addition C = A / x- Specified by:
div
in interfaceDenseMatrix
-
replaceNaN
public JMatrix replaceNaN(double x)
Description copied from interface:DenseMatrix
Replaces NaN's with given value.- Specified by:
replaceNaN
in interfaceDenseMatrix
-
sum
public double sum()
Description copied from interface:DenseMatrix
Returns the sum of all elements in the matrix.- Specified by:
sum
in interfaceDenseMatrix
- Returns:
- the sum of all elements.
-
ata
public JMatrix ata()
Description copied from interface:Matrix
Returns A' * A- Specified by:
ata
in interfaceDenseMatrix
- Specified by:
ata
in interfaceMatrix
-
aat
public JMatrix aat()
Description copied from interface:Matrix
Returns A * A'- Specified by:
aat
in interfaceDenseMatrix
- Specified by:
aat
in interfaceMatrix
-
ax
public double[] ax(double[] x, double[] y)
Description copied from interface:Matrix
y = A * x
-
axpy
public double[] axpy(double[] x, double[] y)
Description copied from interface:Matrix
y = A * x + y
-
axpy
public double[] axpy(double[] x, double[] y, double b)
Description copied from interface:Matrix
y = A * x + b * y
-
atx
public double[] atx(double[] x, double[] y)
Description copied from interface:Matrix
y = A' * x
-
atxpy
public double[] atxpy(double[] x, double[] y)
Description copied from interface:Matrix
y = A' * x + y
-
atxpy
public double[] atxpy(double[] x, double[] y, double b)
Description copied from interface:Matrix
y = A' * x + b * y
-
abmm
public JMatrix abmm(DenseMatrix B)
Description copied from interface:MatrixMultiplication
Returns the result of matrix multiplication A * B.- Specified by:
abmm
in interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
abtmm
public JMatrix abtmm(DenseMatrix B)
Description copied from interface:MatrixMultiplication
Returns the result of matrix multiplication A * B'.- Specified by:
abtmm
in interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
atbmm
public JMatrix atbmm(DenseMatrix B)
Description copied from interface:MatrixMultiplication
Returns the result of matrix multiplication A' * B.- Specified by:
atbmm
in interfaceMatrixMultiplication<DenseMatrix,DenseMatrix>
-
lu
public LU lu()
LU decomposition is computed by a "left-looking", dot-product, Crout/Doolittle algorithm.- Specified by:
lu
in 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:
cholesky
in 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:
qr
in interfaceDenseMatrix
-
svd
public SVD svd()
Description copied from interface:DenseMatrix
Returns the singular value decomposition. Note that the input matrix will hold U on output.- Specified by:
svd
in interfaceDenseMatrix
-
eig
public double[] eig()
Description copied from interface:DenseMatrix
Returns 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:
eig
in interfaceDenseMatrix
-
eigen
public EVD eigen()
Description copied from interface:DenseMatrix
Returns the eigen value decomposition. Note that the input matrix will be overwritten on output.- Specified by:
eigen
in 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.
-
-