Package smile.math.matrix.fp32
Class Matrix
java.lang.Object
smile.math.matrix.fp32.IMatrix
smile.math.matrix.fp32.Matrix
- All Implemented Interfaces:
Serializable
,Cloneable
Dense matrix.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The Cholesky decomposition of a symmetric, positive-definite matrix.static class
Eigenvalue decomposition.static class
The LU decomposition.static class
The QR decomposition.static class
Singular Value Decomposition.Nested classes/interfaces inherited from class smile.math.matrix.fp32.IMatrix
IMatrix.Preconditioner
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaat()
ReturnsA * A'
.static Matrix
ReturnsA * D * B
, where D is a diagonal matrix.add
(float b) A += badd
(float alpha, float[] x, float[] 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 * Bfloat
add
(int i, int j, float b) A[i,j] += bElement-wise addition A += BElement-wise addition A = alpha * A + beta * B^2addDiag
(float b) A[i, i] += baddDiag
(float[] 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.clone()
Returns a deep copy of matrix.float[]
col
(int j) Returns the j-th column.float[]
colMeans()
Returns the mean of each column.cols
(int... cols) Returns the matrix of selected columns.float[]
colSds()
Returns the standard deviations of each column.float[]
colSums()
Returns the sum of each column.static Matrix
column
(double[] A) Returns a column vector/matrix.static Matrix
column
(double[] A, int offset, int length) Returns a column vector/matrix.static Matrix
column
(float[] A) Returns a column vector/matrix.static Matrix
column
(float[] A, int offset, int length) Returns a column vector/matrix.static Matrix
diag
(float[] diag) Returns a square diagonal matrix.static Matrix
diag
(int n, float diag) Returns a square diagonal matrix.static Matrix
diag
(int m, int n, float diag) Returns an m-by-n diagonal matrix.div
(float b) A /= bfloat
div
(int i, int j, float b) A[i,j] /= bElement-wise division A /= Beigen()
Eigenvalue Decomposition.eigen
(boolean vl, boolean vr, boolean overwrite) Eigenvalue Decomposition.boolean
boolean
Returns true if two matrices equal in given precision.static Matrix
eye
(int n) Returns an identity matrix.static Matrix
eye
(int m, int n) Returns an m-by-n identity matrix.void
fill
(float x) Fills the matrix with a value.get
(int[] rows, int[] cols) Returns the matrix of selected rows and columns.float
get
(int i, int j) ReturnsA[i,j]
.protected int
index
(int i, int j) Returns the linearized index of matrix element.inverse()
Returns the inverse of matrix.boolean
Return true if the matrix is symmetric (uplo != null && diag == null
).layout()
Returns the matrix layout.int
ld()
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
(float b) A *= bfloat
mul
(int i, int j, float b) A[i,j] *= bElement-wise multiplication A *= Bvoid
mv
(float[] work, int inputOffset, int outputOffset) Matrix-vector multiplicationA * x
.void
Matrix-vector multiplication.int
ncol()
Returns the number of columns.float
norm()
L2 matrix norm that is the maximum singular value.float
norm1()
L1 matrix norm that is the maximum of column sums.float
norm2()
L2 matrix norm that is the maximum singular value.float
normFro()
Frobenius matrix norm that is the square root of sum of squares of all elements.float
normInf()
L∞ matrix norm that is the maximum of row sums.int
nrow()
Returns the number of rows.static Matrix
of
(double[][] A) Returns a matrix from a two-dimensional array.static Matrix
of
(float[][] A) Returns a matrix from a two-dimensional array.qr()
QR Decomposition.qr
(boolean overwrite) QR Decomposition.static Matrix
rand
(int m, int n) Returns a uniformly distributed random matrix in [0, 1).static Matrix
rand
(int m, int n, float lo, float hi) Returns a uniformly distributed random matrix in given range.static Matrix
rand
(int m, int n, Distribution distribution) Returns a random matrix.static Matrix
randn
(int m, int n) Returns a random matrix of standard normal distribution.replaceNaN
(float x) Replaces NaN's with given value.static Matrix
row
(double[] A) Returns a row vector/matrix.static Matrix
row
(double[] A, int offset, int length) Returns a row vector/matrix.static Matrix
row
(float[] A) Returns a row vector/matrix.static Matrix
row
(float[] A, int offset, int length) Returns a row vector/matrix.float[]
row
(int i) Returns the i-th row.float[]
rowMeans()
Returns the mean of each row.rows
(int... rows) Returns the matrix of selected rows.float[]
rowSds()
Returns the standard deviations of each row.float[]
rowSums()
Returns the sum of each row.scale
(float[] center, float[] scale) Centers and scales the columns of matrix.void
set
(int i, int j, float x) SetsA[i,j] = x
.Sets the matrix value.long
size()
Returns the number of stored matrix elements.Standardizes the columns of matrix.sub
(float b) A -= bfloat
sub
(int i, int j, float 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).float
sum()
Returns the sum of all elements.svd()
Singular Value Decomposition.svd
(boolean vectors, boolean overwrite) Singular Value Decomposition.Returns matrix multiplicationA' * B
.float[][]
toArray()
Return the two-dimensional array of matrix.static Matrix
toeplitz
(float[] a) Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.static Matrix
toeplitz
(float[] kl, float[] 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'
.void
tv
(float[] work, int inputOffset, int outputOffset) Matrix-vector multiplicationA' * x
.uplo()
Gets the format of packed matrix.Sets the format of packed matrix.float
xAx
(float[] x) Returns the quadratic formx' * A * x
.
-
Constructor Details
-
Matrix
public Matrix(int m, int n) Constructor of zero matrix.- Parameters:
m
- the number of rows.n
- the number of columns.
-
Matrix
public Matrix(int m, int n, float a) Constructor. Fills the matrix with given value.- Parameters:
m
- the number of rows.n
- the number of columns.a
- the initial value.
-
Matrix
public Matrix(int m, int n, int ld, float[] A) Constructor.- Parameters:
m
- the number of rows.n
- the number of columns.ld
- the leading dimension.A
- the matrix storage.
-
-
Method Details
-
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.
-
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.
-
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.
-
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 numbers.- Returns:
- the random 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 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 given range.- 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.
-
eye
Returns an identity matrix.- Parameters:
n
- the number of rows/columns.- Returns:
- the identity matrix.
-
eye
Returns an m-by-n identity matrix.- Parameters:
m
- the number of rows.n
- the number of columns.- Returns:
- the identity matrix.
-
diag
Returns a square diagonal matrix.- Parameters:
n
- the number of rows/columns.diag
- the diagonal value.- Returns:
- the diagonal 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 diagonal matrix.
-
diag
Returns a square diagonal matrix.- Parameters:
diag
- the diagonal elements.- Returns:
- the diagonal 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 > j
ku
-A[i, j] = ku[j - i]
fori <= j
- Returns:
- the Toeplitz matrix.
-
nrow
public int nrow()Description copied from class:IMatrix
Returns the number of rows. -
ncol
public int ncol()Description copied from class:IMatrix
Returns the number of columns. -
size
public long size()Description copied from class:IMatrix
Returns 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.
-
clone
Returns a deep copy of matrix. -
toArray
public float[][] 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 int 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 float get(int i, int j) Description copied from class:IMatrix
ReturnsA[i,j]
. -
set
public void set(int i, int j, float x) Description copied from class:IMatrix
SetsA[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 float[] 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 float[] 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.
-
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.
-
cols
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).- 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(float x) Fills 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 float add(int i, int j, float b) A[i,j] += b- Parameters:
i
- the row index.j
- the column index.b
- the operand.- Returns:
- the updated cell value.
-
sub
public float sub(int i, int j, float b) A[i,j] -= b- Parameters:
i
- the row index.j
- the column index.b
- the operand.- Returns:
- the updated cell value.
-
mul
public float mul(int i, int j, float b) A[i,j] *= b- Parameters:
i
- the row index.j
- the column index.b
- the operand.- Returns:
- the updated cell value.
-
div
public float div(int i, int j, float 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 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
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
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 float sum()Returns the sum of all elements.- Returns:
- the sum of all elements.
-
norm1
public float norm1()L1 matrix norm that is the maximum of column sums.- Returns:
- L1 matrix norm.
-
norm2
public float norm2()L2 matrix norm that is the maximum singular value.- Returns:
- L2 matrix norm.
-
norm
public float norm()L2 matrix norm that is the maximum singular value.- Returns:
- L2 matrix norm.
-
normInf
public float normInf()L∞ matrix norm that is the maximum of row sums.- Returns:
- L∞ matrix norm.
-
normFro
public float normFro()Frobenius matrix norm that is the square root of sum of squares of all elements.- Returns:
- Frobenius matrix norm.
-
xAx
public float xAx(float[] 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 float[] rowSums()Returns the sum of each row.- Returns:
- the sum of each row.
-
rowMeans
public float[] rowMeans()Returns the mean of each row.- Returns:
- the mean of each row.
-
rowSds
public float[] rowSds()Returns the standard deviations of each row.- Returns:
- the standard deviations of each row.
-
colSums
public float[] colSums()Returns the sum of each column.- Returns:
- the sum of each column.
-
colMeans
public float[] colMeans()Returns the mean of each column.- Returns:
- the mean of each column.
-
colSds
public float[] 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:IMatrix
Matrix-vector multiplication.
where op is the transpose operation.y = alpha * op(A) * x + beta * y
-
mv
public void mv(float[] work, int inputOffset, int outputOffset) Description copied from class:IMatrix
Matrix-vector multiplicationA * x
. -
tv
public void tv(float[] work, int inputOffset, int outputOffset) Description copied from class:IMatrix
Matrix-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
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
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
eigen
does not always return the eigenvalues and eigenvectors in sorted order. Use theEVD.sort
function 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
eigen
does not always return the eigenvalues and eigenvectors in sorted order. Use thesort
function 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.
-