Class MatrixMath

java.lang.Object
com.j3d.engine.math.matrix.MatrixMath

public abstract class MatrixMath extends Object
Provides a collection of static utility methods for performing mathematical operations on matrices that implement the MatrixInterface. This class includes methods for matrix creation, validation, addition, subtraction, and multiplication.

This is an abstract utility class and cannot be instantiated.

Author:
Lehlogonolo Poole
See Also:
  • Constructor Details

    • MatrixMath

      public MatrixMath()
  • Method Details

    • validate

      protected static void validate(double[][] m)
      Validates the structure of a 2D double array to ensure it can represent a valid matrix. This method checks for three conditions:
      1. The input is a 2D array.
      2. All rows have the same length.
      3. The array does not contain any Double.NaN values.
      Parameters:
      m - The 2D double array to validate.
      Throws:
      IllegalStateException - if the array is not a valid matrix structure.
    • matrixOf

      public static MatrixInterface matrixOf(double[][] m)
      Creates a MatrixInterface instance from a 2D double array.

      This factory method serves as a bridge between raw double[][] arrays and the MatrixInterface ecosystem. It first validates the array to ensure it represents a well-formed matrix.

      Based on the dimensions of the input array, this method will return a specialized, high-performance implementation (e.g., Matrix3 for 3x3) or a generic, anonymous implementation for other sizes.

      Parameters:
      m - The 2D double array to create the matrix from. A deep copy of this array is created to ensure the new matrix is independent of the original array.
      Returns:
      A new MatrixInterface implementation containing a copy of the provided data.
      Throws:
      IllegalStateException - if the array is not a valid matrix structure (e.g., jagged rows, NaN values).
    • equalsRowsEqualCols

      public static boolean equalsRowsEqualCols(MatrixInterface m1, MatrixInterface m2)
      Checks if two matrices have the same number of rows and columns.
      Parameters:
      m1 - The first matrix.
      m2 - The second matrix.
      Returns:
      true if both matrices have identical dimensions, false otherwise.
    • add

      public static MatrixInterface add(MatrixInterface m1, MatrixInterface m2)
      Performs element-wise addition of two matrices.
      Parameters:
      m1 - The first matrix.
      m2 - The second matrix.
      Returns:
      A new MatrixInterface containing the result of the addition.
      Throws:
      MatrixException - if the matrices do not have the same dimensions.
    • sub

      public static MatrixInterface sub(MatrixInterface m1, MatrixInterface m2)
      Performs element-wise subtraction of two matrices (m1 - m2).
      Parameters:
      m1 - The first matrix (minuend).
      m2 - The second matrix (subtrahend).
      Returns:
      A new MatrixInterface containing the result of the subtraction.
      Throws:
      MatrixException - if the matrices do not have the same dimensions.
    • mult

      public static MatrixInterface mult(MatrixInterface m1, MatrixInterface m2)
      Performs matrix multiplication (m1 * m2). The number of columns in the first matrix must be equal to the number of rows in the second matrix.
      Parameters:
      m1 - The first matrix.
      m2 - The second matrix.
      Returns:
      A new MatrixInterface containing the result of the multiplication.
      Throws:
      MatrixException - if the number of columns in m1 does not equal the number of rows in m2.