Package smile.math.matrix
Class BigMatrix
java.lang.Object
smile.math.matrix.IMatrix
smile.math.matrix.BigMatrix
- All Implemented Interfaces:
Serializable,AutoCloseable
Big dense matrix of double precision values for more than
2 billion elements.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe Cholesky decomposition of a symmetric, positive-definite matrix.static classEigenvalue decomposition.static classThe LU decomposition.static classThe QR decomposition.static classSingular Value Decomposition.Nested classes/interfaces inherited from class smile.math.matrix.IMatrix
IMatrix.Preconditioner -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaat()ReturnsA * A'.static BigMatrixReturnsA * D * B, where D is a diagonal matrix.add(double b) A += badd(double alpha, double[] x, double[] y) Rank-1 update A += alpha * x * y'Element-wise addition A = alpha * A + beta * BElement-wise addition A += beta * BElement-wise addition C = alpha * A + beta * Bdoubleadd(int i, int j, double b) A[i,j] += bElement-wise addition A += BElement-wise addition A = alpha * A + beta * B^2addDiag(double b) A[i, i] += baddDiag(double[] b) A[i, i] += b[i]ata()ReturnsA' * A.cholesky()Cholesky decomposition for symmetric and positive definite matrix.cholesky(boolean overwrite) Cholesky decomposition for symmetric and positive definite matrix.voidclose()double[]col(int j) Returns the j-th column.col(int... cols) Returns the matrix of selected columns.double[]colMeans()Returns the mean of each column.double[]colSds()Returns the standard deviations of each column.double[]colSums()Returns the sum of each column.static BigMatrixcolumn(double[] A) Returns a column vector/matrix.static BigMatrixcolumn(double[] A, int offset, int length) Returns a column vector/matrix.copy()Returns a deep copy of matrix.static BigMatrixdiag(double[] diag) Returns a square diagonal matrix.static BigMatrixdiag(int n, double diag) Returns a square diagonal matrix.static BigMatrixdiag(int m, int n, double diag) Returns an m-by-n diagonal matrix.static BigMatrixdiag(org.bytedeco.javacpp.DoublePointer diag) Returns a square diagonal matrix.div(double b) A /= bdoublediv(int i, int j, double b) A[i,j] /= bElement-wise division A /= Beigen()Eigenvalue Decomposition.eigen(boolean vl, boolean vr, boolean overwrite) Eigenvalue Decomposition.booleanbooleanReturns true if two matrices equal in given precision.static BigMatrixeye(int n) Returns an identity matrix.static BigMatrixeye(int m, int n) Returns an m-by-n identity matrix.voidfill(double x) Fill the matrix with a value.get(int[] rows, int[] cols) Returns the matrix of selected rows and columns.doubleget(int i, int j) ReturnsA[i,j].protected longindex(int i, int j) Returns the linearized index of matrix element.inverse()Returns the inverse of matrix.booleanReturn true if the matrix is symmetric (uplo != null && diag == null).layout()Returns the matrix layout.intld()Returns the leading dimension.lu()LU decomposition.lu(boolean overwrite) LU decomposition.Matrix-matrix multiplication.Matrix-matrix multiplication.Returns matrix multiplicationA * B.Returns matrix multiplicationA * B'.mul(double b) A *= bdoublemul(int i, int j, double b) A[i,j] *= bElement-wise multiplication A *= Bvoidmv(double[] work, int inputOffset, int outputOffset) Matrix-vector multiplicationA * x.voidMatrix-vector multiplication.intncol()Returns the number of columns.doublenorm()L2 matrix norm that is the maximum singular value.doublenorm1()L1 matrix norm that is the maximum of column sums.doublenorm2()L2 matrix norm that is the maximum singular value.doublenormFro()Frobenius matrix norm that is the square root of sum of squares of all elements.doublenormInf()L∞ matrix norm that is the maximum of row sums.intnrow()Returns the number of rows.static BigMatrixof(double[][] A) Returns a matrix from a two-dimensional array.qr()QR Decomposition.qr(boolean overwrite) QR Decomposition.static BigMatrixrand(int m, int n) Returns a uniformly distributed random matrix in [0, 1).static BigMatrixrand(int m, int n, double lo, double hi) Returns a random matrix of uniform distribution.static BigMatrixrand(int m, int n, Distribution distribution) Returns a random matrix.static BigMatrixrandn(int m, int n) Returns a random matrix of standard normal distribution.replaceNaN(double x) Replaces NaN's with given value.static BigMatrixrow(double[] A) Returns a row vector/matrix.static BigMatrixrow(double[] A, int offset, int length) Returns a row vector/matrix.double[]row(int i) Returns the i-th row.row(int... rows) Returns the matrix of selected rows.double[]rowMeans()Returns the mean of each row.double[]rowSds()Returns the standard deviations of each row.double[]rowSums()Returns the sum of each row.scale(double[] center, double[] scale) Centers and scales the columns of matrix.voidset(int i, int j, double x) SetsA[i,j] = x.Sets the matrix value.longsize()Returns the number of stored matrix elements.Standardizes the columns of matrix.sub(double b) A -= bdoublesub(int i, int j, double b) A[i,j] -= bElement-wise subtraction A -= Bsubmatrix(int i, int j, int k, int l) Returns the submatrix which top left at (i, j) and bottom right at (k, l).doublesum()Returns the sum of all elements.svd()Singular Value Decomposition.svd(boolean vectors, boolean overwrite) Singular Value Decomposition.Returns matrix multiplicationA' * B.double[][]toArray()Return the two-dimensional array of matrix.static BigMatrixtoeplitz(double[] a) Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.static BigMatrixtoeplitz(double[] kl, double[] ku) Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.Returns the transpose of matrix.transpose(boolean share) Returns the transpose of matrix.Gets the flag if a triangular matrix has unit diagonal elements.triangular(Diag diag) Sets/unsets if the matrix is triangular.Returns matrix multiplicationA' * B'.voidtv(double[] work, int inputOffset, int outputOffset) Matrix-vector multiplicationA' * x.uplo()Gets the format of packed matrix.Sets the format of packed matrix.doublexAx(double[] x) Returns the quadratic formx' * A * x.
-
Constructor Details
-
BigMatrix
public BigMatrix(int m, int n) Constructor of zero matrix.- Parameters:
m- the number of rows.n- the number of columns.
-
BigMatrix
public BigMatrix(int m, int n, double a) Constructor. Fills the matrix with given value.- Parameters:
m- the number of rows.n- the number of columns.a- the initial value.
-
BigMatrix
public BigMatrix(int m, int n, int ld, org.bytedeco.javacpp.DoublePointer A) Constructor.- Parameters:
m- the number of rows.n- the number of columns.ld- the leading dimension.A- the matrix storage.
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
of
Returns a matrix from a two-dimensional array.- Parameters:
A- the two-dimensional array.- Returns:
- the matrix.
-
column
Returns a column vector/matrix.- Parameters:
A- the column vector.- Returns:
- the column vector/matrix.
-
column
Returns a column vector/matrix.- Parameters:
A- the column vector.offset- the offset of the subarray to be used; must be non-negative and no larger than array.length.length- the length of the subarray to be used; must be non-negative and no larger than array.length - offset.- Returns:
- the column vector/matrix.
-
row
Returns a row vector/matrix.- Parameters:
A- the row vector.- Returns:
- the row vector/matrix.
-
row
Returns a row vector/matrix.- Parameters:
A- the row vector.offset- the offset of the subarray to be used; must be non-negative and no larger than array.length.length- the length of the subarray to be used; must be non-negative and no larger than array.length - offset.- Returns:
- the row vector/matrix.
-
rand
Returns a random matrix.- Parameters:
m- the number of rows.n- the number of columns.distribution- the distribution of random number.- Returns:
- the matrix.
-
randn
Returns a random matrix of standard normal distribution.- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- the 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 random matrix of uniform distribution.- 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 matrix.
-
eye
Returns an identity matrix.- Parameters:
n- the number of rows/columns.- Returns:
- the matrix.
-
eye
Returns an m-by-n identity matrix.- Parameters:
m- the number of rows.n- the number of columns.- Returns:
- the matrix.
-
diag
Returns a square diagonal matrix.- Parameters:
n- the number of rows/columns.diag- the diagonal value.- Returns:
- the matrix.
-
diag
Returns an m-by-n diagonal matrix.- Parameters:
m- the number of rows.n- the number of columns.diag- the diagonal value.- Returns:
- the matrix.
-
diag
Returns a square diagonal matrix.- Parameters:
diag- the diagonal elements.- Returns:
- the matrix.
-
diag
Returns a square diagonal matrix.- Parameters:
diag- the diagonal elements.- Returns:
- the 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 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 matrix.
-
nrow
public int nrow()Description copied from class:IMatrixReturns the number of rows. -
ncol
public int ncol()Description copied from class:IMatrixReturns the number of columns. -
size
public long size()Description copied from class:IMatrixReturns the number of stored matrix elements. For conventional matrix, it is simply nrow * ncol. But it is usually much less for band, packed or sparse matrix. -
layout
Returns the matrix layout.- Returns:
- the matrix layout.
-
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.
-
uplo
Sets the format of packed matrix.- Parameters:
uplo- the format of packed matrix.- Returns:
- this matrix.
-
uplo
Gets the format of packed matrix.- Returns:
- the format of packed matrix.
-
triangular
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.
-
triangular
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.
-
copy
Description copied from class:IMatrixReturns a deep copy of matrix. -
toArray
public double[][] toArray()Return the two-dimensional array of matrix.- Returns:
- the two-dimensional array of matrix.
-
set
Sets the matrix value. If the matrices have the same layout, this matrix will share the underlying storage with b.- Parameters:
b- the right hand side of assignment.- Returns:
- this matrix.
-
index
protected long index(int i, int j) Returns the linearized index of matrix element.- Parameters:
i- the row index.j- the column index.- Returns:
- the linearized index.
-
get
public double get(int i, int j) Description copied from class:IMatrixReturnsA[i,j]. -
set
public void set(int i, int j, double x) Description copied from class:IMatrixSetsA[i,j] = x. -
get
Returns the matrix of selected rows and columns. Negative index -i means the i-th row/column from the end.- Parameters:
rows- the row indices.cols- the column indices.- Returns:
- the submatrix.
-
row
public double[] row(int i) Returns the i-th row. Negative index -i means the i-th row from the end.- Parameters:
i- the row index.- Returns:
- the row.
-
col
public double[] col(int j) Returns the j-th column. Negative index -j means the j-th row from the end.- Parameters:
j- the column index.- Returns:
- the column.
-
row
Returns the matrix of selected rows.- Parameters:
rows- the row indices.- Returns:
- the submatrix.
-
col
Returns the matrix of selected columns.- Parameters:
cols- the column indices.- Returns:
- the submatrix.
-
submatrix
Returns the submatrix which top left at (i, j) and bottom right at (k, l). The content of the submatrix will be that of this matrix. Changes to this matrix's content will be visible in the submatrix, and vice versa.- Parameters:
i- the beginning row, inclusive.j- the beginning column, inclusive,k- the ending row, inclusive.l- the ending column, inclusive.- Returns:
- the submatrix.
-
fill
public void fill(double x) Fill the matrix with a value.- Parameters:
x- the value.
-
transpose
Returns the transpose of matrix. The transpose shares the storage with this matrix. Changes to this matrix's content will be visible in the transpose, and vice versa.- Returns:
- the transpose of matrix.
-
transpose
Returns the transpose of matrix.- Parameters:
share- if true, the transpose shares the storage with this matrix. Changes to this matrix's content will be visible in the transpose, and vice versa.- Returns:
- the transpose of matrix.
-
equals
-
equals
Returns true if two matrices equal in given precision.- Parameters:
o- the other matrix.epsilon- a number close to zero.- Returns:
- true if two matrices equal in given precision.
-
add
public double add(int i, int j, double b) A[i,j] += b- Parameters:
i- the row index.j- the column index.b- the operand.- Returns:
- the updated cell value.
-
sub
public double sub(int i, int j, double b) A[i,j] -= b- Parameters:
i- the row index.j- the column index.b- the operand.- Returns:
- the updated cell value.
-
mul
public double mul(int i, int j, double b) A[i,j] *= b- Parameters:
i- the row index.j- the column index.b- the operand.- Returns:
- the updated cell value.
-
div
public double div(int i, int j, double b) A[i,j] /= b- Parameters:
i- the row index.j- the column index.b- the operand.- Returns:
- the updated cell value.
-
addDiag
A[i, i] += b- Parameters:
b- the operand.- Returns:
- this matrix.
-
addDiag
A[i, i] += b[i]- Parameters:
b- the operand.- Returns:
- this matrix.
-
add
A += b- Parameters:
b- the operand.- Returns:
- this matrix.
-
sub
A -= b- Parameters:
b- the operand.- Returns:
- this matrix.
-
mul
A *= b- Parameters:
b- the operand.- Returns:
- this matrix.
-
div
A /= b- Parameters:
b- the operand.- Returns:
- this matrix.
-
add
Element-wise addition A += B- Parameters:
B- the operand.- Returns:
- this matrix.
-
sub
Element-wise subtraction A -= B- Parameters:
B- the operand.- Returns:
- this matrix.
-
mul
Element-wise multiplication A *= B- Parameters:
B- the operand.- Returns:
- this matrix.
-
div
Element-wise division A /= B- Parameters:
B- the operand.- Returns:
- this matrix.
-
add
Element-wise addition A += beta * B- Parameters:
beta- the scalar alpha.B- the operand.- Returns:
- this matrix.
-
add
Element-wise addition C = alpha * A + beta * B- Parameters:
alpha- the scalar alpha.A- the operand.beta- the scalar beta.B- the operand.- Returns:
- this matrix.
-
add
Element-wise addition A = alpha * A + beta * B- Parameters:
alpha- the scalar alpha.beta- the scalar beta.B- the operand.- Returns:
- this matrix.
-
add2
Element-wise addition A = alpha * A + beta * B^2- Parameters:
alpha- the scalar alpha.beta- the scalar beta.B- the operand.- Returns:
- this matrix.
-
add
Rank-1 update A += alpha * x * y'- Parameters:
alpha- the scalar alpha.x- the column vector.y- the row vector.- Returns:
- this matrix.
-
replaceNaN
Replaces NaN's with given value.- Parameters:
x- a real number.- Returns:
- this matrix.
-
sum
public double sum()Returns the sum of all elements.- Returns:
- the sum of all elements.
-
norm1
public double norm1()L1 matrix norm that is the maximum of column sums.- Returns:
- L1 matrix norm.
-
norm2
public double norm2()L2 matrix norm that is the maximum singular value.- Returns:
- L2 matrix norm.
-
norm
public double norm()L2 matrix norm that is the maximum singular value.- Returns:
- L2 matrix norm.
-
normInf
public double normInf()L∞ matrix norm that is the maximum of row sums.- Returns:
- L∞ matrix norm.
-
normFro
public double normFro()Frobenius matrix norm that is the square root of sum of squares of all elements.- Returns:
- Frobenius matrix norm.
-
xAx
public double xAx(double[] x) Returns the quadratic formx' * A * x. The left upper submatrix of A is used in the computation based on the size of x.- Parameters:
x- the vector.- Returns:
- the quadratic form.
-
rowSums
public double[] rowSums()Returns the sum of each row.- Returns:
- the sum of each row.
-
rowMeans
public double[] rowMeans()Returns the mean of each row.- Returns:
- the mean of each row.
-
rowSds
public double[] rowSds()Returns the standard deviations of each row.- Returns:
- the standard deviations of each row.
-
colSums
public double[] colSums()Returns the sum of each column.- Returns:
- the sum of each column.
-
colMeans
public double[] colMeans()Returns the mean of each column.- Returns:
- the mean of each column.
-
colSds
public double[] 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.
-
scale
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 matrix with zero mean and unit variance for each column.
-
inverse
Returns the inverse of matrix.- Returns:
- the inverse of matrix.
-
mv
Description copied from class:IMatrixMatrix-vector multiplication.
where op is the transpose operation.y = alpha * op(A) * x + beta * y -
mv
public void mv(double[] work, int inputOffset, int outputOffset) Description copied from class:IMatrixMatrix-vector multiplicationA * x. -
tv
public void tv(double[] work, int inputOffset, int outputOffset) Description copied from class:IMatrixMatrix-vector multiplicationA' * x. -
mm
Matrix-matrix multiplication.C := A*B- Parameters:
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.- Returns:
- this matrix.
-
mm
public BigMatrix mm(Transpose transA, BigMatrix A, Transpose transB, BigMatrix B, double alpha, double beta) Matrix-matrix multiplication.C := alpha*A*B + beta*C- Parameters:
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.alpha- the scalar alpha.beta- the scalar beta.- Returns:
- this matrix.
-
ata
ReturnsA' * A.- Returns:
A' * A.
-
aat
ReturnsA * A'.- Returns:
A * A'.
-
adb
public static BigMatrix adb(Transpose transA, BigMatrix A, double[] D, Transpose transB, BigMatrix B) ReturnsA * D * B, where D is a diagonal matrix.- Parameters:
transA- normal, transpose, or conjugate transpose operation on the matrix A.A- the operand.D- the diagonal matrix.transB- normal, transpose, or conjugate transpose operation on the matrix B.B- the operand.- Returns:
- the multiplication.
-
mm
Returns matrix multiplicationA * B.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
mt
Returns matrix multiplicationA * B'.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
tm
Returns matrix multiplicationA' * B.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
tt
Returns matrix multiplicationA' * B'.- Parameters:
B- the operand.- Returns:
- the multiplication.
-
lu
LU decomposition.- Returns:
- LU decomposition.
-
lu
LU decomposition.- Parameters:
overwrite- The flag if the decomposition overwrites this matrix.- Returns:
- LU decomposition.
-
cholesky
Cholesky decomposition for symmetric and positive definite matrix.- Returns:
- Cholesky decomposition.
- Throws:
ArithmeticException- if the matrix is not positive definite.
-
cholesky
Cholesky decomposition for symmetric and positive definite matrix.- Parameters:
overwrite- The flag if the decomposition overwrites this matrix.- Returns:
- Cholesky decomposition.
- Throws:
ArithmeticException- if the matrix is not positive definite.
-
qr
QR Decomposition.- Returns:
- QR decomposition.
-
qr
QR Decomposition.- Parameters:
overwrite- The flag if the decomposition overwrites this matrix.- Returns:
- QR decomposition.
-
svd
Singular Value Decomposition. 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. 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 if computing the singular vectors.overwrite- The flag if the decomposition overwrites this matrix.- Returns:
- singular value decomposition.
-
eigen
Eigenvalue Decomposition. 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. 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.overwrite- The flag if the decomposition overwrites this matrix.- Returns:
- eigen value decomposition.
-