Class Matrix

All Implemented Interfaces:
BaseGrid<Double>, DoubleGrid, Serializable, Iterable<Double>

public class Matrix extends DoubleArrayGrid
A specialized DoubleArrayGrid used in a mathematical context. Matrices provide various mathematical operations such as matrix-scalar, matrix-vector, and matrix-matrix arithmetic.
See Also:
  • Constructor Details

    • Matrix

      public Matrix(int rows, int columns)
      Creates a new matrix.
      Parameters:
      rows - The number of rows to initialize
      columns - The number of columns to initialize
    • Matrix

      public Matrix(@Nonnull DoubleGrid g)
      Creates a new matrix.
      Parameters:
      g - The numeric grid of which to copy values from
  • Method Details

    • of

      @Nonnull public static Matrix of(@Nonnull double[][] values)
      Creates a new matrix from a 2D array of values.
      Parameters:
      values - The values of which to contain in the grid
      Returns:
      The constructed matrix
    • identity

      @Nonnull public static Matrix identity(int n)
      Returns a new n*n identity matrix.
      Parameters:
      n - The number of dimensions to create the identity matrix of
      Returns:
      A new n*n identity matrix
    • add

      @Nonnull public Matrix add(double s)
      Adds a scalar to this matrix, then returns the resulting matrix.
      Parameters:
      s - The scalar of which to add to this matrix
      Returns:
      The resulting matrix
    • subtract

      @Nonnull public Matrix subtract(double s)
      Subtracts this matrix by a scalar, then returns the resulting matrix.
      Parameters:
      s - The scalar of which to subtract from this matrix
      Returns:
      The resulting matrix
    • multiply

      @Nonnull public Matrix multiply(double s)
      Multiplies this matrix by a scalar, then returns the resulting matrix.
      Parameters:
      s - The scalar of this to multiply this matrix by
      Returns:
      The resulting matrix
    • divide

      @Nonnull public Matrix divide(double s) throws ArithmeticException
      Divides this matrix by a scalar, then returns the resulting matrix.
      Parameters:
      s - The scalar of which to divide this matrix by
      Returns:
      THe resulting matrix
      Throws:
      ArithmeticException - When the denominator s is zero
    • multiply

      @Nonnull public <V extends Vector<V>> V multiply(@Nonnull V v) throws ArithmeticException
      Multiplies a vector by this matrix, then returns the resulting vector.
      Type Parameters:
      V - The type of vector to multiply by this matrix
      Parameters:
      v - The vector of which to multiply to this matrix
      Returns:
      The resulting vector-matrix product
      Throws:
      ArithmeticException - When the number of columns is not equal to the vector's component count
    • multiply

      @Nonnull public Vector2 multiply(@Nonnull Vector2 v) throws ArithmeticException
      Multiplies a vector by this matrix, then returns the resulting vector.
      Parameters:
      v - The vector of which to multiply to this matrix
      Returns:
      The resulting vector-matrix product
      Throws:
      ArithmeticException - When the number of columns is not equal to the vector's component count
    • multiply

      @Nonnull public Vector3 multiply(@Nonnull Vector3 v) throws ArithmeticException
      Multiplies a vector by this matrix, then returns the resulting vector.
      Parameters:
      v - The vector of which to multiply to this matrix
      Returns:
      The resulting vector-matrix product
      Throws:
      ArithmeticException - When the number of columns is not equal to the vector's component count
    • multiply

      @Nonnull public Vector4 multiply(@Nonnull Vector4 v) throws ArithmeticException
      Multiplies a vector by this matrix, then returns the resulting vector.
      Parameters:
      v - The vector of which to multiply to this matrix
      Returns:
      The resulting vector-matrix product
      Throws:
      ArithmeticException - When the number of columns is not equal to the vector's component count
    • add

      @Nonnull public Matrix add(@Nonnull Matrix m) throws ArithmeticException
      Adds another matrix to this matrix, then returns the resulting matrix.
      Parameters:
      m - The matrix of which to add to this matrix
      Returns:
      The resulting matrix
      Throws:
      ArithmeticException - When the matrices' dimensions are different
    • subtract

      @Nonnull public Matrix subtract(@Nonnull Matrix m) throws ArithmeticException
      Subtracts another matrix from this matrix, then returns the resulting matrix.
      Parameters:
      m - The matrix of which to subtract from this matrix
      Returns:
      The resulting matrix
      Throws:
      ArithmeticException - When the matrices' dimensions are different
    • multiply

      @Nonnull public Matrix multiply(@Nonnull Matrix m) throws ArithmeticException
      Multiplies this matrix by another matrix, then returns the resulting matrix.
      Parameters:
      m - The matrix of which to multiply this matrix by
      Returns:
      The resulting matrix
      Throws:
      ArithmeticException - When the matrices' dimensions are incompatible for multiplication
    • det

      public double det() throws IllegalStateException
      Calculates and returns the determinant of this matrix.
      Returns:
      The determinant of this matrix
      Throws:
      IllegalStateException - When this matrix is not a square matrix (the row count and column count are different)