Class Matrix

java.lang.Object
maxharold.matrix.Matrix

public class Matrix extends Object
Class used for creating matrices and performing matrix operations
  • Constructor Summary

    Constructors
    Constructor
    Description
    Matrix(double[] vector)
    Constructs a Matrix from an array that acts as a row vector
    Matrix(double[][] raw)
    Constructs a Matrix from a 2D array (makes the matrix null if invalid)
    Matrix(int rows, int cols)
    Constructs an empty matrix of a specified size
    Matrix(Vector... rows)
    Constructs a matrix from specified rows as vectors (makes the matrix null if invalid)
  • Method Summary

    Modifier and Type
    Method
    Description
    static Matrix
    add(Matrix m1, Matrix m2)
    Adds one matrix to another
    det()
    Method that calculate the determinant of a square matrix
    double[]
    getCol(int i)
    Returns a column of the matrix as an array
    int
    Method that returns the number of columns in the matrix
    getElement(int row, int col)
    Returns the element in a certain row and column of the matrix
    double[]
    getRow(int i)
    Returns a row of the matrix as an array
    int
    Method that returns the number of rows in the matrix
    Performs hadamard product of two matrices with the current instance of the matrix as the first one
    static Matrix
    Performs hadamard product of two matrices
    Subtracts one matrix from another with the current matrix instance as the first matrix
    mul(Matrix m2)
    Performs matrix multiplication with the current matrix instance as the first matrix
    static Matrix
    mul(Matrix m1, Matrix m2)
    Performs matrix multiplication on two matrices
    Adds one matrix to another with the current matrix instance as the first matrix
    void
    setElement(double val, int i, int j)
    Sets the value of one of the elements of the matrix (does nothing if coordinates are invalid)
    static Matrix
    Subtracts one matrix from another
    T()
    Transposes the current matrix
    Converts the matrix to a string format
    Puts current matrix through matrix multiplication with a column vector

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Matrix

      public Matrix(double[][] raw)
      Constructs a Matrix from a 2D array (makes the matrix null if invalid)
      Parameters:
      raw - The 2D array that contains the matrix
    • Matrix

      public Matrix(double[] vector)
      Constructs a Matrix from an array that acts as a row vector
      Parameters:
      vector - The array that represents the row vector
    • Matrix

      public Matrix(int rows, int cols)
      Constructs an empty matrix of a specified size
      Parameters:
      rows - The number of rows of the matrix
      cols - The number of columns of the matrix
    • Matrix

      public Matrix(Vector... rows)
      Constructs a matrix from specified rows as vectors (makes the matrix null if invalid)
      Parameters:
      rows - the vectors that make up the rows
  • Method Details

    • getRows

      public int getRows()
      Method that returns the number of rows in the matrix
      Returns:
      the number of rows
    • getCols

      public int getCols()
      Method that returns the number of columns in the matrix
      Returns:
      the number of columns in the matrix
    • getRow

      public double[] getRow(int i)
      Returns a row of the matrix as an array
      Parameters:
      i - The i-th row of the matrix
      Returns:
      The i-th row of the matrix as an array (returns null if invalid)
    • getCol

      public double[] getCol(int i)
      Returns a column of the matrix as an array
      Parameters:
      i - i-th column of the matrix
      Returns:
      The i-th column of the matrix as an array (returns null if invalid)
    • getElement

      public Double getElement(int row, int col)
      Returns the element in a certain row and column of the matrix
      Parameters:
      row - The row the desired element lies in
      col - The column the desired element lies in
      Returns:
      The desired element of the matrix (returns null if invalid)
    • setElement

      public void setElement(double val, int i, int j)
      Sets the value of one of the elements of the matrix (does nothing if coordinates are invalid)
      Parameters:
      val - The replacing value
      i - The i-th row the value lies in
      j - The i-th column the value lies in
    • transformVector

      public Vector transformVector(Vector v)
      Puts current matrix through matrix multiplication with a column vector
      Parameters:
      v - the vector
      Returns:
      the transformed vector (returns null if invalid)
    • mul

      public static Matrix mul(Matrix m1, Matrix m2)
      Performs matrix multiplication on two matrices
      Parameters:
      m1 - The first matrix
      m2 - The second matrix
      Returns:
      m1 * m2 (returns null if invalid)
    • mul

      public Matrix mul(Matrix m2)
      Performs matrix multiplication with the current matrix instance as the first matrix
      Parameters:
      m2 - The second matrix
      Returns:
      m1 * m2 (returns null if invalid)
    • add

      public static Matrix add(Matrix m1, Matrix m2)
      Adds one matrix to another
      Parameters:
      m1 - the first matrix
      m2 - the second matrix
      Returns:
      m1 + m2 (returns null if invalid)
    • plus

      public Matrix plus(Matrix m2)
      Adds one matrix to another with the current matrix instance as the first matrix
      Parameters:
      m2 - the second matrix
      Returns:
      current_matrix + m2 (returns null if invalid)
    • subtract

      public static Matrix subtract(Matrix m1, Matrix m2)
      Subtracts one matrix from another
      Parameters:
      m1 - the first matrix
      m2 - the second matrix
      Returns:
      m1 - m2 (returns null if invalid)
    • minus

      public Matrix minus(Matrix m2)
      Subtracts one matrix from another with the current matrix instance as the first matrix
      Parameters:
      m2 - the second matrix
      Returns:
      current_matrix - m2 (returns null if invalid)
    • hadamardProduct

      public static Matrix hadamardProduct(Matrix m1, Matrix m2)
      Performs hadamard product of two matrices
      Parameters:
      m1 - the first matrix
      m2 - the second matrix
      Returns:
      m1 ⊙ m2 (returns null if invalid)
    • hadamardProduct

      public Matrix hadamardProduct(Matrix m2)
      Performs hadamard product of two matrices with the current instance of the matrix as the first one
      Parameters:
      m2 - the second matrix
      Returns:
      current_matrix ⊙ m2 (returns null if invalid)
    • det

      public Double det()
      Method that calculate the determinant of a square matrix
      Returns:
      the determinant (returns null if matrix is not square)
    • T

      public Matrix T()
      Transposes the current matrix
      Returns:
      the transposed matrix
    • toString

      public String toString()
      Converts the matrix to a string format
      Overrides:
      toString in class Object
      Returns:
      The matrix formatted as a string