Class Matrix

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    Column, Row

    public class Matrix
    extends java.lang.Object
    implements java.io.Serializable
    Matrix implementation. This class can handle the basic operations of matrix space, add, subtract, product scalar product, transpose, and other useful operations for this API. For performance reasons, precondition validations are done using asserts instead of the more clear and safe exceptions.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int columns
      Number of rows and columns of the matrix.
      protected double[] matrix
      Matrix's data, stored for row in a sequential array.
      protected int rows
      Number of rows and columns of the matrix.
    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix​(int r, int c)
      Constructor alias for Matrix(r,c,false).
      Matrix​(int r, int c, boolean identity)
      Constructor.
      Matrix​(int r, int c, double[] data)
      Constructor.
      Matrix​(Matrix copy)
      Copy constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(Matrix a, Matrix b)
      Adds two matrices.
      void apply​(Function f, Matrix a)
      Apply one function over the elements of the matrix and left the result on a.
      void applyInIdentity​(Function f, Matrix a)
      Apply one function over the elements of the matrix and left on the main diagonal of a identity matrix 'a'.
      void copy​(Matrix a)
      Copy this matrix to another matrix a.
      double dotProduct​(Matrix a)
      Calculate the dot product between this matrix and another.
      boolean equals​(java.lang.Object b1)
      Check if two matrix are equals position to position with a precision of 1e-7.
      void fill()
      Fill the matrix with random values between (-1, 1).
      void fill​(boolean signed)
      Fill the matrix with random values between [0, 1) if signed is is false, and (-1, 1) if true.
      void fill​(boolean signed, java.util.Random r)
      Fill the matrix with random values between [0, 1) if signed is is false, and (-1, 1) if true.
      double[] getCol​(int index)
      Return an array with the values of the specified column.
      int getColumns()  
      static java.util.Random getDefaultRandom()  
      double[] getRow​(int index)
      Return an array with the values of the specified row.
      int getRows()  
      int hashCode()  
      void increment​(int i, int j, double v)
      Increments the value of one position by v.
      void multiply​(double a, Matrix b)
      Multiply this matrix by an scalar.
      void multiply​(Matrix a, Matrix b)
      Multiply two matrix.
      void multiplyAndAdd​(double a, Matrix b, Matrix result)
      Return in result the value of (this*a + b) for each This(i,j)
      double position​(int i, int j)
      Return the value on the position (i,j).
      void position​(int i, int j, double v)
      Set the value v on the position (i,j).
      static Matrix random​(int r, int c)
      Create a new Matrix filled with low random numbers.
      static Matrix random​(int r, int c, boolean signed)
      Create a new Matrix filled with low random numbers.
      static Matrix random​(int r, int c, boolean signed, java.util.Random rand)
      Create a new Matrix filled with low random numbers.
      void scale​(int i, int j, double v)
      Scales the value of one position by v.
      void setRow​(int index, double[] data)
      Replace one row of values of this matrix.
      void setValue​(double v)
      Set a value on each position of the matrix.
      Column solve​(Column rhs)
      Return the reduced echelon form of this matrix using the argument as a right hand side colum.
      void subtract​(Matrix a, Matrix b)
      Subtract two matrices.
      void subtractAndCopy​(Matrix b, Matrix resultSubtract, Matrix resultCopy)
      Subtract the value of this with the value of b, and let the result on resultSubstract Also, copy the original value of this into resultCopy.
      void swap​(int i1, int i2)
      Swaps two rows.
      java.lang.String toString()
      Return the string representation of this matrix.
      java.lang.String toString​(int precision)  
      Matrix transpose()
      Return the transpose of this matrix.
      void transpose​(Matrix a)
      Transpose this matrix and left the result on a.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • matrix

        protected final double[] matrix
        Matrix's data, stored for row in a sequential array.
      • rows

        protected final int rows
        Number of rows and columns of the matrix.
      • columns

        protected final int columns
        Number of rows and columns of the matrix.
    • Constructor Detail

      • Matrix

        public Matrix​(int r,
                      int c,
                      boolean identity)
        Constructor. Allocated the matrix and could initialize with the identity. By default, the matrix is created filled with zeroes.
        Parameters:
        r - number of rows
        c - number of columns
        identity - if you need initialized with an identity.
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero.
      • Matrix

        public Matrix​(int r,
                      int c)
        Constructor alias for Matrix(r,c,false).
        Parameters:
        r - number of rows
        c - number of columns
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero.
      • Matrix

        public Matrix​(int r,
                      int c,
                      double[] data)
        Constructor.

        Creates a matrix and initialize with the data on data.

        The values are read as row based: data -> row1, row2, ..., rown

        Parameters:
        r - number of rows
        c - number of columns
        data - values to initialize the matrix (length must be r * c).
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero or if data is null or has the wrong dimension.
      • Matrix

        public Matrix​(Matrix copy)
        Copy constructor. Creates a copy of a matrix using another matrix. Constructor alias for Matrix(copy.rows, copy.columns, copy.matrix).
        Parameters:
        copy - Source of the copy
    • Method Detail

      • getDefaultRandom

        public static java.util.Random getDefaultRandom()
      • random

        public static Matrix random​(int r,
                                    int c,
                                    boolean signed)
        Create a new Matrix filled with low random numbers.
        Parameters:
        r - number of rows
        c - number of columns
        signed - true if the matrix should be filled with positive and negative numbers false if the numbers should be greater or equal than zero.
        Returns:
        a new matrix filled with low random numbers.
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero.
        See Also:
        fill(boolean, java.util.Random)
      • random

        public static Matrix random​(int r,
                                    int c,
                                    boolean signed,
                                    java.util.Random rand)
        Create a new Matrix filled with low random numbers.
        Parameters:
        r - number of rows
        c - number of columns
        signed - true if the matrix should be filled with positive and negative numbers false if the numbers should be greater or equal than zero.
        rand - The Random object used to fill the matrix, if null it will fallback to ThreadLocalRandom.current()
        Returns:
        a new matrix filled with low random numbers.
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero.
        See Also:
        fill(boolean, java.util.Random)
      • random

        public static Matrix random​(int r,
                                    int c)
        Create a new Matrix filled with low random numbers.
        Parameters:
        r - number of rows
        c - number of columns
        Returns:
        a new matrix filled with low random numbers.
        Throws:
        java.lang.IllegalArgumentException - if either r or c are less or equal than zero.
        See Also:
        random(int, int, boolean), random(int, int, boolean, java.util.Random)
      • add

        public void add​(Matrix a,
                        Matrix b)
        Adds two matrices. b = this + a

        The matrix b must be created and has the same dimension of this and a.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The matrix to add
        b - The matrix to put the result
      • subtract

        public void subtract​(Matrix a,
                             Matrix b)
        Subtract two matrices. b = this - a.

        The matrix b must be created and has the same dimension of this and a.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The matrix to subtract
        b - The matrix to put the result
      • multiply

        public void multiply​(double a,
                             Matrix b)
        Multiply this matrix by an scalar. b = this * a.

        The matrix b must be created and has the same dimension of this.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The scalar to multiply
        b - The matrix to put the result
      • multiply

        public void multiply​(Matrix a,
                             Matrix b)
        Multiply two matrix. b = this * a

        The matrix a must be created and has the right dimensions, that is a.rows = this.columns.

        The matrix b must be created and has the right dimensions, that is b.rows = this.rows and b.columns = a.columns.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The matrix to multiply
        b - The matrix to put the result
      • apply

        public void apply​(Function f,
                          Matrix a)
        Apply one function over the elements of the matrix and left the result on a. For each element on this a(i,j) = F(this(i,j)).

        The matrix a must have the same dimension as this.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        f - function to apply.
        a - The matrix to put the result.
      • applyInIdentity

        public void applyInIdentity​(Function f,
                                    Matrix a)
        Apply one function over the elements of the matrix and left on the main diagonal of a identity matrix 'a'. For each element on this a(i,i) = F(this(i,j)). The matrix this must be a column matrix. The matrix a must be created and has the same dimension of this and a. NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.
        Parameters:
        f - function to apply.
        a - The matrix to put the result.
      • fill

        public void fill​(boolean signed)
        Fill the matrix with random values between [0, 1) if signed is is false, and (-1, 1) if true.
        Parameters:
        signed - false if all the numbers should be positive, false otherwise
        See Also:
        fill(boolean, java.util.Random)
      • fill

        public void fill​(boolean signed,
                         java.util.Random r)
        Fill the matrix with random values between [0, 1) if signed is is false, and (-1, 1) if true.

        This method is based only in Random.nextDouble(), so in case other intervals are needed the only thing that's needed is a custom implementation of nextDouble(), for instance:

             Random myRand = new Random(){
                 public double nextDouble() {
                     return super.nextDouble() / 1000.;
                 }
             }
         
        Parameters:
        signed - false if all the numbers should be positive, false otherwise
        r - The Random object used to fill the matrix, if null it will fallback to ThreadLocalRandom.current()
      • copy

        public void copy​(Matrix a)
        Copy this matrix to another matrix a.

        The matrix a must have the same dimension as this.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The matrix to put the result.
      • dotProduct

        public double dotProduct​(Matrix a)
        Calculate the dot product between this matrix and another. This method can safely calculate the dot product between row-column, row-row, column-column matrices.
        Parameters:
        a - The matrix to multiply
        Returns:
        the scalar of the dot product.
        Throws:
        java.lang.IllegalArgumentException - if either this or a are neither row nor column matrices.
      • transpose

        public void transpose​(Matrix a)
        Transpose this matrix and left the result on a.

        The matrix a must be created and has the right dimensions.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - The matrix to put the result.
      • transpose

        public Matrix transpose()
        Return the transpose of this matrix.
        Returns:
        A new matrix with the transpose of this.
      • setRow

        public void setRow​(int index,
                           double[] data)
        Replace one row of values of this matrix.

        The number of values of data must match with the number of columns of this.

        NOTE: Assertions of the dimensions and indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        index - The index of the row to place the values.
        data - The values to put in that row.
      • setValue

        public void setValue​(double v)
        Set a value on each position of the matrix.
        Parameters:
        v - The value to put in the matrix.
      • getRow

        public double[] getRow​(int index)
        Return an array with the values of the specified row.

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        index - The index of the row to return.
        Returns:
        the array with the values.
      • getCol

        public double[] getCol​(int index)
        Return an array with the values of the specified column.

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        index - The index of the column to return.
        Returns:
        the array with the values.
      • position

        public final double position​(int i,
                                     int j)
        Return the value on the position (i,j).

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        i - index of the row
        j - index of the column
        Returns:
        the value of that position
      • position

        public final void position​(int i,
                                   int j,
                                   double v)
        Set the value v on the position (i,j).

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        i - index of the row
        j - index of the column
        v - the value to put into.
      • subtractAndCopy

        public void subtractAndCopy​(Matrix b,
                                    Matrix resultSubtract,
                                    Matrix resultCopy)
        Subtract the value of this with the value of b, and let the result on resultSubstract Also, copy the original value of this into resultCopy.
        Parameters:
        b - Matrix to subtract
        resultSubtract - Matrix to hold the result of the subtraction
        resultCopy - Matrix to hold the copy of this.
      • multiplyAndAdd

        public void multiplyAndAdd​(double a,
                                   Matrix b,
                                   Matrix result)
        Return in result the value of (this*a + b) for each This(i,j)
        Parameters:
        a - constant to multiply
        b - matrix to add
        result - Matrix to hold the result of the operation.
      • increment

        public final void increment​(int i,
                                    int j,
                                    double v)
        Increments the value of one position by v.

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        i - index of the row
        j - index of the column
        v - value to increment.
      • scale

        public final void scale​(int i,
                                int j,
                                double v)
        Scales the value of one position by v.

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        i - index of the row
        j - index of the column
        v - value to scale.
      • swap

        public void swap​(int i1,
                         int i2)
        Swaps two rows.

        NOTE: Assertions of the indexes are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        i1 - index of the first row
        i2 - index of the second row
      • solve

        public Column solve​(Column rhs)
        Return the reduced echelon form of this matrix using the argument as a right hand side colum.
        Parameters:
        rhs - Column to use as right hand side
        Returns:
        rref([this rhs])(:,end)
        Since:
        1.2.0-RIDDLER
      • equals

        public boolean equals​(java.lang.Object b1)
        Check if two matrix are equals position to position with a precision of 1e-7. If the dimensions mismatch they aren't equals. If one position differs by more than 1e-7 then are different.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        b1 - The matrix to compare
        Returns:
        true if are equals, false otherwise.
      • hashCode

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

        public java.lang.String toString()
        Return the string representation of this matrix. Useful to write on a file or for debugging.
        Overrides:
        toString in class java.lang.Object
        Returns:
        An string with the values of the matrix.
      • toString

        public java.lang.String toString​(int precision)
      • getRows

        public int getRows()
        Returns:
        The number of rows
      • getColumns

        public int getColumns()
        Returns:
        The number of columns