Interface Matrix
- All Superinterfaces:
Iterable<MatrixEntry>
- All Known Implementing Classes:
AbstractMatrix,BandMatrix,CompColMatrix,CompDiagMatrix,CompRowMatrix,DenseMatrix,DistColMatrix,DistRowMatrix,FlexCompColMatrix,FlexCompRowMatrix,LowerSPDBandMatrix,LowerSPDDenseMatrix,LowerSPDPackMatrix,LowerSymmBandMatrix,LowerSymmDenseMatrix,LowerSymmPackMatrix,LowerTriangBandMatrix,LowerTriangDenseMatrix,LowerTriangPackMatrix,SPDTridiagMatrix,SymmTridiagMatrix,TridiagMatrix,UnitLowerTriangBandMatrix,UnitLowerTriangDenseMatrix,UnitLowerTriangPackMatrix,UnitUpperTriangBandMatrix,UnitUpperTriangDenseMatrix,UnitUpperTriangPackMatrix,UpperSPDBandMatrix,UpperSPDDenseMatrix,UpperSPDPackMatrix,UpperSymmBandMatrix,UpperSymmDenseMatrix,UpperSymmPackMatrix,UpperTriangBandMatrix,UpperTriangDenseMatrix,UpperTriangPackMatrix
doubles in a rectangular 2D
array, and it is used alongside Vector in numerical
computations. Implementing classes decides on the actual storage.
Basic operations
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.
Iterators
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.
Basic linear algebra
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:
- Additions
- Matrices can be added to each other, even if their underlying matrix structures are different.
- Multiplications
- A matrix can be multiplied with vectors and other matrices. For increased efficiency, a multiplication can be combined with addition and scaling, and transpose matrix multiplications are also available.
- Rank-updates
- A matrix can be efficiently updated using low-rank updates. The updates can be contained in both matrices or vectors.
- Transpositions
- In-place transpositions of square matrices is supported, and the transpose of a matrix can be stored in another matrix of compatible size (possibly non-rectangular)
- Solvers
- Many dense and structured sparse matrices have fast, direct solvers, and can be used to solve linear systems without creating a factorization. These solvers are typically backed by subroutines in LAPACK
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionA = alpha*B + A.voidadd(int row, int column, double value) A(row,column) += valueA = B + A.copy()Creates a deep copy of the matrixdoubleget(int row, int column) ReturnsA(row,column)booleanisSquare()Returns true if the matrix is squareC = alpha*A*By = alpha*A*xC = A*By = A*xC = alpha*A*B + Cy = alpha*A*x + yC = A*B + Cy = A*x + ydoublenorm(Matrix.Norm type) Computes the given norm of the matrixintNumber of columns in the matrixintnumRows()Number of rows in the matrixA = alpha*C*CT + A.A = alpha*x*xT + A.A = alpha*x*yT + A.A = C*CT + A.A = x*xT + A.A = x*yT + A.A = alpha*B*CT + alpha*C*BT + A.A = alpha*x*yT + alpha*y*xT + A.A = B*CT + C*BT + A.A = x*yT + y*xT + A.scale(double alpha) A = alpha*AA=alpha*B.voidset(int row, int column, double value) A(row,column) = valueA=B.X = A\B.x = A\b.transABmult(double alpha, Matrix B, Matrix C) C = alpha*AT*BTtransABmult(Matrix B, Matrix C) C = AT*BTtransABmultAdd(double alpha, Matrix B, Matrix C) C = alpha*AT*BT + CtransABmultAdd(Matrix B, Matrix C) C = AT*BT + CtransAmult(double alpha, Matrix B, Matrix C) C = alpha*AT*BtransAmult(Matrix B, Matrix C) C = AT*BtransAmultAdd(double alpha, Matrix B, Matrix C) C = alpha*AT*B + CtransAmultAdd(Matrix B, Matrix C) C = AT*B + CtransBmult(double alpha, Matrix B, Matrix C) C = alpha*A*BTtransBmult(Matrix B, Matrix C) C = A*BTtransBmultAdd(double alpha, Matrix B, Matrix C) C = alpha*A*BT + CtransBmultAdd(Matrix B, Matrix C) C = A*BT + Cy = alpha*AT*xy = AT*xtransMultAdd(double alpha, Vector x, Vector y) y = alpha*AT*x + ytransMultAdd(Vector x, Vector y) y = AT*x + yTransposes the matrix in-place.Sets the tranpose of this matrix intoB.transRank1(double alpha, Matrix C) A = alpha*CT*C + AThe matrices must be square and of the same sizetransRank1(Matrix C) A = CT*C + AThe matrices must be square and of the same sizetransRank2(double alpha, Matrix B, Matrix C) A = alpha*BT*C + alpha*CT*B + A.transRank2(Matrix B, Matrix C) A = BT*C + CT*B + A.transSolve(Matrix B, Matrix X) X = AT\B.transSolve(Vector b, Vector x) x = AT\b.zero()Zeros all the entries in the matrix, while preserving any underlying structure.Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
numRows
int numRows()Number of rows in the matrix -
numColumns
int numColumns()Number of columns in the matrix -
isSquare
boolean isSquare()Returns true if the matrix is square -
set
void set(int row, int column, double value) A(row,column) = value -
add
void add(int row, int column, double value) A(row,column) += value -
get
double get(int row, int column) ReturnsA(row,column) -
copy
Matrix copy()Creates a deep copy of the matrix- Returns:
- A
-
zero
Matrix zero()Zeros all the entries in the matrix, while preserving any underlying structure. Useful for general, unstructured matrices.- Returns:
- A
-
mult
y = A*x- Parameters:
x- Vector of sizeA.numColumns()y- Vector of sizeA.numRows()- Returns:
- y
-
mult
y = alpha*A*x- Parameters:
x- Vector of sizeA.numColumns()y- Vector of sizeA.numRows()- Returns:
- y
-
multAdd
y = A*x + y- Parameters:
x- Vector of sizeA.numColumns()y- Vector of sizeA.numRows()- Returns:
- y
-
multAdd
y = alpha*A*x + y- Parameters:
x- Vector of sizeA.numColumns()y- Vector of sizeA.numRows()- Returns:
- y
-
transMult
y = AT*x- Parameters:
x- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
transMult
y = alpha*AT*x- Parameters:
x- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
transMultAdd
y = AT*x + y- Parameters:
x- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
transMultAdd
y = alpha*AT*x + y- Parameters:
x- Vector of sizeA.numRows()y- Vector of sizeA.numColumns()- Returns:
- y
-
solve
x = 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- Parameters:
b- Vector of sizeA.numRows()x- Vector of sizeA.numColumns()- Returns:
- x
- Throws:
MatrixSingularException- If the matrix is singularMatrixNotSPDException- If the solver assumes that the matrix is symmetrical, positive definite, but that that property does not hold
-
transSolve
x = 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- Parameters:
b- Vector of sizeA.numColumns()x- Vector of sizeA.numRows()- Returns:
- x
- Throws:
MatrixSingularException- If the matrix is singularMatrixNotSPDException- If the solver assumes that the matrix is symmetrical, positive definite, but that that property does not hold
-
rank1
A = x*xT + A. The matrix must be square, and the vector of the same length- Returns:
- A
-
rank1
A = alpha*x*xT + A. The matrix must be square, and the vector of the same length- Returns:
- A
-
rank1
A = x*yT + A. The matrix must be square, and the vectors of the same length- Returns:
- A
-
rank1
A = alpha*x*yT + A. The matrix must be square, and the vectors of the same length- Returns:
- A
-
rank2
A = x*yT + y*xT + A. The matrix must be square, and the vectors of the same length- Returns:
- A
-
rank2
A = alpha*x*yT + alpha*y*xT + A. The matrix must be square, and the vectors of the same length- Returns:
- A
-
mult
C = A*B- Parameters:
B- Matrix such thatB.numRows() == A.numColumns()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numRows()andB.numColumns() == C.numColumns()- Returns:
- C
-
mult
C = alpha*A*B- Parameters:
B- Matrix such thatB.numRows() == A.numColumns()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numRows()andB.numColumns() == C.numColumns()- Returns:
- C
-
multAdd
C = A*B + C- Parameters:
B- Matrix such thatB.numRows() == A.numColumns()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numRows()andB.numColumns() == C.numColumns()- Returns:
- C
-
multAdd
C = alpha*A*B + C- Parameters:
B- Matrix such thatB.numRows() == A.numColumns()andB.numColumns() == C.numColumns()C- Matrix such thatC.numRows() == A.numRows()andB.numColumns() == C.numColumns()- Returns:
- C
-
transAmult
C = AT*B- 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
C = alpha*AT*B- 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
C = AT*B + C- 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
C = alpha*AT*B + C- 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
C = A*BT- 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
C = alpha*A*BT- 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
C = A*BT + C- 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
C = alpha*A*BT + C- 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
-
transABmult
C = AT*BT- 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
C = alpha*AT*BT- 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
C = AT*BT + C- 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
C = alpha*AT*BT + C- 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
-
solve
X = 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- Parameters:
B- Matrix with the same number of rows asA, and the same number of columns asXX- Matrix with a number of rows equalA.numColumns(), and the same number of columns asB- Returns:
- X
- Throws:
MatrixSingularException- If the matrix is singularMatrixNotSPDException- If the solver assumes that the matrix is symmetrical, positive definite, but that that property does not hold
-
transSolve
X = 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- 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
- Throws:
MatrixSingularException- If the matrix is singularMatrixNotSPDException- If the solver assumes that the matrix is symmetrical, positive definite, but that that property does not hold
-
rank1
A = C*CT + A. The matrices must be square and of the same size- Returns:
- A
-
rank1
A = alpha*C*CT + A. The matrices must be square and of the same size- Returns:
- A
-
transRank1
A = CT*C + AThe matrices must be square and of the same size- Returns:
- A
-
transRank1
A = alpha*CT*C + AThe matrices must be square and of the same size- Returns:
- A
-
rank2
A = B*CT + C*BT + A. This matrix must be square- Parameters:
B- Matrix with the same number of rows asAand the same number of columns asCC- Matrix with the same number of rows asAand the same number of columns asB- Returns:
- A
-
rank2
A = alpha*B*CT + alpha*C*BT + A. This matrix must be square- Parameters:
B- Matrix with the same number of rows asAand the same number of columns asCC- Matrix with the same number of rows asAand the same number of columns asB- Returns:
- A
-
transRank2
A = BT*C + CT*B + A. This matrix must be square- 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
A = alpha*BT*C + alpha*CT*B + A. This matrix must be square- 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
-
scale
A = alpha*A- Returns:
- A
-
set
A=B. The matrices must be of the same size- Returns:
- A
-
set
A=alpha*B. The matrices must be of the same size- Returns:
- A
-
add
A = B + A. The matrices must be of the same size- Returns:
- A
-
add
A = alpha*B + A. The matrices must be of the same size- Returns:
- A
-
transpose
Matrix transpose()Transposes the matrix in-place. In most cases, the matrix must be square for this to work.- Returns:
- This matrix
-
transpose
Sets the tranpose of this matrix intoB. Matrix dimensions must be compatible- Parameters:
B- Matrix with as many rows as this matrix has columns, and as many columns as this matrix has rows- Returns:
- The matrix
B=AT
-
norm
Computes the given norm of the matrix- Parameters:
type- The type of norm to compute
-