Class Matrix

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Matrix>

    public class Matrix
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Comparable<Matrix>
    A class representing a Matrix
    Author:
    Bhagat
    See Also:
    Serialized Form
    • Field Detail

      • data

        private double[][] data
        a 2D double array storing to internal data of the Matrix
      • rows

        private int rows
        the number of rows in the matrix;
      • columns

        private int columns
        the number of columns in the matrix;
    • Constructor Detail

      • Matrix

        public Matrix​(int rows,
                      int columns)
        Create a matrix with a specified number of rows and columns
        Parameters:
        rows - the rows
        columns - the columns
      • Matrix

        public Matrix​(double[][] data)
        Create a matrix with specified data
        Parameters:
        data - the data in the form of a two dimensional double array
    • Method Detail

      • getVectorRows

        public Vector[] getVectorRows()
        Returns:
        a list of vectors representing the rows of the matrix
      • getVectorColumns

        public Vector[] getVectorColumns()
        Returns:
        a list of vectors representing the columns of the matrix
      • setColumns

        public Matrix setColumns​(Vector[] vs)
        sets all the columns from an array of vectors
        Parameters:
        vs - an array of the vectors to set into the Matrix
        Returns:
        a reference to this matrix
      • setColumns

        public Matrix setColumns​(Matrix[] ms)
        sets all the columns from an array of matrices
        Parameters:
        ms - an array of the matrices to set into the Matrix
        Returns:
        a reference to this matrix
      • setRows

        public Matrix setRows​(Vector[] vs)
        sets all the rows from an array of vectors
        Parameters:
        vs - an array of the vectors to set into the Matrix
        Returns:
        a reference to this matrix
      • setRows

        public Matrix setRows​(Matrix[] ms)
        sets all the rows from an array of matrices
        Parameters:
        ms - an array of the matrices to set into the Matrix
        Returns:
        a reference to this matrix
      • map

        public Matrix map​(Function<java.lang.Double,​java.lang.Double> function)
        maps a function onto each element in the matrix
        Parameters:
        function - the function to map
        Returns:
        a reference of this matrix after the mapping
      • mapWithIndex

        public Matrix mapWithIndex​(Function<Matrix.MatrixIndex,​java.lang.Double> function)
        maps a function onto each element in the matrix
        Parameters:
        function - the function to map that receives MatrixIndex object
        Returns:
        a reference of this matrix after the mapping
      • clone

        public Matrix clone()
        Clones this matrix with a new memory allocation
        Overrides:
        clone in class java.lang.Object
        Returns:
        the cloned matrix
      • randomize

        public void randomize()
        Fills the Matrix with random values from -1 to 1
      • randomize

        public void randomize​(double min,
                              double max)
        Fills the Matrix with random values from min to max
        Parameters:
        min - the minimum random number
        max - the maximum random number
      • transpose

        public Matrix transpose()
        Transposes the matrix
        Returns:
        the transposed matrix
      • inverse

        public Matrix inverse()
        Finds the inverse of the matrix
        Returns:
        the inverse
      • cofactor

        public Matrix cofactor()
        Finds the cofactor of the matrix
        Returns:
        the cofactor
      • multiply

        public Matrix multiply​(Matrix m)
        performs matrix multiplication
        Parameters:
        m - the matrix to multiply with
        Returns:
        the resultant matrix
      • multiply

        public Matrix multiply​(Vector v)
        performs matrix multiplication
        Parameters:
        v - the vector to multiply with
        Returns:
        the resultant matrix
      • eigenproblem

        public Matrix.EigenSolution eigenproblem​(int iterations)
        Solves the eigen problem
        Parameters:
        iterations - the number of iterations for the QR algorithm
        Returns:
        an object containing both the eigenvalues and eigenvectors
      • eigenvaluestest

        public double[] eigenvaluestest​(int iterations)
      • eigenvalues

        public double[] eigenvalues​(int iterations)
        computes the eigenvalues of the matrix

        Note: this method may return extra eigenvalues that do not have a valid eigenvector. Use eigenproblem() for a better solution

        Parameters:
        iterations - the number of iterations for the QR algorithm
        Returns:
        the eigenvalues
      • eigenvectors

        public Vector[] eigenvectors​(double[] eigenvalues)
        calculates the eigenvectors based on the eigenvalues provided
        Parameters:
        eigenvalues - the eigenvalues
        Returns:
        the eigenvectors
      • eigenvector

        private Vector eigenvector​(double eigenvalue)
      • singularValues

        public double[] singularValues​(int iterations)
        Find the singular values of a matrix
        Parameters:
        iterations - the number of iterations for the QR algorithm
        Returns:
        the singular values of the matrix
      • singularSolution

        public Matrix.SingularSolution singularSolution​(int iterations)
        Finds both the singular values and corresponding eigenvectors
        Parameters:
        iterations - the number of iterations for the QR algorithm
        Returns:
        an object containing the singular values and eigenvectors
      • compareTo

        public int compareTo​(Matrix m)
        compares two matrices by determinant
        Specified by:
        compareTo in interface java.lang.Comparable<Matrix>
        Parameters:
        m - the matrix to compare to
        Returns:
        the integer representing which determinant is greater
      • equals

        public boolean equals​(Matrix m)
        checks if two matrices have the same data
        Parameters:
        m - the matrix to compare with
        Returns:
        whether or not they are the same
      • hadamardProduct

        public Matrix hadamardProduct​(Matrix m)
        performs the hadamard product which is element wise multiplication
        Parameters:
        m - the matrix to multiply
        Returns:
        a reference to this matrix
      • multiply

        public Matrix multiply​(double d)
        performs the scalar multiplication on the matrix
        Parameters:
        d - the scalar to multiply with
        Returns:
        a reference to this matrix
      • divide

        public Matrix divide​(double d)
        performs the scalar division on the matrix
        Parameters:
        d - the scalar to divide with
        Returns:
        a reference to this matrix
      • rowEchelonForm

        public Matrix rowEchelonForm()
        converts the matrix to row echelon form
        Returns:
        the matrix in row echelon form
      • reducedRowEchelonForm

        public Matrix reducedRowEchelonForm()
        converts the matrix to reduced row echelon form
        Returns:
        the matrix in reduced row echelon form
      • add

        public Matrix add​(Matrix m)
        performs element wise addition
        Parameters:
        m - the matrix to add with
        Returns:
        a reference to this matrix
      • subtract

        public Matrix subtract​(Matrix m)
        performs element wise subtraction
        Parameters:
        m - the matrix to subtract with
        Returns:
        a reference to this matrix
      • divide

        public Matrix divide​(Matrix m)
        performs element wise division
        Parameters:
        m - the matrix to divide with
        Returns:
        a reference to this matrix
      • toVector

        public Vector toVector()
        converts this matrix into a vector
        Returns:
        the vector
      • isSquare

        public boolean isSquare()
        Returns:
        true if it is a square matrix
      • getData

        public double[][] getData()
        Returns:
        the data
      • setData

        public void setData​(double[][] data)
        Parameters:
        data - the data
      • getRows

        public int getRows()
        Returns:
        the rows
      • getColumns

        public int getColumns()
        Returns:
        the columns
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the Matrix
      • inner

        public static Matrix inner​(Matrix a,
                                   Matrix b)
        performs the inner product on two matrices
        Parameters:
        a - a matrix
        b - another matrix
        Returns:
        the matrix result
      • outer

        public static Matrix outer​(Matrix a,
                                   Matrix b)
        performs the outer product on two matrices
        Parameters:
        a - a matrix
        b - another matrix
        Returns:
        the matrix result
      • transpose

        public static Matrix transpose​(Matrix m)
        transposes the rows and columns of a matrix to the columns and rows
        Parameters:
        m - the matrix to transpose
        Returns:
        the transposed matrix
      • singularValueDecomposition

        public static Matrix[] singularValueDecomposition​(Matrix A,
                                                          int iterations)
        Performs singular value decomposition on a matrix
        Parameters:
        A - the matrix to find the SVD decomposition of
        iterations - the number of iterations for the QR algorithm
        Returns:
        an array of matrices that contain {U, Sigma, V}
      • singularValueDecompositionOuter

        public static Matrix.OuterProductSVD singularValueDecompositionOuter​(Matrix A,
                                                                             int iterations)
        performs singular value decomposition and returns the output in outer product form
        Parameters:
        A - the matrix to factor
        iterations - the number of iterations for the QR algorithm
        Returns:
        an object holding the solutions for the factorization
      • QR

        public static Matrix[] QR​(Matrix A)
        Computes the QR factorization of a matrix
        Parameters:
        A - the matrix
        Returns:
        an array of matrices where the first matrix is Q and the second one is R
      • cofactor

        public static Matrix cofactor​(Matrix A)
        finds the cofactor of a matrix
        Parameters:
        A - the matrix
        Returns:
        the cofactor matrix
      • inverse

        public static Matrix inverse​(Matrix A)
        computes the inverse matrix of the input matrix
        Parameters:
        A - the input matrix
        Returns:
        the inverse of the input matrix
      • identityMatrix

        public static Matrix identityMatrix​(int size)
        generates an identity matrix with a specified size
        Parameters:
        size - the size is the number of rows and columns
        Returns:
        the generated matrix