Class MatrixUtils
- java.lang.Object
-
- org.hipparchus.linear.MatrixUtils
-
public class MatrixUtils extends Object
A collection of static methods that operate on or return matrices.
-
-
Field Summary
Fields Modifier and Type Field Description static RealMatrixFormat
DEFAULT_FORMAT
The default format forRealMatrix
objects.static RealMatrixFormat
OCTAVE_FORMAT
A format forRealMatrix
objects compatible with octave.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Array2DRowRealMatrix
bigFractionMatrixToRealMatrix(FieldMatrix<BigFraction> m)
static RealMatrix
blockInverse(RealMatrix m, int splitIndex)
Computes the inverse of the given matrix by splitting it into 4 sub-matrices.static void
checkAdditionCompatible(AnyMatrix left, AnyMatrix right)
Check if matrices are addition compatible.static void
checkColumnIndex(AnyMatrix m, int column)
Check if a column index is valid.static void
checkMatrixIndex(AnyMatrix m, int row, int column)
Check if matrix indices are valid.static void
checkMultiplicationCompatible(AnyMatrix left, AnyMatrix right)
Check if matrices are multiplication compatiblestatic void
checkRowIndex(AnyMatrix m, int row)
Check if a row index is valid.static void
checkSameColumnDimension(AnyMatrix left, AnyMatrix right)
Check if matrices have the same number of columns.static void
checkSameRowDimension(AnyMatrix left, AnyMatrix right)
Check if matrices have the same number of rows.static void
checkSubMatrixIndex(AnyMatrix m, int[] selectedRows, int[] selectedColumns)
Check if submatrix ranges indices are valid.static void
checkSubMatrixIndex(AnyMatrix m, int startRow, int endRow, int startColumn, int endColumn)
Check if submatrix ranges indices are valid.static void
checkSubtractionCompatible(AnyMatrix left, AnyMatrix right)
Check if matrices are subtraction compatiblestatic void
checkSymmetric(RealMatrix matrix, double eps)
Checks whether a matrix is symmetric.static <T extends FieldElement<T>>
FieldMatrix<T>createColumnFieldMatrix(T[] columnData)
Creates a columnFieldMatrix
using the data from the input array.static RealMatrix
createColumnRealMatrix(double[] columnData)
Creates a columnRealMatrix
using the data from the input array.static <T extends FieldElement<T>>
FieldMatrix<T>createFieldDiagonalMatrix(T[] diagonal)
Returns a diagonal matrix with specified elements.static <T extends FieldElement<T>>
FieldMatrix<T>createFieldIdentityMatrix(Field<T> field, int dimension)
Returnsdimension x dimension
identity matrix.static <T extends FieldElement<T>>
FieldMatrix<T>createFieldMatrix(Field<T> field, int rows, int columns)
Returns aFieldMatrix
with specified dimensions.static <T extends FieldElement<T>>
FieldMatrix<T>createFieldMatrix(T[][] data)
Returns aFieldMatrix
whose entries are the the values in the the input array.static <T extends FieldElement<T>>
FieldVector<T>createFieldVector(Field<T> field, int dimension)
Creates aFieldVector
with specified dimensions.static <T extends FieldElement<T>>
FieldVector<T>createFieldVector(T[] data)
Creates aFieldVector
using the data from the input array.static RealMatrix
createRealDiagonalMatrix(double[] diagonal)
Returns a diagonal matrix with specified elements.static RealMatrix
createRealIdentityMatrix(int dimension)
Returnsdimension x dimension
identity matrix.static RealMatrix
createRealMatrix(double[][] data)
Returns aRealMatrix
whose entries are the the values in the the input array.static RealMatrix
createRealMatrix(int rows, int columns)
Returns aRealMatrix
with specified dimensions.static RealVector
createRealVector(double[] data)
Creates aRealVector
using the data from the input array.static RealVector
createRealVector(int dimension)
Creates aRealVector
with specified dimensions.static <T extends FieldElement<T>>
FieldMatrix<T>createRowFieldMatrix(T[] rowData)
Create a rowFieldMatrix
using the data from the input array.static RealMatrix
createRowRealMatrix(double[] rowData)
Create a rowRealMatrix
using the data from the input array.static Array2DRowRealMatrix
fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
static RealMatrix
inverse(RealMatrix matrix)
Computes the inverse of the given matrix.static RealMatrix
inverse(RealMatrix matrix, double threshold)
Computes the inverse of the given matrix.static boolean
isSymmetric(RealMatrix matrix, double eps)
Checks whether a matrix is symmetric.static RealMatrix
matrixExponential(RealMatrix rm)
Computes the matrix exponential of the given matrix.static List<RealVector>
orthonormalize(List<RealVector> independent, double threshold, DependentVectorsHandler handler)
Orthonormalize a list of vectors.static <T extends CalculusFieldElement<T>>
List<FieldVector<T>>orthonormalize(Field<T> field, List<FieldVector<T>> independent, T threshold, DependentVectorsHandler handler)
Orthonormalize a list of vectors.static void
solveLowerTriangularSystem(RealMatrix rm, RealVector b)
Solve a system of composed of a Lower Triangular MatrixRealMatrix
.static void
solveUpperTriangularSystem(RealMatrix rm, RealVector b)
Solver a system composed of an Upper Triangular MatrixRealMatrix
.
-
-
-
Field Detail
-
DEFAULT_FORMAT
public static final RealMatrixFormat DEFAULT_FORMAT
The default format forRealMatrix
objects.
-
OCTAVE_FORMAT
public static final RealMatrixFormat OCTAVE_FORMAT
A format forRealMatrix
objects compatible with octave.
-
-
Method Detail
-
createRealMatrix
public static RealMatrix createRealMatrix(int rows, int columns)
Returns aRealMatrix
with specified dimensions.The type of matrix returned depends on the dimension. Below 212 elements (i.e. 4096 elements or 64×64 for a square matrix) which can be stored in a 32kB array, a
Array2DRowRealMatrix
instance is built. Above this threshold aBlockRealMatrix
instance is built.The matrix elements are all set to 0.0.
- Parameters:
rows
- number of rows of the matrixcolumns
- number of columns of the matrix- Returns:
- RealMatrix with specified dimensions
- See Also:
createRealMatrix(double[][])
-
createFieldMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(Field<T> field, int rows, int columns)
Returns aFieldMatrix
with specified dimensions.The type of matrix returned depends on the dimension. Below 212 elements (i.e. 4096 elements or 64×64 for a square matrix), a
FieldMatrix
instance is built. Above this threshold aBlockFieldMatrix
instance is built.The matrix elements are all set to field.getZero().
- Type Parameters:
T
- the type of the field elements- Parameters:
field
- field to which the matrix elements belongrows
- number of rows of the matrixcolumns
- number of columns of the matrix- Returns:
- FieldMatrix with specified dimensions
- See Also:
createFieldMatrix(FieldElement[][])
-
createRealMatrix
public static RealMatrix createRealMatrix(double[][] data) throws MathIllegalArgumentException, NullArgumentException
Returns aRealMatrix
whose entries are the the values in the the input array.The type of matrix returned depends on the dimension. Below 212 elements (i.e. 4096 elements or 64×64 for a square matrix) which can be stored in a 32kB array, a
Array2DRowRealMatrix
instance is built. Above this threshold aBlockRealMatrix
instance is built.The input array is copied, not referenced.
- Parameters:
data
- input array- Returns:
- RealMatrix containing the values of the array
- Throws:
MathIllegalArgumentException
- ifdata
is not rectangular (not all rows have the same length).MathIllegalArgumentException
- if a row or column is empty.NullArgumentException
- if eitherdata
ordata[0]
isnull
.MathIllegalArgumentException
- ifdata
is not rectangular.- See Also:
createRealMatrix(int, int)
-
createFieldMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(T[][] data) throws MathIllegalArgumentException, NullArgumentException
Returns aFieldMatrix
whose entries are the the values in the the input array.The type of matrix returned depends on the dimension. Below 212 elements (i.e. 4096 elements or 64×64 for a square matrix), a
FieldMatrix
instance is built. Above this threshold aBlockFieldMatrix
instance is built.The input array is copied, not referenced.
- Type Parameters:
T
- the type of the field elements- Parameters:
data
- input array- Returns:
- a matrix containing the values of the array.
- Throws:
MathIllegalArgumentException
- ifdata
is not rectangular (not all rows have the same length).MathIllegalArgumentException
- if a row or column is empty.NullArgumentException
- if eitherdata
ordata[0]
isnull
.- See Also:
createFieldMatrix(Field, int, int)
-
createRealIdentityMatrix
public static RealMatrix createRealIdentityMatrix(int dimension)
Returnsdimension x dimension
identity matrix.- Parameters:
dimension
- dimension of identity matrix to generate- Returns:
- identity matrix
- Throws:
IllegalArgumentException
- if dimension is not positive
-
createFieldIdentityMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createFieldIdentityMatrix(Field<T> field, int dimension)
Returnsdimension x dimension
identity matrix.- Type Parameters:
T
- the type of the field elements- Parameters:
field
- field to which the elements belongdimension
- dimension of identity matrix to generate- Returns:
- identity matrix
- Throws:
IllegalArgumentException
- if dimension is not positive
-
createRealDiagonalMatrix
public static RealMatrix createRealDiagonalMatrix(double[] diagonal)
Returns a diagonal matrix with specified elements.- Parameters:
diagonal
- diagonal elements of the matrix (the array elements will be copied)- Returns:
- diagonal matrix
-
createFieldDiagonalMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createFieldDiagonalMatrix(T[] diagonal)
Returns a diagonal matrix with specified elements.- Type Parameters:
T
- the type of the field elements- Parameters:
diagonal
- diagonal elements of the matrix (the array elements will be copied)- Returns:
- diagonal matrix
-
createRealVector
public static RealVector createRealVector(double[] data) throws MathIllegalArgumentException, NullArgumentException
Creates aRealVector
using the data from the input array.- Parameters:
data
- the input data- Returns:
- a data.length RealVector
- Throws:
MathIllegalArgumentException
- ifdata
is empty.NullArgumentException
- ifdata
isnull
.
-
createRealVector
public static RealVector createRealVector(int dimension)
Creates aRealVector
with specified dimensions.- Parameters:
dimension
- dimension of the vector- Returns:
- a new vector
- Since:
- 1.3
-
createFieldVector
public static <T extends FieldElement<T>> FieldVector<T> createFieldVector(T[] data) throws MathIllegalArgumentException, NullArgumentException
Creates aFieldVector
using the data from the input array.- Type Parameters:
T
- the type of the field elements- Parameters:
data
- the input data- Returns:
- a data.length FieldVector
- Throws:
MathIllegalArgumentException
- ifdata
is empty.NullArgumentException
- ifdata
isnull
.MathIllegalArgumentException
- ifdata
has 0 elements
-
createFieldVector
public static <T extends FieldElement<T>> FieldVector<T> createFieldVector(Field<T> field, int dimension)
Creates aFieldVector
with specified dimensions.- Type Parameters:
T
- the type of the field elements- Parameters:
field
- field to which array elements belongdimension
- dimension of the vector- Returns:
- a new vector
- Since:
- 1.3
-
createRowRealMatrix
public static RealMatrix createRowRealMatrix(double[] rowData) throws MathIllegalArgumentException, NullArgumentException
Create a rowRealMatrix
using the data from the input array.- Parameters:
rowData
- the input row data- Returns:
- a 1 x rowData.length RealMatrix
- Throws:
MathIllegalArgumentException
- ifrowData
is empty.NullArgumentException
- ifrowData
isnull
.
-
createRowFieldMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createRowFieldMatrix(T[] rowData) throws MathIllegalArgumentException, NullArgumentException
Create a rowFieldMatrix
using the data from the input array.- Type Parameters:
T
- the type of the field elements- Parameters:
rowData
- the input row data- Returns:
- a 1 x rowData.length FieldMatrix
- Throws:
MathIllegalArgumentException
- ifrowData
is empty.NullArgumentException
- ifrowData
isnull
.
-
createColumnRealMatrix
public static RealMatrix createColumnRealMatrix(double[] columnData) throws MathIllegalArgumentException, NullArgumentException
Creates a columnRealMatrix
using the data from the input array.- Parameters:
columnData
- the input column data- Returns:
- a columnData x 1 RealMatrix
- Throws:
MathIllegalArgumentException
- ifcolumnData
is empty.NullArgumentException
- ifcolumnData
isnull
.
-
createColumnFieldMatrix
public static <T extends FieldElement<T>> FieldMatrix<T> createColumnFieldMatrix(T[] columnData) throws MathIllegalArgumentException, NullArgumentException
Creates a columnFieldMatrix
using the data from the input array.- Type Parameters:
T
- the type of the field elements- Parameters:
columnData
- the input column data- Returns:
- a columnData x 1 FieldMatrix
- Throws:
MathIllegalArgumentException
- ifdata
is empty.NullArgumentException
- ifcolumnData
isnull
.
-
checkSymmetric
public static void checkSymmetric(RealMatrix matrix, double eps)
Checks whether a matrix is symmetric.- Parameters:
matrix
- Matrix to check.eps
- Relative tolerance.- Throws:
MathIllegalArgumentException
- if the matrix is not square.MathIllegalArgumentException
- if the matrix is not symmetric.
-
isSymmetric
public static boolean isSymmetric(RealMatrix matrix, double eps)
Checks whether a matrix is symmetric.- Parameters:
matrix
- Matrix to check.eps
- Relative tolerance.- Returns:
true
ifmatrix
is symmetric.
-
checkMatrixIndex
public static void checkMatrixIndex(AnyMatrix m, int row, int column) throws MathIllegalArgumentException
Check if matrix indices are valid.- Parameters:
m
- Matrix.row
- Row index to check.column
- Column index to check.- Throws:
MathIllegalArgumentException
- ifrow
orcolumn
is not a valid index.
-
checkRowIndex
public static void checkRowIndex(AnyMatrix m, int row) throws MathIllegalArgumentException
Check if a row index is valid.- Parameters:
m
- Matrix.row
- Row index to check.- Throws:
MathIllegalArgumentException
- ifrow
is not a valid index.
-
checkColumnIndex
public static void checkColumnIndex(AnyMatrix m, int column) throws MathIllegalArgumentException
Check if a column index is valid.- Parameters:
m
- Matrix.column
- Column index to check.- Throws:
MathIllegalArgumentException
- ifcolumn
is not a valid index.
-
checkSubMatrixIndex
public static void checkSubMatrixIndex(AnyMatrix m, int startRow, int endRow, int startColumn, int endColumn) throws MathIllegalArgumentException
Check if submatrix ranges indices are valid. Rows and columns are indicated counting from 0 ton - 1
.- Parameters:
m
- Matrix.startRow
- Initial row index.endRow
- Final row index.startColumn
- Initial column index.endColumn
- Final column index.- Throws:
MathIllegalArgumentException
- if the indices are invalid.MathIllegalArgumentException
- ifendRow < startRow
orendColumn < startColumn
.
-
checkSubMatrixIndex
public static void checkSubMatrixIndex(AnyMatrix m, int[] selectedRows, int[] selectedColumns) throws MathIllegalArgumentException, NullArgumentException
Check if submatrix ranges indices are valid. Rows and columns are indicated counting from 0 to n-1.- Parameters:
m
- Matrix.selectedRows
- Array of row indices.selectedColumns
- Array of column indices.- Throws:
NullArgumentException
- ifselectedRows
orselectedColumns
arenull
.MathIllegalArgumentException
- if the row or column selections are empty (zero length).MathIllegalArgumentException
- if row or column selections are not valid.
-
checkAdditionCompatible
public static void checkAdditionCompatible(AnyMatrix left, AnyMatrix right) throws MathIllegalArgumentException
Check if matrices are addition compatible.- Parameters:
left
- Left hand side matrix.right
- Right hand side matrix.- Throws:
MathIllegalArgumentException
- if the matrices are not addition compatible.
-
checkSubtractionCompatible
public static void checkSubtractionCompatible(AnyMatrix left, AnyMatrix right) throws MathIllegalArgumentException
Check if matrices are subtraction compatible- Parameters:
left
- Left hand side matrix.right
- Right hand side matrix.- Throws:
MathIllegalArgumentException
- if the matrices are not addition compatible.
-
checkMultiplicationCompatible
public static void checkMultiplicationCompatible(AnyMatrix left, AnyMatrix right) throws MathIllegalArgumentException
Check if matrices are multiplication compatible- Parameters:
left
- Left hand side matrix.right
- Right hand side matrix.- Throws:
MathIllegalArgumentException
- if matrices are not multiplication compatible.
-
checkSameColumnDimension
public static void checkSameColumnDimension(AnyMatrix left, AnyMatrix right) throws MathIllegalArgumentException
Check if matrices have the same number of columns.- Parameters:
left
- Left hand side matrix.right
- Right hand side matrix.- Throws:
MathIllegalArgumentException
- if matrices don't have the same number of columns.- Since:
- 1.3
-
checkSameRowDimension
public static void checkSameRowDimension(AnyMatrix left, AnyMatrix right) throws MathIllegalArgumentException
Check if matrices have the same number of rows.- Parameters:
left
- Left hand side matrix.right
- Right hand side matrix.- Throws:
MathIllegalArgumentException
- if matrices don't have the same number of rows.- Since:
- 1.3
-
fractionMatrixToRealMatrix
public static Array2DRowRealMatrix fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
- Parameters:
m
- Matrix to convert.- Returns:
- the converted matrix.
-
bigFractionMatrixToRealMatrix
public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(FieldMatrix<BigFraction> m)
- Parameters:
m
- Matrix to convert.- Returns:
- the converted matrix.
-
solveLowerTriangularSystem
public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b) throws MathIllegalArgumentException, MathRuntimeException
Solve a system of composed of a Lower Triangular MatrixRealMatrix
.This method is called to solve systems of equations which are of the lower triangular form. The matrix
RealMatrix
is assumed, though not checked, to be in lower triangular form. The vectorRealVector
is overwritten with the solution. The matrix is checked that it is square and its dimensions match the length of the vector.- Parameters:
rm
- RealMatrix which is lower triangularb
- RealVector this is overwritten- Throws:
MathIllegalArgumentException
- if the matrix and vector are not conformableMathIllegalArgumentException
- if the matrixrm
is not squareMathRuntimeException
- if the absolute value of one of the diagonal coefficient ofrm
is lower thanPrecision.SAFE_MIN
-
solveUpperTriangularSystem
public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b) throws MathIllegalArgumentException, MathRuntimeException
Solver a system composed of an Upper Triangular MatrixRealMatrix
.This method is called to solve systems of equations which are of the lower triangular form. The matrix
RealMatrix
is assumed, though not checked, to be in upper triangular form. The vectorRealVector
is overwritten with the solution. The matrix is checked that it is square and its dimensions match the length of the vector.- Parameters:
rm
- RealMatrix which is upper triangularb
- RealVector this is overwritten- Throws:
MathIllegalArgumentException
- if the matrix and vector are not conformableMathIllegalArgumentException
- if the matrixrm
is not squareMathRuntimeException
- if the absolute value of one of the diagonal coefficient ofrm
is lower thanPrecision.SAFE_MIN
-
blockInverse
public static RealMatrix blockInverse(RealMatrix m, int splitIndex)
Computes the inverse of the given matrix by splitting it into 4 sub-matrices.- Parameters:
m
- Matrix whose inverse must be computed.splitIndex
- Index that determines the "split" line and column. The element corresponding to this index will part of the upper-left sub-matrix.- Returns:
- the inverse of
m
. - Throws:
MathIllegalArgumentException
- ifm
is not square.
-
inverse
public static RealMatrix inverse(RealMatrix matrix) throws MathIllegalArgumentException, NullArgumentException
Computes the inverse of the given matrix.By default, the inverse of the matrix is computed using the QR-decomposition, unless a more efficient method can be determined for the input matrix.
Note: this method will use a singularity threshold of 0, use
inverse(RealMatrix, double)
if a different threshold is needed.- Parameters:
matrix
- Matrix whose inverse shall be computed- Returns:
- the inverse of
matrix
- Throws:
NullArgumentException
- ifmatrix
isnull
MathIllegalArgumentException
- if m is singularMathIllegalArgumentException
- if matrix is not square
-
inverse
public static RealMatrix inverse(RealMatrix matrix, double threshold) throws MathIllegalArgumentException, NullArgumentException
Computes the inverse of the given matrix.By default, the inverse of the matrix is computed using the QR-decomposition, unless a more efficient method can be determined for the input matrix.
- Parameters:
matrix
- Matrix whose inverse shall be computedthreshold
- Singularity threshold- Returns:
- the inverse of
m
- Throws:
NullArgumentException
- ifmatrix
isnull
MathIllegalArgumentException
- if matrix is singularMathIllegalArgumentException
- if matrix is not square
-
matrixExponential
public static RealMatrix matrixExponential(RealMatrix rm)
Computes the matrix exponential of the given matrix. The algorithm implementation follows the Pade approximant method ofHigham, Nicholas J. “The Scaling and Squaring Method for the Matrix Exponential Revisited.” SIAM Journal on Matrix Analysis and Applications 26, no. 4 (January 2005): 1179–93.
- Parameters:
rm
- RealMatrix whose inverse shall be computed- Returns:
- The inverse of
rm
- Throws:
MathIllegalArgumentException
- if matrix is not square
-
orthonormalize
public static List<RealVector> orthonormalize(List<RealVector> independent, double threshold, DependentVectorsHandler handler)
Orthonormalize a list of vectors.Orthonormalization is performed by using the Modified Gram-Schmidt process.
- Parameters:
independent
- list of independent vectorsthreshold
- projected vectors with a norm less than or equal to this threshold are considered to have zero norm, hence the vectors they come from are not independent from previous vectorshandler
- handler for dependent vectors- Returns:
- orthonormal basis having the same span as
independent
- Since:
- 2.1
-
orthonormalize
public static <T extends CalculusFieldElement<T>> List<FieldVector<T>> orthonormalize(Field<T> field, List<FieldVector<T>> independent, T threshold, DependentVectorsHandler handler)
Orthonormalize a list of vectors.Orthonormalization is performed by using the Modified Gram-Schmidt process.
- Type Parameters:
T
- type of the field elements- Parameters:
independent
- list of independent vectorsthreshold
- projected vectors with a norm less than or equal to this threshold are considered to have zero norm, hence the vectors they come from are not independent from previous vectorsfield
- type of the files elementshandler
- handler for dependent vectors- Returns:
- orthonormal basis having the same span as
independent
- Since:
- 2.1
-
-