Class Matrix

java.lang.Object
me.autobot.lib.math.linalg.Matrix

public class Matrix extends Object
A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Matrix(double[][] matrix)
    Creates a new matrix with the given 2D array
    Matrix(int rows, int cols)
    Creates a new matrix with the given number of rows and columns
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Matrix other)
    Adds the matrix to another matrix
    static Matrix
    Creates a new identity matrix with the given size
    double
    Gets the determinant of the matrix
    int
    Gets the dimension of the matrix
    double
    get(int row, int col)
    Gets the value at the given row and column
    int
    Gets the number of columns in the matrix
    double[][]
    Gets the matrix as a 2D array
    int
    Gets the number of rows in the matrix
    Inverts the matrix
    Multiplies the matrix by another matrix
    Multiplies the matrix by a vector
    scale(double scalar)
    Scales the matrix by a scalar
    void
    set(int row, int col, double value)
    Sets the value at the given row and column
    submatrix(int row, int col)
    Gets the submatrix of the matrix
    Subtracts the matrix from another matrix
    void
    swapRows(int row1, int row2)
    Swaps two rows in this matrix
    Gets the string representation of the matrix
    toString(int decimals)
    Gets the string representation of the matrix
    Transposes the matrix

    Methods inherited from class java.lang.Object

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

    • Matrix

      public Matrix(double[][] matrix)
      Creates a new matrix with the given 2D array
      Parameters:
      matrix - The 2D array to create the matrix with
    • Matrix

      public Matrix(int rows, int cols)
      Creates a new matrix with the given number of rows and columns
      Parameters:
      rows - The number of rows in the matrix
      cols - The number of columns in the matrix
  • Method Details

    • createIdentityMatrix

      public static Matrix createIdentityMatrix(int size)
      Creates a new identity matrix with the given size
      Parameters:
      size - The size of the identity matrix
      Returns:
      The identity matrix
    • getMatrix

      public double[][] getMatrix()
      Gets the matrix as a 2D array
      Returns:
      The matrix as a 2D array
    • getRows

      public int getRows()
      Gets the number of rows in the matrix
      Returns:
      The number of rows in the matrix
    • getCols

      public int getCols()
      Gets the number of columns in the matrix
      Returns:
      The number of columns in the matrix
    • get

      public double get(int row, int col)
      Gets the value at the given row and column
      Parameters:
      row - The row to get the value from
      col - The column to get the value from
      Returns:
      The value at the given row and column
    • set

      public void set(int row, int col, double value)
      Sets the value at the given row and column
      Parameters:
      row - The row to set the value at
      col - The column to set the value at
      value - The value to set
    • swapRows

      public void swapRows(int row1, int row2)
      Swaps two rows in this matrix
      Parameters:
      row1 - The first row to swap The row must be within the bounds of the matrix
      row2 - The second row to swap The row must be within the bounds of the matrix
    • add

      public Matrix add(Matrix other)
      Adds the matrix to another matrix
      Parameters:
      other - The matrix to add The matrix must have the same dimensions as this matrix
      Returns:
      The sum of the two matrices
    • subtract

      public Matrix subtract(Matrix other)
      Subtracts the matrix from another matrix
      Parameters:
      other - The matrix to subtract
      Returns:
      The difference of the two matrices
    • multiply

      public Matrix multiply(Matrix other)
      Multiplies the matrix by another matrix
      Parameters:
      other - The matrix to multiply by
      Returns:
      The product of the two matrices
    • multiply

      public Matrix multiply(Vector v)
      Multiplies the matrix by a vector
      Parameters:
      v - The vector to multiply by
      Returns:
      The product of the matrix and the vector
    • scale

      public Matrix scale(double scalar)
      Scales the matrix by a scalar
      Parameters:
      scalar - The scalar to scale the matrix by
      Returns:
      The scaled matrix
    • transpose

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

      public Matrix submatrix(int row, int col)
      Gets the submatrix of the matrix
      Parameters:
      row - The row to remove
      col - The column to remove
      Returns:
      The submatrix
    • determinant

      public double determinant()
      Gets the determinant of the matrix
      Returns:
      The determinant of the matrix
    • inverse

      public Matrix inverse()
      Inverts the matrix
      Returns:
      The inverted matrix
    • dimension

      public int dimension()
      Gets the dimension of the matrix
      Returns:
      The dimension of the matrix
    • toString

      public String toString()
      Gets the string representation of the matrix
      Overrides:
      toString in class Object
      Returns:
      The string representation of the matrix
    • toString

      public String toString(int decimals)
      Gets the string representation of the matrix
      Parameters:
      decimals - The number of decimals to round to
      Returns:
      The string representation of the matrix