public class Matrix extends DMatrix
Modifier and Type | Class and Description |
---|---|
static class |
Matrix.Cholesky
The Cholesky decomposition of a symmetric, positive-definite matrix.
|
static class |
Matrix.EVD
Eigenvalue decomposition.
|
static class |
Matrix.LU
The LU decomposition.
|
static class |
Matrix.QR
The QR decomposition.
|
static class |
Matrix.SVD
Singular Value Decomposition.
|
Constructor and Description |
---|
Matrix(double[] A)
Constructor of a column vector/matrix with given array as the internal storage.
|
Matrix(double[][] A)
Constructor.
|
Matrix(double[] A,
int offset,
int length)
Constructor of a column vector/matrix with given array as the internal storage.
|
Matrix(int m,
int n)
Constructor of zero matrix.
|
Matrix(int m,
int n,
double a)
Constructor.
|
Matrix(int m,
int n,
double[][] A)
Constructor.
|
Matrix(int m,
int n,
int ld,
java.nio.DoubleBuffer A)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Matrix |
aat()
Returns A * A'
|
Matrix |
adb(Transpose transA,
Transpose transB,
Matrix B,
double[] diag)
Returns A * D * B, where D is a diagonal matrix.
|
Matrix |
add(double b)
A += b
|
Matrix |
add(double alpha,
double[] x,
double[] y)
Rank-1 update A += alpha * x * y'
|
Matrix |
add(double alpha,
double beta,
Matrix B)
Element-wise addition A = alpha * A + beta * B
|
Matrix |
add(double alpha,
Matrix B)
Element-wise addition A += alpha * B
|
Matrix |
add(double alpha,
Matrix A,
double beta,
Matrix B)
Element-wise addition C = alpha * A + beta * B
|
double |
add(int i,
int j,
double b)
A[i,j] += b
|
double |
add(int i,
int j,
double alpha,
double beta)
A[i,j] = alpha * A[i,j] + beta
|
Matrix |
add(int i,
int j,
double alpha,
double beta,
Matrix B)
Element-wise submatrix addition A[i, j] = alpha * A[i, j] + beta * B
|
Matrix |
add(int i,
int j,
double alpha,
Matrix B)
Element-wise submatrix addition A[i, j] += alpha * B
|
Matrix |
add(Matrix B)
Element-wise addition A += B
|
Matrix |
ata()
Returns A' * A
|
Matrix.Cholesky |
cholesky()
Cholesky decomposition for symmetric and positive definite matrix.
|
Matrix.Cholesky |
cholesky(boolean overwrite)
Cholesky decomposition for symmetric and positive definite matrix.
|
Matrix |
clone()
Returns a deep copy of matrix.
|
Matrix |
col(int... cols)
Returns the matrix of selected columns.
|
double[] |
col(int j)
Returns the j-th column.
|
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 Matrix |
diag(double[] diag)
Returns a square diagonal matrix with the elements of vector
v on the main diagonal.
|
Matrix |
div(double b)
A /= b
|
Matrix |
div(double alpha,
Matrix B)
Element-wise division A /= alpha * B
|
Matrix |
div(double alpha,
Matrix A,
Matrix B)
Element-wise division C = alpha * A / B
|
double |
div(int i,
int j,
double b)
A[i,j] /= b
|
Matrix |
div(int i,
int j,
double alpha,
Matrix B)
Element-wise submatrix division A[i, j] /= alpha * B
|
Matrix |
div(Matrix B)
Element-wise division A /= B
|
Matrix.EVD |
eigen()
Eigenvalue Decomposition.
|
Matrix.EVD |
eigen(boolean vl,
boolean vr,
boolean overwrite)
Eigenvalue Decomposition.
|
boolean |
equals(Matrix o,
double eps)
Returns if two matrices equals given an error margin.
|
boolean |
equals(java.lang.Object o) |
static Matrix |
eye(int n)
Returns an n-by-n identity matrix.
|
static Matrix |
eye(int m,
int n)
Returns an m-by-n identity matrix.
|
void |
fill(double x)
Fill the matrix with a value.
|
double |
get(int i,
int j)
Returns A[i, j].
|
protected int |
index(int i,
int j)
Returns the linear index of matrix element.
|
Matrix |
inverse()
Returns the inverse matrix.
|
boolean |
isSubmatrix()
Returns if the matrix is a submatrix.
|
boolean |
isSymmetric()
Return if the matrix is symmetric (uplo != null && diag == null).
|
Layout |
layout()
Returns the matrix layout.
|
int |
ld()
Returns the leading dimension.
|
Matrix.LU |
lu()
LU decomposition.
|
Matrix.LU |
lu(boolean overwrite)
LU decomposition.
|
Matrix |
mm(Matrix B)
Returns matrix multiplication A * B.
|
void |
mm(Transpose transA,
Transpose transB,
double alpha,
Matrix B,
double beta,
Matrix C)
Matrix-matrix multiplication.
|
Matrix |
mt(Matrix B)
Returns matrix multiplication A * B'.
|
Matrix |
mul(double b)
A *= b
|
Matrix |
mul(double alpha,
Matrix B)
Element-wise multiplication A *= alpha * B
|
Matrix |
mul(double alpha,
Matrix A,
Matrix B)
Element-wise multiplication C = alpha * A * B
|
double |
mul(int i,
int j,
double b)
A[i,j] *= b
|
Matrix |
mul(int i,
int j,
double alpha,
Matrix B)
Element-wise submatrix multiplication A[i, j] *= alpha * B
|
Matrix |
mul(Matrix B)
Element-wise multiplication A *= B
|
void |
mv(double[] work,
int inputOffset,
int outputOffset)
Matrix-vector multiplication A * x.
|
void |
mv(Transpose trans,
double alpha,
double[] x,
double beta,
double[] y)
Matrix-vector multiplication.
|
void |
mv(Transpose trans,
double alpha,
java.nio.DoubleBuffer x,
double beta,
java.nio.DoubleBuffer y)
Matrix-vector multiplication.
|
int |
ncols()
Returns the number of columns.
|
double |
norm()
L2 matrix norm.
|
double |
norm1()
L1 matrix norm.
|
double |
norm2()
L2 matrix norm.
|
double |
normFro()
Frobenius matrix norm.
|
double |
normInf()
Infinity matrix norm.
|
int |
nrows()
Returns the number of rows.
|
static Matrix |
of(Layout layout,
int m,
int n)
Creates a matrix.
|
static Matrix |
of(Layout layout,
int m,
int n,
int ld,
java.nio.DoubleBuffer A)
Creates a matrix.
|
Matrix.QR |
qr()
QR Decomposition.
|
Matrix.QR |
qr(boolean overwrite)
QR Decomposition.
|
static Matrix |
rand(int m,
int n,
Distribution distribution)
Returns a random matrix.
|
static Matrix |
rand(int m,
int n,
double lo,
double hi)
Returns a random matrix of uniform distribution.
|
static Matrix |
randn(int m,
int n)
Returns a random matrix of standard normal distribution.
|
Matrix |
replaceNaN(double x)
Replaces NaN's with given value.
|
Matrix |
row(int... rows)
Returns the matrix of selected rows.
|
double[] |
row(int i)
Returns the i-th row.
|
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.
|
Matrix |
scale()
Centers and scales the columns of matrix.
|
Matrix |
scale(double[] center,
double[] scale)
Centers and scales the columns of matrix.
|
Matrix |
set(int i,
int j,
double x)
Sets A[i, j] = x.
|
Matrix |
set(int i,
int j,
Matrix B)
Sets submatrix A[i,j] = B.
|
long |
size()
Returns the number of stored matrix elements.
|
Matrix |
sub(double b)
A -= b
|
Matrix |
sub(double alpha,
Matrix B)
Element-wise subtraction A -= alpha * B
|
Matrix |
sub(double alpha,
Matrix A,
double beta,
Matrix B)
Element-wise subtraction C = alpha * A - beta * B
|
double |
sub(int i,
int j,
double b)
A[i,j] -= b
|
Matrix |
sub(int i,
int j,
double alpha,
Matrix B)
Element-wise submatrix subtraction A[i, j] -= alpha * B
|
Matrix |
sub(Matrix B)
Element-wise subtraction A -= B
|
Matrix |
submatrix(int i,
int j,
int k,
int l)
Returns the submatrix which top left at (i, j) and bottom right at (k, l).
|
double |
sum()
Returns the sum of all elements in the matrix.
|
Matrix.SVD |
svd()
Singular Value Decomposition.
|
Matrix.SVD |
svd(boolean vectors,
boolean overwrite)
Singular Value Decomposition.
|
Matrix |
tm(Matrix B)
Returns matrix multiplication A' * B.
|
double[][] |
toArray()
Return the two-dimensional array of matrix.
|
static Matrix |
toeplitz(double[] a)
Returns a symmetric Toeplitz matrix in which each descending diagonal
from left to right is constant.
|
static Matrix |
toeplitz(double[] kl,
double[] ku)
Returns a Toeplitz matrix in which each descending diagonal
from left to right is constant.
|
Matrix |
transpose()
Returns the transpose of matrix.
|
Diag |
triangular()
Gets the flag if a triangular matrix has unit diagonal elements.
|
Matrix |
triangular(Diag diag)
Sets/unsets if the matrix is triangular.
|
Matrix |
tt(Matrix B)
Returns matrix multiplication A' * B'.
|
void |
tv(double[] work,
int inputOffset,
int outputOffset)
Matrix-vector multiplication A' * x.
|
UPLO |
uplo()
Gets the format of packed matrix.
|
Matrix |
uplo(UPLO uplo)
Sets the format of packed matrix.
|
double |
xAx(double[] x)
Returns x' * A * x.
|
apply, diag, market, mv, mv, mv, trace, tv, tv, tv, update
public Matrix(int m, int n)
m
- the number of rows.n
- the number of columns.public Matrix(int m, int n, double a)
m
- the number of rows.n
- the number of columns.a
- the initial value.public Matrix(int m, int n, double[][] A)
m
- the number of rows.n
- the number of columns.A
- the array of matrix.public Matrix(double[][] A)
A
- the array of matrix.public Matrix(double[] A)
A
- The array of column vector.public Matrix(double[] A, int offset, int length)
A
- The array of 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.public Matrix(int m, int n, int ld, java.nio.DoubleBuffer A)
m
- the number of rows.n
- the number of columns.ld
- the leading dimension.A
- the matrix storage.public static Matrix of(Layout layout, int m, int n)
layout
- the matrix layout.m
- the number of rows.n
- the number of columns.public static Matrix of(Layout layout, int m, int n, int ld, java.nio.DoubleBuffer A)
layout
- the matrix layout.m
- the number of rows.n
- the number of columns.ld
- the leading dimension.A
- the matrix storage.public static Matrix eye(int n)
n
- the number of rows/columns.public static Matrix eye(int m, int n)
m
- the number of rows.n
- the number of columns.public static Matrix randn(int m, int n)
public static Matrix rand(int m, int n, Distribution distribution)
distribution
- the distribution of random number.public static Matrix rand(int m, int n, double lo, double hi)
lo
- the lower bound of uniform distribution.hi
- the upper bound of uniform distribution.public static Matrix diag(double[] diag)
diag
- the diagonal elements.public static Matrix toeplitz(double[] a)
a
- A[i, j] = a[i - j] for i >= j (or a[j - i] when j > i)public static Matrix toeplitz(double[] kl, double[] ku)
kl
- A[i, j] = kl[i - j] for i > jku
- A[i, j] = ku[j - i] for i <= jpublic int nrows()
IMatrix
public int ncols()
IMatrix
public long size()
IMatrix
public Layout layout()
public int ld()
public boolean isSubmatrix()
public boolean isSymmetric()
public UPLO uplo()
public Matrix triangular(Diag diag)
diag
- if not null, it specifies if the triangular matrix has unit diagonal elements.public Diag triangular()
public Matrix clone()
clone
in class java.lang.Object
public double[][] toArray()
public double[] row(int i)
public double[] col(int j)
public Matrix row(int... rows)
public Matrix col(int... cols)
public Matrix submatrix(int i, int j, int k, int l)
i
- the beginning row, inclusive.j
- the beginning column, inclusive,k
- the ending row, inclusive.l
- the ending column, inclusive.public void fill(double x)
public Matrix transpose()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean equals(Matrix o, double eps)
o
- the other matrix.eps
- the error margin.protected int index(int i, int j)
public double get(int i, int j)
DMatrix
public Matrix set(int i, int j, double x)
DMatrix
public double add(int i, int j, double b)
public double sub(int i, int j, double b)
public double mul(int i, int j, double b)
public double div(int i, int j, double b)
public Matrix add(double b)
public Matrix sub(double b)
public Matrix mul(double b)
public Matrix div(double b)
public Matrix add(int i, int j, double alpha, Matrix B)
public Matrix sub(int i, int j, double alpha, Matrix B)
public Matrix mul(int i, int j, double alpha, Matrix B)
public Matrix div(int i, int j, double alpha, Matrix B)
public Matrix add(double alpha, Matrix A, double beta, Matrix B)
public Matrix sub(double alpha, Matrix A, double beta, Matrix B)
public Matrix mul(double alpha, Matrix A, Matrix B)
public double add(int i, int j, double alpha, double beta)
public Matrix add(int i, int j, double alpha, double beta, Matrix B)
public Matrix add(double alpha, double beta, Matrix B)
public Matrix add(double alpha, double[] x, double[] y)
public Matrix replaceNaN(double x)
public double sum()
public double norm1()
public double norm2()
public double norm()
public double normInf()
public double normFro()
public double xAx(double[] x)
public double[] rowSums()
public double[] rowMeans()
public double[] rowSds()
public double[] colSums()
public double[] colMeans()
public double[] colSds()
public Matrix scale()
public Matrix scale(double[] center, double[] scale)
center
- column center. If null, no centering.scale
- column scale. If null, no scaling.public Matrix inverse()
public void mv(Transpose trans, double alpha, java.nio.DoubleBuffer x, double beta, java.nio.DoubleBuffer y)
y = alpha * A * x + beta * y
public void mv(Transpose trans, double alpha, double[] x, double beta, double[] y)
DMatrix
y = alpha * op(A) * x + beta * y
where op is the transpose operation.public void mv(double[] work, int inputOffset, int outputOffset)
IMatrix
public void tv(double[] work, int inputOffset, int outputOffset)
IMatrix
public void mm(Transpose transA, Transpose transB, double alpha, Matrix B, double beta, Matrix C)
C := alpha*A*B + beta*C
public Matrix ata()
public Matrix aat()
public Matrix adb(Transpose transA, Transpose transB, Matrix B, double[] diag)
public Matrix.LU lu()
public Matrix.LU lu(boolean overwrite)
overwrite
- The flag if the decomposition overwrites this matrix.public Matrix.Cholesky cholesky()
java.lang.ArithmeticException
- if the matrix is not positive definite.public Matrix.Cholesky cholesky(boolean overwrite)
overwrite
- The flag if the decomposition overwrites this matrix.java.lang.ArithmeticException
- if the matrix is not positive definite.public Matrix.QR qr()
public Matrix.QR qr(boolean overwrite)
overwrite
- The flag if the decomposition overwrites this matrix.public Matrix.SVD svd()
public Matrix.SVD svd(boolean vectors, boolean overwrite)
vectors
- The flag if computing the singular vectors.overwrite
- The flag if the decomposition overwrites this matrix.public Matrix.EVD eigen()
By default eigen
does not always return the eigenvalues
and eigenvectors in sorted order. Use the EVD.sort
function
to put the eigenvalues in descending order and reorder the corresponding
eigenvectors.
public Matrix.EVD eigen(boolean vl, boolean vr, boolean overwrite)
By default eigen
does not always return the eigenvalues
and eigenvectors in sorted order. Use the sort
function
to put the eigenvalues in descending order and reorder the corresponding
eigenvectors.
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.