Interface ConstMatrix<T extends ConstMatrix<T>>
- All Known Implementing Classes:
SimpleBase
,SimpleMatrix
Interface that only implements operations in SimpleBase
that are read only.
A "shallow immutable" matrix where none of its API allow you to modify the matrix. However (similar to const
in C++) you can access the modifiable matrix by downcasting to SimpleMatrix
or by externally modifying
in a function that has access to the original object. This interface acts as a strong suggestion that the matrix
should not be modified. However, the only way to ensure that no external code modifies this matrix is to create a
local copy that can't be accessed externally.
NOTE: Implementations of ConstMatrix must extend SimpleBase
or else it won't work when given as
input to any class based off of SimpleBase
.
-
Method Summary
Modifier and TypeMethodDescriptionint
bits()
Size of internal array elements.cols
(int begin, int end) Extracts the specified columns from the matrix.combine
(int insertRow, int insertCol, ConstMatrix<?> B) Creates a new matrix that is a combination of this matrix and matrix B.concatColumns
(ConstMatrix<?>... matrices) Concatenates all the matrices together along their columns.concatRows
(ConstMatrix<?>... matrices) Concatenates all the matrices together along their columns.double
The condition p = 2 number of a matrix is used to measure the sensitivity of the linear system Ax=b.Returns the complex conjugate of this matrix.copy()
Creates and returns a matrix which is identical to this one.Creates a matrix that is the same type and shapedouble
Computes the determinant of the matrix.Computes the determinant of a complex matrix.diag()
If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returneddivide
(double val) Returns the result of dividing each element by 'val': bi,j = ai,j/valdouble
dot
(ConstMatrix<?> v) Computes the dot product (a.k.a.elementDiv
(ConstMatrix<?> b) Returns a matrix which is the result of an element by element division of 'this' and 'b': ci,j = ai,j/bi,jReturns a matrix which is the result of an element by element exp of 'this' ci,j = Math.exp(ai,j)Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.log(ai,j)double
Returns the maximum real value of all the elements in this matrix.double
Returns the maximum absolute value of all the elements in this matrix.double
Returns the minimum real value of all the elements in this matrix.double
Returns the minimum absolute value of all the elements in this matrix.elementMult
(ConstMatrix<?> b) Returns a matrix which is the result of an element by element multiplication of 'this' and 'b': ci,j = ai,j*bi,jApplies a user defined complex-valued function to a real or complex-valued matrix.Applies a user defined real-valued function to a real-valued matrix.elementPower
(double b) Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ belementPower
(ConstMatrix<?> b) Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ bi,jdouble
Computes the sum of all the elements in the matrix.Computes the sum of all the elements in the matrix.extractMatrix
(int y0, int y1, int x0, int x1) Creates a new SimpleMatrix which is a submatrix of this matrix.extractVector
(boolean extractRow, int element) Extracts a row or column from this matrix.double
get
(int index) Returns the value of the matrix at the specified index of the 1D row major array.double
get
(int row, int col) Returns the value of the specified matrix element.void
get
(int row, int col, Complex_F64 output) Used to get the complex value of a matrix element.getColumn
(int col) Returns the specified column in 'this' matrix as a column vector.default double
getImag
(int row, int col) Shorthand forgetImaginary(int, int)
double
getImaginary
(int row, int col) Returns the imaginary component of the element.int
getIndex
(int row, int col) Returns the index in the matrix's array.int
Returns the number of columns in this matrix.default int
Returns the number of elements in this matrix, which is equal to the number of rows times the number of columns.int
Returns the number of rows in this matrix.double
getReal
(int row, int col) Returns the real component of the element.getRow
(int row) Returns the specified row in 'this' matrix as a row vector.getType()
Returns the type of matrix it is wrapping.boolean
Checks to see if any of the elements in this matrix are either NaN or infinite.default T
imag()
Convenience function.Returns a matrix that contains the imaginary valued portion of a complex matrix.invert()
Returns the inverse of this matrix.
b = a-1boolean
isIdentical
(ConstMatrix<?> a, double tol) Checks to see if matrix 'a' is the same as this matrix within the specified tolerance.boolean
isInBounds
(int row, int col) Returns true of the specified matrix element is valid element inside this matrix.boolean
isVector()
Returns true if this matrix is a vector.iterator
(boolean rowMajor, int minRow, int minCol, int maxRow, int maxCol) Creates a new iterator for traversing through a submatrix inside this matrix.kron
(ConstMatrix<?> B) Computes the Kronecker product between this matrix and the provided B matrix:
C = kron(A,B)Returns a real matrix that has the complex magnitude of each element in the matrix.minus
(double b) Returns the result of matrix-double subtraction:
c = a - b
where c is the returned matrix, a is this matrix, and b is the passed in double.minus
(ConstMatrix<?> B) Returns the result of matrix subtraction:
c = a - b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.minusComplex
(double real, double imag) Subtracts a complex scalar from each element in the matrix.mult
(ConstMatrix<?> B) Returns a matrix which is the result of matrix multiplication:
c = a * b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.negative()
Returns a new matrix whose elements are the negative of 'this' matrix's elements.
bij = -aijdouble
normF()
Computes the Frobenius normal of the matrix:
normF = Sqrt{ ∑i=1:m ∑j=1:n { aij2} }plus
(double b) Returns the result of scalar addition:
c = a + b
where c is the returned matrix, a is this matrix, and b is the passed in double.plus
(double beta, ConstMatrix<?> B) Performs a matrix addition and scale operation.
c = a + β*b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.plus
(ConstMatrix<?> B) Returns the result of matrix addition:
c = a + b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.plusComplex
(double real, double imag) Adds a complex scalar from each element in the matrix.void
print()
Prints the matrix to standard out.void
Prints the matrix to standard out given aPrintStream.printf(java.lang.String, java.lang.Object...)
style floating point format, e.g.Computes the Moore-Penrose pseudo-inversereal()
Returns a matrix that contains the real valued portion of a complex matrix.rows
(int begin, int end) Extracts the specified rows from the matrix.void
saveToFileCSV
(String fileName) Saves this matrix to a file in a CSV format.void
saveToMatrixMarket
(String fileName) Saves this matrix to a file in a matrix market format.scale
(double val) Returns the result of scaling each element by 'val':
bi,j = val*ai,jscaleComplex
(double real, double imag) Scales/multiplies each element in the matrix by the complex number.solve
(ConstMatrix<?> B) Solves for X in the following equation:
x = a-1b
where 'a' is this matrix and 'b' is an n by p matrix.double[][]
toArray2()
Returns 2D array of doubles using theSimpleBase.get(int, int)
method.double
trace()
Computes the trace of the matrix.Computes the trace of a complex matrix.Returns the transpose of this matrix.
aTReturns a matrix that is the conjugate transpose.
-
Method Details
-
transpose
T transpose()Returns the transpose of this matrix.
aT- Returns:
- A matrix that is n by m.
- See Also:
-
transposeConjugate
T transposeConjugate()Returns a matrix that is the conjugate transpose. If real then this is the same as callingtranspose()
. -
mult
Returns a matrix which is the result of matrix multiplication:
c = a * b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.- Parameters:
B
- A matrix that is n by p. Not modified.- Returns:
- The results of this operation.
- See Also:
-
kron
Computes the Kronecker product between this matrix and the provided B matrix:
C = kron(A,B)- Parameters:
B
- The right matrix in the operation. Not modified.- Returns:
- Kronecker product between this matrix and B.
- See Also:
-
plus
Returns the result of matrix addition:
c = a + b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.- Parameters:
B
- m by n matrix. Not modified.- Returns:
- The results of this operation.
-
minus
Returns the result of matrix subtraction:
c = a - b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.- Parameters:
B
- m by n matrix. Not modified.- Returns:
- The results of this operation.
- See Also:
-
minus
Returns the result of matrix-double subtraction:
c = a - b
where c is the returned matrix, a is this matrix, and b is the passed in double.NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.
- Parameters:
b
- Value subtracted from each element- Returns:
- The results of this operation.
- See Also:
-
minusComplex
Subtracts a complex scalar from each element in the matrix. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.- Parameters:
real
- Real component of scalar valueimag
- Imaginary component of scalar value- Returns:
- The results of this operation.
-
plus
Returns the result of scalar addition:
c = a + b
where c is the returned matrix, a is this matrix, and b is the passed in double.NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.
- Parameters:
b
- Value added to each element- Returns:
- A matrix that contains the results.
- See Also:
-
plusComplex
Adds a complex scalar from each element in the matrix. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.- Parameters:
real
- Real component of scalar valueimag
- Imaginary component of scalar value- Returns:
- The results of this operation.
-
plus
Performs a matrix addition and scale operation.
c = a + β*b
where c is the returned matrix, a is this matrix, and b is the passed in matrix.NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.
- Parameters:
B
- m by n matrix. Not modified.- Returns:
- A matrix that contains the results.
- See Also:
-
dot
Computes the dot product (a.k.a. inner product) between this vector and vector 'v'.- Parameters:
v
- The second vector in the dot product. Not modified.- Returns:
- dot product
-
isVector
boolean isVector()Returns true if this matrix is a vector. A vector is defined as a matrix that has either one row or column.- Returns:
- Returns true for vectors and false otherwise.
-
scale
Returns the result of scaling each element by 'val':
bi,j = val*ai,j- Parameters:
val
- The multiplication factor. If matrix is complex then the imaginary component is zero.- Returns:
- The scaled matrix.
- See Also:
-
scaleComplex
Scales/multiplies each element in the matrix by the complex number. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.- Parameters:
real
- Real component of scalar valueimag
- Imaginary component of scalar value- Returns:
- Scaled matrix
-
divide
Returns the result of dividing each element by 'val': bi,j = ai,j/val
- Parameters:
val
- Divisor. If matrix is complex then the imaginary component is zero.- Returns:
- Matrix with its elements divided by the specified value.
- See Also:
-
invert
T invert()Returns the inverse of this matrix.
b = a-1
If the matrix could not be inverted then SingularMatrixException is thrown. Even if no exception is thrown the matrix could still be singular or nearly singular.
- Returns:
- The inverse of this matrix.
- See Also:
-
pseudoInverse
T pseudoInverse()Computes the Moore-Penrose pseudo-inverse
- Returns:
- inverse computed using the pseudo inverse.
-
solve
Solves for X in the following equation:
x = a-1b
where 'a' is this matrix and 'b' is an n by p matrix.If the system could not be solved then SingularMatrixException is thrown. Even if no exception is thrown 'a' could still be singular or nearly singular.
- Parameters:
B
- n by p matrix. Not modified.- Returns:
- The solution for 'x' that is n by p.
- See Also:
-
normF
double normF()Computes the Frobenius normal of the matrix:
normF = Sqrt{ ∑i=1:m ∑j=1:n { aij2} }- Returns:
- The matrix's Frobenius normal.
- See Also:
-
conditionP2
double conditionP2()The condition p = 2 number of a matrix is used to measure the sensitivity of the linear system Ax=b. A value near one indicates that it is a well conditioned matrix.
- Returns:
- The condition number.
- See Also:
-
determinant
double determinant()Computes the determinant of the matrix.- Returns:
- The determinant.
- See Also:
-
determinantComplex
Complex_F64 determinantComplex()Computes the determinant of a complex matrix. If the matrix is real then the imaginary component is always zero.- Returns:
- The determinant.
- See Also:
-
trace
double trace()Computes the trace of the matrix.
- Returns:
- The trace of the matrix.
- See Also:
-
traceComplex
Complex_F64 traceComplex()Computes the trace of a complex matrix. If the matrix is real then the imaginary component is always zero.
- Returns:
- The trace of the matrix.
- See Also:
-
get
double get(int row, int col) Returns the value of the specified matrix element. Performs a bounds check to make sure the requested element is part of the matrix.NOTE: Complex matrices will throw an exception
- Parameters:
row
- The row of the element.col
- The column of the element.- Returns:
- The value of the element.
-
get
double get(int index) Returns the value of the matrix at the specified index of the 1D row major array.- Parameters:
index
- The element's index whose value is to be returned- Returns:
- The value of the specified element.
- See Also:
-
get
Used to get the complex value of a matrix element.- Parameters:
row
- The row of the element.col
- The column of the element.output
- Storage for the value
-
getReal
double getReal(int row, int col) Returns the real component of the element. If a real matrix this is the same as callingget(int, int)
.- Parameters:
row
- The row of the element.col
- The column of the element.
-
getImaginary
double getImaginary(int row, int col) Returns the imaginary component of the element. If a real matrix this will always be zero.- Parameters:
row
- The row of the element.col
- The column of the element.
-
getImag
default double getImag(int row, int col) Shorthand forgetImaginary(int, int)
-
getIndex
int getIndex(int row, int col) Returns the index in the matrix's array.- Parameters:
row
- The row number.col
- The column number.- Returns:
- The index of the specified element.
- See Also:
-
iterator
Creates a new iterator for traversing through a submatrix inside this matrix. It can be traversed by row or by column. Range of elements is inclusive, e.g. minRow = 0 and maxRow = 1 will include rows 0 and 1. The iteration starts at (minRow,minCol) and ends at (maxRow,maxCol)- Parameters:
rowMajor
- true means it will traverse through the submatrix by row first, false by columns.minRow
- first row it will start at.minCol
- first column it will start at.maxRow
- last row it will stop at.maxCol
- last column it will stop at.- Returns:
- A new MatrixIterator
-
copy
T copy()Creates and returns a matrix which is identical to this one.- Returns:
- A new identical matrix.
-
getNumRows
int getNumRows()Returns the number of rows in this matrix.- Returns:
- number of rows.
-
getNumCols
int getNumCols()Returns the number of columns in this matrix.- Returns:
- number of columns.
-
getNumElements
default int getNumElements()Returns the number of elements in this matrix, which is equal to the number of rows times the number of columns.- Returns:
- The number of elements in the matrix.
-
print
void print()Prints the matrix to standard out. -
print
Prints the matrix to standard out given a
PrintStream.printf(java.lang.String, java.lang.Object...)
style floating point format, e.g. print("%f"). -
toArray2
double[][] toArray2()Returns 2D array of doubles using theSimpleBase.get(int, int)
method.- Returns:
- 2D array of doubles.
-
extractMatrix
Creates a new SimpleMatrix which is a submatrix of this matrix.
si-y0 , j-x0 = oij for all y0 ≤ i < y1 and x0 ≤ j < x1
where 'sij' is an element in the submatrix and 'oij' is an element in the original matrix.If any of the inputs are set to SimpleMatrix.END then it will be set to the last row or column in the matrix.
- Parameters:
y0
- Start row.y1
- Stop row + 1.x0
- Start column.x1
- Stop column + 1.- Returns:
- The submatrix.
-
extractVector
Extracts a row or column from this matrix. The returned vector will either be a row or column vector depending on the input type.
- Parameters:
extractRow
- If true a row will be extracted.element
- The row or column the vector is contained in.- Returns:
- Extracted vector.
- See Also:
-
getRow
Returns the specified row in 'this' matrix as a row vector.- Parameters:
row
- Row in the matrix- Returns:
- Extracted vector
- See Also:
-
getColumn
Returns the specified column in 'this' matrix as a column vector.- Parameters:
col
- Column in the matrix- Returns:
- Extracted vector
- See Also:
-
diag
T diag()If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returned
- Returns:
- Diagonal elements inside a vector or a square matrix with the same diagonal elements.
- See Also:
-
isIdentical
Checks to see if matrix 'a' is the same as this matrix within the specified tolerance.- Parameters:
a
- The matrix it is being compared against.tol
- How similar they must be to be equals.- Returns:
- If they are equal within tolerance of each other.
-
hasUncountable
boolean hasUncountable()Checks to see if any of the elements in this matrix are either NaN or infinite.- Returns:
- True of an element is NaN or infinite. False otherwise.
-
combine
Creates a new matrix that is a combination of this matrix and matrix B. B is written into A at the specified location if needed the size of A is increased by growing it. A is grown by padding the new area with zeros.
While useful when adding data to a matrix which will be solved for it is also much less efficient than predeclaring a matrix and inserting data into it.
If insertRow or insertCol is set to SimpleMatrix.END then it will be combined at the last row or column respectively.
- Parameters:
insertRow
- Row where matrix B is written in to.insertCol
- Column where matrix B is written in to.B
- The matrix that is written into A.- Returns:
- A new combined matrix.
-
elementMax
double elementMax()Returns the maximum real value of all the elements in this matrix.- Returns:
- Largest real value of any element.
-
elementMin
double elementMin()Returns the minimum real value of all the elements in this matrix.- Returns:
- Smallest real value of any element.
-
elementMaxAbs
double elementMaxAbs()Returns the maximum absolute value of all the elements in this matrix. This is equivalent to the infinite p-norm of the matrix.- Returns:
- Largest absolute value of any element.
-
elementMinAbs
double elementMinAbs()Returns the minimum absolute value of all the elements in this matrix.- Returns:
- Smallest absolute value of any element.
-
elementSum
double elementSum()Computes the sum of all the elements in the matrix. Only works on real matrices.- Returns:
- Sum of all the elements.
-
elementSumComplex
Complex_F64 elementSumComplex()Computes the sum of all the elements in the matrix. Works with both real and complex matrices.- Returns:
- Sum of all the elements.
-
elementMult
Returns a matrix which is the result of an element by element multiplication of 'this' and 'b': ci,j = ai,j*bi,j
- Parameters:
b
- A simple matrix.- Returns:
- The element by element multiplication of 'this' and 'b'.
-
elementDiv
Returns a matrix which is the result of an element by element division of 'this' and 'b': ci,j = ai,j/bi,j
- Parameters:
b
- A simple matrix.- Returns:
- The element by element division of 'this' and 'b'.
-
elementPower
Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ bi,j
- Parameters:
b
- A simple matrix.- Returns:
- The element by element power of 'this' and 'b'.
-
elementPower
Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ b
- Parameters:
b
- Scalar- Returns:
- The element by element power of 'this' and 'b'.
-
elementExp
T elementExp()Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.exp(ai,j)
- Returns:
- The element by element power of 'this' and 'b'.
-
elementLog
T elementLog()Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.log(ai,j)
- Returns:
- The element by element power of 'this' and 'b'.
-
elementOp
Applies a user defined real-valued function to a real-valued matrix.
ci,j = op(i, j, ai,j)If the matrix is sparse then this is only applied to non-zero elements
-
elementOp
Applies a user defined complex-valued function to a real or complex-valued matrix.
ci,j = op(i, j, ai,j)If the matrix is sparse then this is only applied to non-zero elements
-
negative
T negative()Returns a new matrix whose elements are the negative of 'this' matrix's elements.
bij = -aij- Returns:
- A matrix that is the negative of the original.
-
conjugate
T conjugate()Returns the complex conjugate of this matrix.
-
magnitude
T magnitude()Returns a real matrix that has the complex magnitude of each element in the matrix. For a real matrix this is the abs()
-
saveToFileCSV
Saves this matrix to a file in a CSV format. For the file format see
MatrixIO
.- Throws:
IOException
-
saveToMatrixMarket
Saves this matrix to a file in a matrix market format. For the file format see
MatrixIO
.- Throws:
IOException
-
isInBounds
boolean isInBounds(int row, int col) Returns true of the specified matrix element is valid element inside this matrix.- Parameters:
row
- Row index.col
- Column index.- Returns:
- true if it is a valid element in the matrix.
-
bits
int bits()Size of internal array elements. 32 or 64 bits -
rows
Extracts the specified rows from the matrix.- Parameters:
begin
- First row (inclusive).end
- Last row (exclusive).- Returns:
- Submatrix that contains the specified rows.
-
cols
Extracts the specified columns from the matrix.- Parameters:
begin
- First column (inclusive).end
- Last column (exclusive).- Returns:
- Submatrix that contains the specified columns.
-
concatColumns
Concatenates all the matrices together along their columns. If the rows do not match the upper elements are set to zero.
A = [ this, m[0] , ... , m[n-1] ]- Parameters:
matrices
- Set of matrices- Returns:
- Resulting matrix
-
concatRows
Concatenates all the matrices together along their columns. If the rows do not match the upper elements are set to zero.
A = [ this; m[0] ; ... ; m[n-1] ]- Parameters:
matrices
- Set of matrices- Returns:
- Resulting matrix
-
getType
MatrixType getType()Returns the type of matrix it is wrapping. -
real
T real()Returns a matrix that contains the real valued portion of a complex matrix. For a real valued matrix this will return a copy. -
imaginary
T imaginary()Returns a matrix that contains the imaginary valued portion of a complex matrix. For a real valued matrix this will return a matrix full of zeros. -
imag
Convenience function. Seeimaginary()
-
createLike
T createLike()Creates a matrix that is the same type and shape- Returns:
- New matrix
-