Package no.uib.cipr.matrix
Class AbstractMatrix
- java.lang.Object
-
- no.uib.cipr.matrix.AbstractMatrix
-
- All Implemented Interfaces:
java.lang.Iterable<MatrixEntry>,Matrix
- Direct Known Subclasses:
BandMatrix,CompColMatrix,CompDiagMatrix,CompRowMatrix,DenseMatrix,DistColMatrix,DistRowMatrix,FlexCompColMatrix,FlexCompRowMatrix,LowerSymmBandMatrix,LowerSymmDenseMatrix,LowerSymmPackMatrix,LowerTriangBandMatrix,LowerTriangDenseMatrix,LowerTriangPackMatrix,SymmTridiagMatrix,TridiagMatrix,UpperSymmBandMatrix,UpperSymmDenseMatrix,UpperSymmPackMatrix,UpperTriangBandMatrix,UpperTriangDenseMatrix,UpperTriangPackMatrix
public abstract class AbstractMatrix extends java.lang.Object implements Matrix
Partial implementation ofMatrix. The following methods throwUnsupportedOperationException, and should be overridden by a subclass:get(int,int)set(int,int,double)copy- All the direct solution methods
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 tomultAdd(double,Matrix,Matrix). Subclasses can thus focus on overriding the kernel operations, which are:-
multAdd(double,Vector,Vector)andtransMultAdd(double,Vector,Vector). -
rank1(double,Vector,Vector)andrank1(double,Vector,Vector). -
multAdd(double,Matrix,Matrix),transAmultAdd(double,Matrix,Matrix),transBmultAdd(double,Matrix,Matrix), andtransABmultAdd(double,Matrix,Matrix). -
scale(double). -
set(double,Matrix)andadd(double,Matrix). -
transposeandtranspose(Matrix). - All the norms.
Finally, a default iterator is provided by this class, which works by calling the
getfunction. A tailored replacement should be used by subclasses.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface no.uib.cipr.matrix.Matrix
Matrix.Norm
-
-
Field Summary
Fields Modifier and Type Field Description protected intnumColumnsNumber of columnsprotected intnumRowsNumber of rows
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractMatrix(int numRows, int numColumns)Constructor for AbstractMatrixprotectedAbstractMatrix(Matrix A)Constructor for AbstractMatrix, same size as A.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrixadd(double alpha, Matrix B)A = alpha*B + A.voidadd(int row, int column, double value)A(row,column) += valueMatrixadd(Matrix B)A = B + A.protected voidcheck(int row, int column)Checks the passed row and column indicesprotected voidcheckMultAdd(Matrix B, Matrix C)Checks the arguments tomultandmultAddprotected voidcheckMultAdd(Vector x, Vector y)Checks the arguments tomultandmultAddprotected voidcheckRank1(Matrix C)Checks that a matrix rank1 update is possible for the given matrixprotected voidcheckRank1(Vector x, Vector y)Checks that a vector rank1 update is possible for the given vectorsprotected voidcheckRank2(Matrix B, Matrix C)Checks that a rank2 update is legal for the given argumentsprotected voidcheckRank2(Vector x, Vector y)Checks that a vector rank2 update is legal with the given vectorsprotected voidcheckSize(Matrix B)Checks that the sizes of this matrix and the given conformprotected voidcheckSolve(Matrix B, Matrix X)Checks that a matrix inversion is legal for the given arguments.protected voidcheckSolve(Vector b, Vector x)Checks that a matrix inversion is legal for the given arguments.protected voidcheckTransABmultAdd(Matrix B, Matrix C)Checks the arguments totransABmultAddandtransABmultAddprotected voidcheckTransAmultAdd(Matrix B, Matrix C)Checks the arguments totransAmultandtransAmultAddprotected voidcheckTransBmultAdd(Matrix B, Matrix C)Checks the arguments totransBmultandtransBmultAddprotected voidcheckTransMultAdd(Vector x, Vector y)Checks the arguments totransMultandtransMultAddprotected voidcheckTranspose()Checks that the matrix may be transposedprotected voidcheckTranspose(Matrix B)Checks that this matrix can be transposed into the given matrixprotected voidcheckTransRank1(Matrix C)Checks that a transposed rank1 update is leagal with the given argumentprotected voidcheckTransRank2(Matrix B, Matrix C)Checks that a transposed rank2 update is leagal with the given argumentsMatrixcopy()Creates a deep copy of the matrixdoubleget(int row, int column)ReturnsA(row,column)booleanisSquare()Returns true if the matrix is squarejava.util.Iterator<MatrixEntry>iterator()protected doublemax()Returns the largest absolute valueprotected doublemax(double[] x)Returns the largest element of the passed arrayMatrixmult(double alpha, Matrix B, Matrix C)C = alpha*A*BVectormult(double alpha, Vector x, Vector y)y = alpha*A*xMatrixmult(Matrix B, Matrix C)C = A*BVectormult(Vector x, Vector y)y = A*xMatrixmultAdd(double alpha, Matrix B, Matrix C)C = alpha*A*B + CVectormultAdd(double alpha, Vector x, Vector y)y = alpha*A*x + yMatrixmultAdd(Matrix B, Matrix C)C = A*B + CVectormultAdd(Vector x, Vector y)y = A*x + ydoublenorm(Matrix.Norm type)Computes the given norm of the matrixprotected doublenorm1()Computes the 1 normprotected doublenormF()Computes the Frobenius norm.protected doublenormInf()Computes the infinity normintnumColumns()Number of columns in the matrixintnumRows()Number of rows in the matrixMatrixrank1(double alpha, Matrix C)A = alpha*C*CT + A.Matrixrank1(double alpha, Vector x)A = alpha*x*xT + A.Matrixrank1(double alpha, Vector x, Vector y)A = alpha*x*yT + A.Matrixrank1(Matrix C)A = C*CT + A.Matrixrank1(Vector x)A = x*xT + A.Matrixrank1(Vector x, Vector y)A = x*yT + A.Matrixrank2(double alpha, Matrix B, Matrix C)A = alpha*B*CT + alpha*C*BT + A.Matrixrank2(double alpha, Vector x, Vector y)A = alpha*x*yT + alpha*y*xT + A.Matrixrank2(Matrix B, Matrix C)A = B*CT + C*BT + A.Matrixrank2(Vector x, Vector y)A = x*yT + y*xT + A.Matrixscale(double alpha)A = alpha*AMatrixset(double alpha, Matrix B)A=alpha*B.voidset(int row, int column, double value)A(row,column) = valueMatrixset(Matrix B)A=B.Matrixsolve(Matrix B, Matrix X)X = A\B.Vectorsolve(Vector b, Vector x)x = A\b.java.lang.StringtoString()MatrixtransABmult(double alpha, Matrix B, Matrix C)C = alpha*AT*BTMatrixtransABmult(Matrix B, Matrix C)C = AT*BTMatrixtransABmultAdd(double alpha, Matrix B, Matrix C)C = alpha*AT*BT + CMatrixtransABmultAdd(Matrix B, Matrix C)C = AT*BT + CMatrixtransAmult(double alpha, Matrix B, Matrix C)C = alpha*AT*BMatrixtransAmult(Matrix B, Matrix C)C = AT*BMatrixtransAmultAdd(double alpha, Matrix B, Matrix C)C = alpha*AT*B + CMatrixtransAmultAdd(Matrix B, Matrix C)C = AT*B + CMatrixtransBmult(double alpha, Matrix B, Matrix C)C = alpha*A*BTMatrixtransBmult(Matrix B, Matrix C)C = A*BTMatrixtransBmultAdd(double alpha, Matrix B, Matrix C)C = alpha*A*BT + CMatrixtransBmultAdd(Matrix B, Matrix C)C = A*BT + CVectortransMult(double alpha, Vector x, Vector y)y = alpha*AT*xVectortransMult(Vector x, Vector y)y = AT*xVectortransMultAdd(double alpha, Vector x, Vector y)y = alpha*AT*x + yVectortransMultAdd(Vector x, Vector y)y = AT*x + yMatrixtranspose()Transposes the matrix in-place.Matrixtranspose(Matrix B)Sets the tranpose of this matrix intoB.MatrixtransRank1(double alpha, Matrix C)A = alpha*CT*C + AThe matrices must be square and of the same sizeMatrixtransRank1(Matrix C)A = CT*C + AThe matrices must be square and of the same sizeMatrixtransRank2(double alpha, Matrix B, Matrix C)A = alpha*BT*C + alpha*CT*B + A.MatrixtransRank2(Matrix B, Matrix C)A = BT*C + CT*B + A.MatrixtransSolve(Matrix B, Matrix X)X = AT\B.VectortransSolve(Vector b, Vector x)x = AT\b.Matrixzero()Zeros all the entries in the matrix, while preserving any underlying structure.
-
-
-
Constructor Detail
-
AbstractMatrix
protected AbstractMatrix(int numRows, int numColumns)Constructor for AbstractMatrix
-
AbstractMatrix
protected AbstractMatrix(Matrix A)
Constructor for AbstractMatrix, same size as A. The invoking constructor should set this matrix equal the argument matrix
-
-
Method Detail
-
numRows
public int numRows()
Description copied from interface:MatrixNumber of rows in the matrix
-
numColumns
public int numColumns()
Description copied from interface:MatrixNumber of columns in the matrix- Specified by:
numColumnsin interfaceMatrix
-
isSquare
public boolean isSquare()
Description copied from interface:MatrixReturns true if the matrix is square
-
set
public void set(int row, int column, double value)Description copied from interface:MatrixA(row,column) = value
-
add
public void add(int row, int column, double value)Description copied from interface:MatrixA(row,column) += value
-
get
public double get(int row, int column)Description copied from interface:MatrixReturnsA(row,column)
-
check
protected void check(int row, int column)Checks the passed row and column indices
-
copy
public Matrix copy()
Description copied from interface:MatrixCreates a deep copy of the matrix
-
zero
public Matrix zero()
Description copied from interface:MatrixZeros all the entries in the matrix, while preserving any underlying structure. Useful for general, unstructured matrices.
-
mult
public Vector mult(double alpha, Vector x, Vector y)
Description copied from interface:Matrixy = alpha*A*x
-
multAdd
public Vector multAdd(Vector x, Vector y)
Description copied from interface:Matrixy = A*x + y
-
multAdd
public Vector multAdd(double alpha, Vector x, Vector y)
Description copied from interface:Matrixy = alpha*A*x + y
-
checkMultAdd
protected void checkMultAdd(Vector x, Vector y)
Checks the arguments tomultandmultAdd
-
transMult
public Vector transMult(Vector x, Vector y)
Description copied from interface:Matrixy = AT*x
-
transMult
public Vector transMult(double alpha, Vector x, Vector y)
Description copied from interface:Matrixy = alpha*AT*x
-
transMultAdd
public Vector transMultAdd(Vector x, Vector y)
Description copied from interface:Matrixy = AT*x + y- Specified by:
transMultAddin interfaceMatrix- Parameters:
x- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
transMultAdd
public Vector transMultAdd(double alpha, Vector x, Vector y)
Description copied from interface:Matrixy = alpha*AT*x + y- Specified by:
transMultAddin interfaceMatrixx- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
checkTransMultAdd
protected void checkTransMultAdd(Vector x, Vector y)
Checks the arguments totransMultandtransMultAdd
-
solve
public Vector solve(Vector b, Vector x)
Description copied from interface:Matrixx = A\b. Not all matrices support this operation, those that do not throwUnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
-
transSolve
public Vector transSolve(Vector b, Vector x)
Description copied from interface:Matrixx = AT\b. Not all matrices support this operation, those that do not throwUnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver- Specified by:
transSolvein interfaceMatrix- Parameters:
b- Vector of sizeA.numColumns()x- Vector of sizeA.numRows()- Returns:
- x
-
checkSolve
protected void checkSolve(Vector b, Vector x)
Checks that a matrix inversion is legal for the given arguments. This is for the square case, not for least-squares problems
-
rank1
public Matrix rank1(Vector x)
Description copied from interface:MatrixA = x*xT + A. The matrix must be square, and the vector of the same length
-
rank1
public Matrix rank1(double alpha, Vector x)
Description copied from interface:MatrixA = alpha*x*xT + A. The matrix must be square, and the vector of the same length
-
rank1
public Matrix rank1(Vector x, Vector y)
Description copied from interface:MatrixA = x*yT + A. The matrix must be square, and the vectors of the same length
-
rank1
public Matrix rank1(double alpha, Vector x, Vector y)
Description copied from interface:MatrixA = alpha*x*yT + A. The matrix must be square, and the vectors of the same length
-
checkRank1
protected void checkRank1(Vector x, Vector y)
Checks that a vector rank1 update is possible for the given vectors
-
rank2
public Matrix rank2(Vector x, Vector y)
Description copied from interface:MatrixA = x*yT + y*xT + A. The matrix must be square, and the vectors of the same length
-
rank2
public Matrix rank2(double alpha, Vector x, Vector y)
Description copied from interface:MatrixA = alpha*x*yT + alpha*y*xT + A. The matrix must be square, and the vectors of the same length
-
checkRank2
protected void checkRank2(Vector x, Vector y)
Checks that a vector rank2 update is legal with the given vectors
-
mult
public Matrix mult(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*A*B
-
multAdd
public Matrix multAdd(Matrix B, Matrix C)
Description copied from interface:MatrixC = A*B + C
-
multAdd
public Matrix multAdd(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*A*B + C
-
checkMultAdd
protected void checkMultAdd(Matrix B, Matrix C)
Checks the arguments tomultandmultAdd
-
transAmult
public Matrix transAmult(Matrix B, Matrix C)
Description copied from interface:MatrixC = AT*B- Specified by:
transAmultin interfaceMatrix- Parameters:
B- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transAmult
public Matrix transAmult(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*AT*B- Specified by:
transAmultin interfaceMatrixB- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transAmultAdd
public Matrix transAmultAdd(Matrix B, Matrix C)
Description copied from interface:MatrixC = AT*B + C- Specified by:
transAmultAddin interfaceMatrix- Parameters:
B- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transAmultAdd
public Matrix transAmultAdd(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*AT*B + C- Specified by:
transAmultAddin interfaceMatrixB- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
checkTransAmultAdd
protected void checkTransAmultAdd(Matrix B, Matrix C)
Checks the arguments totransAmultandtransAmultAdd
-
transBmult
public Matrix transBmult(Matrix B, Matrix C)
Description copied from interface:MatrixC = A*BT- Specified by:
transBmultin interfaceMatrix- Parameters:
B- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transBmult
public Matrix transBmult(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*A*BT- Specified by:
transBmultin interfaceMatrixB- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transBmultAdd
public Matrix transBmultAdd(Matrix B, Matrix C)
Description copied from interface:MatrixC = A*BT + C- Specified by:
transBmultAddin interfaceMatrix- Parameters:
B- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
transBmultAdd
public Matrix transBmultAdd(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*A*BT + C- Specified by:
transBmultAddin interfaceMatrixB- Matrix such thatB.numRows() == A.numRows()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numColumns() == C.numColumns()- Returns:
- C
-
checkTransBmultAdd
protected void checkTransBmultAdd(Matrix B, Matrix C)
Checks the arguments totransBmultandtransBmultAdd
-
transABmult
public Matrix transABmult(Matrix B, Matrix C)
Description copied from interface:MatrixC = AT*BT- Specified by:
transABmultin interfaceMatrix- Parameters:
B- Matrix such thatB.numColumns() == A.numRows()andB.numRows() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numRows() == C.numColumns()- Returns:
- C
-
transABmult
public Matrix transABmult(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*AT*BT- Specified by:
transABmultin interfaceMatrixB- Matrix such thatB.numColumns() == A.numRows()andB.numRows() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numRows() == C.numColumns()- Returns:
- C
-
transABmultAdd
public Matrix transABmultAdd(Matrix B, Matrix C)
Description copied from interface:MatrixC = AT*BT + C- Specified by:
transABmultAddin interfaceMatrix- Parameters:
B- Matrix such thatB.numColumns() == A.numRows()andB.numRows() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numRows() == C.numColumns()- Returns:
- C
-
transABmultAdd
public Matrix transABmultAdd(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixC = alpha*AT*BT + C- Specified by:
transABmultAddin interfaceMatrixB- Matrix such thatB.numColumns() == A.numRows()andB.numRows() == C.numColumns()C- Matrix such thatC.numRows() == A.numColumns()andB.numRows() == C.numColumns()- Returns:
- C
-
checkTransABmultAdd
protected void checkTransABmultAdd(Matrix B, Matrix C)
Checks the arguments totransABmultAddandtransABmultAdd
-
solve
public Matrix solve(Matrix B, Matrix X)
Description copied from interface:MatrixX = A\B. Not all matrices support this operation, those that do not throwUnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
-
transSolve
public Matrix transSolve(Matrix B, Matrix X)
Description copied from interface:MatrixX = AT\B. Not all matrices support this operation, those that do not throwUnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated transpose solver- Specified by:
transSolvein interfaceMatrix- Parameters:
B- Matrix with a number of rows equalA.numColumns(), and the same number of columns asXX- Matrix with the same number of rows asA, and the same number of columns asB- Returns:
- X
-
checkSolve
protected void checkSolve(Matrix B, Matrix X)
Checks that a matrix inversion is legal for the given arguments. This is for the square case, not for least-squares problems
-
rank1
public Matrix rank1(Matrix C)
Description copied from interface:MatrixA = C*CT + A. The matrices must be square and of the same size
-
rank1
public Matrix rank1(double alpha, Matrix C)
Description copied from interface:MatrixA = alpha*C*CT + A. The matrices must be square and of the same size
-
checkRank1
protected void checkRank1(Matrix C)
Checks that a matrix rank1 update is possible for the given matrix
-
transRank1
public Matrix transRank1(Matrix C)
Description copied from interface:MatrixA = CT*C + AThe matrices must be square and of the same size- Specified by:
transRank1in interfaceMatrix- Returns:
- A
-
transRank1
public Matrix transRank1(double alpha, Matrix C)
Description copied from interface:MatrixA = alpha*CT*C + AThe matrices must be square and of the same size- Specified by:
transRank1in interfaceMatrix- Returns:
- A
-
checkTransRank1
protected void checkTransRank1(Matrix C)
Checks that a transposed rank1 update is leagal with the given argument
-
rank2
public Matrix rank2(Matrix B, Matrix C)
Description copied from interface:MatrixA = B*CT + C*BT + A. This matrix must be square
-
rank2
public Matrix rank2(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixA = alpha*B*CT + alpha*C*BT + A. This matrix must be square
-
checkRank2
protected void checkRank2(Matrix B, Matrix C)
Checks that a rank2 update is legal for the given arguments
-
transRank2
public Matrix transRank2(Matrix B, Matrix C)
Description copied from interface:MatrixA = BT*C + CT*B + A. This matrix must be square- Specified by:
transRank2in interfaceMatrix- Parameters:
B- Matrix with the same number of rows asCand the same number of columns asAC- Matrix with the same number of rows asBand the same number of columns asA- Returns:
- A
-
transRank2
public Matrix transRank2(double alpha, Matrix B, Matrix C)
Description copied from interface:MatrixA = alpha*BT*C + alpha*CT*B + A. This matrix must be square- Specified by:
transRank2in interfaceMatrixB- Matrix with the same number of rows asCand the same number of columns asAC- Matrix with the same number of rows asBand the same number of columns asA- Returns:
- A
-
checkTransRank2
protected void checkTransRank2(Matrix B, Matrix C)
Checks that a transposed rank2 update is leagal with the given arguments
-
set
public Matrix set(Matrix B)
Description copied from interface:MatrixA=B. The matrices must be of the same size
-
set
public Matrix set(double alpha, Matrix B)
Description copied from interface:MatrixA=alpha*B. The matrices must be of the same size
-
add
public Matrix add(Matrix B)
Description copied from interface:MatrixA = B + A. The matrices must be of the same size
-
add
public Matrix add(double alpha, Matrix B)
Description copied from interface:MatrixA = alpha*B + A. The matrices must be of the same size
-
checkSize
protected void checkSize(Matrix B)
Checks that the sizes of this matrix and the given conform
-
transpose
public Matrix transpose()
Description copied from interface:MatrixTransposes the matrix in-place. In most cases, the matrix must be square for this to work.
-
checkTranspose
protected void checkTranspose()
Checks that the matrix may be transposed
-
transpose
public Matrix transpose(Matrix B)
Description copied from interface:MatrixSets the tranpose of this matrix intoB. Matrix dimensions must be compatible
-
checkTranspose
protected void checkTranspose(Matrix B)
Checks that this matrix can be transposed into the given matrix
-
norm
public double norm(Matrix.Norm type)
Description copied from interface:MatrixComputes the given norm of the matrix
-
norm1
protected double norm1()
Computes the 1 norm
-
normF
protected double normF()
Computes the Frobenius norm. This implementation is overflow resistant
-
normInf
protected double normInf()
Computes the infinity norm
-
max
protected double max()
Returns the largest absolute value
-
max
protected double max(double[] x)
Returns the largest element of the passed array
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
iterator
public java.util.Iterator<MatrixEntry> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<MatrixEntry>
-
-