Class Matrix<F extends Field<F>>
- All Implemented Interfaces:
javolution.lang.Immutable
,javolution.lang.Realtime
,javolution.lang.ValueType
,GroupAdditive<Matrix<F>>
,Ring<Matrix<F>>
,Structure<Matrix<F>>
,VectorSpace<Matrix<F>,
F>
- Direct Known Subclasses:
ComplexMatrix
,DenseMatrix
,Float64Matrix
,SparseMatrix
This class represents a rectangular table of elements of a ring-like algebraic structure.
Instances of this class can be used to resolve system of linear equations
involving any kind of Field
elements
(e.g. Real
,
Complex
,
Amount<?>
,
Function
, etc).
For example:[code]
// Creates a dense matrix (2x2) of Rational numbers.
DenseMatrix
Non-commutative field multiplication is supported. Invertible square matrices may form a non-commutative field (also called a division ring). In which case this class may be used to resolve system of linear equations with matrix coefficients.
Implementation Note: Matrices may use StackContext
and ConcurrentContext
in order to
minimize heap allocation and accelerate calculations on multi-core
systems.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadjoint()
Returns the adjoint of this matrix.abstract F
cofactor
(int i, int j) Returns the cofactor of an element in this matrix.copy()
Returns a copy of this matrixallocated
by the calling thread (possibly on the stack).abstract F
Returns the determinant of this matrix.Returns this matrix divided by the one specified.boolean
Indicates if this matrix is strictly equal to the object specified.boolean
equals
(Matrix<F> that, Comparator<F> cmp) Indicates if this matrix can be considered equals to the one specified using the specified comparator when testing for element equality.abstract F
get
(int i, int j) Returns a single element from this matrix.getColumn
(int j) Returns the column identified by the specified index in this matrix.Returns the diagonal vector.abstract int
Returns the number of columnsn
for this matrix.abstract int
Returns the number of rowsm
for this matrix.getRow
(int i) Returns the row identified by the specified index in this matrix.int
hashCode()
Returns a hash code value for this matrix.inverse()
Returns the inverse of this matrix (must be square).boolean
isSquare()
Indicates if this matrix is square.Returns the difference between this matrix and the one specified.opposite()
Returns the negation of this matrix.Returns the sum of this matrix with the one specified.pow
(int exp) Returns this matrix raised at the specified exponent.Returns the inverse or pseudo-inverse if this matrix if not square.Solves this matrix for the specified matrix (returnsx
such asthis · x = y
).Solves this matrix for the specified vector (returnsx
such asthis · x = y
).Returns the linear algebraic matrix tensor product of this matrix and another (Kronecker product).Returns the product of this matrix by the specified factor.Returns the product of this matrix with the one specified.Returns the product of this matrix by the specified vector.final String
toString()
Returns the text representation of this matrix as ajava.lang.String
.javolution.text.Text
toText()
Returns the text representation of this matrix.trace()
Returns the trace of this matrix.Returns the transpose of this matrix.Returns the vectorization of this matrix.
-
Field Details
-
XML
Holds the default XML representation for matrices. For example:[code]
-
-
Constructor Details
-
Matrix
protected Matrix()Default constructor (for sub-classes).
-
-
Method Details
-
getNumberOfRows
public abstract int getNumberOfRows()Returns the number of rowsm
for this matrix.- Returns:
- m, the number of rows.
-
getNumberOfColumns
public abstract int getNumberOfColumns()Returns the number of columnsn
for this matrix.- Returns:
- n, the number of columns.
-
get
Returns a single element from this matrix.- Parameters:
i
- the row index (range [0..m[).j
- the column index (range [0..n[).- Returns:
- the element read at [i,j].
- Throws:
IndexOutOfBoundsException
-((i < 0) || (i >= m)) || ((j < 0) || (j >= n))
-
getRow
Returns the row identified by the specified index in this matrix.- Parameters:
i
- the row index (range [0..m[).- Returns:
- the vector holding the specified row.
- Throws:
IndexOutOfBoundsException
-(i < 0) || (i >= m)
-
getColumn
Returns the column identified by the specified index in this matrix.- Parameters:
j
- the column index (range [0..n[).- Returns:
- the vector holding the specified column.
- Throws:
IndexOutOfBoundsException
-(j < 0) || (j >= n)
-
getDiagonal
Returns the diagonal vector.- Returns:
- the vector holding the diagonal elements.
-
opposite
Returns the negation of this matrix.- Specified by:
opposite
in interfaceGroupAdditive<F extends Field<F>>
- Returns:
-this
.
-
plus
Returns the sum of this matrix with the one specified.- Specified by:
plus
in interfaceGroupAdditive<F extends Field<F>>
- Parameters:
that
- the matrix to be added.- Returns:
this + that
.- Throws:
DimensionException
- matrices's dimensions are different.
-
minus
Returns the difference between this matrix and the one specified.- Parameters:
that
- the matrix to be subtracted.- Returns:
this - that
.- Throws:
DimensionException
- matrices's dimensions are different.
-
times
Returns the product of this matrix by the specified factor. -
times
Returns the product of this matrix by the specified vector.- Parameters:
v
- the vector.- Returns:
this · v
- Throws:
DimensionException
- ifv.getDimension() != this.getNumberOfColumns()
-
times
Returns the product of this matrix with the one specified. -
inverse
Returns the inverse of this matrix (must be square).- Returns:
1 / this
- Throws:
DimensionException
- if this matrix is not square.
-
divide
Returns this matrix divided by the one specified.- Parameters:
that
- the matrix divisor.- Returns:
this / that
.- Throws:
DimensionException
- if that matrix is not square or dimensions do not match.
-
pseudoInverse
Returns the inverse or pseudo-inverse if this matrix if not square.Note: To resolve the equation
A * X = B
, it is usually faster to calculateA.lu().solve(B)
rather thanA.inverse().times(B)
.- Returns:
- the inverse or pseudo-inverse of this matrix.
-
determinant
Returns the determinant of this matrix.- Returns:
- this matrix determinant.
- Throws:
DimensionException
- if this matrix is not square.
-
transpose
Returns the transpose of this matrix.- Returns:
A'
.
-
cofactor
Returns the cofactor of an element in this matrix. It is the value obtained by evaluating the determinant formed by the elements not in that particular row or column.- Parameters:
i
- the row index.j
- the column index.- Returns:
- the cofactor of
THIS[i,j]
. - Throws:
DimensionException
- matrix is not square or its dimension is less than 2.
-
adjoint
Returns the adjoint of this matrix. It is obtained by replacing each element in this matrix with its cofactor and applying a + or - sign according (-1)**(i+j), and then finding the transpose of the resulting matrix.- Returns:
- the adjoint of this matrix.
- Throws:
DimensionException
- if this matrix is not square or if its dimension is less than 2.
-
isSquare
public boolean isSquare()Indicates if this matrix is square.- Returns:
getNumberOfRows() == getNumberOfColumns()
-
solve
Solves this matrix for the specified vector (returnsx
such asthis · x = y
).- Parameters:
y
- the vector for which the solution is calculated.- Returns:
x
such asthis · x = y
- Throws:
DimensionException
- if that matrix is not square or dimensions do not match.
-
solve
Solves this matrix for the specified matrix (returnsx
such asthis · x = y
).- Parameters:
y
- the matrix for which the solution is calculated.- Returns:
x
such asthis · x = y
- Throws:
DimensionException
- if that matrix is not square or dimensions do not match.
-
pow
Returns this matrix raised at the specified exponent.- Parameters:
exp
- the exponent.- Returns:
thisexp
- Throws:
DimensionException
- if this matrix is not square.
-
trace
Returns the trace of this matrix.- Returns:
- the sum of the diagonal elements.
-
tensor
Returns the linear algebraic matrix tensor product of this matrix and another (Kronecker product). The default implementation returns aDenseMatrix
.- Parameters:
that
- the second matrix.- Returns:
this ⊗ that
- See Also:
-
vectorization
Returns the vectorization of this matrix. The vectorization of a matrix is the column vector obtain by stacking the columns of the matrix on top of one another. The default implementation returns aDenseVector
.- Returns:
- the vectorization of this matrix.
- See Also:
-
toText
public javolution.text.Text toText()Returns the text representation of this matrix.- Specified by:
toText
in interfacejavolution.lang.Realtime
- Returns:
- the text representation of this matrix.
-
toString
Returns the text representation of this matrix as ajava.lang.String
. -
equals
Indicates if this matrix can be considered equals to the one specified using the specified comparator when testing for element equality. The specified comparator may allow for some tolerance in the difference between the matrix elements.- Parameters:
that
- the matrix to compare for equality.cmp
- the comparator to use when testing for element equality.- Returns:
true
if this matrix and the specified matrix are both matrices with equal elements according to the specified comparator;false
otherwise.
-
equals
Indicates if this matrix is strictly equal to the object specified. -
hashCode
public int hashCode()Returns a hash code value for this matrix. Equals objects have equal hash codes. -
copy
Returns a copy of this matrixallocated
by the calling thread (possibly on the stack).- Specified by:
copy
in interfacejavolution.lang.ValueType
- Returns:
- an identical and independant copy of this matrix.
-