public abstract class AbstractMatrix extends Object implements Matrix
Matrix. The following methods throw
UnsupportedOperationException, and should be overridden by a
subclass:
get(int,int)set(int,int,double)copy
For the rest of the methods, simple default implementations using a matrix
iterator has been provided. There are some kernel operations which the
simpler operations forward to, for instance, mult(Matrix,Matrix)
forwards to multAdd(double,Matrix,Matrix). Subclasses can thus
focus on overriding the kernel operations, which are:
multAdd(double,Vector,Vector) and
transMultAdd(double,Vector,Vector).rank1(double,Vector,Vector) and
rank1(double,Vector,Vector).multAdd(double,Matrix,Matrix),
transAmultAdd(double,Matrix,Matrix),
transBmultAdd(double,Matrix,Matrix), and
transABmultAdd(double,Matrix,Matrix).scale(double).set(double,Matrix) and add(double,Matrix).transpose and transpose(Matrix).
Finally, a default iterator is provided by this class, which works by calling
the get function. A tailored replacement should be used by
subclasses.
Matrix.Norm| Modifier and Type | Field and Description |
|---|---|
protected int |
numColumns
Number of columns
|
protected int |
numRows
Number of rows
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractMatrix(int numRows,
int numColumns)
Constructor for AbstractMatrix
|
protected |
AbstractMatrix(Matrix A)
Constructor for AbstractMatrix, same size as A.
|
| 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. |
protected void |
check(int row,
int column)
Checks the passed row and column indices
|
protected void |
checkMultAdd(Matrix B,
Matrix C)
Checks the arguments to
mult and multAdd |
protected void |
checkMultAdd(Vector x,
Vector y)
Checks the arguments to
mult and multAdd |
protected void |
checkRank1(Matrix C)
Checks that a matrix rank1 update is possible for the given matrix
|
protected void |
checkRank1(Vector x,
Vector y)
Checks that a vector rank1 update is possible for the given vectors
|
protected void |
checkRank2(Matrix B,
Matrix C)
Checks that a rank2 update is legal for the given arguments
|
protected void |
checkRank2(Vector x,
Vector y)
Checks that a vector rank2 update is legal with the given vectors
|
protected void |
checkSize(Matrix B)
Checks that the sizes of this matrix and the given conform
|
protected void |
checkSolve(Matrix B,
Matrix X)
Checks that a matrix inversion is legal for the given arguments.
|
protected void |
checkSolve(Vector b,
Vector x)
Checks that a matrix inversion is legal for the given arguments.
|
protected void |
checkTransABmultAdd(Matrix B,
Matrix C)
Checks the arguments to
transABmultAdd and
transABmultAdd |
protected void |
checkTransAmultAdd(Matrix B,
Matrix C)
Checks the arguments to
transAmult and
transAmultAdd |
protected void |
checkTransBmultAdd(Matrix B,
Matrix C)
Checks the arguments to
transBmult and
transBmultAdd |
protected void |
checkTransMultAdd(Vector x,
Vector y)
Checks the arguments to
transMult and
transMultAdd |
protected void |
checkTranspose()
Checks that the matrix may be transposed
|
protected void |
checkTranspose(Matrix B)
Checks that this matrix can be transposed into the given matrix
|
protected void |
checkTransRank1(Matrix C)
Checks that a transposed rank1 update is leagal with the given argument
|
protected void |
checkTransRank2(Matrix B,
Matrix C)
Checks that a transposed rank2 update is leagal with the given arguments
|
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
|
Iterator<MatrixEntry> |
iterator() |
protected double |
max()
Returns the largest absolute value
|
protected double |
max(double[] x)
Returns the largest element of the passed array
|
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
|
protected double |
norm1()
Computes the 1 norm
|
protected double |
normF()
Computes the Frobenius norm.
|
protected double |
normInf()
Computes the infinity norm
|
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. |
String |
toString() |
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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorprotected int numRows
protected int numColumns
protected AbstractMatrix(int numRows,
int numColumns)
protected AbstractMatrix(Matrix A)
public int numRows()
Matrixpublic int numColumns()
MatrixnumColumns in interface Matrixpublic boolean isSquare()
Matrixpublic void set(int row,
int column,
double value)
MatrixA(row,column) = valuepublic void add(int row,
int column,
double value)
MatrixA(row,column) += valuepublic double get(int row,
int column)
MatrixA(row,column)protected void check(int row,
int column)
public Matrix copy()
Matrixpublic Matrix zero()
Matrixpublic Vector mult(double alpha, Vector x, Vector y)
Matrixy = alpha*A*xpublic Vector multAdd(Vector x, Vector y)
Matrixy = A*x + ypublic Vector multAdd(double alpha, Vector x, Vector y)
Matrixy = alpha*A*x + yprotected void checkMultAdd(Vector x, Vector y)
mult and multAddpublic Vector transMult(Vector x, Vector y)
Matrixy = AT*xpublic Vector transMult(double alpha, Vector x, Vector y)
Matrixy = alpha*AT*xpublic Vector transMultAdd(Vector x, Vector y)
Matrixy = AT*x + ytransMultAdd in interface Matrixx - Vector of size A.numRows()y - Vector of size A.numColumns()public Vector transMultAdd(double alpha, Vector x, Vector y)
Matrixy = alpha*AT*x + ytransMultAdd in interface Matrixx - Vector of size A.numRows()y - Vector of size A.numColumns()protected void checkTransMultAdd(Vector x, Vector y)
transMult and
transMultAddpublic Vector solve(Vector b, Vector x)
Matrixx = 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
solverpublic Vector transSolve(Vector b, Vector x)
Matrixx = 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 solvertransSolve in interface Matrixb - Vector of size A.numColumns()x - Vector of size A.numRows()protected void checkSolve(Vector b, Vector x)
public Matrix rank1(Vector x)
MatrixA = x*xT + A. The matrix must be square, and the
vector of the same lengthpublic Matrix rank1(double alpha, Vector x)
MatrixA = alpha*x*xT + A. The matrix must be square,
and the vector of the same lengthpublic Matrix rank1(Vector x, Vector y)
MatrixA = x*yT + A. The matrix must be square, and the
vectors of the same lengthpublic Matrix rank1(double alpha, Vector x, Vector y)
MatrixA = alpha*x*yT + A. The matrix must be square,
and the vectors of the same lengthprotected void checkRank1(Vector x, Vector y)
public Matrix rank2(Vector x, Vector y)
MatrixA = x*yT + y*xT + A. The matrix must
be square, and the vectors of the same lengthpublic Matrix rank2(double alpha, Vector x, Vector y)
MatrixA = alpha*x*yT + alpha*y*xT + A. The
matrix must be square, and the vectors of the same lengthprotected void checkRank2(Vector x, Vector y)
public Matrix mult(double alpha, Matrix B, Matrix C)
MatrixC = alpha*A*Bpublic Matrix multAdd(Matrix B, Matrix C)
MatrixC = A*B + Cpublic Matrix multAdd(double alpha, Matrix B, Matrix C)
MatrixC = alpha*A*B + Cprotected void checkMultAdd(Matrix B, Matrix C)
mult and multAddpublic Matrix transAmult(Matrix B, Matrix C)
MatrixC = AT*BtransAmult in interface MatrixB - 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()public Matrix transAmult(double alpha, Matrix B, Matrix C)
MatrixC = alpha*AT*BtransAmult in interface MatrixB - 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()public Matrix transAmultAdd(Matrix B, Matrix C)
MatrixC = AT*B + CtransAmultAdd in interface MatrixB - 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()public Matrix transAmultAdd(double alpha, Matrix B, Matrix C)
MatrixC = alpha*AT*B + CtransAmultAdd in interface MatrixB - 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()protected void checkTransAmultAdd(Matrix B, Matrix C)
transAmult and
transAmultAddpublic Matrix transBmult(Matrix B, Matrix C)
MatrixC = A*BTtransBmult in interface MatrixB - 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()public Matrix transBmult(double alpha, Matrix B, Matrix C)
MatrixC = alpha*A*BTtransBmult in interface MatrixB - 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()public Matrix transBmultAdd(Matrix B, Matrix C)
MatrixC = A*BT + CtransBmultAdd in interface MatrixB - 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()public Matrix transBmultAdd(double alpha, Matrix B, Matrix C)
MatrixC = alpha*A*BT + CtransBmultAdd in interface MatrixB - 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()protected void checkTransBmultAdd(Matrix B, Matrix C)
transBmult and
transBmultAddpublic Matrix transABmult(Matrix B, Matrix C)
MatrixC = AT*BTtransABmult in interface MatrixB - 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()public Matrix transABmult(double alpha, Matrix B, Matrix C)
MatrixC = alpha*AT*BTtransABmult in interface MatrixB - 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()public Matrix transABmultAdd(Matrix B, Matrix C)
MatrixC = AT*BT + CtransABmultAdd in interface MatrixB - 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()public Matrix transABmultAdd(double alpha, Matrix B, Matrix C)
MatrixC = alpha*AT*BT + CtransABmultAdd in interface MatrixB - 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()protected void checkTransABmultAdd(Matrix B, Matrix C)
transABmultAdd and
transABmultAddpublic Matrix solve(Matrix B, Matrix X)
MatrixX = 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
solverpublic Matrix transSolve(Matrix B, Matrix X)
MatrixX = 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
solvertransSolve in interface MatrixB - Matrix with a number of rows equal A.numColumns()
, and the same number of columns as XX - Matrix with the same number of rows as A, and the
same number of columns as Bprotected void checkSolve(Matrix B, Matrix X)
public Matrix rank1(Matrix C)
MatrixA = C*CT + A. The matrices must be square and of
the same sizepublic Matrix rank1(double alpha, Matrix C)
MatrixA = alpha*C*CT + A. The matrices must be square
and of the same sizeprotected void checkRank1(Matrix C)
public Matrix transRank1(Matrix C)
MatrixA = CT*C + A The matrices must be square and of
the same sizetransRank1 in interface Matrixpublic Matrix transRank1(double alpha, Matrix C)
MatrixA = alpha*CT*C + A The matrices must be square
and of the same sizetransRank1 in interface Matrixprotected void checkTransRank1(Matrix C)
public Matrix rank2(Matrix B, Matrix C)
MatrixA = B*CT + C*BT + A. This matrix must
be squarepublic Matrix rank2(double alpha, Matrix B, Matrix C)
MatrixA = alpha*B*CT + alpha*C*BT + A. This
matrix must be squareprotected void checkRank2(Matrix B, Matrix C)
public Matrix transRank2(Matrix B, Matrix C)
MatrixA = BT*C + CT*B + A. This matrix must
be squaretransRank2 in interface MatrixB - Matrix with the same number of rows as C and the
same number of columns as AC - Matrix with the same number of rows as B and the
same number of columns as Apublic Matrix transRank2(double alpha, Matrix B, Matrix C)
MatrixA = alpha*BT*C + alpha*CT*B + A. This
matrix must be squaretransRank2 in interface MatrixB - Matrix with the same number of rows as C and the
same number of columns as AC - Matrix with the same number of rows as B and the
same number of columns as Aprotected void checkTransRank2(Matrix B, Matrix C)
public Matrix set(Matrix B)
MatrixA=B. The matrices must be of the same sizepublic Matrix set(double alpha, Matrix B)
MatrixA=alpha*B. The matrices must be of the same sizepublic Matrix add(Matrix B)
MatrixA = B + A. The matrices must be of the same sizepublic Matrix add(double alpha, Matrix B)
MatrixA = alpha*B + A. The matrices must be of the same sizeprotected void checkSize(Matrix B)
public Matrix transpose()
Matrixprotected void checkTranspose()
public Matrix transpose(Matrix B)
MatrixB. Matrix dimensions
must be compatibleprotected void checkTranspose(Matrix B)
public double norm(Matrix.Norm type)
Matrixprotected double norm1()
protected double normF()
protected double normInf()
protected double max()
protected double max(double[] x)
public Iterator<MatrixEntry> iterator()
iterator in interface Iterable<MatrixEntry>Copyright © 2015. All Rights Reserved.