Class Matrix

java.lang.Object
math.matrix.expressParser.Matrix

public class Matrix extends Object
  • Field Details

    • lambda

      public static final String lambda
      See Also:
    • name

      private String name
      The simple name used to label this Matrix object.
    • array

      private double[][] array
      the data array used to create this Matrix object
    • det

      private static double det
      attribute used to cofactorDet the detMultiplier of the Matrix object.
  • Constructor Details

    • Matrix

      public Matrix(int rows, int cols)
      Parameters:
      rows - The number of row in the Matrix.
      cols - The number of columns in the Matrix.
    • Matrix

      public Matrix(String name)
      Parameters:
      name - the simple name used to identify used by the user to label this Matrix object. Assigns name unknown to the Matrix object and a 2D array that has just a row and a column
    • Matrix

      public Matrix(double[][] array)
      Parameters:
      array - the data array used to create this Matrix object
    • Matrix

      public Matrix(Matrix matrix)
      Parameters:
      matrix - constructs a new Matrix object having similar properties to the one passed as argument, but holding no reference to its data.
  • Method Details

    • getRows

      public int getRows()
      Returns:
      the number of row in this matrix object
    • getCols

      public int getCols()
      Returns:
      the number of columns in this matrix object
    • setArray

      public void setArray(double[][] array)
      Parameters:
      array - sets the data of this matrix
    • getArray

      public double[][] getArray()
      Returns:
      the data of this matrix
    • getElem

      public double getElem(int row, int col)
    • setDet

      private static void setDet(double det)
      Parameters:
      det - the detMultiplier attribute of objects of this class
    • getDet

      private static double getDet()
      Returns:
      the detMultiplier
    • setName

      public void setName(String name)
      Parameters:
      name - set's the simple name used to identify used by the user to label this Matrix object.
    • getName

      public String getName()
      Returns:
      the simple name used to identify used by the user to label this Matrix object.
    • getRow

      private double[] getRow(int row)
      Parameters:
      row - The row whose contents we desire.
      Returns:
      the contents of the row.
    • swapRow

      public void swapRow(int row1, int row2)
      Parameters:
      row1 - The first row.
      row2 - The second row. BE CAREFUL!!!!
      THIS METHOD ACTS ON THE MATRIX OBJECT ON WHICH IT IS CALLED AND MODIFIES IT!
    • swapColumn

      public void swapColumn(int col1, int col2)
      Parameters:
      col1 - The first row.
      col2 - The second row. BE CAREFUL!!!!
      THIS METHOD ACTS ON THE MATRIX OBJECT ON WHICH IT IS CALLED AND MODIFIES IT!
    • fill

      public void fill()
      Fills this matrix object with values
    • add

      public Matrix add(Matrix matrice)
      Parameters:
      matrice - the matrix to be added to this one. The operation is ( this + matrice )
      Returns:
      a Matrix containing the product matrix.
    • subtract

      public Matrix subtract(Matrix matrice)
      Parameters:
      matrice - the matrix to be subtracted from this one. The operation is ( this - matrice )
      Returns:
      a Matrix containing the product matrix.
    • scalarMultiply

      public Matrix scalarMultiply(double scalar)
      Parameters:
      scalar - the constant to be multiplied with this matrix The operation is ( scalar X matrice )
      Returns:
      an array containing the product matrix.
    • scalarDivide

      public Matrix scalarDivide(double scalar)
      Parameters:
      scalar - the constant to be multiplied with this matrix The operation is ( matrice/scalar )
      Returns:
      an array containing the scaled matrix.
    • multiply

      public static Matrix multiply(Matrix matrice1, Matrix matrice2)
      The operation of matrix multiplication. For this method to run, The pre-multiplying matrix must have its number of columns equal to the number of row in the pre-multiplying one. The product matrix is one that has its number of columns equal to the number of columns in the pre-multiplying matrix, and its row equal to that in the post-multiplying matrix. So if the operation is A X B = C, and A is an m X n matrix while B is an r X p matrix, then r = n is a necessary condition for the operation to occur. Also, C is an m X p matrix.
      Parameters:
      matrice1 - the matrix to be pre-multiplying the other one. The operation is ( matrice1 X matrice2 )
      matrice2 - the post-multiplying matrix
      Returns:
      a new Matrix object containing the product matrix of the operation matrice1 X matrice2
    • multiply

      public void multiply(Matrix matrice)
      Parameters:
      matrice - the matrix to be multiplied by this one. This operation modifies this matrix and changes its data array into that of the product matrix The operation is ( this X matrice )
    • pow

      public static Matrix pow(Matrix mat, int pow)
      Parameters:
      mat - the matrix to raise to a given power
      pow - the power to raise this matrix to
      Returns:
      the
    • power

      public static Matrix power(Matrix mat, int pow)
    • unitMatrix

      public Matrix unitMatrix()
      Returns:
      a unit matrix of the same dimension as this matrix object
    • unitMatrix

      public static Matrix unitMatrix(int rowSize, int colSize)
      Parameters:
      rowSize - the number of row that the unit matrix will have
      colSize - the number of columns that the unit matrix will have
      Returns:
      a unit matrix having number of row = rowSize and number of columns=colSize.
    • unitMatrix

      public static Matrix unitMatrix(Matrix mat)
      Parameters:
      mat - the Matrix object that we wish to construct a unit matrix of similar dimensions for.
      Returns:
      a unit matrix of equal dimensions as this unit matrix.
    • columnJoin

      public static Matrix columnJoin(Matrix mat1, Matrix mat2)
      Place the first Matrix object side by side with the second one passed as argument to this method. The result is a new matrix where: if 3 4 5 7 5 9 mat1 = 2 3 1 and mat2 = 4 2 6 1 6 7 5 7 3 in a new matrix object (mat). e.g 3 4 5 7 5 9 2 3 1 4 2 6 1 6 7 5 7 3 A necessary condition for this method to run is that the 2 objects must have an equal number of row. IF THIS CONDITION IS NOT MET, THE METHOD RETURNS A ZERO MATRIX.
      Parameters:
      mat1 - the first Matrix object
      mat2 - the second Matrix object that we column join with this one
      Returns:
      a new Matrix object that contains this Matrix object placed side by side with the Matrix object passed as argument.
    • update

      public boolean update(double value, int row, int column)
      Parameters:
      value - The value to insert
      row - The row where the value is to be inserted.
      column - The column where the value is to be inserted.
    • rowJoin

      public static Matrix rowJoin(Matrix mat1, Matrix mat2)
      Place the first Matrix object side by side with the second one passed as argument to this method. The result is a new matrix where: if 3 4 5 7 5 9 mat1 = 2 3 1 and mat2 = 4 2 6 1 6 7 5 7 3 in a new matrix object (mat). e.g 3 4 5 2 3 1 1 6 7 7 5 9 4 2 6 5 7 3 A necessary condition for this method to run is that the 2 objects must have an equal number of columns. IF THIS CONDITION IS NOT MET, THE METHOD RETURNS A ZERO MATRIX.
      Parameters:
      mat1 - the first Matrix object
      mat2 - the second Matrix object that we row join with this one
      Returns:
      a new Matrix object that contains the first Matrix object argument placed top to bottom with the second Matrix object argument.
    • columnDeleteFromEnd

      public void columnDeleteFromEnd(int column)
      Deletes all the specified number of columns from the Matrix object starting from the end of the Matrix object
      Parameters:
      column - the number of columns to remove from the Matrix object. This method will take the object that calls it and perform this operation on it. So it modifies the Matrix object that calls it. Be careful, as data will be lost. e.g if 3 4 5 6 7 8 9 2 1 8 1 4 7 0 A = 3 3 2 1 5 7 1 then the call: A.columnDeleteFromEnd(3) will delete the last three columns in this object leaving: 3 4 5 6 A = 2 1 8 1 3 3 2 1
    • columnDeleteFromStart

      public void columnDeleteFromStart(int column)
      Deletes all the specified number of columns from the Matrix object from the beginning of the Matrix object
      Parameters:
      column - the number of columns to remove from the Matrix object's beginning. This method will take the object that calls it and perform this operation on it. So it modifies the Matrix object that calls it. Be careful, as data will be lost. e.g if 3 4 5 6 7 8 9 2 1 8 1 4 7 0 A = 3 3 2 1 5 7 1 then the call: A.columnDeleteFromStart(3) will delete the last three columns in this object leaving: 6 7 8 9 A= 1 4 7 0 1 5 7 1
    • rowDeleteFromEnd

      public void rowDeleteFromEnd(int numOfRows)
      Deletes the specified number of row from the end of the Matrix object
      Parameters:
      numOfRows - the number of row to remove from the Matrix object's beginning. This method will take the object that calls it and perform this operation on it. So it modifies the Matrix object that calls it. Be careful, as data will be lost. e.g if 3 4 5 6 2 1 8 1 A = 3 3 2 1 7 8 9 2 4 7 0 5 5 7 1 8 then the call: A.rowDeleteFromEnd(3) will delete the last three row in this object leaving: 3 4 5 6 2 1 8 1 A = 3 3 2 1
    • rowDeleteFromStart

      public void rowDeleteFromStart(int numOfRows)
      Deletes the specified number of row from the beginning of the Matrix object
      Parameters:
      numOfRows - the number of row to remove from the Matrix object's beginning. This method will take the object that calls it and perform this operation on it. So it modifies the Matrix object that calls it. Be careful, as data will be lost. e.g if 3 4 5 6 2 1 8 1 A = 3 3 2 1 7 8 9 2 4 7 0 5 5 7 1 8 then the call: A.rowDeleteFromStart(3) will delete the last three row in this object leaving: A = 7 8 9 2 4 7 0 5 5 7 1 8
    • reduceToTriangularMatrix

      public Matrix reduceToTriangularMatrix()
      Returns:
      an upper triangular matrix obtained by row reduction.
    • reduceToRowEchelonMatrix

      public Matrix reduceToRowEchelonMatrix()
      Returns:
      an upper triangular matrix obtained by row reduction.
    • solveEquation

      public Matrix solveEquation()
      Used to solve a system of simultaneous equations placed in this Matrix object.
      Returns:
      a Matrix object containing the solution matrix.
    • solveEquation

      public static Matrix solveEquation(Matrix matrix)
      Used to solve a system of simultaneous equations placed in the Matrix object.
      Parameters:
      matrix - The row X row+1 matrix, containing the system of linear equations
      Returns:
      a Matrix object containing the solution matrix.
    • transpose

      public Matrix transpose()
      Returns:
      the transpose of this Matrix object. Does not modify this matrix object but generates the transpose of this Matrix object as another Matrix object.
    • adjoint

      public Matrix adjoint()
    • getCofactorMatrix

      public Matrix getCofactorMatrix()
      Returns:
      a matrix that contains the cofactors of the elements of this Matrix.
    • minor

      public Matrix minor(int i, int j)
      Parameters:
      i - the row on which the element whose minor is needed lies.
      j - the column on which the element whose minor is needed lies.
      Returns:
      the minor of this matrix relative to this element.
    • isSquareMatrix

      public boolean isSquareMatrix()
      Returns:
      true if this Matrix object is a square matrix.
    • isSystemOfEquations

      public boolean isSystemOfEquations()
      Returns:
      true if this Matrix object can represent a system of equations solvable by reduction to triangular form and subsequent evaluation.
    • $2X2determinant

      private static double $2X2determinant(Matrix m)
      Parameters:
      m - a 2 X 2 matrix
      Returns:
      the detMultiplier of this matrix
    • $2X2determinantForEigen

      private static String $2X2determinantForEigen(String[][] m)
      Parameters:
      m - a 2 X 2 matrix
      Returns:
      the detMultiplier of this matrix
    • topRowScalarMultiply

      private static Matrix topRowScalarMultiply(Matrix m, double scalar)
      Parameters:
      m - the matrix whose top row is to be multiplied by a scalar Multiplies the top row of a matrix by a scalar. This is an important operation during the evaluation of a detMultiplier.
    • det

      private static double det(Matrix m)
      Sarus' rule for computing determinants. This technique is too slow and memory intensive for large matrices..n>=10. Please use the determ() instance method. It uses a O(cube_n) algorithm as against this method's O(n!)
      Parameters:
      m - the Matrix object whose detMultiplier is desired.
      Returns:
      the detMultiplier of the matrix
    • determinant

      public double determinant()
      Returns:
      the determinant of this matrix using a row reduction technique.
    • randomFill

      public void randomFill()
      Fills the matrix with randomly generated values between 1 and 101.
    • randomFill

      public void randomFill(int n)
      Fills the matrix with randomly generated values between 1 and n
      Parameters:
      n - the maximum possible size of the integer numbers generated.
    • isMatrixValue

      public static boolean isMatrixValue(String matrixValue)
      Parameters:
      matrixValue - A string that is to be checked if it conforms to valid syntax for representing a matrix in this software.
      Returns:
      true if the command string is a valid matrix.e.g [2,1,4:5,3,-2:4,4,5] value.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      a string representation of the matrix in row and columns.
    • printTextMatrix

      public static void printTextMatrix(String[][] mat)
      Parameters:
      mat - The string matrix
    • getRowMatrix

      public Matrix getRowMatrix(int row)
      Parameters:
      row - The row in this Matrix object to be converted into a new Matrix object. This operation generates a new Matrix object which is a row matrix.
    • getColumnMatrix

      public Matrix getColumnMatrix(int column)
      Parameters:
      column - The column to be converted into a new Matrix object. This operation generates a new Matrix object which is a column matrix.
    • oldInverse

      public Matrix oldInverse()
      Computes the inverse of this Matrix object. This technique should never be used in practise as it is a mere proof of concept. It computes the inverse by computing the roots of the equations and then reverse engineering the form A.x = B to get the inverse matrix.
      Returns:
      the inverse of the Matrix as another Matrix object.
    • inverse

      public Matrix inverse()
      Row reduction technique used to compute the inverse of the matrix. Always use this technique.
      Returns:
      the inverse of the Matrix as another Matrix object.
    • determ

      public double determ()
      Row reduction technique used to compute the determinant of this matrix. The other method using recursion is not worth it above n = 10; The memory consumed by the process and the time used to compute it is incomparable to this method's performance.
      Returns:
      the inverse of the Matrix as another Matrix object.
    • findDetEigen

      private static String findDetEigen(String[][] cofactors)
      Parameters:
      cofactors - The matrix of cofactors.
      Returns:
      the algebraic expression for the determinant of the matrix of cofactors.
    • getCharacteristicPolynomialForEigenVector

      public final String getCharacteristicPolynomialForEigenVector()
    • findCharacteristicPolynomialForEigenValues

      public static final String findCharacteristicPolynomialForEigenValues(Matrix m)
      Parameters:
      m - The Matrix object
      Returns:
      the characteristic polynomial
    • getCofactor

      private Matrix getCofactor(int rw, int cl)
      Parameters:
      rw - The row of a given element
      cl - The position of that element
      Returns:
      the cofactor sub-matrix used to calculate the cofactor element of the specified position
    • getCofactorTextArray

      private static String[][] getCofactorTextArray(String[][] matrix, int rw, int cl)
      Parameters:
      matrix - A 2d matrix array
      rw - The row of a given element
      cl - The position of that element
      Returns:
      the cofactor sub-matrix used to calculate the cofactor element of the specified position
    • generateExpressionMap

      private static HashMap<Double,Double> generateExpressionMap(String variableName, List<String> scan)
      Generates the expression map of the expression... a map whose keys are the powers of the variable of the expression and whose values are the coefficients of the variables. e.g. 3x^2-2x+1 would produce: [{2,3},{1,-2},{0,1}]
      Parameters:
      variableName - The name of the variable
      scan - The list to smoothen
      Returns:
      the expression map
    • uniVariableExpressionExpander

      private static final String uniVariableExpressionExpander(String variableName, String... exprs)
      Parameters:
      variableName - The variable
      exprs - The different expressions of the variable to be multiplied
      Returns:
      the expanded product of the expressions.
    • uniVariableExpressionExpander

      private static final String uniVariableExpressionExpander(String variableName, String expr1, String expr2)
      Parameters:
      expression - Must be a math expression of the form:(polynomial_1)(polynomial_2) e.g: (1*x^1-2)(3*x^2+5*x^1+8)
      Returns:
    • appendLogic

      private static String appendLogic(double coeff, String variableName, double exp)
    • print

      public void print()
    • main

      public static void main(String... args)
      (2-x)(3-x)(1-x)=(6-5x+x^2)(1-x)=6-11x+6x^2-x^3 {1, 2, 3, 4, 5} {6, 7, 8, 9, 0} {1, 2, 3, 4, 5} {6, 7, 8, 9, 0} {1, 2, 3, 4, 5}
      Parameters:
      args - The command line args