Class MatrixUtils


  • public class MatrixUtils
    extends Object
    A collection of static methods that operate on or return matrices.
    • Method Detail

      • createRealMatrix

        public static RealMatrix createRealMatrix​(int rows,
                                                  int columns)
        Returns a 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.

        Parameters:
        rows - number of rows of the matrix
        columns - 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 a 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().

        Type Parameters:
        T - the type of the field elements
        Parameters:
        field - field to which the matrix elements belong
        rows - number of rows of the matrix
        columns - number of columns of the matrix
        Returns:
        FieldMatrix with specified dimensions
        See Also:
        createFieldMatrix(FieldElement[][])
      • createRealIdentityMatrix

        public static RealMatrix createRealIdentityMatrix​(int dimension)
        Returns dimension 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)
        Returns dimension x dimension identity matrix.
        Type Parameters:
        T - the type of the field elements
        Parameters:
        field - field to which the elements belong
        dimension - 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​(int dimension)
        Creates a RealVector 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​(Field<T> field,
                                                                                   int dimension)
        Creates a FieldVector with specified dimensions.
        Type Parameters:
        T - the type of the field elements
        Parameters:
        field - field to which array elements belong
        dimension - dimension of the vector
        Returns:
        a new vector
        Since:
        1.3
      • 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 if matrix 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 - if row or column 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 to n - 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 - if endRow < startRow or endColumn < startColumn.
      • 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
      • 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 - if m 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 of

        Higham, 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 vectors
        threshold - 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 vectors
        handler - 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 vectors
        threshold - 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 vectors
        field - type of the files elements
        handler - handler for dependent vectors
        Returns:
        orthonormal basis having the same span as independent
        Since:
        2.1