public final class MatrixUtils extends Object
This class is up-to-date with commons-math 3.6.1.
| Modifier and Type | Field and Description |
|---|---|
static String |
CARRIER_RETURN
Carrier return ('\n').
|
static String |
CLOSING_BRACKET
Closing bracket character (']').
|
static String |
CLOSING_CURLY_BRACKET
Closing curly bracket character ('}').
|
static String |
COMMA
Comma character (',').
|
static RealMatrixFormat |
DEFAULT_FORMAT
The default
RealMatrixFormat. |
static RealMatrixFormat |
JAVA_FORMAT
The JAVA format for
RealMatrix objects. |
static int |
MAX_DIGITS
Maximum number of fraction digits for no numerical approximation.
|
static String |
NUMBER_FORMAT
Format number.
|
static RealMatrixFormat |
NUMPY_FORMAT
The NUMPY format for
RealMatrix objects. |
static RealMatrixFormat |
OCTAVE_FORMAT
A format for
RealMatrix objects compatible with octave. |
static String |
OPENING_BRACKET
Opening bracket character ('[').
|
static String |
OPENING_CURLY_BRACKET
Opening curly bracket character ('{').
|
static RealMatrixFormat |
SCILAB_FORMAT
The SCILAB format for
RealMatrix objects. |
static String |
SEMICOLON
Semicolon character (';') .
|
static String |
SPACE
Space character (' ').
|
static RealMatrixFormat |
SUMMARY_FORMAT
Summary visual format for
RealMatrix objects displayed on several rows. |
static RealMatrixFormat |
VISUAL_FORMAT
Visual format for
RealMatrix objects displayed on several rows. |
| Modifier and Type | Method and 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 RealMatrixFormat |
buildSummaryMatrixFormat(int summarySize)
Build a summary matrix format with different specific size for sub-corners blocs square dimensions of the summary
view.
|
static void |
checkAdditionCompatible(AnyMatrix left,
AnyMatrix right)
Check if matrices are addition compatible.
|
static void |
checkArrayIndex(double[][] array,
int row,
int column)
Checks if the provided indices are valid row and column indices for a given 2D-array.
|
static void |
checkColumnDimension(int columnDimension)
Checks the validity of the provided column dimension (must be strictly positive).
|
static void |
checkColumnIndex(AnyMatrix m,
int column)
Checks if an index is a valid column index for a given matrix.
|
static void |
checkColumnIndices(AnyMatrix m,
int[] indices)
Checks if the provided indices are valid column indices for a given matrix.
|
static void |
checkDimension(int expected,
int actual)
Check if the provided dimension is the one expected.
|
static void |
checkDuplicates(int[] array)
Checks if the provided array contains duplicates.
|
static void |
checkMatrixArray(double[][] data)
Checks the validity of a matrix data array.
|
static void |
checkMatrixArray(double[][] data,
boolean checkColumnDimensions)
Checks the validity of a matrix data array.
|
static void |
checkMatrixArray(Object[][] data)
Checks the validity of a matrix data array.
|
static void |
checkMatrixArray(Object[][] data,
boolean checkColumnDimensions)
Checks the validity of a matrix data array.
|
static void |
checkMatrixIndex(AnyMatrix m,
int row,
int column)
Checks if the provided indices are valid row and column indices are for a given matrix.
|
static void |
checkMultiplicationCompatible(AnyMatrix left,
AnyMatrix right)
Check if matrices are multiplication compatible
|
static void |
checkMultiplicationCompatible(AnyMatrix left,
AnyMatrix right,
boolean rightToTransposed)
Check if matrices are multiplication compatible
|
static void |
checkRowDimension(int rowDimension)
Checks the validity of the provided row dimension (must be strictly positive).
|
static void |
checkRowIndex(AnyMatrix m,
int row)
Checks if an index is a valid row index for a given matrix.
|
static void |
checkRowIndices(AnyMatrix m,
int[] indices)
Checks if the provided indices are valid row indices for a given matrix.
|
static void |
checkSubMatrixIndex(AnyMatrix m,
int[] selectedRows,
int[] selectedColumns)
Checks if the provided sub-matrix indices are valid for a given matrix.
|
static void |
checkSubMatrixIndex(AnyMatrix m,
int startRow,
int endRow,
int startColumn,
int endColumn)
Checks if the provided sub-matrix ranges are valid for a given matrix.
|
static void |
checkSubtractionCompatible(AnyMatrix left,
AnyMatrix right)
Check if matrices are subtraction compatible
|
static void |
checkSymmetric(RealMatrix matrix,
double eps)
Checks whether a matrix is symmetric.
|
static void |
checkSymmetry(RealMatrix matrix,
double absTol,
double relTol)
Checks if the matrix is symmetric and throws an exception if that's not the case.
|
static RealMatrix |
concatenateHorizontally(RealMatrix left,
RealMatrix right)
Concatenates two matrices horizontally.
|
static RealMatrix |
concatenateVertically(RealMatrix upper,
RealMatrix lower)
Concatenates two matrices vertically.
|
static double[][] |
copyArray(double[][] array)
Returns a deep copy of a 2D
double array. |
static <T extends FieldElement<T>> |
createColumnFieldMatrix(T[] columnData)
Creates a column
FieldMatrix using the data from the input array. |
static RealMatrix |
createColumnRealMatrix(double[] columnData)
Creates a column
RealMatrix using the data from the input array. |
static <T extends FieldElement<T>> |
createFieldDiagonalMatrix(T[] diagonal)
Returns a diagonal matrix with specified elements.
|
static <T extends FieldElement<T>> |
createFieldIdentityMatrix(Field<T> field,
int dimension)
Returns
dimension x dimension identity matrix. |
static <T extends FieldElement<T>> |
createFieldMatrix(Field<T> field,
int rows,
int columns)
Returns a
FieldMatrix with specified dimensions. |
static <T extends FieldElement<T>> |
createFieldMatrix(T[][] data)
Returns a
FieldMatrix whose entries are the the values in the the input array. |
static <T extends FieldElement<T>> |
createFieldVector(T[] data)
Creates a
FieldVector using the data from the input array. |
static RealMatrix |
createRealDiagonalMatrix(double[] diagonal)
Returns a diagonal matrix with specified elements.
|
static RealMatrix |
createRealIdentityMatrix(int dimension)
Returns
dimension x dimension identity matrix. |
static RealMatrix |
createRealIdentityMatrix(int dimension,
boolean diagonalMatrixInstance)
Returns
dimension x dimension identity matrix. |
static RealMatrix |
createRealMatrix(double[][] data)
Returns a
RealMatrix whose entries are the the values in the the input array. |
static RealMatrix |
createRealMatrix(double[][] data,
boolean forceCopyArray)
Returns a
RealMatrix whose entries are the the values in the the input array. |
static RealMatrix |
createRealMatrix(int rows,
int columns)
Returns a
RealMatrix with specified dimensions. |
static RealVector |
createRealVector(double[] data)
Creates a
RealVector using the data from the input array. |
static <T extends FieldElement<T>> |
createRowFieldMatrix(T[] rowData)
Create a row
FieldMatrix using the data from the input array. |
static RealMatrix |
createRowRealMatrix(double[] rowData)
Create a row
RealMatrix using the data from the input array. |
static RealMatrix |
deserializeRealMatrix(ObjectInputStream ois)
Deserialize a
RealMatrix. |
static RealMatrix |
deserializeRealMatrix(String file)
Deserialize a
RealMatrix. |
static RealVector |
deserializeRealVector(ObjectInputStream ois)
Deserialize a
RealVector field in a class. |
static Array2DRowRealMatrix |
fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
|
static int[] |
getColumnPermutationIndexArray(AnyMatrix m,
int[] preSelectedColumns)
Builds a column permutation index array for a given matrix, starting with the preselected
columns.
|
static int[] |
getRowPermutationIndexArray(AnyMatrix m,
int[] preSelectedRows)
Builds a row permutation index array for a given matrix, starting with the preselected rows.
|
static RealMatrix |
multiplyByTranspose(double alpha,
RealMatrix matrixL,
RealMatrix matrixR)
Multiplies the matrix L by RT and by a scalar factor α.
|
static RealMatrix |
multiplyByTranspose(RealMatrix matrixL,
RealMatrix matrixR)
Multiplies the matrix L by RT.
|
static RealMatrix |
resize(RealMatrix matrix,
int rowDim,
int colDim)
Resizes the provided matrix to a NxM matrix.
|
static void |
serializeRealMatrix(RealMatrix matrix,
ObjectOutputStream oos)
Serialize a
RealMatrix. |
static void |
serializeRealMatrix(RealMatrix matrix,
String file)
Serialize a
RealMatrix. |
static void |
serializeRealVector(RealVector vector,
ObjectOutputStream oos)
Serialize a
RealVector. |
static void |
solveLowerTriangularSystem(RealMatrix rm,
RealVector b)
Solve a system of composed of a Lower Triangular Matrix
RealMatrix. |
static void |
solveUpperTriangularSystem(RealMatrix rm,
RealVector b)
Solver a system composed of an Upper Triangular Matrix
RealMatrix. |
static void |
writeMatrix(RealMatrix matrix,
String outputFileName)
Utility function to print a matrix in a file with the machine precision.
|
static void |
writeMatrix(RealMatrix matrix,
String outputFileName,
String numberFormat)
Utility function to print a matrix in a file with the provided format.
|
static void |
writeMatrixStructure(RealMatrix matrix,
String outputFileName)
Utility function to print a matrix in a file to see its structure (for example for sparse matrices).
|
public static final String NUMBER_FORMAT
public static final String OPENING_CURLY_BRACKET
public static final String CLOSING_CURLY_BRACKET
public static final String OPENING_BRACKET
public static final String CLOSING_BRACKET
public static final String SPACE
public static final String COMMA
public static final String SEMICOLON
public static final String CARRIER_RETURN
public static final int MAX_DIGITS
NumberFormat.setMaximumFractionDigits(int).public static final RealMatrixFormat DEFAULT_FORMAT
RealMatrixFormat.public static final RealMatrixFormat JAVA_FORMAT
RealMatrix objects.
This format guarantee no numerical approximation.
public static final RealMatrixFormat OCTAVE_FORMAT
RealMatrix objects compatible with octave.
This format guarantee no numerical approximation.
public static final RealMatrixFormat NUMPY_FORMAT
RealMatrix objects.
This format guarantee no numerical approximation.
public static final RealMatrixFormat SCILAB_FORMAT
RealMatrix objects.
This format guarantee no numerical approximation.
public static final RealMatrixFormat VISUAL_FORMAT
RealMatrix objects displayed on several rows.
The pattern "% 11.5g" by default set the significant digit accuracy at 5, the width equal to 11, the format is automatically set to digital or scientific and the space between % and 11 allows to display the sign for scientific values.
public static final RealMatrixFormat SUMMARY_FORMAT
RealMatrix objects displayed on several rows.
The pattern "% 11.5g" by default set the significant digit accuracy at 5, the width equal to 11, the format is automatically set to digital or scientific and the space between % and 11 allows to display the sign for scientific values. The summary mode, with an index set to 3, will display the 3x3 sub-matrix in each corner and the rows and columns total number.
public static RealMatrixFormat buildSummaryMatrixFormat(int summarySize)
The pattern "% 11.5g" by default set the significant digit accuracy at 5, the width equal to 11, the format is automatically set to digital or scientific and the space between % and 11 allows to display the sign for scientific values.
summarySize - Sub-corners blocs square dimensions for summary viewspecialized with user-specified sub-corners blocs square dimensionpublic static RealMatrix createRealMatrix(int rows, int columns)
RealMatrix 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 a BlockRealMatrix instance is built.
The matrix elements are all set to 0.0.
rows - the number of rows of the matrixcolumns - the number of columns of the matrixRealMatrix with specified dimensionscreateRealMatrix(double[][])public static void writeMatrixStructure(RealMatrix matrix, String outputFileName) throws IOException
Non zero values are represented with "1" and zero values with "0".
matrix - The matrix to writeoutputFileName - The name of the fileIOException - if an I/O exception occurspublic static void writeMatrix(RealMatrix matrix, String outputFileName) throws IOException
matrix - The matrix to writeoutputFileName - The name of the fileIOException - if an I/O exception occurspublic static void writeMatrix(RealMatrix matrix, String outputFileName, String numberFormat) throws IOException
matrix - The matrix to writeoutputFileName - The name of the filenumberFormat - Number print format ("% .17E " for example for a scientific format with 17 decimal numbers)IOException - if an I/O exception occurspublic static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(Field<T> field, int rows, int columns)
FieldMatrix 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 a
BlockFieldMatrix instance is built.
The matrix elements are all set to field.getZero().
T - the type of the field elementsfield - the field to which the matrix elements belongrows - number of rows of the matrixcolumns - number of columns of the matrixFieldMatrix with specified dimensionscreateFieldMatrix(FieldElement[][])public static RealMatrix createRealMatrix(double[][] data)
RealMatrix 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 a BlockRealMatrix instance is built.
The input array is copied, not referenced.
data - the input arrayRealMatrix containing the values of the input arrayNullArgumentException - if either data or data[0] is nullNoDataException - if a row or column is emptyDimensionMismatchException - if data is not rectangular (not all rows have the same length)createRealMatrix(int, int)public static RealMatrix createRealMatrix(double[][] data, boolean forceCopyArray)
RealMatrix 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 a BlockRealMatrix instance is built.
The input array is either copied or referenced, depending on the forceCopyArray argument.
data - the input arrayforceCopyArray - if true or if the matrix has more than 4096 elements, the input array is copied, otherwise it is
referencedRealMatrix containing the values of the input arrayNullArgumentException - if either data or data[0] is null.NoDataException - if a row or column is empty.DimensionMismatchException - if data is not rectangular (not all rows have the same length)createRealMatrix(int, int)public static <T extends FieldElement<T>> FieldMatrix<T> createFieldMatrix(T[][] data)
FieldMatrix 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 a
BlockFieldMatrix instance is built.
The input array is copied, not referenced.
T - the type of the field elementsdata - the input arrayNullArgumentException - if either data or data[0] is nullNoDataException - if a row or column is empty.DimensionMismatchException - if data is not rectangular (not all rows have the same length)createFieldMatrix(Field, int, int)public static RealMatrix createRealIdentityMatrix(int dimension)
dimension x dimension identity matrix.
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 a BlockRealMatrix instance is built.
dimension - the dimension of identity matrix to be generatedNotStrictlyPositiveException - if the specified dimension is not strictly positivecreateRealMatrix(int, int)public static RealMatrix createRealIdentityMatrix(int dimension, boolean diagonalMatrixInstance)
dimension x dimension identity matrix.
If diagonalMatrixInstance is set to true, the generated identity matrix is a
DiagonalMatrix instance. Otherwise, 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 a BlockRealMatrix instance
is built.
Although DiagonalMatrix instances are optimized for storing diagonal matrices, they do have some
limitations. Setting off-diagonal elements to non-zero values is forbidden, which means any attempt to modify the
matrix will most likely result in an exception.
dimension - the dimension of identity matrix to be generateddiagonalMatrixInstance - if true, the generated identity matrix is a DiagonalMatrix instance,
otherwise it is either an Array2DRowRealMatrix or a BlockRealMatrix instanceNotStrictlyPositiveException - if the specified dimension is not strictly positivecreateRealMatrix(int, int)public static <T extends FieldElement<T>> FieldMatrix<T> createFieldIdentityMatrix(Field<T> field, int dimension)
dimension x dimension identity matrix.T - the type of the field elementsfield - field to which the elements belongdimension - dimension of identity matrix to generateIllegalArgumentException - if dimension is not positivepublic static RealMatrix createRealDiagonalMatrix(double[] diagonal)
diagonal - diagonal elements of the matrix (the array elements will be copied)public static <T extends FieldElement<T>> FieldMatrix<T> createFieldDiagonalMatrix(T[] diagonal)
T - the type of the field elementsdiagonal - diagonal elements of the matrix (the array elements
will be copied)public static RealVector createRealVector(double[] data)
RealVector using the data from the input array.data - the input dataNoDataException - if data is empty.NullArgumentException - if data is null.public static <T extends FieldElement<T>> FieldVector<T> createFieldVector(T[] data)
FieldVector using the data from the input array.T - the type of the field elementsdata - the input dataNoDataException - if data is empty.NullArgumentException - if data is null.ZeroException - if data has 0 elementspublic static RealMatrix createRowRealMatrix(double[] rowData)
RealMatrix using the data from the input array.rowData - the input row dataNoDataException - if rowData is empty.NullArgumentException - if rowData is null.public static <T extends FieldElement<T>> FieldMatrix<T> createRowFieldMatrix(T[] rowData)
FieldMatrix using the data from the input array.T - the type of the field elementsrowData - the input row dataNoDataException - if rowData is empty.NullArgumentException - if rowData is null.public static RealMatrix createColumnRealMatrix(double[] columnData)
RealMatrix using the data from the input array.columnData - the input column dataNoDataException - if columnData is empty.NullArgumentException - if columnData is null.public static <T extends FieldElement<T>> FieldMatrix<T> createColumnFieldMatrix(T[] columnData)
FieldMatrix using the data from the input array.T - the type of the field elementscolumnData - the input column dataNoDataException - if data is empty.NullArgumentException - if columnData is null.public static RealMatrix concatenateHorizontally(RealMatrix left, RealMatrix right)
left - the left matrix [NxM]right - the right matrix [NxL]DimensionMismatchException - if the two matrices don't have the same number of rowspublic static RealMatrix concatenateVertically(RealMatrix upper, RealMatrix lower)
upper - the upper matrix [MxN]lower - the upper matrix [LxN]DimensionMismatchException - if the two matrices don't have the same number of columnspublic static RealMatrix resize(RealMatrix matrix, int rowDim, int colDim)
The provided matrix is truncated or extended, depending on whether its dimensions are bigger or smaller than the requested dimensions. If extended, the terms added are set to zero.
matrix - the matrix to be truncatedrowDim - the row dimension N of the matrix returnedcolDim - the column dimension M of the matrix returnedpublic static RealMatrix multiplyByTranspose(RealMatrix matrixL, RealMatrix matrixR)
This methods allows to combine matrix multiplication and matrix transposition for optimization purposes.
matrixL - the left-side matrix LmatrixR - the right-side matrix R (not its transpose)public static RealMatrix multiplyByTranspose(double alpha, RealMatrix matrixL, RealMatrix matrixR)
This methods allows to combine scalar multiplication, matrix multiplication and matrix transposition for optimization purposes.
alpha - the scalar factor αmatrixL - the left-side matrix LmatrixR - the right-side matrix R (not its transpose)public static double[][] copyArray(double[][] array)
double array.array - the array to be copiednull if the supplied array is nullpublic static void checkMatrixArray(double[][] data)
To be valid, the provided data array must not be null or empty, its first row must also not be
null or empty, and the rows must all have the same length.
data - the matrix data array to be checkedNullArgumentException - if the data array is nullNoDataException - if the data array is emptyDimensionMismatchException - if the rows of the data array do not have a constant number of columnspublic static void checkMatrixArray(double[][] data,
boolean checkColumnDimensions)
To be valid, the provided data array must not be null or empty, and its first row must also not be
null or empty. Setting checkColumnDimensions to true , enables an additional check which
verifies that the rows all have the same length.
data - the matrix data array to be checkedcheckColumnDimensions - whether or not to check if the rows all have the same dimensionNullArgumentException - if the data array is nullNoDataException - if the data array is emptyDimensionMismatchException - if the rows of the data array do not have a constant number of columns (optional
check)public static void checkMatrixArray(Object[][] data)
To be valid, the provided data array must not be null or empty, its first row must also not be
null or empty, and the rows must all have the same length.
data - the matrix data array to be checkedNullArgumentException - if the data array is nullNoDataException - if the data array is emptyDimensionMismatchException - if the rows of the data array do not have a constant number of columnspublic static void checkMatrixArray(Object[][] data, boolean checkColumnDimensions)
To be valid, the provided data array must not be null or empty, and its first row must also not be
null or empty. Setting checkColumnDimensions to true , enables an additional check which
verifies that the rows all have the same length.
data - the matrix data array to be checkedcheckColumnDimensions - whether or not to check if the rows all have the same dimensionNullArgumentException - if the data array is nullNoDataException - if the data array is emptyDimensionMismatchException - if the rows of the data array do not have a constant number of columns (optional
check)public static void checkRowDimension(int rowDimension)
rowDimension - the row dimension to be checkedNotStrictlyPositiveException - if the row dimension is not strictly positivepublic static void checkColumnDimension(int columnDimension)
columnDimension - the column dimension to be checkedNotStrictlyPositiveException - if the column dimension is not strictly positivepublic static void checkSymmetric(RealMatrix matrix, double eps)
matrix - Matrix to check.eps - Relative tolerance.NonSquareMatrixException - if the matrix is not square.NonSymmetricMatrixException - if the matrix is not symmetric.public static void checkArrayIndex(double[][] array,
int row,
int column)
array - the arrayrow - the row index to be checkedcolumn - the column index to be checkedOutOfRangeException - if the provided indices are not valid row or column indicespublic static void checkMatrixIndex(AnyMatrix m, int row, int column)
m - the matrixrow - the row index to be checkedcolumn - the column index to be checkedOutOfRangeException - if the provided indices are not valid row or column indicespublic static void checkRowIndex(AnyMatrix m, int row)
m - the matrixrow - the row index to be checkedOutOfRangeException - if the provided index is not a valid row indexpublic static void checkColumnIndex(AnyMatrix m, int column)
m - the matrixcolumn - the column index to be checkedOutOfRangeException - if the provided index is not a valid column indexpublic static void checkRowIndices(AnyMatrix m, int[] indices)
This method throws an exception if the provided array is not a valid row index array. An index array is
considered to be valid if it is not null or empty, and if each of its indices are between 0 to n-1, with
n the row dimension of the matrix.
m - the matrixindices - the array of row indicesNullArgumentException - if the provided index array is nullNoDataException - if the provided index array is empty (zero length)OutOfRangeException - if one of the provided indices is not a valid row indexpublic static void checkColumnIndices(AnyMatrix m, int[] indices)
This method throws an exception if the provided array is not a valid row index array. An index array is
considered to be valid if it is not null or empty, and if each of its indices are between 0 to n-1, with
n the column dimension of the matrix.
m - the matrixindices - the array of column indicesNullArgumentException - if the provided index array is nullNoDataException - if the provided index array is empty (zero length)OutOfRangeException - if one of the provided indices is not a valid column indexpublic static void checkSubMatrixIndex(AnyMatrix m, int startRow, int endRow, int startColumn, int endColumn)
Rows and columns are indicated counting from 0 to n - 1.
m - the matrixstartRow - the initial row indexendRow - the final row indexstartColumn - the initial column indexendColumn - the final column indexOutOfRangeException - if one of the provided indices is not a valid row or column indexNumberIsTooSmallException - if endRow < startRow or endColumn < startColumn.public static void checkSubMatrixIndex(AnyMatrix m, int[] selectedRows, int[] selectedColumns)
Rows and columns are indicated counting from 0 to n-1.
m - the matrixselectedRows - the array of row indicesselectedColumns - the array of column indicesNullArgumentException - if any of the provided index arrays is nullNoDataException - if any of the provided index arrays is empty (zero length)OutOfRangeException - if any of the provided indices is not a valid row or column indexpublic static void checkDuplicates(int[] array)
array - the array to be checkedMathIllegalArgumentException - if the provided array contains any duplicatespublic static void checkSymmetry(RealMatrix matrix, double absTol, double relTol)
matrix - the matrix to be checkedabsTol - the absolute threshold above which two off-diagonal elements are considered to be
differentrelTol - the relative threshold above which two off-diagonal elements are considered to be
differentNonSymmetricMatrixException - if the provided matrix is not symmetricpublic static int[] getRowPermutationIndexArray(AnyMatrix m, int[] preSelectedRows)
This methods creates a permutation index array by completing the supplied index array with the missing row indices (in increasing order). The preselected indices must be valid row indices for the supplied matrix. Any duplicate found in the supplied index array is simply ignored.
Usage examples, for a 5x4 matrix:
getRowPermutation(m, {2, 3}) => {2, 3, 0, 1, 4}
getRowPermutation(m, {0, 4, 1, 0}) => {0, 4, 1, 2, 3}
m - the matrixpreSelectedRows - the preselected row indicespublic static int[] getColumnPermutationIndexArray(AnyMatrix m, int[] preSelectedColumns)
This methods creates a permutation index array by completing the supplied index array with the missing column indices (in increasing order). The preselected indices must be valid column indices for the supplied matrix. Any duplicate found in the supplied index array is simply ignored.
Usage examples, for a 4x5 matrix:
getColumnPermutation(m, {2, 3}) => {2, 3, 0, 1, 4}
getColumnPermutation(m, {0, 4, 1, 0}) => {0, 4, 1, 2, 3}
m - the matrixpreSelectedColumns - the preselected column indicespublic static void checkDimension(int expected,
int actual)
expected - the expected dimensionactual - the actual dimensionDimensionMismatchException - if the provided dimension is not the one expectedpublic static void checkAdditionCompatible(AnyMatrix left, AnyMatrix right)
left - Left hand side matrix.right - Right hand side matrix.MatrixDimensionMismatchException - if the matrices are not addition compatible.public static void checkSubtractionCompatible(AnyMatrix left, AnyMatrix right)
left - Left hand side matrix.right - Right hand side matrix.MatrixDimensionMismatchException - if the matrices are not subtraction compatible.public static void checkMultiplicationCompatible(AnyMatrix left, AnyMatrix right)
left - Left hand side matrix.right - Right hand side matrix.DimensionMismatchException - if getColumnDimension(left) != getRowDimension(right)public static void checkMultiplicationCompatible(AnyMatrix left, AnyMatrix right, boolean rightToTransposed)
left - Left hand side matrix.right - Right hand side matrix.rightToTransposed - indicate if the right matrix will be transposed before the operationDimensionMismatchException - if getColumnDimension(left) != getRowDimension(right)public static Array2DRowRealMatrix fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
m - Matrix to convert.public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(FieldMatrix<BigFraction> m)
m - Matrix to convert.public static void serializeRealVector(RealVector vector, ObjectOutputStream oos) throws IOException
RealVector.
This method is intended to be called from within a private writeObject method (after a call to
oos.defaultWriteObject()) in a class that has a RealVector field, which should be declared
transient. This way, the default handling does not serialize the vector (the RealVector
interface is not serializable by default) but this method does serialize it specifically.
The following example shows how a simple class with a name and a real vector should be written:
public class NamedVector implements Serializable {
private final String name;
private final transient RealVector coefficients;
// omitted constructors, getters ...
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject(); // takes care of name field
MatrixUtils.serializeRealVector(coefficients, oos);
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject(); // takes care of name field
MatrixUtils.deserializeRealVector(this, "coefficients", ois);
}
}
vector - real vector to serializeoos - stream where the real vector should be writtenIOException - if object cannot be written to streamdeserializeRealVector(ObjectInputStream)public static RealVector deserializeRealVector(ObjectInputStream ois) throws IOException
RealVector field in a class.
This method is intended to be called from within a private readObject method (after a call to
ois.defaultReadObject()) in a class that has a RealVector field, which should be declared
transient. This way, the default handling does not deserialize the vector (the RealVector
interface is not serializable by default) but this method does deserialize it specifically.
ois - stream from which the real vector should be readIOException - if object cannot be read from the streamserializeRealVector(RealVector, ObjectOutputStream)public static void serializeRealMatrix(RealMatrix matrix, String file) throws IOException
RealMatrix.matrix - real matrix to serializefile - file where the real matrix should be writtenIOException - if an I/O error occursdeserializeRealMatrix(String)public static void serializeRealMatrix(RealMatrix matrix, ObjectOutputStream oos) throws IOException
RealMatrix.
This method is intended to be called from within a private writeObject method (after a call to
oos.defaultWriteObject()) in a class that has a RealMatrix field, which should be declared
transient. This way, the default handling does not serialize the matrix (the RealMatrix
interface is not serializable by default) but this method does serialize it specifically.
The following example shows how a simple class with a name and a real matrix should be written:
public class NamedMatrix implements Serializable {
private final String name;
private final transient RealMatrix coefficients;
// omitted constructors, getters ...
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject(); // takes care of name field
MatrixUtils.serializeRealMatrix(coefficients, oos);
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject(); // takes care of name field
MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
}
}
matrix - real matrix to serializeoos - stream where the real matrix should be writtenIOException - if object cannot be written to streamdeserializeRealMatrix(ObjectInputStream)public static RealMatrix deserializeRealMatrix(String file) throws FileNotFoundException, IOException, ClassNotFoundException
RealMatrix.file - file from which the real matrix should be readFileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be
opened for readingIOException - if an I/O error occursClassNotFoundException - Class of a serialized object cannot be foundserializeRealMatrix(RealMatrix, String)public static RealMatrix deserializeRealMatrix(ObjectInputStream ois) throws IOException, ClassNotFoundException
RealMatrix.
This method is intended to be called from within a private readObject method (after a call to
ois.defaultReadObject()) in a class that has a RealMatrix field, which should be declared
transient. This way, the default handling does not deserialize the matrix (the RealMatrix
interface is not serializable by default) but this method does deserialize it specifically.
ois - stream from which the real matrix should be readIOException - if object cannot be read from the streamIOException - if an I/O error occursClassNotFoundException - Class of a serialized object cannot be foundserializeRealMatrix(RealMatrix, ObjectOutputStream)public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b)
RealMatrix.
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 vector RealVector
is overwritten with the solution. The matrix is checked that it is square and its dimensions match the length of
the vector.
rm - RealMatrix which is lower triangularb - RealVector this is overwrittenDimensionMismatchException - if the matrix and vector are not compatibleNonSquareMatrixException - if the matrix rm is not squareMathArithmeticException - if the absolute value of one of the diagonal coefficient of rm is lower than
Precision.SAFE_MINpublic static void solveUpperTriangularSystem(RealMatrix rm, RealVector b)
RealMatrix.
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 vector RealVector
is overwritten with the solution. The matrix is checked that it is square and its dimensions match the length of
the vector.
rm - RealMatrix which is upper triangularb - RealVector this is overwrittenDimensionMismatchException - if the matrix and vector are not compatibleNonSquareMatrixException - if the matrix rm is not squareMathArithmeticException - if the absolute value of one of the diagonal coefficient of rm is lower than
Precision.SAFE_MINpublic static RealMatrix blockInverse(RealMatrix m, int splitIndex)
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.m.NonSquareMatrixException - if m is not square.Copyright © 2024 CNES. All rights reserved.