Class DenseMatrix
java.lang.Object
smile.tensor.DenseMatrix
- All Implemented Interfaces:
Serializable, Matrix, Tensor
- Direct Known Subclasses:
Vector
A dense matrix is a matrix where a large proportion of its elements
are non-zero. This class provides a skeletal implementation of the
Matrix interface.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaat()ReturnsA * A'.voidadd(double alpha, DenseMatrix A, double beta, DenseMatrix B) Adds two matricesadd(DenseMatrix B) Adds two matricesata()ReturnsA' * A.axpy(double alpha, DenseMatrix x) Computes a constant alpha times a matrix x plus this matrix y.cholesky()Cholesky decomposition for symmetric and positive definite matrix.colMeans()Returns the mean of each column.String[]colNames()Returns the column names.colSds()Returns the standard deviations of each column.colSums()Returns the sum of each column.abstract Vectorcolumn(int j) Returns the j-th column.columns(int... cols) Returns the matrix of selected columns.columns(int from, int to) Returns the submatrix of selected columns.abstract DenseMatrixcopy()Returns a deep copy of matrix.diag()Gets the flag if a triangular matrix has unit diagonal elements.static DenseMatrixdiagflat(double[] diag) Returns the diagonal matrix with the elements of given array.static DenseMatrixdiagflat(float[] diag) Returns the diagonal matrix with the elements of given array.eigen()Right Eigenvalue Decomposition.eigen(boolean vl, boolean vr) Eigenvalue Decomposition.booleaneye(int n) Returns an identity matrix of the same scalar type as this matrix.eye(int m, int n) Returns an identity matrix of the same scalar type as this matrix.static DenseMatrixeye(ScalarType scalarType, int n) Returns an identity matrix.static DenseMatrixeye(ScalarType scalarType, int m, int n) Returns an identity matrix.abstract voidfill(double value) Assigns the specified value to each element of the specified vector.voidPerforms the rank-1 update operation.inverse()Returns the inverse of matrix.booleanReturn true if the matrix is symmetric (uplo != null && diag == null).intld()Returns the leading dimension.lu()LU decomposition.memory()Returns the memory storage of matrix.static voidmm(double alpha, Transpose transA, DenseMatrix A, Transpose transB, DenseMatrix B, double beta, DenseMatrix C) Matrix-matrix multiplication.mm(DenseMatrix B) Matrix multiplicationA * B.mt(DenseMatrix B) Matrix multiplicationA * B'.voidMatrix-vector multiplication.intncol()Returns the number of columns.intnrow()Returns the number of rows.static DenseMatrixof(double[][] A) Returns a matrix from a two-dimensional array.static DenseMatrixof(float[][] A) Returns a matrix from a two-dimensional array.order()Returns the matrix layout.qr()QR Decomposition.static DenseMatrixrand(ScalarType scalarType, int m, int n) Returns a uniformly distributed random matrix in [0, 1).static DenseMatrixrand(ScalarType scalarType, int m, int n, double lo, double hi) Returns a uniformly distributed random matrix in [lo, hi).static DenseMatrixrand(ScalarType scalarType, int m, int n, Distribution distribution) Returns a random matrix with given distribution.static DenseMatrixrandn(ScalarType scalarType, int m, int n) Returns a random matrix of standard normal distribution.row(int i) Returns the i-th row.rowMeans()Returns the mean of each row.String[]rowNames()Returns the row names.rows(int... rows) Returns the matrix of selected rows.rows(int from, int to) Returns the submatrix of selected rows.rowSums()Returns the sum of each row.scale(double alpha) A *= alphaStandardizes the columns of matrix.standardize(Vector center, Vector scale) Centers and scales the columns of matrix.sub(DenseMatrix B) Subtracts two matricessubmatrix(int i, int j, int k, int l) Returns the submatrix which top left at (i, j) and bottom right at (k, l).svd()Singular Value Decomposition.svd(boolean vectors) Singular Value Decomposition.tm(DenseMatrix B) Matrix multiplicationA' * B.static DenseMatrixtoeplitz(double[] a) Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.static DenseMatrixtoeplitz(double[] kl, double[] ku) Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.static DenseMatrixtoeplitz(float[] a) Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.static DenseMatrixtoeplitz(float[] kl, float[] ku) Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.toString()Returns the transpose of matrix.uplo()Gets the format of packed matrix.withColNames(String[] names) Sets the column names.Sets/unsets if the matrix is triangular.withRowNames(String[] names) Sets the row names.Sets the format of packed matrix.zeros(int m, int n) Returns a zero matrix of the same scalar type as this matrix.static DenseMatrixzeros(ScalarType scalarType, int m, int n) Returns a zero matrix.Methods inherited from class Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Matrix
add, apply, diagonal, dim, div, get, get, length, mul, mv, mv, mv, mv, mv, reshape, set, set, shape, size, sub, toArray, toArray, toString, toString, trace, tv, tv, tv, tv, tv, update, vector, vector, vector, xAxMethods inherited from interface Tensor
scalarType
-
Method Details
-
toString
-
equals
-
copy
Description copied from interface:MatrixReturns a deep copy of matrix. -
transpose
Description copied from interface:MatrixReturns the transpose of matrix. The transpose may share the storage with this matrix. -
scale
Description copied from interface:MatrixA *= alpha -
nrow
-
ncol
-
order
-
memory
Returns the memory storage of matrix.- Returns:
- the memory storage of matrix.
-
ld
public int ld()Returns the leading dimension.- Returns:
- the leading dimension.
-
isSymmetric
public boolean isSymmetric()Return true if the matrix is symmetric (uplo != null && diag == null).- Returns:
- true if the matrix is symmetric.
-
withUplo
Sets the format of packed matrix.- Parameters:
uplo- the format of packed matrix.- Returns:
- this matrix.
-
uplo
-
withDiag
Sets/unsets if the matrix is triangular.- Parameters:
diag- if not null, it specifies if the triangular matrix has unit diagonal elements.- Returns:
- this matrix.
-
diag
Gets the flag if a triangular matrix has unit diagonal elements. Returns null if the matrix is not triangular.- Returns:
- the flag if a triangular matrix has unit diagonal elements.
-
rowNames
-
withRowNames
Sets the row names.- Parameters:
names- the row names.- Returns:
- this matrix.
-
colNames
-
withColNames
Sets the column names.- Parameters:
names- the column names.- Returns:
- this matrix.
-
fill
public abstract void fill(double value) Assigns the specified value to each element of the specified vector.- Parameters:
value- the value to be stored in all elements of the vector.
-
row
Returns the i-th row. Negative index -i means the i-th row from the end.- Parameters:
i- the row index.- Returns:
- the row.
-
column
Returns the j-th column. Negative index -j means the j-th row from the end.- Parameters:
j- the column index.- Returns:
- the column.
-
rows
Returns the matrix of selected rows. Negative index -i means the i-th row from the end.- Parameters:
rows- the row indices.- Returns:
- the submatrix.
-
columns
Returns the matrix of selected columns.- Parameters:
cols- the column indices.- Returns:
- the submatrix.
-
rows
Returns the submatrix of selected rows.- Parameters:
from- the beginning row, inclusive.to- the ending row, exclusive.- Returns:
- the submatrix.
-
columns
Returns the submatrix of selected columns.- Parameters:
from- the beginning column, inclusive.to- the ending column, exclusive.- Returns:
- the submatrix.
-
submatrix
Returns the submatrix which top left at (i, j) and bottom right at (k, l).- Parameters:
i- the beginning row, inclusive.j- the beginning column, inclusive,k- the ending row, exclusive.l- the ending column, exclusive.- Returns:
- the submatrix.
-
colSums
-
rowSums
-
colMeans
-
rowMeans
-
colSds
Returns the standard deviations of each column.- Returns:
- the standard deviations of each column.
-
standardize
Standardizes the columns of matrix.- Returns:
- a new matrix with zero mean and unit variance for each column.
-
standardize
Centers and scales the columns of matrix.- Parameters:
center- column center. If null, no centering.scale- column scale. If null, no scaling.- Returns:
- a new normalized matrix.
-
add
Adds two matricesC = alpha * A + beta * B- Parameters:
alpha- the scalar alpha.A- the input matrix.beta- the scalar beta.B- the input matrix.
-
axpy
Computes a constant alpha times a matrix x plus this matrix y. The result overwrites this matrix y.- Parameters:
alpha- Ifalpha = 0this routine returns without any computation.x- Input matrix.- Returns:
- this matrix.
-
add
Adds two matricesA += B- Parameters:
B- the input matrix.- Returns:
- this matrix.
-
sub
Subtracts two matricesA -= B- Parameters:
B- the input matrix.- Returns:
- this matrix.
-
mv
Description copied from interface:MatrixMatrix-vector multiplication.y = alpha * A * x + beta * y -
mm
public static void mm(double alpha, Transpose transA, DenseMatrix A, Transpose transB, DenseMatrix B, double beta, DenseMatrix C) Matrix-matrix multiplication.C := alpha*A*B + beta*C- Parameters:
alpha- the scalar alpha.transA- normal, transpose, or conjugate transpose operation on the matrix A.A- the operand.transB- normal, transpose, or conjugate transpose operation on the matrix B.B- the operand.beta- the scalar beta.C- the operand.
-
mm
Matrix multiplicationA * B.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
tm
Matrix multiplicationA' * B.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
mt
Matrix multiplicationA * B'.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
ata
-
aat
-
ger
-
inverse
-
lu
LU decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.- Returns:
- LU decomposition.
-
cholesky
Cholesky decomposition for symmetric and positive definite matrix. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.- Returns:
- Cholesky decomposition.
- Throws:
ArithmeticException- if the matrix is not positive definite.
-
qr
QR Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.- Returns:
- QR decomposition.
-
svd
Singular Value Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. Returns a compact SVD of m-by-n matrix A:m > n— Only the first n columns of U are computed, and S is n-by-n.m = n— Equivalent to full SVD.m < n— Only the first m columns of V are computed, and S is m-by-m.
- Returns:
- singular value decomposition.
-
svd
Singular Value Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. Returns a compact SVD of m-by-n matrix A:m > n— Only the first n columns of U are computed, and S is n-by-n.m = n— Equivalent to full SVD.m < n— Only the first m columns of V are computed, and S is m-by-m.
- Parameters:
vectors- The flag whether computing the singular vectors.- Returns:
- singular value decomposition.
-
eigen
Right Eigenvalue Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. For a symmetric matrix, all eigenvalues are real values. Otherwise, the eigenvalues may be complex numbers.By default
eigendoes not always return the eigenvalues and eigenvectors in sorted order. Use theEVD.sortfunction to put the eigenvalues in descending order and reorder the corresponding eigenvectors.- Returns:
- eign value decomposition.
-
eigen
Eigenvalue Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. For a symmetric matrix, all eigenvalues are real values. Otherwise, the eigenvalues may be complex numbers.By default
eigendoes not always return the eigenvalues and eigenvectors in sorted order. Use thesortfunction to put the eigenvalues in descending order and reorder the corresponding eigenvectors.- Parameters:
vl- The flag if computing the left eigenvectors.vr- The flag if computing the right eigenvectors.- Returns:
- eigen value decomposition.
-
of
Returns a matrix from a two-dimensional array.- Parameters:
A- the two-dimensional array.- Returns:
- the matrix.
-
of
Returns a matrix from a two-dimensional array.- Parameters:
A- the two-dimensional array.- Returns:
- the matrix.
-
zeros
Returns a zero matrix.- Parameters:
scalarType- the scalar type.m- the number of rows.n- the number of columns.- Returns:
- a zero matrix.
-
zeros
Returns a zero matrix of the same scalar type as this matrix.- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- a zero matrix.
-
eye
Returns an identity matrix.- Parameters:
scalarType- the scalar type.n- the number of columns.- Returns:
- an identity matrix.
-
eye
Returns an identity matrix.- Parameters:
scalarType- the scalar type.m- the number of rows.n- the number of columns.- Returns:
- an identity matrix.
-
eye
Returns an identity matrix of the same scalar type as this matrix.- Parameters:
n- the number of columns.- Returns:
- an identity matrix.
-
eye
Returns an identity matrix of the same scalar type as this matrix.- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- an identity matrix.
-
diagflat
Returns the diagonal matrix with the elements of given array.- Parameters:
diag- the diagonal elements.- Returns:
- the diagonal matrix.
-
diagflat
Returns the diagonal matrix with the elements of given array.- Parameters:
diag- the diagonal elements.- Returns:
- the diagonal matrix.
-
randn
Returns a random matrix of standard normal distribution.- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- the random matrix.
-
rand
Returns a random matrix with given distribution.- Parameters:
m- the number of rows.n- the number of columns.distribution- the distribution of random numbers.- Returns:
- the random matrix.
-
rand
Returns a uniformly distributed random matrix in [0, 1).- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- the random matrix.
-
rand
Returns a uniformly distributed random matrix in [lo, hi).- Parameters:
m- the number of rows.n- the number of columns.lo- the lower bound of uniform distribution.hi- the upper bound of uniform distribution.- Returns:
- the random matrix.
-
toeplitz
Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.- Parameters:
a- A[i, j] = a[i - j] fori >= j(or a[j - i] whenj > i)- Returns:
- the Toeplitz matrix.
-
toeplitz
Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.- Parameters:
a- A[i, j] = a[i - j] fori >= j(or a[j - i] whenj > i)- Returns:
- the Toeplitz matrix.
-
toeplitz
Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.- Parameters:
kl-A[i, j] = kl[i - j]fori > jku-A[i, j] = ku[j - i]fori <= j- Returns:
- the Toeplitz matrix.
-
toeplitz
Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.- Parameters:
kl-A[i, j] = kl[i - j]fori > jku-A[i, j] = ku[j - i]fori <= j- Returns:
- the Toeplitz matrix.
-