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.
|
protected int numRows
protected int numColumns
protected AbstractMatrix(int numRows, int numColumns)
protected AbstractMatrix(Matrix A)
public int numRows()
Matrix
public int numColumns()
Matrix
numColumns
in interface Matrix
public boolean isSquare()
Matrix
public void set(int row, int column, double value)
Matrix
A(row,column) = value
public void add(int row, int column, double value)
Matrix
A(row,column) += value
public double get(int row, int column)
Matrix
A(row,column)
protected void check(int row, int column)
public Matrix copy()
Matrix
public Matrix zero()
Matrix
public Vector mult(double alpha, Vector x, Vector y)
Matrix
y = alpha*A*x
public Vector multAdd(Vector x, Vector y)
Matrix
y = A*x + y
public Vector multAdd(double alpha, Vector x, Vector y)
Matrix
y = alpha*A*x + y
protected void checkMultAdd(Vector x, Vector y)
mult
and multAdd
public Vector transMult(Vector x, Vector y)
Matrix
y = AT*x
public Vector transMult(double alpha, Vector x, Vector y)
Matrix
y = alpha*AT*x
public Vector transMultAdd(Vector x, Vector y)
Matrix
y = AT*x + y
transMultAdd
in interface Matrix
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
public Vector transMultAdd(double alpha, Vector x, Vector y)
Matrix
y = alpha*AT*x + y
transMultAdd
in interface Matrix
x
- Vector of size A.numRows()
y
- Vector of size A.numColumns()
protected void checkTransMultAdd(Vector x, Vector y)
transMult
and
transMultAdd
public Vector solve(Vector b, Vector x)
Matrix
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 solverpublic Vector transSolve(Vector b, Vector x)
Matrix
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 solvertransSolve
in interface Matrix
b
- Vector of size A.numColumns()
x
- Vector of size A.numRows()
protected void checkSolve(Vector b, Vector x)
public Matrix rank1(Vector x)
Matrix
A = x*xT + A
. The matrix must be square, and
the vector of the same lengthpublic Matrix rank1(double alpha, Vector x)
Matrix
A = alpha*x*xT + A
. The matrix must be
square, and the vector of the same lengthpublic Matrix rank1(Vector x, Vector y)
Matrix
A = x*yT + A
. The matrix must be square, and
the vectors of the same lengthpublic Matrix rank1(double alpha, Vector x, Vector y)
Matrix
A = 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)
Matrix
A = 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)
Matrix
A = 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)
Matrix
C = alpha*A*B
public Matrix multAdd(Matrix B, Matrix C)
Matrix
C = A*B + C
public Matrix multAdd(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*A*B + C
protected void checkMultAdd(Matrix B, Matrix C)
mult
and multAdd
public Matrix transAmult(Matrix B, Matrix C)
Matrix
C = AT*B
transAmult
in interface Matrix
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()
public Matrix transAmult(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*AT*B
transAmult
in interface Matrix
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()
public Matrix transAmultAdd(Matrix B, Matrix C)
Matrix
C = AT*B + C
transAmultAdd
in interface Matrix
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()
public Matrix transAmultAdd(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*AT*B + C
transAmultAdd
in interface Matrix
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()
protected void checkTransAmultAdd(Matrix B, Matrix C)
transAmult
and
transAmultAdd
public Matrix transBmult(Matrix B, Matrix C)
Matrix
C = A*BT
transBmult
in interface Matrix
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()
public Matrix transBmult(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*A*BT
transBmult
in interface Matrix
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()
public Matrix transBmultAdd(Matrix B, Matrix C)
Matrix
C = A*BT + C
transBmultAdd
in interface Matrix
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()
public Matrix transBmultAdd(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*A*BT + C
transBmultAdd
in interface Matrix
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()
protected void checkTransBmultAdd(Matrix B, Matrix C)
transBmult
and
transBmultAdd
public Matrix transABmult(Matrix B, Matrix C)
Matrix
C = AT*BT
transABmult
in interface Matrix
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()
public Matrix transABmult(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*AT*BT
transABmult
in interface Matrix
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()
public Matrix transABmultAdd(Matrix B, Matrix C)
Matrix
C = AT*BT + C
transABmultAdd
in interface Matrix
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()
public Matrix transABmultAdd(double alpha, Matrix B, Matrix C)
Matrix
C = alpha*AT*BT + C
transABmultAdd
in interface Matrix
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()
protected void checkTransABmultAdd(Matrix B, Matrix C)
transABmultAdd
and
transABmultAdd
public Matrix solve(Matrix B, Matrix X)
Matrix
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 solverpublic Matrix transSolve(Matrix B, Matrix X)
Matrix
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
solvertransSolve
in interface Matrix
B
- 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
protected void checkSolve(Matrix B, Matrix X)
public Matrix rank1(Matrix C)
Matrix
A = C*CT + A
. The matrices must be square
and of the same sizepublic Matrix rank1(double alpha, Matrix C)
Matrix
A = alpha*C*CT + A
. The matrices must be
square and of the same sizeprotected void checkRank1(Matrix C)
public Matrix transRank1(Matrix C)
Matrix
A = CT*C + A
The matrices must be square and
of the same sizetransRank1
in interface Matrix
public Matrix transRank1(double alpha, Matrix C)
Matrix
A = alpha*CT*C + A
The matrices must be
square and of the same sizetransRank1
in interface Matrix
protected void checkTransRank1(Matrix C)
public Matrix rank2(Matrix B, Matrix C)
Matrix
A = B*CT + C*BT + A
. This
matrix must be squarepublic Matrix rank2(double alpha, Matrix B, Matrix C)
Matrix
A = 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)
Matrix
A = BT*C + CT*B + A
. This
matrix must be squaretransRank2
in interface Matrix
B
- 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
public Matrix transRank2(double alpha, Matrix B, Matrix C)
Matrix
A = alpha*BT*C + alpha*CT*B + A
.
This matrix must be squaretransRank2
in interface Matrix
B
- 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
protected void checkTransRank2(Matrix B, Matrix C)
public Matrix set(Matrix B)
Matrix
A=B
. The matrices must be of the same sizepublic Matrix set(double alpha, Matrix B)
Matrix
A=alpha*B
. The matrices must be of the same sizepublic Matrix add(Matrix B)
Matrix
A = B + A
. The matrices must be of the same sizepublic Matrix add(double alpha, Matrix B)
Matrix
A = alpha*B + A
. The matrices must be of the same sizeprotected void checkSize(Matrix B)
public Matrix transpose()
Matrix
protected void checkTranspose()
public Matrix transpose(Matrix B)
Matrix
B
. Matrix
dimensions must be compatibleprotected void checkTranspose(Matrix B)
public double norm(Matrix.Norm type)
Matrix
protected 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 © 2013. All Rights Reserved.