Package org.la4j

Class Matrix

    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix()
      Creates a zero-shape matrix.
      Matrix​(int rows, int columns)
      Creates a matrix of given shape rows x columns;
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      Matrix add​(double value)
      Adds given value (v) to every element of this matrix (A).
      Matrix add​(Matrix that)
      Adds given that matrix (B) to this matrix (A).
      abstract <T> T apply​(MatrixMatrixOperation<T> operation, Matrix that)
      Pipes this matrix to a given operation.
      abstract <T> T apply​(MatrixOperation<T> operation)
      Pipes this matrix to a given operation.
      abstract <T> T apply​(MatrixVectorOperation<T> operation, Vector that)
      Pipes this matrix to a given operation.
      Matrix blank()
      Creates the blank matrix (a zero matrix with same size) of this matrix.
      Matrix blankOfColumns​(int columns)
      Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: columns.
      Matrix blankOfRows​(int rows)
      Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: rows.
      abstract Matrix blankOfShape​(int rows, int columns)
      Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: rows x columns.
      static Matrix block​(Matrix a, Matrix b, Matrix c, Matrix d)
      Creates a block Matrix of the given blocks a, b, c and d.
      ColumnMajorMatrixIterator columnMajorIterator()
      Returns a column-major matrix iterator.
      int columns()
      Returns the number of columns of this matrix.
      static Matrix constant​(int rows, int columns, double constant)
      Creates a constant Matrix of the given shape and value.
      Matrix copy()
      Copies this matrix.
      Matrix copyOfColumns​(int columns)
      Copies this matrix into the new matrix with specified column dimension: columns.
      Matrix copyOfRows​(int rows)
      Copies this matrix into the new matrix with specified row dimension: rows.
      abstract Matrix copyOfShape​(int rows, int columns)
      Copies this matrix into the new matrix with specified dimensions: rows and columns.
      double determinant()
      Calculates the determinant of this matrix.
      static Matrix diagonal​(int size, double diagonal)
      Creates a diagonal Matrix of the given size whose diagonal elements are equal to diagonal.
      double diagonalProduct()
      Calculates the product of diagonal elements of this matrix.
      Matrix divide​(double value)
      Divides every element of this matrix (A) by given value (v).
      void each​(MatrixProcedure procedure)
      Applies given procedure to each element of this matrix.
      void eachInColumn​(int j, VectorProcedure procedure)
      Applies given procedure to each element of specified column of this matrix.
      void eachInRow​(int i, VectorProcedure procedure)
      Applies given procedure to each element of specified row of this matrix.
      protected void ensureDimensionsAreCorrect​(int rows, int columns)  
      protected void ensureIndexesAreInBounds​(int i, int j)  
      boolean equals​(java.lang.Object o)  
      boolean equals​(Matrix matrix, double precision)
      Returns true when matrix is equal to given matrix with given precision
      double euclideanNorm()
      Calculates an Euclidean norm of this matrix, a.k.a.
      protected void fail​(java.lang.String message)  
      double fold​(MatrixAccumulator accumulator)
      Folds all elements of this matrix with given accumulator.
      double foldColumn​(int j, VectorAccumulator accumulator)
      Folds all elements of specified column in this matrix with given accumulator.
      double[] foldColumns​(VectorAccumulator accumulator)
      Folds all elements (in a column-by-column manner) of this matrix with given accumulator.
      double foldRow​(int i, VectorAccumulator accumulator)
      Folds all elements of specified row in this matrix with given accumulator.
      double[] foldRows​(VectorAccumulator accumulator)
      Folds all elements (in row-by-row manner) of this matrix with given accumulator.
      static Matrix from1DArray​(int rows, int columns, double[] array)
      Creates a Matrix of the given 1D array w/o copying the underlying array.
      static Matrix from2DArray​(double[][] array)
      Creates a Matrix of the given 2D array w/o copying the underlying array.
      static Matrix fromCSV​(java.lang.String csv)
      Parses Matrix from the given CSV string.
      static Matrix fromMatrixMarket​(java.lang.String mm)
      Parses Matrix from the given Matrix Market string.
      abstract double get​(int i, int j)
      Gets the specified element of this matrix.
      abstract Vector getColumn​(int j)
      Copies the specified column of this matrix into the vector.
      abstract Vector getRow​(int i)
      Copies the specified row of this matrix into the vector.
      Matrix hadamardProduct​(Matrix that)
      Calculates the Hadamard (element-wise) product of this and given that matrix.
      int hashCode()  
      static Matrix identity​(int size)
      Creates an identity Matrix of the given size.
      private void indent​(java.lang.StringBuilder sb, int howMany)  
      double infinityNorm()
      Calculates an Infinity norm of this matrix.
      Matrix insert​(Matrix that)
      Inserts a given that (B) into this matrix (A).
      Matrix insert​(Matrix that, int rows, int columns)
      Inserts a given that matrix (B) into this matrix (A).
      Matrix insert​(Matrix that, int destRow, int destColumn, int rows, int columns)
      Inserts a given that matrix (B) into this matrix (A).
      Matrix insert​(Matrix that, int srcRow, int srcColumn, int destRow, int destColumn, int rows, int columns)
      Inserts a given that matrix (B) into this matrix (A).
      Matrix insertColumn​(int j, Vector column)
      Adds one column to matrix.
      Matrix insertRow​(int i, Vector row)
      Adds one row to matrix.
      boolean is​(AdvancedMatrixPredicate predicate)
      Checks whether this matrix compiles with given predicate or not.
      boolean is​(MatrixPredicate predicate)
      Checks whether this matrix compiles with given predicate or not.
      MatrixIterator iterator()
      Returns a matrix iterator.
      VectorIterator iteratorOfColumn​(int j)
      Returns a vector iterator of the given column {code j}.
      VectorIterator iteratorOfRow​(int i)
      Returns a vector iterator of the given row {code i}.
      Matrix kroneckerProduct​(Matrix that)
      Calculates the Kronecker product of this matrix (A) and given that matrix (B).
      double manhattanNorm()
      Calculates a Manhattan norm of this matrix, a.k.a.
      double max()
      Searches for the maximum value of the elements of this matrix.
      double maxInColumn​(int j)
      Searches for the maximum value of specified column in this matrix.
      double maxInRow​(int i)
      Searches for the maximum value of specified row in this matrix.
      double min()
      Searches for the minimum value of the elements of this matrix.
      double minInColumn​(int j)
      Searches for the minimum value of specified column in this matrix.
      double minInRow​(int i)
      Searches for the minimum value of specified row in this matrix.
      java.lang.String mkString​(java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
      Converts this matrix into the string representation.
      java.lang.String mkString​(java.text.NumberFormat formatter)
      Converts this matrix into the string representation.
      java.lang.String mkString​(java.text.NumberFormat formatter, java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
      Converts this matrix into the string representation.
      Matrix multiply​(double value)
      Scales this matrix by given value (v).
      Matrix multiply​(Matrix that)
      Multiplies this matrix (A) by given that matrix (B).
      Vector multiply​(Vector that)
      Multiplies this matrix (A) by given that vector (x).
      Matrix multiplyByItsTranspose()
      Multiplies this matrix by its transpose.
      boolean non​(AdvancedMatrixPredicate predicate)
      Checks whether this matrix compiles with given predicate or not.
      boolean non​(MatrixPredicate predicate)
      Checks whether this matrix compiles with given predicate or not.
      double norm()
      Calculates an Euclidean norm of this matrix, a.k.a.
      Matrix power​(int n)
      Powers this matrix of given exponent {code n}.
      double product()
      Multiplies up all elements of this matrix.
      static Matrix random​(int rows, int columns, java.util.Random random)
      Creates a random Matrix of the given shape: rows x columns.
      static Matrix randomSymmetric​(int size, java.util.Random random)
      Creates a random symmetric Matrix of the given size.
      int rank()
      Calculates the rank of this matrix.
      Matrix removeColumn​(int j)
      Removes one column from matrix.
      Matrix removeFirstColumn()
      Removes first column from matrix.
      Matrix removeFirstRow()
      Removes first row from matrix.
      Matrix removeLastColumn()
      Removes last column from matrix.
      Matrix removeLastRow()
      Removes last row from matrix.
      Matrix removeRow​(int i)
      Removes one row from matrix.
      Matrix rotate()
      Rotates this matrix by 90 degrees to the right.
      RowMajorMatrixIterator rowMajorIterator()
      Returns a row-major matrix iterator.
      int rows()
      Returns the number of rows of this matrix.
      Matrix select​(int[] rowIndices, int[] columnIndices)
      Returns a new matrix with the selected rows and columns.
      abstract void set​(int i, int j, double value)
      Sets the specified element of this matrix to given value.
      void setAll​(double value)
      Sets all elements of this matrix to the given value.
      void setColumn​(int j, double value)
      Sets all elements of the specified column of this matrix to given value.
      void setColumn​(int j, Vector column)
      Copies given column into the specified column of this matrix.
      void setRow​(int i, double value)
      Sets all elements of the specified row of this matrix to given value.
      void setRow​(int i, Vector row)
      Copies given row into the specified row of this matrix.
      Matrix shuffle()
      Shuffles this matrix.
      Matrix slice​(int fromRow, int fromColumn, int untilRow, int untilColumn)
      Retrieves the specified sub-matrix of this matrix.
      Matrix sliceBottomRight​(int fromRow, int fromColumn)
      Retrieves the specified sub-matrix of this matrix.
      Matrix sliceTopLeft​(int untilRow, int untilColumn)
      Retrieves the specified sub-matrix of this matrix.
      Matrix subtract​(double value)
      Subtracts given value (v) from every element of this matrix (A).
      Matrix subtract​(Matrix that)
      Subtracts given that matrix (B) from this matrix (A).
      double sum()
      Summarizes up all elements of this matrix.
      void swapColumns​(int i, int j)
      Swaps the specified columns of this matrix.
      void swapRows​(int i, int j)
      Swaps the specified rows of this matrix.
      <T extends Matrix>
      T
      to​(MatrixFactory<T> factory)
      Converts this matrix using the given factory.
      abstract byte[] toBinary()
      Encodes this matrix into a byte array.
      ColumnMajorSparseMatrix toColumnMajorSparseMatrix()
      Converts this matrix into a column-major sparse matrix.
      Vector toColumnVector()
      Converts this matrix into the column vector.
      java.lang.String toCSV()
      Converts this matrix into the CSV (Comma Separated Value) string.
      java.lang.String toCSV​(java.text.NumberFormat formatter)
      Converts this matrix into the CSV (Comma Separated Value) string using the given formatter.
      DenseMatrix toDenseMatrix()
      Converts this matrix into a dense matrix.
      java.lang.String toMatrixMarket()
      Converts this matrix into the Matrix Market string.
      abstract java.lang.String toMatrixMarket​(java.text.NumberFormat formatter)
      Converts this matrix into the Matrix Market string using the given number formatter.
      RowMajorSparseMatrix toRowMajorSparseMatrix()
      Converts this matrix into a row-major sparse matrix.
      Vector toRowVector()
      Converts this matrix into the row vector.
      SparseMatrix toSparseMatrix()
      Converts this matrix into a sparse matrix.
      java.lang.String toString()  
      double trace()
      Calculates the trace of this matrix.
      Matrix transform​(MatrixFunction function)
      Builds a new matrix by applying given function to each element of this matrix.
      Matrix transformColumn​(int j, VectorFunction function)
      Builds a new matrix by applying given function to each element of specified column in this matrix.
      Matrix transformRow​(int i, VectorFunction function)
      Builds a new matrix by applying given function to each element of specified row in this matrix.
      Matrix transpose()
      Transposes this matrix.
      static Matrix unit​(int rows, int columns)
      Creates an unit Matrix of the given shape: rows x columns.
      void update​(MatrixFunction function)
      Updates all elements of this matrix by applying given function.
      void updateAt​(int i, int j, MatrixFunction function)
      Updates the specified element of this matrix by applying given function.
      void updateColumn​(int j, VectorFunction function)
      Updates all elements of the specified column in this matrix by applying given function.
      void updateRow​(int i, VectorFunction function)
      Updates all elements of the specified row in this matrix by applying given function.
      MatrixDecompositor withDecompositor​(LinearAlgebra.DecompositorFactory factory)
      Creates a new decompositor by given factory of this matrix.
      MatrixInverter withInverter​(LinearAlgebra.InverterFactory factory)
      Creates a new inverter by given factory of this matrix.
      LinearSystemSolver withSolver​(LinearAlgebra.SolverFactory factory)
      Creates a new solver by given factory of this matrix.
      static Matrix zero​(int rows, int columns)
      Creates a zero Matrix of the given shape: rows x columns.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • DEFAULT_ROWS_DELIMITER

        private static final java.lang.String DEFAULT_ROWS_DELIMITER
        See Also:
        Constant Field Values
      • DEFAULT_COLUMNS_DELIMITER

        private static final java.lang.String DEFAULT_COLUMNS_DELIMITER
        See Also:
        Constant Field Values
      • DEFAULT_FORMATTER

        private static final java.text.NumberFormat DEFAULT_FORMATTER
      • INDENTS

        private static final java.lang.String[] INDENTS
      • rows

        protected int rows
      • columns

        protected int columns
    • Constructor Detail

      • Matrix

        public Matrix()
        Creates a zero-shape matrix.
      • Matrix

        public Matrix​(int rows,
                      int columns)
        Creates a matrix of given shape rows x columns;
    • Method Detail

      • zero

        public static Matrix zero​(int rows,
                                  int columns)
        Creates a zero Matrix of the given shape: rows x columns.
      • constant

        public static Matrix constant​(int rows,
                                      int columns,
                                      double constant)
        Creates a constant Matrix of the given shape and value.
      • diagonal

        public static Matrix diagonal​(int size,
                                      double diagonal)
        Creates a diagonal Matrix of the given size whose diagonal elements are equal to diagonal.
      • unit

        public static Matrix unit​(int rows,
                                  int columns)
        Creates an unit Matrix of the given shape: rows x columns.
      • identity

        public static Matrix identity​(int size)
        Creates an identity Matrix of the given size.
      • random

        public static Matrix random​(int rows,
                                    int columns,
                                    java.util.Random random)
        Creates a random Matrix of the given shape: rows x columns.
      • randomSymmetric

        public static Matrix randomSymmetric​(int size,
                                             java.util.Random random)
        Creates a random symmetric Matrix of the given size.
      • from1DArray

        public static Matrix from1DArray​(int rows,
                                         int columns,
                                         double[] array)
        Creates a Matrix of the given 1D array w/o copying the underlying array.
      • from2DArray

        public static Matrix from2DArray​(double[][] array)
        Creates a Matrix of the given 2D array w/o copying the underlying array.
      • fromCSV

        public static Matrix fromCSV​(java.lang.String csv)
        Parses Matrix from the given CSV string.
        Parameters:
        csv - the string in CSV format
        Returns:
        a parsed matrix
      • fromMatrixMarket

        public static Matrix fromMatrixMarket​(java.lang.String mm)
        Parses Matrix from the given Matrix Market string.
        Parameters:
        mm - the string in Matrix Market format
        Returns:
        a parsed matrix
      • get

        public abstract double get​(int i,
                                   int j)
        Gets the specified element of this matrix.
        Parameters:
        i - element's row index
        j - element's column index
        Returns:
        the element of this matrix
      • set

        public abstract void set​(int i,
                                 int j,
                                 double value)
        Sets the specified element of this matrix to given value.
        Parameters:
        i - element's row index
        j - element's column index
        value - element's new value
      • getRow

        public abstract Vector getRow​(int i)
        Copies the specified row of this matrix into the vector.
        Parameters:
        i - the row index
        Returns:
        the row represented as vector
      • getColumn

        public abstract Vector getColumn​(int j)
        Copies the specified column of this matrix into the vector.
        Parameters:
        j - the column index
        Returns:
        the column represented as vector
      • blankOfShape

        public abstract Matrix blankOfShape​(int rows,
                                            int columns)
        Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: rows x columns.
        Returns:
        blank matrix
      • copyOfShape

        public abstract Matrix copyOfShape​(int rows,
                                           int columns)
        Copies this matrix into the new matrix with specified dimensions: rows and columns.
        Parameters:
        rows - the number of rows in new matrix
        columns - the number of columns in new matrix
        Returns:
        the copy of this matrix with new size
      • apply

        public abstract <T> T apply​(MatrixOperation<T> operation)
        Pipes this matrix to a given operation.
        Type Parameters:
        T - the result type
        Parameters:
        operation - the matrix operation (an operation that takes a matrix and returns T)
        Returns:
        the result of an operation applied to this matrix
      • apply

        public abstract <T> T apply​(MatrixMatrixOperation<T> operation,
                                    Matrix that)
        Pipes this matrix to a given operation.
        Type Parameters:
        T - the result type
        Parameters:
        operation - the matrix-matrix operation (an operation that takes two matrices and returns T)
        that - the right hand matrix of the given operation
        Returns:
        the result of an operation applied to this matrix
      • apply

        public abstract <T> T apply​(MatrixVectorOperation<T> operation,
                                    Vector that)
        Pipes this matrix to a given operation.
        Type Parameters:
        T - the result type
        Parameters:
        operation - the matrix-vector operation (an operation that takes matrix and vector and returns T)
        that - the right hand vector of the given operation
        Returns:
        the result of an operation applied to this matrix
      • toBinary

        public abstract byte[] toBinary()
        Encodes this matrix into a byte array.
        Returns:
        a byte array representing this matrix
      • toMatrixMarket

        public abstract java.lang.String toMatrixMarket​(java.text.NumberFormat formatter)
        Converts this matrix into the Matrix Market string using the given number formatter.
        Returns:
        a string in Matrix Market format representing this matrix;
      • setAll

        public void setAll​(double value)
        Sets all elements of this matrix to the given value.
        Parameters:
        value - the element's new value
      • setRow

        public void setRow​(int i,
                           double value)

        Sets all elements of the specified row of this matrix to given value.

        Parameters:
        i - the row index
        value - the element's new value
      • setColumn

        public void setColumn​(int j,
                              double value)

        Sets all elements of the specified column of this matrix to given value.

        Parameters:
        j - the column index
        value - the element's new value
      • swapRows

        public void swapRows​(int i,
                             int j)
        Swaps the specified rows of this matrix.
        Parameters:
        i - the row index
        j - the row index
      • swapColumns

        public void swapColumns​(int i,
                                int j)
        Swaps the specified columns of this matrix.
        Parameters:
        i - the column index
        j - the column index
      • rows

        public int rows()
        Returns the number of rows of this matrix.
        Returns:
        the number of rows
      • columns

        public int columns()
        Returns the number of columns of this matrix.
        Returns:
        the number of columns
      • transpose

        public Matrix transpose()
        Transposes this matrix.
        Returns:
        the transposed matrix
      • rotate

        public Matrix rotate()
        Rotates this matrix by 90 degrees to the right.
        Returns:
        the rotated matrix
      • power

        public Matrix power​(int n)
        Powers this matrix of given exponent {code n}.
        Parameters:
        n - the exponent
        Returns:
        the powered matrix
      • multiply

        public Matrix multiply​(double value)
        Scales this matrix by given value (v).
        Parameters:
        value - the scale factor
        Returns:
        A * v
      • multiply

        public Vector multiply​(Vector that)
        Multiplies this matrix (A) by given that vector (x).
        Parameters:
        that - the vector
        Returns:
        A * x
      • multiply

        public Matrix multiply​(Matrix that)
        Multiplies this matrix (A) by given that matrix (B).
        Parameters:
        that - the right hand matrix for multiplication
        Returns:
        A * B
      • multiplyByItsTranspose

        public Matrix multiplyByItsTranspose()
        Multiplies this matrix by its transpose.
        Returns:
        this matrix multiplied by its transpose
      • subtract

        public Matrix subtract​(double value)
        Subtracts given value (v) from every element of this matrix (A).
        Parameters:
        value - the right hand value for subtraction
        Returns:
        A - v
      • subtract

        public Matrix subtract​(Matrix that)
        Subtracts given that matrix (B) from this matrix (A).
        Parameters:
        that - the right hand matrix for subtraction
        Returns:
        A - B
      • add

        public Matrix add​(double value)
        Adds given value (v) to every element of this matrix (A).
        Parameters:
        value - the right hand value for addition
        Returns:
        A + v
      • add

        public Matrix add​(Matrix that)
        Adds given that matrix (B) to this matrix (A).
        Parameters:
        that - the right hand matrix for addition
        Returns:
        A + B
      • insert

        public Matrix insert​(Matrix that)
        Inserts a given that (B) into this matrix (A). The original values are overwritten by the new ones.
        Parameters:
        that - the matrix to insert, from the first row and column
        Returns:
        a matrix with the parameter inserted into it
      • insert

        public Matrix insert​(Matrix that,
                             int rows,
                             int columns)
        Inserts a given that matrix (B) into this matrix (A). The original values are overwritten by the new ones.
        Parameters:
        that - the matrix to insert
        rows - number of rows to insert
        columns - number of columns to insert
        Returns:
        a matrix with the parameter inserted into it
      • insert

        public Matrix insert​(Matrix that,
                             int destRow,
                             int destColumn,
                             int rows,
                             int columns)
        Inserts a given that matrix (B) into this matrix (A). The original values are overwritten by the new ones.
        Parameters:
        that - the matrix to insert
        destRow - the row to insert at in the destination matrix
        destColumn - the column to insert at in the destination matrix
        rows - number of rows to insert
        columns - number of columns to insert
        Returns:
        a matrix with the parameter inserted into it
      • insert

        public Matrix insert​(Matrix that,
                             int srcRow,
                             int srcColumn,
                             int destRow,
                             int destColumn,
                             int rows,
                             int columns)
        Inserts a given that matrix (B) into this matrix (A). The original values are overwritten by the new ones.
        Parameters:
        that - the matrix to insert
        srcRow - the row to start at in the source matrix
        srcColumn - the column to start at in the source matrix
        destRow - the row to insert at in the destination matrix
        destColumn - the column to insert at in the destination matrix
        rows - number of rows to insert
        columns - number of columns to insert
        Returns:
        a matrix with the parameter inserted into it
      • divide

        public Matrix divide​(double value)
        Divides every element of this matrix (A) by given value (v).
        Parameters:
        value - the right hand value for division
        Returns:
        A / v
      • kroneckerProduct

        public Matrix kroneckerProduct​(Matrix that)
        Calculates the Kronecker product of this matrix (A) and given that matrix (B).
        Parameters:
        that - the right hand matrix for Kronecker product
        Returns:
        A (+) B
      • diagonalProduct

        public double diagonalProduct()
        Calculates the product of diagonal elements of this matrix.
        Returns:
        the product of diagonal elements of this matrix
      • norm

        public double norm()
        Calculates an Euclidean norm of this matrix, a.k.a. frobenius norm
        Returns:
        an Euclidean norm
      • euclideanNorm

        public double euclideanNorm()
        Calculates an Euclidean norm of this matrix, a.k.a. frobenius norm
        Returns:
        an Euclidean norm
      • manhattanNorm

        public double manhattanNorm()
        Calculates a Manhattan norm of this matrix, a.k.a. taxicab norm
        Returns:
        a Manhattan norm
      • infinityNorm

        public double infinityNorm()
        Calculates an Infinity norm of this matrix.
        Returns:
        an Infinity norm
      • product

        public double product()
        Multiplies up all elements of this matrix.
        Returns:
        the product of all elements of this matrix
      • sum

        public double sum()
        Summarizes up all elements of this matrix.
        Returns:
        the sum of all elements of this matrix
      • hadamardProduct

        public Matrix hadamardProduct​(Matrix that)
        Calculates the Hadamard (element-wise) product of this and given that matrix.
        Parameters:
        that - the right hand matrix for Hadamard product
        Returns:
        the Hadamard product of two matrices
      • setRow

        public void setRow​(int i,
                           Vector row)
        Copies given row into the specified row of this matrix.
        Parameters:
        i - the row index
        row - the row represented as vector
      • setColumn

        public void setColumn​(int j,
                              Vector column)
        Copies given column into the specified column of this matrix.
        Parameters:
        j - the column index
        column - the column represented as vector
      • insertRow

        public Matrix insertRow​(int i,
                                Vector row)
        Adds one row to matrix.
        Parameters:
        i - the row index
        Returns:
        matrix with row.
      • insertColumn

        public Matrix insertColumn​(int j,
                                   Vector column)
        Adds one column to matrix.
        Parameters:
        j - the column index
        Returns:
        matrix with column.
      • removeRow

        public Matrix removeRow​(int i)
        Removes one row from matrix.
        Parameters:
        i - the row index
        Returns:
        matrix without row.
      • removeColumn

        public Matrix removeColumn​(int j)
        Removes one column from matrix.
        Parameters:
        j - the column index
        Returns:
        matrix without column.
      • removeFirstRow

        public Matrix removeFirstRow()
        Removes first row from matrix.
        Returns:
        matrix without first row.
      • removeFirstColumn

        public Matrix removeFirstColumn()
        Removes first column from matrix.
        Returns:
        matrix without first column
      • removeLastRow

        public Matrix removeLastRow()
        Removes last row from matrix.
        Returns:
        matrix without last row
      • removeLastColumn

        public Matrix removeLastColumn()
        Removes last column from matrix.
        Returns:
        matrix without last column
      • blank

        public Matrix blank()
        Creates the blank matrix (a zero matrix with same size) of this matrix.
        Returns:
        blank matrix
      • blankOfRows

        public Matrix blankOfRows​(int rows)
        Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: rows. The columns number remains the same.
        Returns:
        blank matrix
      • blankOfColumns

        public Matrix blankOfColumns​(int columns)
        Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: columns. The rows number remains the same.
        Returns:
        blank matrix
      • copy

        public Matrix copy()
        Copies this matrix.
        Returns:
        the copy of this matrix
      • copyOfRows

        public Matrix copyOfRows​(int rows)
        Copies this matrix into the new matrix with specified row dimension: rows.
        Parameters:
        rows - the number of rows in new matrix
        Returns:
        the copy of this matrix with new size
      • copyOfColumns

        public Matrix copyOfColumns​(int columns)
        Copies this matrix into the new matrix with specified column dimension: columns.
        Parameters:
        columns - the number of columns in new matrix
        Returns:
        the copy of this matrix with new size
      • shuffle

        public Matrix shuffle()
        Shuffles this matrix.

        Copies this matrix into the matrix that contains the same elements but with the elements shuffled around (which might also result in the same matrix (with a small likelihood)).

        Returns:
        the shuffled matrix
      • slice

        public Matrix slice​(int fromRow,
                            int fromColumn,
                            int untilRow,
                            int untilColumn)
        Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices.
        Parameters:
        fromRow - the beginning of the row indices interval
        fromColumn - the beginning of the column indices interval
        untilRow - the ending of the row indices interval
        untilColumn - the ending of the column indices interval
        Returns:
        the sub-matrix of this matrix
      • sliceTopLeft

        public Matrix sliceTopLeft​(int untilRow,
                                   int untilColumn)
        Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices. The top left points of both intervals are fixed to zero.
        Parameters:
        untilRow - the ending of the row indices interval
        untilColumn - the ending of the column indices interval
        Returns:
        the sub-matrix of this matrix
      • sliceBottomRight

        public Matrix sliceBottomRight​(int fromRow,
                                       int fromColumn)
        Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices. The bottom right points of both intervals are fixed to matrix dimensions - it's rows and columns correspondingly.
        Parameters:
        fromRow - the beginning of the row indices interval
        fromColumn - the beginning of the column indices interval
        Returns:
        the sub-matrix of this matrix
      • select

        public Matrix select​(int[] rowIndices,
                             int[] columnIndices)
        Returns a new matrix with the selected rows and columns. This method can be used either return a specific subset of rows and/or columns or to permute the indices in an arbitrary order. The list of indices are allowed to contain duplicates indices. This is more general than slice() which selects only contiguous blocks. However, where applicable slice() is probably more efficient.
        Parameters:
        rowIndices - the array of row indices
        columnIndices - the array of column indices
        Returns:
        the new matrix with the selected rows and columns
        Throws:
        java.lang.IllegalArgumentException - if invalid row or column indices are provided
      • each

        public void each​(MatrixProcedure procedure)
        Applies given procedure to each element of this matrix.
        Parameters:
        procedure - the matrix procedure
      • eachInRow

        public void eachInRow​(int i,
                              VectorProcedure procedure)
        Applies given procedure to each element of specified row of this matrix.
        Parameters:
        i - the row index
        procedure - the vector procedure
      • eachInColumn

        public void eachInColumn​(int j,
                                 VectorProcedure procedure)
        Applies given procedure to each element of specified column of this matrix.
        Parameters:
        j - the column index
        procedure - the vector procedure
      • max

        public double max()
        Searches for the maximum value of the elements of this matrix.
        Returns:
        maximum value of this matrix
      • min

        public double min()
        Searches for the minimum value of the elements of this matrix.
        Returns:
        minimum value of this matrix
      • maxInRow

        public double maxInRow​(int i)
        Searches for the maximum value of specified row in this matrix.
        Parameters:
        i - the row index
        Returns:
        maximum value of specified row in this matrix
      • minInRow

        public double minInRow​(int i)
        Searches for the minimum value of specified row in this matrix.
        Parameters:
        i - the row index
        Returns:
        minimum value of specified row in this matrix
      • maxInColumn

        public double maxInColumn​(int j)
        Searches for the maximum value of specified column in this matrix.
        Parameters:
        j - the column index
        Returns:
        maximum value of specified column in this matrix
      • minInColumn

        public double minInColumn​(int j)
        Searches for the minimum value of specified column in this matrix.
        Parameters:
        j - the column index
        Returns:
        minimum value of specified column in this matrix
      • transform

        public Matrix transform​(MatrixFunction function)
        Builds a new matrix by applying given function to each element of this matrix.
        Parameters:
        function - the matrix function
        Returns:
        the transformed matrix
      • transformRow

        public Matrix transformRow​(int i,
                                   VectorFunction function)
        Builds a new matrix by applying given function to each element of specified row in this matrix.
        Parameters:
        i - the row index
        function - the vector function
        Returns:
        the transformed matrix
      • transformColumn

        public Matrix transformColumn​(int j,
                                      VectorFunction function)
        Builds a new matrix by applying given function to each element of specified column in this matrix.
        Parameters:
        j - the column index
        function - the vector function
        Returns:
        the transformed matrix
      • update

        public void update​(MatrixFunction function)
        Updates all elements of this matrix by applying given function.
        Parameters:
        function - the matrix function
      • updateAt

        public void updateAt​(int i,
                             int j,
                             MatrixFunction function)
        Updates the specified element of this matrix by applying given function.
        Parameters:
        i - the row index
        j - the column index
        function - the matrix function
      • updateRow

        public void updateRow​(int i,
                              VectorFunction function)
        Updates all elements of the specified row in this matrix by applying given function.
        Parameters:
        i - the row index
        function - the vector function
      • updateColumn

        public void updateColumn​(int j,
                                 VectorFunction function)
        Updates all elements of the specified column in this matrix by applying given function.
        Parameters:
        j - the column index
        function - the vector function
      • fold

        public double fold​(MatrixAccumulator accumulator)
        Folds all elements of this matrix with given accumulator.
        Parameters:
        accumulator - the matrix accumulator
        Returns:
        the accumulated value
      • foldRow

        public double foldRow​(int i,
                              VectorAccumulator accumulator)
        Folds all elements of specified row in this matrix with given accumulator.
        Parameters:
        i - the row index
        accumulator - the vector accumulator
        Returns:
        the accumulated value
      • foldRows

        public double[] foldRows​(VectorAccumulator accumulator)
        Folds all elements (in row-by-row manner) of this matrix with given accumulator.
        Parameters:
        accumulator - the vector accumulator
        Returns:
        the accumulated double array
      • foldColumn

        public double foldColumn​(int j,
                                 VectorAccumulator accumulator)
        Folds all elements of specified column in this matrix with given accumulator.
        Parameters:
        j - the column index
        accumulator - the vector accumulator
        Returns:
        the accumulated value
      • foldColumns

        public double[] foldColumns​(VectorAccumulator accumulator)
        Folds all elements (in a column-by-column manner) of this matrix with given accumulator.
        Parameters:
        accumulator - the vector accumulator
        Returns:
        the accumulated double array
      • is

        public boolean is​(MatrixPredicate predicate)
        Checks whether this matrix compiles with given predicate or not.
        Parameters:
        predicate - the matrix predicate
        Returns:
        whether this matrix compiles with predicate
      • is

        public boolean is​(AdvancedMatrixPredicate predicate)
        Checks whether this matrix compiles with given predicate or not.
        Parameters:
        predicate - the advanced matrix predicate
        Returns:
        whether this matrix compiles with predicate
      • non

        public boolean non​(MatrixPredicate predicate)
        Checks whether this matrix compiles with given predicate or not.
        Parameters:
        predicate - the matrix predicate
        Returns:
        whether this matrix compiles with predicate
      • non

        public boolean non​(AdvancedMatrixPredicate predicate)
        Checks whether this matrix compiles with given predicate or not.
        Parameters:
        predicate - the advanced matrix predicate
        Returns:
        whether this matrix compiles with predicate
      • toRowVector

        public Vector toRowVector()
        Converts this matrix into the row vector.
        Returns:
        the row vector of this matrix
      • toColumnVector

        public Vector toColumnVector()
        Converts this matrix into the column vector.
        Returns:
        the column vector of this matrix
      • withSolver

        public LinearSystemSolver withSolver​(LinearAlgebra.SolverFactory factory)
        Creates a new solver by given factory of this matrix.
        Parameters:
        factory - the solver factory
        Returns:
        the linear system solver of this matrix
      • withInverter

        public MatrixInverter withInverter​(LinearAlgebra.InverterFactory factory)
        Creates a new inverter by given factory of this matrix.
        Parameters:
        factory - the inverter factory
        Returns:
        the inverter of this matrix
      • withDecompositor

        public MatrixDecompositor withDecompositor​(LinearAlgebra.DecompositorFactory factory)
        Creates a new decompositor by given factory of this matrix.
        Parameters:
        factory - the decompositor factory
        Returns:
        the decompositor of this matrix
      • equals

        public boolean equals​(Matrix matrix,
                              double precision)
        Returns true when matrix is equal to given matrix with given precision
        Parameters:
        matrix - matrix
        precision - given precision
        Returns:
        equals of this matrix to that
      • mkString

        public java.lang.String mkString​(java.text.NumberFormat formatter)
        Converts this matrix into the string representation.
        Parameters:
        formatter - the number formatter
        Returns:
        the matrix converted to a string
      • mkString

        public java.lang.String mkString​(java.lang.String rowsDelimiter,
                                         java.lang.String columnsDelimiter)
        Converts this matrix into the string representation.
        Parameters:
        rowsDelimiter - the rows' delimiter
        columnsDelimiter - the columns' delimiter
        Returns:
        the matrix converted to a string
      • mkString

        public java.lang.String mkString​(java.text.NumberFormat formatter,
                                         java.lang.String rowsDelimiter,
                                         java.lang.String columnsDelimiter)
        Converts this matrix into the string representation.
        Parameters:
        formatter - the number formatter
        rowsDelimiter - the rows' delimiter
        columnsDelimiter - the columns' delimiter
        Returns:
        the matrix converted to a string
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • iterator

        public MatrixIterator iterator()
        Returns a matrix iterator.
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Double>
        Returns:
        a matrix iterator
      • rowMajorIterator

        public RowMajorMatrixIterator rowMajorIterator()
        Returns a row-major matrix iterator.
        Returns:
        a row-major matrix iterator.
      • columnMajorIterator

        public ColumnMajorMatrixIterator columnMajorIterator()
        Returns a column-major matrix iterator.
        Returns:
        a column-major matrix iterator.
      • iteratorOfRow

        public VectorIterator iteratorOfRow​(int i)
        Returns a vector iterator of the given row {code i}.
        Returns:
        a vector iterator
      • iteratorOfColumn

        public VectorIterator iteratorOfColumn​(int j)
        Returns a vector iterator of the given column {code j}.
        Returns:
        a vector iterator
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • to

        public <T extends Matrix> T to​(MatrixFactory<T> factory)
        Converts this matrix using the given factory.
        Type Parameters:
        T - type of the result matrix
        Parameters:
        factory - the factory that creates an output matrix
        Returns:
        converted matrix
      • toSparseMatrix

        public SparseMatrix toSparseMatrix()
        Converts this matrix into a sparse matrix.
        Returns:
        a sparse matrix
      • toDenseMatrix

        public DenseMatrix toDenseMatrix()
        Converts this matrix into a dense matrix.
        Returns:
        a dense matrix
      • toRowMajorSparseMatrix

        public RowMajorSparseMatrix toRowMajorSparseMatrix()
        Converts this matrix into a row-major sparse matrix.
        Returns:
        a row-major sparse matrix
      • toColumnMajorSparseMatrix

        public ColumnMajorSparseMatrix toColumnMajorSparseMatrix()
        Converts this matrix into a column-major sparse matrix.
        Returns:
        a row-major sparse matrix
      • toCSV

        public java.lang.String toCSV()
        Converts this matrix into the CSV (Comma Separated Value) string.
        Returns:
        a CSV string representing this matrix
      • toMatrixMarket

        public java.lang.String toMatrixMarket()
        Converts this matrix into the Matrix Market string.
        Returns:
        a string in Matrix Market format representing this matrix;
      • toCSV

        public java.lang.String toCSV​(java.text.NumberFormat formatter)
        Converts this matrix into the CSV (Comma Separated Value) string using the given formatter.
        Parameters:
        formatter - the number formatter
        Returns:
        a CSV string representing this matrix
      • ensureDimensionsAreCorrect

        protected void ensureDimensionsAreCorrect​(int rows,
                                                  int columns)
      • ensureIndexesAreInBounds

        protected void ensureIndexesAreInBounds​(int i,
                                                int j)
      • fail

        protected void fail​(java.lang.String message)
      • indent

        private void indent​(java.lang.StringBuilder sb,
                            int howMany)