public interface Matrix extends Iterable<MatrixEntry>
double
s in a rectangular 2D
array, and it is used alongside Vector
in numerical
computations. Implementing classes decides on the actual storage.
Use numRows
and numColumns
to get the basic
size of a matrix. get(int,int)
gets an element, and there are
corresponding set(int,int,double)
and
add(int,int,double)
methods as well. Note that matrix indices
are zero-based (typical for Java and C). This means that the row-indices
range from 0 to numRows-1
, likewise for the columns. It is
legal to have numRows
or numColumns
equal zero.
Other basic operations are zero
which zeros all the entries of
the matrix, which can be cheaper than either zeroing the matrix manually, or
creating a new matrix, and the operation copy
which creates a
deep copy of the matrix. This copy has separate storage, but starts with the
same contents as the current matrix.
The matrix interface extends Iterable
, and the iterator
returns a MatrixEntry
which contains current index and entry
value. Note that the iterator may skip non-zero entries. Using an iterator,
many simple and efficient algorithms can be created. The iterator also
permits changing values in the matrix, however only non-zero entries can be
changed.
A large selection of basic linear algebra operations are available. To ensure high efficiency, little or no internal memory allocation is done, and the user is required to supply the output arguments.
The operations available include:
Modifier and Type | Interface and Description |
---|---|
static class |
Matrix.Norm
Supported matrix-norms.
|
Modifier and Type | Method and Description |
---|---|
Matrix |
add(double alpha,
Matrix B)
A = alpha*B + A . |
void |
add(int row,
int column,
double value)
A(row,column) += value |
Matrix |
add(Matrix B)
A = B + A . |
Matrix |
copy()
Creates a deep copy of the matrix
|
double |
get(int row,
int column)
Returns
A(row,column) |
boolean |
isSquare()
Returns true if the matrix is square
|
Matrix |
mult(double alpha,
Matrix B,
Matrix C)
C = alpha*A*B |
Vector |
mult(double alpha,
Vector x,
Vector y)
y = alpha*A*x |
Matrix |
mult(Matrix B,
Matrix C)
C = A*B |
Vector |
mult(Vector x,
Vector y)
y = A*x |
Matrix |
multAdd(double alpha,
Matrix B,
Matrix C)
C = alpha*A*B + C |
Vector |
multAdd(double alpha,
Vector x,
Vector y)
y = alpha*A*x + y |
Matrix |
multAdd(Matrix B,
Matrix C)
C = A*B + C |
Vector |
multAdd(Vector x,
Vector y)
y = A*x + y |
double |
norm(Matrix.Norm type)
Computes the given norm of the matrix
|
int |
numColumns()
Number of columns in the matrix
|
int |
numRows()
Number of rows in the matrix
|
Matrix |
rank1(double alpha,
Matrix C)
A = alpha*C*CT + A . |
Matrix |
rank1(double alpha,
Vector x)
A = alpha*x*xT + A . |
Matrix |
rank1(double alpha,
Vector x,
Vector y)
A = alpha*x*yT + A . |
Matrix |
rank1(Matrix C)
A = C*CT + A . |
Matrix |
rank1(Vector x)
A = x*xT + A . |
Matrix |
rank1(Vector x,
Vector y)
A = x*yT + A . |
Matrix |
rank2(double alpha,
Matrix B,
Matrix C)
A = alpha*B*CT + alpha*C*BT + A . |
Matrix |
rank2(double alpha,
Vector x,
Vector y)
A = alpha*x*yT + alpha*y*xT + A . |
Matrix |
rank2(Matrix B,
Matrix C)
A = B*CT + C*BT + A . |
Matrix |
rank2(Vector x,
Vector y)
A = x*yT + y*xT + A . |
Matrix |
scale(double alpha)
A = alpha*A |
Matrix |
set(double alpha,
Matrix B)
A=alpha*B . |
void |
set(int row,
int column,
double value)
A(row,column) = value |
Matrix |
set(Matrix B)
A=B . |
Matrix |
solve(Matrix B,
Matrix X)
X = A\B . |
Vector |
solve(Vector b,
Vector x)
x = A\b . |
Matrix |
transABmult(double alpha,
Matrix B,
Matrix C)
C = alpha*AT*BT |
Matrix |
transABmult(Matrix B,
Matrix C)
C = AT*BT |
Matrix |
transABmultAdd(double alpha,
Matrix B,
Matrix C)
C = alpha*AT*BT + C |
Matrix |
transABmultAdd(Matrix B,
Matrix C)
C = AT*BT + C |
Matrix |
transAmult(double alpha,
Matrix B,
Matrix C)
C = alpha*AT*B |
Matrix |
transAmult(Matrix B,
Matrix C)
C = AT*B |
Matrix |
transAmultAdd(double alpha,
Matrix B,
Matrix C)
C = alpha*AT*B + C |
Matrix |
transAmultAdd(Matrix B,
Matrix C)
C = AT*B + C |
Matrix |
transBmult(double alpha,
Matrix B,
Matrix C)
C = alpha*A*BT |
Matrix |
transBmult(Matrix B,
Matrix C)
C = A*BT |
Matrix |
transBmultAdd(double alpha,
Matrix B,
Matrix C)
C = alpha*A*BT + C |
Matrix |
transBmultAdd(Matrix B,
Matrix C)
C = A*BT + C |
Vector |
transMult(double alpha,
Vector x,
Vector y)
y = alpha*AT*x |
Vector |
transMult(Vector x,
Vector y)
y = AT*x |
Vector |
transMultAdd(double alpha,
Vector x,
Vector y)
y = alpha*AT*x + y |
Vector |
transMultAdd(Vector x,
Vector y)
y = AT*x + y |
Matrix |
transpose()
Transposes the matrix in-place.
|
Matrix |
transpose(Matrix B)
Sets the tranpose of this matrix into
B . |
Matrix |
transRank1(double alpha,
Matrix C)
A = alpha*CT*C + A The matrices must be
square and of the same size |
Matrix |
transRank1(Matrix C)
A = CT*C + A The matrices must be square and
of the same size |
Matrix |
transRank2(double alpha,
Matrix B,
Matrix C)
A = alpha*BT*C + alpha*CT*B + A . |
Matrix |
transRank2(Matrix B,
Matrix C)
A = BT*C + CT*B + A . |
Matrix |
transSolve(Matrix B,
Matrix X)
X = AT\B . |
Vector |
transSolve(Vector b,
Vector x)
x = AT\b . |
Matrix |
zero()
Zeros all the entries in the matrix, while preserving any underlying
structure.
|
int numRows()
int numColumns()
boolean isSquare()
void set(int row, int column, double value)
A(row,column) = value
void add(int row, int column, double value)
A(row,column) += value
double get(int row, int column)
A(row,column)
Matrix copy()
Matrix zero()
Vector mult(Vector x, Vector y)
y = A*x
x
- Vector of size A.numColumns()
y
- Vector of size A.numRows()
Vector mult(double alpha, Vector x, Vector y)
y = alpha*A*x
x
- Vector of size A.numColumns()
y
- Vector of size A.numRows()
Vector multAdd(Vector x, Vector y)
y = A*x + y
x
- Vector of size A.numColumns()
y
- Vector of size A.numRows()
Vector multAdd(double alpha, Vector x, Vector y)
y = alpha*A*x + y
x
- Vector of size A.numColumns()
y
- Vector of size A.numRows()
Vector transMult(Vector x, Vector y)
y = AT*x
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
Vector transMult(double alpha, Vector x, Vector y)
y = alpha*AT*x
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
Vector transMultAdd(Vector x, Vector y)
y = AT*x + y
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
Vector transMultAdd(double alpha, Vector x, Vector y)
y = alpha*AT*x + y
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
Vector solve(Vector b, Vector x) throws MatrixSingularException, MatrixNotSPDException
x = A\b
. Not all matrices support this operation, those
that do not throw UnsupportedOperationException
. Note
that it is often more efficient to use a matrix decomposition and its
associated solverb
- Vector of size A.numRows()
x
- Vector of size A.numColumns()
MatrixSingularException
- If the matrix is singularMatrixNotSPDException
- If the solver assumes that the matrix is symmetrical,
positive definite, but that that property does not holdVector transSolve(Vector b, Vector x) throws MatrixSingularException, MatrixNotSPDException
x = AT\b
. Not all matrices support this
operation, those that do not throw
UnsupportedOperationException
. Note that it is often more
efficient to use a matrix decomposition and its associated solverb
- Vector of size A.numColumns()
x
- Vector of size A.numRows()
MatrixSingularException
- If the matrix is singularMatrixNotSPDException
- If the solver assumes that the matrix is symmetrical,
positive definite, but that that property does not holdMatrix rank1(Vector x)
A = x*xT + A
. The matrix must be square, and
the vector of the same lengthMatrix rank1(double alpha, Vector x)
A = alpha*x*xT + A
. The matrix must be
square, and the vector of the same lengthMatrix rank1(Vector x, Vector y)
A = x*yT + A
. The matrix must be square, and
the vectors of the same lengthMatrix rank1(double alpha, Vector x, Vector y)
A = alpha*x*yT + A
. The matrix must be
square, and the vectors of the same lengthMatrix rank2(Vector x, Vector y)
A = x*yT + y*xT + A
. The matrix
must be square, and the vectors of the same lengthMatrix rank2(double alpha, Vector x, Vector y)
A = alpha*x*yT + alpha*y*xT + A
.
The matrix must be square, and the vectors of the same lengthMatrix mult(Matrix B, Matrix C)
C = A*B
B
- Matrix such that B.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
Matrix mult(double alpha, Matrix B, Matrix C)
C = alpha*A*B
B
- Matrix such that B.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
Matrix multAdd(Matrix B, Matrix C)
C = A*B + C
B
- Matrix such that B.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
Matrix multAdd(double alpha, Matrix B, Matrix C)
C = alpha*A*B + C
B
- Matrix such that B.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
Matrix transAmult(Matrix B, Matrix C)
C = AT*B
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transAmult(double alpha, Matrix B, Matrix C)
C = alpha*AT*B
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transAmultAdd(Matrix B, Matrix C)
C = AT*B + C
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transAmultAdd(double alpha, Matrix B, Matrix C)
C = alpha*AT*B + C
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transBmult(Matrix B, Matrix C)
C = A*BT
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transBmult(double alpha, Matrix B, Matrix C)
C = alpha*A*BT
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transBmultAdd(Matrix B, Matrix C)
C = A*BT + C
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transBmultAdd(double alpha, Matrix B, Matrix C)
C = alpha*A*BT + C
B
- Matrix such that B.numRows() == A.numRows()
and
B.numColumns() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numColumns() == C.numColumns()
Matrix transABmult(Matrix B, Matrix C)
C = AT*BT
B
- Matrix such that B.numColumns() == A.numRows()
and B.numRows() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numRows() == C.numColumns()
Matrix transABmult(double alpha, Matrix B, Matrix C)
C = alpha*AT*BT
B
- Matrix such that B.numColumns() == A.numRows()
and B.numRows() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numRows() == C.numColumns()
Matrix transABmultAdd(Matrix B, Matrix C)
C = AT*BT + C
B
- Matrix such that B.numColumns() == A.numRows()
and B.numRows() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numRows() == C.numColumns()
Matrix transABmultAdd(double alpha, Matrix B, Matrix C)
C = alpha*AT*BT + C
B
- Matrix such that B.numColumns() == A.numRows()
and B.numRows() == C.numColumns()
C
- Matrix such that C.numRows() == A.numColumns()
and B.numRows() == C.numColumns()
Matrix solve(Matrix B, Matrix X) throws MatrixSingularException, MatrixNotSPDException
X = A\B
. Not all matrices support this operation, those
that do not throw UnsupportedOperationException
. Note
that it is often more efficient to use a matrix decomposition and its
associated solverB
- Matrix with the same number of rows as A
, and
the same number of columns as X
X
- Matrix with a number of rows equal A.numColumns()
,
and the same number of columns as B
MatrixSingularException
- If the matrix is singularMatrixNotSPDException
- If the solver assumes that the matrix is symmetrical,
positive definite, but that that property does not holdMatrix transSolve(Matrix B, Matrix X) throws MatrixSingularException, MatrixNotSPDException
X = AT\B
. Not all matrices support this
operation, those that do not throw
UnsupportedOperationException
. Note that it is often more
efficient to use a matrix decomposition and its associated transpose
solverB
- Matrix with a number of rows equal A.numColumns()
,
and the same number of columns as X
X
- Matrix with the same number of rows as A
, and
the same number of columns as B
MatrixSingularException
- If the matrix is singularMatrixNotSPDException
- If the solver assumes that the matrix is symmetrical,
positive definite, but that that property does not holdMatrix rank1(Matrix C)
A = C*CT + A
. The matrices must be square
and of the same sizeMatrix rank1(double alpha, Matrix C)
A = alpha*C*CT + A
. The matrices must be
square and of the same sizeMatrix transRank1(Matrix C)
A = CT*C + A
The matrices must be square and
of the same sizeMatrix transRank1(double alpha, Matrix C)
A = alpha*CT*C + A
The matrices must be
square and of the same sizeMatrix rank2(Matrix B, Matrix C)
A = B*CT + C*BT + A
. This
matrix must be squareB
- Matrix with the same number of rows as A
and
the same number of columns as C
C
- Matrix with the same number of rows as A
and
the same number of columns as B
Matrix rank2(double alpha, Matrix B, Matrix C)
A = alpha*B*CT + alpha*C*BT + A
.
This matrix must be squareB
- Matrix with the same number of rows as A
and
the same number of columns as C
C
- Matrix with the same number of rows as A
and
the same number of columns as B
Matrix transRank2(Matrix B, Matrix C)
A = BT*C + CT*B + A
. This
matrix must be squareB
- Matrix with the same number of rows as C
and
the same number of columns as A
C
- Matrix with the same number of rows as B
and
the same number of columns as A
Matrix transRank2(double alpha, Matrix B, Matrix C)
A = alpha*BT*C + alpha*CT*B + A
.
This matrix must be squareB
- Matrix with the same number of rows as C
and
the same number of columns as A
C
- Matrix with the same number of rows as B
and
the same number of columns as A
Matrix scale(double alpha)
A = alpha*A
Matrix add(double alpha, Matrix B)
A = alpha*B + A
. The matrices must be of the same sizeMatrix transpose()
Matrix transpose(Matrix B)
B
. Matrix
dimensions must be compatibleB
- Matrix with as many rows as this matrix has columns, and as
many columns as this matrix has rowsB=AT
double norm(Matrix.Norm type)
type
- The type of norm to computeCopyright © 2013. All Rights Reserved.