public class MatrixUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static Array2DRowRealMatrix |
bigFractionMatrixToRealMatrix(FieldMatrix<BigFraction> m)
|
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 |
checkMultiplicationCompatible(AnyMatrix left,
AnyMatrix right)
Check if matrices are multiplication compatible
|
static void |
checkRowIndex(AnyMatrix m,
int row)
Check if a row index is valid.
|
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 compatible
|
static BigMatrix |
createBigIdentityMatrix(int dimension)
Deprecated.
since 2.0, replaced by
createFieldIdentityMatrix(Field, int) |
static BigMatrix |
createBigMatrix(java.math.BigDecimal[][] data)
Deprecated.
since 2.0 replaced by
createFieldMatrix(FieldElement[][]) |
static BigMatrix |
createBigMatrix(java.math.BigDecimal[][] data,
boolean copyArray)
Deprecated.
since 2.0 replaced by
createFieldMatrix(FieldElement[][]) |
static BigMatrix |
createBigMatrix(double[][] data)
Deprecated.
since 2.0 replaced by
createFieldMatrix(FieldElement[][]) |
static BigMatrix |
createBigMatrix(java.lang.String[][] data)
Deprecated.
since 2.0 replaced by
createFieldMatrix(FieldElement[][]) |
static BigMatrix |
createColumnBigMatrix(java.math.BigDecimal[] columnData)
Deprecated.
since 2.0 replaced by
createColumnFieldMatrix(FieldElement[]) |
static BigMatrix |
createColumnBigMatrix(double[] columnData)
Deprecated.
since 2.0 replaced by
createColumnFieldMatrix(FieldElement[]) |
static BigMatrix |
createColumnBigMatrix(java.lang.String[] columnData)
Deprecated.
since 2.0 replaced by
createColumnFieldMatrix(FieldElement[]) |
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 |
createRealMatrix(double[][] data)
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 BigMatrix |
createRowBigMatrix(java.math.BigDecimal[] rowData)
Deprecated.
since 2.0 replaced by
createRowFieldMatrix(FieldElement[]) |
static BigMatrix |
createRowBigMatrix(double[] rowData)
Deprecated.
since 2.0 replaced by
createRowFieldMatrix(FieldElement[]) |
static BigMatrix |
createRowBigMatrix(java.lang.String[] rowData)
Deprecated.
since 2.0 replaced by
createRowFieldMatrix(FieldElement[]) |
static <T extends FieldElement<T>> |
createRowFieldMatrix(T[] rowData)
Creates a row
FieldMatrix using the data from the input
array. |
static RealMatrix |
createRowRealMatrix(double[] rowData)
Creates a row
RealMatrix using the data from the input
array. |
static void |
deserializeRealMatrix(java.lang.Object instance,
java.lang.String fieldName,
java.io.ObjectInputStream ois)
Deserialize a
RealMatrix field in a class. |
static void |
deserializeRealVector(java.lang.Object instance,
java.lang.String fieldName,
java.io.ObjectInputStream ois)
Deserialize a
RealVector field in a class. |
static Array2DRowRealMatrix |
fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
|
static void |
serializeRealMatrix(RealMatrix matrix,
java.io.ObjectOutputStream oos)
Serialize a
RealMatrix . |
static void |
serializeRealVector(RealVector vector,
java.io.ObjectOutputStream oos)
Serialize a
RealVector . |
public 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
- number of rows of the matrixcolumns
- number of columns of the matrixcreateRealMatrix(double[][])
public 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
- field to which the matrix elements belongrows
- number of rows of the matrixcolumns
- number of columns of the matrixcreateFieldMatrix(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
- input arrayjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if either data
or
data[0]
is nullcreateRealMatrix(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
- input arrayjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if either data
or
data[0]
is nullcreateFieldMatrix(Field, int, int)
public static RealMatrix createRealIdentityMatrix(int dimension)
dimension x dimension
identity matrix.dimension
- dimension of identity matrix to generatejava.lang.IllegalArgumentException
- if dimension is not positivepublic 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 generatejava.lang.IllegalArgumentException
- if dimension is not positive@Deprecated public static BigMatrix createBigIdentityMatrix(int dimension)
createFieldIdentityMatrix(Field, int)
dimension x dimension
identity matrix.dimension
- dimension of identity matrix to generatejava.lang.IllegalArgumentException
- 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)@Deprecated public static BigMatrix createBigMatrix(double[][] data)
createFieldMatrix(FieldElement[][])
BigMatrix
whose entries are the the values in the
the input array. The input array is copied, not referenced.data
- input arrayjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if data is null@Deprecated public static BigMatrix createBigMatrix(java.math.BigDecimal[][] data)
createFieldMatrix(FieldElement[][])
BigMatrix
whose entries are the the values in the
the input array. The input array is copied, not referenced.data
- input arrayjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if data is null@Deprecated public static BigMatrix createBigMatrix(java.math.BigDecimal[][] data, boolean copyArray)
createFieldMatrix(FieldElement[][])
BigMatrix
whose entries are the the values in the
the input array.
If an array is built specially in order to be embedded in a
BigMatrix and not used directly, the copyArray
may be
set to false
data
- data for new matrixcopyArray
- if true, the input array will be copied, otherwise
it will be referencedjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if data
is nullcreateRealMatrix(double[][])
@Deprecated public static BigMatrix createBigMatrix(java.lang.String[][] data)
createFieldMatrix(FieldElement[][])
BigMatrix
whose entries are the the values in the
the input array. The input array is copied, not referenced.data
- input arrayjava.lang.IllegalArgumentException
- if data
is not rectangular
(not all rows have the same length) or emptyjava.lang.NullPointerException
- if data is nullpublic static RealVector createRealVector(double[] data)
RealVector
using the data from the input array.data
- the input datajava.lang.IllegalArgumentException
- if data
is emptyjava.lang.NullPointerException
- if data
is nullpublic 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 datajava.lang.IllegalArgumentException
- if data
is emptyjava.lang.NullPointerException
- if data
is nullpublic static RealMatrix createRowRealMatrix(double[] rowData)
RealMatrix
using the data from the input
array.rowData
- the input row datajava.lang.IllegalArgumentException
- if rowData
is emptyjava.lang.NullPointerException
- if rowData
is nullpublic 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 datajava.lang.IllegalArgumentException
- if rowData
is emptyjava.lang.NullPointerException
- if rowData
is null@Deprecated public static BigMatrix createRowBigMatrix(double[] rowData)
createRowFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.rowData
- the input row datajava.lang.IllegalArgumentException
- if rowData
is emptyjava.lang.NullPointerException
- if rowData
is null@Deprecated public static BigMatrix createRowBigMatrix(java.math.BigDecimal[] rowData)
createRowFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.rowData
- the input row datajava.lang.IllegalArgumentException
- if rowData
is emptyjava.lang.NullPointerException
- if rowData
is null@Deprecated public static BigMatrix createRowBigMatrix(java.lang.String[] rowData)
createRowFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.rowData
- the input row datajava.lang.IllegalArgumentException
- if rowData
is emptyjava.lang.NullPointerException
- if rowData
is nullpublic static RealMatrix createColumnRealMatrix(double[] columnData)
RealMatrix
using the data from the input
array.columnData
- the input column datajava.lang.IllegalArgumentException
- if columnData
is emptyjava.lang.NullPointerException
- if columnData
is nullpublic 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 datajava.lang.IllegalArgumentException
- if columnData
is emptyjava.lang.NullPointerException
- if columnData
is null@Deprecated public static BigMatrix createColumnBigMatrix(double[] columnData)
createColumnFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.columnData
- the input column datajava.lang.IllegalArgumentException
- if columnData
is emptyjava.lang.NullPointerException
- if columnData
is null@Deprecated public static BigMatrix createColumnBigMatrix(java.math.BigDecimal[] columnData)
createColumnFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.columnData
- the input column datajava.lang.IllegalArgumentException
- if columnData
is emptyjava.lang.NullPointerException
- if columnData
is null@Deprecated public static BigMatrix createColumnBigMatrix(java.lang.String[] columnData)
createColumnFieldMatrix(FieldElement[])
BigMatrix
using the data from the input
array.columnData
- the input column datajava.lang.IllegalArgumentException
- if columnData
is emptyjava.lang.NullPointerException
- if columnData
is nullpublic static void checkRowIndex(AnyMatrix m, int row)
m
- matrix containing the submatrixrow
- row index to checkMatrixIndexException
- if index is not validpublic static void checkColumnIndex(AnyMatrix m, int column) throws MatrixIndexException
m
- matrix containing the submatrixcolumn
- column index to checkMatrixIndexException
- if index is not validpublic static void checkSubMatrixIndex(AnyMatrix m, int startRow, int endRow, int startColumn, int endColumn)
m
- matrix containing the submatrixstartRow
- Initial row indexendRow
- Final row indexstartColumn
- Initial column indexendColumn
- Final column indexMatrixIndexException
- if the indices are not validpublic static void checkSubMatrixIndex(AnyMatrix m, int[] selectedRows, int[] selectedColumns) throws MatrixIndexException
m
- matrix containing the submatrixselectedRows
- Array of row indices.selectedColumns
- Array of column indices.MatrixIndexException
- if row or column selections are not validpublic static void checkAdditionCompatible(AnyMatrix left, AnyMatrix right) throws java.lang.IllegalArgumentException
left
- left hand side matrixright
- right hand side matrixjava.lang.IllegalArgumentException
- if matrices are not addition compatiblepublic static void checkSubtractionCompatible(AnyMatrix left, AnyMatrix right) throws java.lang.IllegalArgumentException
left
- left hand side matrixright
- right hand side matrixjava.lang.IllegalArgumentException
- if matrices are not subtraction compatiblepublic static void checkMultiplicationCompatible(AnyMatrix left, AnyMatrix right) throws java.lang.IllegalArgumentException
left
- left hand side matrixright
- right hand side matrixjava.lang.IllegalArgumentException
- if matrices are not multiplication compatiblepublic static Array2DRowRealMatrix fractionMatrixToRealMatrix(FieldMatrix<Fraction> m)
m
- matrix to convertpublic static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(FieldMatrix<BigFraction> m)
m
- matrix to convertpublic static void serializeRealVector(RealVector vector, java.io.ObjectOutputStream oos) throws java.io.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 writtenjava.io.IOException
- if object cannot be written to streamdeserializeRealVector(Object, String, ObjectInputStream)
public static void deserializeRealVector(java.lang.Object instance, java.lang.String fieldName, java.io.ObjectInputStream ois) throws java.lang.ClassNotFoundException, java.io.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.
instance
- instance in which the field must be set upfieldName
- name of the field within the class (may be private and final)ois
- stream from which the real vector should be readjava.lang.ClassNotFoundException
- if a class in the stream cannot be foundjava.io.IOException
- if object cannot be read from the streamserializeRealVector(RealVector, ObjectOutputStream)
public static void serializeRealMatrix(RealMatrix matrix, java.io.ObjectOutputStream oos) throws java.io.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 writtenjava.io.IOException
- if object cannot be written to streamdeserializeRealMatrix(Object, String, ObjectInputStream)
public static void deserializeRealMatrix(java.lang.Object instance, java.lang.String fieldName, java.io.ObjectInputStream ois) throws java.lang.ClassNotFoundException, java.io.IOException
RealMatrix
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
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.
instance
- instance in which the field must be set upfieldName
- name of the field within the class (may be private and final)ois
- stream from which the real matrix should be readjava.lang.ClassNotFoundException
- if a class in the stream cannot be foundjava.io.IOException
- if object cannot be read from the streamserializeRealMatrix(RealMatrix, ObjectOutputStream)
Copyright © 2010 - 2020 Adobe. All Rights Reserved