Class Matrix

java.lang.Object
imagingbook.common.math.Matrix

public abstract class Matrix extends Object

This class defines a set of static methods for calculations with vectors and matrices using native Java arrays without any enclosing object structures. Matrices are simple two-dimensional arrays A[r][c], where r is the (vertical) row index and c is the (horizontal) column index (as common in linear algebra). This means that matrices are really vectors of row vectors. Only arrays of type float and double are supported. All matrices are assumed to be rectangular (i.e., all rows are of equal length).

Note: Methods named with a trailing 'D' (e.g., multiplyD(double, double[])) operate destructively, i.e., modify one of the supplied arguments.

Version:
2022/08/29
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Thrown when the dimensions of matrix/vector arguments do not match.
    static class 
    Thrown when a non-square matrix is encountered where a square matrix is assumed.
    static class 
    Thrown when source and target objects are identical but shouldn't.
    static class 
    Thrown when the length of some vector is zero.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default matrix symmetry tolerance.
    static char
    Leading delimiter used for lists of vector and matrix elements by toString() methods.
    static Locale
    Locale used for printing decimal numbers by toString() methods.
    static char
    Trailing delimiter used for lists of vector and matrix elements by toString() methods.
    static char
    Character used to separate successive vector and matrix elements by toString() methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[][]
    add(double[][] A, double[][] B)
    Calculates and returns the sum of the specified double matrix (non-destructively).
    static double[]
    add(double[] a, double[] b)
    Calculates and returns the sum of the specified double vectors (non-destructively).
    static float[][]
    add(float[][] A, float[][] B)
    Calculates and returns the sum of the specified float matrix (non-destructively).
    static float[]
    add(float[] a, float[] b)
    Calculates and returns the sum of the specified float vectors (non-destructively).
    static void
    addD(double[][] A, double[][] B)
    Adds the elements of the first double matrix to the second vector (destructively).
    static void
    addD(double[] a, double[] b)
    Adds the elements of the first double vector to the second vector (destructively).
    static void
    addD(float[][] A, float[][] B)
    Adds the elements of the first float matrix to the second vector (destructively).
    static void
    addD(float[] a, float[] b)
    Adds the elements of the first float vector to the second vector (destructively).
    static void
    copyD(double[] source, double[] target)
    Copy data from one double[] vector to another.
    static void
    copyD(float[] source, float[] target)
    Copy data from one float[] vector to another.
    static double
    determinant(double[][] A)
    Calculates and returns the determinant of the given double[][] matrix.
    static double
    Calculates and returns the determinant of the given RealMatrix.
    static double
    determinant2x2(double[][] A)
    Calculates and returns the determinant of the given 2x2 double[][] matrix.
    static float
    determinant2x2(float[][] A)
    Calculates and returns the determinant of the given 2x2 float[][] matrix.
    static double
    determinant3x3(double[][] A)
    Calculates and returns the determinant of the given 3x3 double[][] matrix.
    static float
    determinant3x3(float[][] A)
    Calculates and returns the determinant of the given 3x3 float[][] matrix.
    double[][]
    diagMatrix(double[] d)
    Creates and returns a diagonal matrix from the specified vector.
    static double
    distL2(double[] a, double[] b)
    Calculates the L2 distance between two vectors (points) in n-dimensional space.
    static float
    distL2(float[] a, float[] b)
    Calculates the L2 distance between two vectors (points) in n-dimensional space.
    static double
    distL2squared(double[] a, double[] b)
    Calculates the squared L2 distance between two vectors (points) in n-dimensional space.
    static float
    distL2squared(float[] a, float[] b)
    Calculates the squared L2 distance between two vectors (points) in n-dimensional space.
    static double
    dotProduct(double[] a, double[] b)
    Calculates and returns the dot (inner or scalar) product of two vectors, which must have the same length.
    static double[]
    duplicate(double[] a)
    Returns a copy of the given double[] vector.
    static double[][]
    duplicate(double[][] A)
    Returns a copy of the given double[][] matrix.
    static float[]
    duplicate(float[] a)
    Returns a copy of the given float[] vector.
    static float[][]
    duplicate(float[][] A)
    Returns a copy of the given float[][] matrix.
    static void
    fillD(double[][] A, double val)
    Fills the given double matrix with the specified value (destructively).
    static void
    fillD(double[] x, double val)
    Fills the given double vector with the specified value (destructively).
    static void
    fillD(float[][] A, float val)
    Fills the given float matrix with the specified value (destructively).
    static void
    fillD(float[] x, float val)
    Fills the given float vector with the specified value (destructively).
    static double[]
    flatten(double[][] A)
    Returns the values of the specified matrix as a double[] with elements arranged in row-major order.
    static float[]
    flatten(float[][] A)
    Returns the values of the specified matrix as a float[] with elements arranged in row-major order.
    static double[]
    Returns the values of the specified matrix as a double[] with elements arranged in row-major order.
    static double[][]
    fromLongBits(long[][] A)
    Converts a long[][] with 64-bit double representations to the equivalent double[][].
    static double[]
    getColumn(double[][] A, int c)
    Returns a particular column of the specified double matrix as a double vector.
    static float[]
    getColumn(float[][] A, int c)
    Returns a particular column of the specified float matrix as a float vector.
    static double[]
    getDiagonal(double[][] A)
    Returns a vector with the diagonal elements of the specified matrix.
    static float[]
    getDiagonal(float[][] A)
    Returns a vector with the diagonal elements of the specified matrix.
    static RealVector
    Returns a vector with the diagonal elements of the specified matrix.
    static int
    getNumberOfColumns(double[][] A)
    Returns the number of columns of the specified double matrix.
    static int
    getNumberOfColumns(float[][] A)
    Returns the number of columns of the specified float matrix.
    static int
    Returns the number of columns of the specified RealMatrix.
    static int
    getNumberOfRows(double[][] A)
    Returns the number of rows of the specified double matrix.
    static int
    getNumberOfRows(float[][] A)
    Returns the number of rows of the specified float matrix.
    static int
    Returns the number of rows of the specified RealMatrix.
    static double[]
    getRow(double[][] A, int r)
    Returns a particular row of the specified double matrix as a double vector.
    static float[]
    getRow(float[][] A, int r)
    Returns a particular row of the specified float matrix as a float vector.
    static double[][]
    idMatrix(int size)
    Creates and returns a new identity matrix of the specified size.
    static int
    idxMax(double[] a)
    Returns the index of the largest element in the specified vector.
    static int
    idxMax(float[] a)
    Returns the index of the largest element in the specified vector.
    static int
    idxMin(double[] a)
    Returns the index of the smallest element in the specified vector.
    static int
    idxMin(float[] a)
    Returns the index of the smallest element in the specified vector.
    static double[][]
    inverse(double[][] A)
    Calculates and returns the inverse of the given matrix, which must be square.
    static float[][]
    inverse(float[][] A)
    Calculates and returns the inverse of the given matrix, which must be square.
    static boolean
    Checks is the given square matrix is positive definite.
    static boolean
    isPositiveDefinite(RealMatrix A, double tolerance)
    Checks is the given square matrix is positive definite, using the specified symmetry tolerance value.
    static boolean
    isRectangular(double[][] A)
    Checks if all rows of the given matrix have the same length.
    static boolean
    isRectangular(float[][] A)
    Checks if all rows of the given matrix have the same length.
    static boolean
    isSingular(double[][] A)
    Checks is the given square double[][] matrix is non-singular.
    static boolean
    Checks is the given square matrix is non-singular.
    static boolean
    isSquare(double[][] A)
    Checks it the given matrix has the same number of rows and columns.
    static boolean
    isSquare(float[][] A)
    Checks it the given matrix has the same number of rows and columns.
    static boolean
    isSymmetric(double[][] A)
    Checks is the given square matrix is symmetric, using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
    static boolean
    isSymmetric(double[][] A, double relTolerance)
    Checks is the given square matrix is symmetric using the specified relative tolerance.
    static boolean
    Checks is the given square matrix is symmetric, using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
    static boolean
    isSymmetric(RealMatrix A, double relTolerance)
    Checks is the given square matrix is symmetric using the specified relative tolerance.
    static boolean
    isZero(double[] a)
    Checks if all elements of the specified double[] vector are zero using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
    static boolean
    isZero(double[][] A)
    Checks if all elements of the specified double[][] matrix are zero using default tolerance value (Arithmetic.EPSILON_DOUBLE).
    static boolean
    isZero(double[][] A, double tolerance)
    Checks if all elements of the specified double[][] matrix are zero using the specified tolerance value.
    static boolean
    isZero(double[] a, double tolerance)
    Checks if all elements of the specified double[] vector are zero using the specified tolerance value.
    static boolean
    isZero(float[] a)
    Checks if all elements of the specified float[] vector are zero using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
    static boolean
    isZero(float[] a, float tolerance)
    Checks if all elements of the specified float[] vector are zero using the specified tolerance value.
    static double[]
    join(double[]... as)
    Joins (concatenates) a sequence of vectors into a single vector.
    static float[]
    join(float[]... as)
    Joins (concatenates) a sequence of vectors into a single vector.
    static double[]
    lerp(double[] a, double[] b, double t)
    Performs linear interpolation between two vectors a and b, which must have the same length.
    static float[]
    lerp(float[] a, float[] b, float t)
    Performs linear interpolation between two vectors a and b, which must have the same length.
    static double[][]
    makeDoubleMatrix(int rows, int cols, double... values)
    Creates and returns a double[][] matrix containing the specified values.
    static double[]
    makeDoubleVector(double... values)
    Creates and returns a double[] vector from the specified values.
    static float[][]
    makeFloatMatrix(int rows, int cols, float... values)
    Creates and returns a float[][] matrix containing the specified values.
    static float[]
    makeFloatVector(float... values)
    Creates and returns a float[] vector from the specified values.
    static RealMatrix
    makeRealMatrix(int rows, int cols, double... values)
    Creates and returns a RealMatrix containing the specified double values.
    static RealVector
    makeRealVector(double... values)
    Creates and returns a RealVector from the specified double values.
    static double
    max(double[] a)
    Returns the largest value in the specified double[] vector.
    static double
    max(double[][] A)
    Returns the largest value in the specified double[][] matrix.
    static float
    max(float[] a)
    Returns the largest value in the specified float[] vector.
    static float
    max(float[][] A)
    Returns the largest value in the specified float[][] matrix.
    static double
    min(double[] a)
    Returns the smallest value in the specified vector.
    static float
    min(float[] a)
    Returns the smallest value in the specified vector.
    static double[]
    multiply(double[][] A, double[] x)
    Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
    static double[][]
    multiply(double[][] A, double[][] B)
    Returns the product of two double[][] matrices A, B as a new double[][] matrix.
    static double[]
    multiply(double[] x, double[][] A)
    Multiplies a vector x with a matrix A "from the left", i.e., y = x * A, where x is treated as a row vector and the result y is also a row vector.
    static double[]
    multiply(double s, double[] x)
    Multiplies a double[] vector by a scalar and returns a new double[] vector (non-destructive).
    static double[][]
    multiply(double s, double[][] A)
    Multiplies a double[][] matrix by a scalar and returns a new double[][] matrix (non-destructive).
    static float[]
    multiply(float[][] A, float[] x)
    Multiplies a matrix A with a vector x from the right, i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
    static float[][]
    multiply(float[][] A, float[][] B)
    Returns the product of two float[][] matrices A, B as a new float[][] matrix.
    static float[]
    multiply(float s, float[] x)
    Multiplies a float[] vector by a scalar and returns a new float[] vector (non-destructive).
    static float[][]
    multiply(float s, float[][] A)
    Multiplies a float[][] matrix by a scalar and returns a new float[][] matrix (non-destructive).
    static void
    multiplyD(double[][] A, double[][] B, double[][] C)
    Calculates the product of two double[][] matrices A, B and places the results in the third double[][] matrix C, which is modified (destructively).
    static void
    multiplyD(double[][] A, double[] x, double[] y)
    Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
    static void
    multiplyD(double[] x, double[][] A, double[] y)
    Destructive version of multiply(double[], double[][]).
    static void
    multiplyD(double s, double[] x)
    Multiplies a double[] vector by a scalar.
    static void
    multiplyD(double s, double[][] A)
    Multiplies a double[][] matrix by a scalar.
    static void
    multiplyD(float[][] A, float[][] B, float[][] C)
    Calculates the product of two float[][] matrices A, B and places the results in the third float[][] matrix C, which is modified (destructively).
    static void
    multiplyD(float[][] A, float[] x, float[] y)
    Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
    static void
    multiplyD(float s, float[] x)
    Multiplies a float[] vector by a scalar.
    static void
    multiplyD(float s, float[][] A)
    Multiplies a float[][] matrix by a scalar.
    static double
    norm(double[][] A)
    Calculates and returns the Froebenius norm of the given matrix.
    static double[]
    normalize(double[] a)
    Normalizes the specified double[] vector to unit (L2) norm and returns the result as a new double[] vector.
    static float[]
    normalize(float[] a)
    Normalizes the specified float[] vector to unit (L2) norm and returns the result as a new float[] vector.
    static void
    normalizeD(double[] a)
    Normalizes the specified double[] vector to unit (L2) norm.
    static void
    normalizeD(float[] a)
    Normalizes the specified float[] vector to unit (L2) norm.
    static double
    normL1(double[] a)
    Calculates and returns the L1 norm of the given vector.
    static float
    normL1(float[] a)
    Calculates and returns the L1 norm of the given vector.
    static double
    normL2(double[] a)
    Calculates and returns the L2 norm of the given vector.
    static float
    normL2(float[] x)
    Calculates and returns the L2 norm of the given vector.
    static double
    normL2squared(double[] a)
    Calculates and returns the squared L2 norm of the given vector.
    static double
    normL2squared(float[] a)
    Calculates and returns the squared L2 norm of the given vector.
    static double[][]
    outerProduct(double[] a, double[] b)
    Calculates and returns the outer product of two vectors, which is a matrix of size (m,n), where m is the length of the first vector and m is the length of the second vector.
    static void
    printToStream(double[][] A, PrintStream strm)
    Outputs a string representation of the given matrix to the specified output stream.
    static void
    printToStream(double[] a, PrintStream strm)
    Outputs a string representation of the given vector to the specified output stream.
    static void
    printToStream(float[][] A, PrintStream strm)
    Outputs a string representation of the given matrix to the specified output stream.
    static void
    printToStream(float[] a, PrintStream strm)
    Outputs a string representation of the given vector to the specified output stream.
    static void
    printToStream(long[][] A, PrintStream strm)
     
    static boolean
    sameSize(double[][] A, double[][] B)
    Checks if the given matrices have the same size.
    static boolean
    sameSize(double[] a, double[] b)
    Checks if the given vectors have the same length.
    static boolean
    sameSize(float[][] A, float[][] B)
    Checks if the given matrices have the same size.
    static boolean
    sameSize(float[] a, float[] b)
    Checks if the given vectors have the same length.
    static double[]
    solve(double[][] A, double[] b)
    Finds the exact solution x for the linear system of equations A * x = b.
    static RealVector
    Finds the exact solution x for the linear system of equations A * x = b.
    static double[]
    sort(double[] a)
    Returns a sorted copy of the given double[] vector.
    static float[]
    sort(float[] a)
    Returns a sorted copy of the given float[] vector.
    static double[]
    subtract(double[] a, double[] b)
    Calculates and returns the difference (a - b) of the specified double vectors (non-destructively).
    static float[]
    subtract(float[] a, float[] b)
    Calculates and returns the difference (a - b) of the specified float vectors (non-destructively).
    static double
    sum(double[] a)
    Calculates and returns the sum of the elements in the specified double[] vector.
    static double
    sum(double[][] A)
    Calculates and returns the sum of the elements in the specified double[][] matrix.
    static double
    sum(float[] a)
    Calculates and returns the sum of the elements in the specified float[] vector.
    static double
    sum(float[][] A)
    Calculates and returns the sum of the elements in the specified float[][] matrix.
    static double
    sumColumn(double[][] A, int col)
    Calculates the sum of the elements in the specified matrix column.
    static double
    sumColumn(float[][] A, int col)
    Calculates the sum of the elements in the specified matrix column.
    static double[]
    sumColumns(double[][] A)
    Calculates the sums of all matrix columns and returns them as a vector with one element per column.
    static float[]
    sumColumns(float[][] A)
    Calculates the sums of all matrix columns and returns them as a vector with one element per column.
    static double
    sumRow(double[][] A, int row)
    Calculates the sum of the elements in the specified matrix row.
    static double
    sumRow(float[][] A, int row)
    Calculates the sum of the elements in the specified matrix row.
    static double[]
    sumRows(double[][] A)
    Calculates the sums of all matrix rows and returns them as a vector with one element per row.
    static float[]
    sumRows(float[][] A)
    Calculates the sums of all matrix rows and returns them as a vector with one element per row.
    static double[]
    toCartesian(double[] ah)
    Converts a homogeneous vector to its equivalent Cartesian vector, which is one element shorter.
    static double[]
    toDouble(float[] a)
    Converts a float[] to a double[].
    static double[][]
    toDouble(float[][] A)
    Converts a float[][] to a double[][].
    static float[]
    toFloat(double[] a)
    Converts a double[] to a float[].
    static float[][]
    toFloat(double[][] A)
    Converts a double[][] to a float[][].
    static double[]
    toHomogeneous(double[] ac)
    Converts a Cartesian vector to an equivalent homogeneous vector by attaching an additional 1-element.
    static long[][]
    toLongBits(double[][] A)
    Converts a double[][] to a long[][] whose elements contain the 64-bit representation of the original double values.
    static String
    toString(double[] a)
    Returns a string representation of the specified vector.
    static String
    toString(double[][] A)
    Returns a string representation of the specified matrix.
    static String
    toString(float[] a)
    Returns a string representation of the specified vector.
    static String
    toString(float[][] A)
    Returns a string representation of the specified matrix.
    static String
    toString(long[][] A)
    Returns a string representation of the specified matrix with long elements.
    static String
    Returns a string representation of the specified matrix.
    static String
    Returns a string representation of the specified vector.
    static double
    trace(double[][] A)
    Calculates and returns the trace of the given double[][] matrix.
    static float
    trace(float[][] A)
    Calculates and returns the trace of the given float[][] matrix.
    static double[][]
    transpose(double[][] A)
    Returns the transpose of the given double[][] matrix.
    static float[][]
    transpose(float[][] A)
    Returns the transpose of the given float[][] matrix.
    static double[]
    zeroVector(int length)
    Creates and returns a new zero-valued double[] vector of the specified length.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PrintLocale

      public static Locale PrintLocale
      Locale used for printing decimal numbers by toString() methods.
    • SeparationChar

      public static char SeparationChar
      Character used to separate successive vector and matrix elements by toString() methods.
    • LeftDelimitChar

      public static char LeftDelimitChar
      Leading delimiter used for lists of vector and matrix elements by toString() methods.
    • RightDelimitChar

      public static char RightDelimitChar
      Trailing delimiter used for lists of vector and matrix elements by toString() methods.
    • DefaultSymmetryTolerance

      public static final double DefaultSymmetryTolerance
      Default matrix symmetry tolerance.
      See Also:
  • Method Details

    • makeDoubleMatrix

      public static double[][] makeDoubleMatrix(int rows, int cols, double... values)

      Creates and returns a double[][] matrix containing the specified values. Throws a IllegalArgumentException if the number of supplied values does not exactly match the matrix size. Example for creating a 2x3 matrix:

       double[][] A = makeDoubleMatrix(2, 3,
                      1.0, 2.0, 3.0,
                      4.0, 5.0, 6.0 );

      See also flatten(double[][]).

      Parameters:
      rows - the number of matrix rows
      cols - the number of matrix columns
      values - a sequence of matrix values in row-major order (may also be passed as a double[])
      Returns:
      a double[][] matrix
    • makeFloatMatrix

      public static float[][] makeFloatMatrix(int rows, int cols, float... values)

      Creates and returns a float[][] matrix containing the specified values. Throws a IllegalArgumentException if the number of supplied values does not exactly match the matrix size. Example for creating a 2x3 matrix:

       float[][] A = makeFloatMatrix(2, 3,
                      1.0f, 2.0f, 3.0f,
                      4.0f, 5.0f, 6.0f );

      See also flatten(float[][]).

      Parameters:
      rows - the number of matrix rows
      cols - the number of matrix columns
      values - a sequence of matrix values in row-major order (may also be passed as a float[])
      Returns:
      a float[][] matrix
    • makeRealMatrix

      public static RealMatrix makeRealMatrix(int rows, int cols, double... values)

      Creates and returns a RealMatrix containing the specified double values. Calls makeDoubleMatrix(int, int, double...). Example for creating a 2x3 matrix:

       RealMatrix A = makeRealMatrix(2, 3,
                      1.0, 2.0, 3.0,
                      4.0, 5.0, 6.0 );

      See also flatten(RealMatrix).

      Parameters:
      rows - the number of matrix rows
      cols - the number of matrix columns
      values - a sequence of matrix values in row-major order (may also be passed as a double[])
      Returns:
      a RealMatrix
    • flatten

      public static double[] flatten(double[][] A)
      Returns the values of the specified matrix as a double[] with elements arranged in row-major order. The matrix must be fully rectangular (all rows of same length). See also makeDoubleMatrix(int, int, double...).
      Parameters:
      A - a matrix
      Returns:
      a double[] with the matrix elements
    • flatten

      public static float[] flatten(float[][] A)
      Returns the values of the specified matrix as a float[] with elements arranged in row-major order. The matrix must be fully rectangular (all rows of same length). See also makeFloatMatrix(int, int, float...).
      Parameters:
      A - a matrix
      Returns:
      a float[] with the matrix elements
    • flatten

      public static double[] flatten(RealMatrix A)
      Returns the values of the specified matrix as a double[] with elements arranged in row-major order. See also makeRealMatrix(int, int, double...).
      Parameters:
      A - a matrix
      Returns:
      a double[] with the matrix elements
    • makeDoubleVector

      public static double[] makeDoubleVector(double... values)
      Creates and returns a double[] vector from the specified values.
      Parameters:
      values - a sequence of vector values (may also be passed as a single double[])
      Returns:
      a double[]
    • makeFloatVector

      public static float[] makeFloatVector(float... values)
      Creates and returns a float[] vector from the specified values.
      Parameters:
      values - a sequence of vector values (may also be passed as a single float[])
      Returns:
      a float[]
    • makeRealVector

      public static RealVector makeRealVector(double... values)
      Creates and returns a RealVector from the specified double values.
      Parameters:
      values - a sequence of vector values (may also be passed as a single double[])
      Returns:
      a RealVector
    • zeroVector

      public static double[] zeroVector(int length)
      Creates and returns a new zero-valued double[] vector of the specified length. Throws an exception if the length is less than 1.
      Parameters:
      length - the length of the vector
      Returns:
      a double[] with zero values
    • idMatrix

      public static double[][] idMatrix(int size)
      Creates and returns a new identity matrix of the specified size. Throws an exception if the size is less than 1.
      Parameters:
      size - the size of the matrix
      Returns:
      an identity matrix
    • getNumberOfRows

      public static int getNumberOfRows(double[][] A)
      Returns the number of rows of the specified double matrix.
      Parameters:
      A - a double[][] matrix
      Returns:
      the number of rows
    • getNumberOfColumns

      public static int getNumberOfColumns(double[][] A)
      Returns the number of columns of the specified double matrix.
      Parameters:
      A - a double[][] matrix
      Returns:
      the number of columns
    • getNumberOfRows

      public static int getNumberOfRows(float[][] A)
      Returns the number of rows of the specified float matrix.
      Parameters:
      A - a float[][] matrix
      Returns:
      the number of rows
    • getNumberOfColumns

      public static int getNumberOfColumns(float[][] A)
      Returns the number of columns of the specified float matrix.
      Parameters:
      A - a float[][] matrix
      Returns:
      the number of columns
    • getNumberOfRows

      public static int getNumberOfRows(RealMatrix A)
      Returns the number of rows of the specified RealMatrix.
      Parameters:
      A - a RealMatrix
      Returns:
      the number of rows
    • getNumberOfColumns

      public static int getNumberOfColumns(RealMatrix A)
      Returns the number of columns of the specified RealMatrix.
      Parameters:
      A - a RealMatrix
      Returns:
      the number of columns
    • getRow

      public static double[] getRow(double[][] A, int r)
      Returns a particular row of the specified double matrix as a double vector.
      Parameters:
      A - a double[][] matrix
      r - the row index (starting with 0)
      Returns:
      a double vector
    • getRow

      public static float[] getRow(float[][] A, int r)
      Returns a particular row of the specified float matrix as a float vector.
      Parameters:
      A - a float[][] matrix
      r - the row index (starting with 0)
      Returns:
      a float vector
    • getColumn

      public static double[] getColumn(double[][] A, int c)
      Returns a particular column of the specified double matrix as a double vector.
      Parameters:
      A - a double[][] matrix
      c - the column index (starting with 0)
      Returns:
      a double vector
    • getColumn

      public static float[] getColumn(float[][] A, int c)
      Returns a particular column of the specified float matrix as a float vector.
      Parameters:
      A - a float[][] matrix
      c - the column index (starting with 0)
      Returns:
      a float vector
    • isRectangular

      public static boolean isRectangular(float[][] A)
      Checks if all rows of the given matrix have the same length.
      Parameters:
      A - a float[][] matrix
      Returns:
      true iff the matrix is rectangular
    • isRectangular

      public static boolean isRectangular(double[][] A)
      Checks if all rows of the given matrix have the same length.
      Parameters:
      A - a double[][] matrix
      Returns:
      true iff the matrix is rectangular
    • isSquare

      public static boolean isSquare(double[][] A)
      Checks it the given matrix has the same number of rows and columns.
      Parameters:
      A - a double[][] matrix
      Returns:
      true iff the matrix is square
    • isSquare

      public static boolean isSquare(float[][] A)
      Checks it the given matrix has the same number of rows and columns.
      Parameters:
      A - a float[][] matrix
      Returns:
      true iff the matrix is square
    • getDiagonal

      public static double[] getDiagonal(double[][] A)
      Returns a vector with the diagonal elements of the specified matrix.
      Parameters:
      A - a square double[][] matrix
      Returns:
      the vector of diagonal elements
    • getDiagonal

      public static float[] getDiagonal(float[][] A)
      Returns a vector with the diagonal elements of the specified matrix.
      Parameters:
      A - a square float[][] matrix
      Returns:
      the vector of diagonal elements
    • getDiagonal

      public static RealVector getDiagonal(RealMatrix A)
      Returns a vector with the diagonal elements of the specified matrix.
      Parameters:
      A - a square RealMatrix
      Returns:
      the RealVector of diagonal elements
    • diagMatrix

      public double[][] diagMatrix(double[] d)
      Creates and returns a diagonal matrix from the specified vector. See also getDiagonal(double[][]).
      Parameters:
      d - vector of diagonal matrix elements
      Returns:
      a diagonal matrix
    • isSingular

      public static boolean isSingular(double[][] A) throws Matrix.NonsquareMatrixException
      Checks is the given square double[][] matrix is non-singular.
      Parameters:
      A - a square double[][] matrix
      Returns:
      true if the matrix is singular
      Throws:
      Matrix.NonsquareMatrixException - if the matrix is not square
    • isSingular

      public static boolean isSingular(RealMatrix A) throws Matrix.NonsquareMatrixException
      Checks is the given square matrix is non-singular.
      Parameters:
      A - a square RealMatrix
      Returns:
      true if the matrix is singular
      Throws:
      Matrix.NonsquareMatrixException - if the matrix is not square
    • isSymmetric

      public static boolean isSymmetric(RealMatrix A, double relTolerance)
      Checks is the given square matrix is symmetric using the specified relative tolerance.
      Parameters:
      A - a square matrix
      relTolerance - relative symmetry tolerance
      Returns:
      true if the matrix is symmetric
    • isSymmetric

      public static boolean isSymmetric(RealMatrix A)
      Checks is the given square matrix is symmetric, using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      A - a square matrix
      Returns:
      true if the matrix is symmetric
    • isSymmetric

      public static boolean isSymmetric(double[][] A, double relTolerance)
      Checks is the given square matrix is symmetric using the specified relative tolerance.
      Parameters:
      A - a square matrix
      relTolerance - relative symmetry tolerance
      Returns:
      true if the matrix is symmetric
    • isSymmetric

      public static boolean isSymmetric(double[][] A)
      Checks is the given square matrix is symmetric, using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      A - a square matrix
      Returns:
      true if the matrix is symmetric
    • isPositiveDefinite

      public static boolean isPositiveDefinite(RealMatrix A, double tolerance)
      Checks is the given square matrix is positive definite, using the specified symmetry tolerance value.
      Parameters:
      A - a square matrix
      tolerance - the absolute positivity and relative symmetry tolerance
      Returns:
      true if the matrix is positive definite
    • isPositiveDefinite

      public static boolean isPositiveDefinite(RealMatrix A)
      Checks is the given square matrix is positive definite.
      Parameters:
      A - a square matrix
      Returns:
      true if the matrix is positive definite
    • sameSize

      public static boolean sameSize(double[] a, double[] b)
      Checks if the given vectors have the same length.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      true iff both vectors have the same length
    • sameSize

      public static boolean sameSize(float[] a, float[] b)
      Checks if the given vectors have the same length.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      true iff both vectors have the same length
    • sameSize

      public static boolean sameSize(double[][] A, double[][] B)
      Checks if the given matrices have the same size.
      Parameters:
      A - first matrix
      B - second matrix
      Returns:
      true iff both matrices have the same size
    • sameSize

      public static boolean sameSize(float[][] A, float[][] B)
      Checks if the given matrices have the same size.
      Parameters:
      A - first matrix
      B - second matrix
      Returns:
      true iff both matrices have the same size
    • duplicate

      public static double[] duplicate(double[] a)
      Returns a copy of the given double[] vector.
      Parameters:
      a - a double[] vector
      Returns:
      a copy of the vector
    • duplicate

      public static float[] duplicate(float[] a)
      Returns a copy of the given float[] vector.
      Parameters:
      a - a float[] vector
      Returns:
      a copy of the vector
    • duplicate

      public static double[][] duplicate(double[][] A)
      Returns a copy of the given double[][] matrix.
      Parameters:
      A - a double[][] matrix
      Returns:
      a copy of the matrix
    • duplicate

      public static float[][] duplicate(float[][] A)
      Returns a copy of the given float[][] matrix.
      Parameters:
      A - a float[][] matrix
      Returns:
      a copy of the matrix
    • copyD

      public static void copyD(float[] source, float[] target)
      Copy data from one float[] vector to another. The two vectors must have the same length (not checked).
      Parameters:
      source - the source vector (unmodified)
      target - the target vector (modified)
    • copyD

      public static void copyD(double[] source, double[] target)
      Copy data from one double[] vector to another. The two vectors must have the same length (not checked).
      Parameters:
      source - the source vector (unmodified)
      target - the target vector (modified)
    • toFloat

      public static float[] toFloat(double[] a)
      Converts a double[] to a float[].
      Parameters:
      a - the original double[] array
      Returns:
      a copy of the array of type float[]
    • toFloat

      public static float[][] toFloat(double[][] A)
      Converts a double[][] to a float[][].
      Parameters:
      A - the original double[][] array
      Returns:
      a copy of the array of type float[][]
    • toLongBits

      public static long[][] toLongBits(double[][] A)
      Converts a double[][] to a long[][] whose elements contain the 64-bit representation of the original double values. Its printed representation is comparatively compact (see toString(long[][])). This is useful for defining literal double arrays without losing any precision (by avoiding conversion to decimal representation). Note that the long are bit representations and cannot be used to perform any arithmetic calculations.
      Parameters:
      A - the original double array
      Returns:
      a copy of the array of type float[][]
      See Also:
    • fromLongBits

      public static double[][] fromLongBits(long[][] A)
      Converts a long[][] with 64-bit double representations to the equivalent double[][]. This is useful for defining literal double arrays without losing any precision (by avoiding conversion to decimal representation).
      Parameters:
      A - a long array
      Returns:
      the equivalent double array
      See Also:
    • toDouble

      public static double[] toDouble(float[] a)
      Converts a float[] to a double[].
      Parameters:
      a - the original float[] array
      Returns:
      a copy of the array of type double[]
    • toDouble

      public static double[][] toDouble(float[][] A)
      Converts a float[][] to a double[][].
      Parameters:
      A - the original float[][] array
      Returns:
      a copy of the array of type double[][]
    • fillD

      public static void fillD(double[] x, double val)
      Fills the given double vector with the specified value (destructively).
      Parameters:
      x - a vector (which is modified)
      val - the fill value
    • fillD

      public static void fillD(float[] x, float val)
      Fills the given float vector with the specified value (destructively).
      Parameters:
      x - a vector (which is modified)
      val - the fill value
    • fillD

      public static void fillD(double[][] A, double val)
      Fills the given double matrix with the specified value (destructively).
      Parameters:
      A - a matrix (which is modified)
      val - the fill value
    • fillD

      public static void fillD(float[][] A, float val)
      Fills the given float matrix with the specified value (destructively).
      Parameters:
      A - a matrix (which is modified)
      val - the fill value
    • add

      public static double[] add(double[] a, double[] b)
      Calculates and returns the sum of the specified double vectors (non-destructively). An exception is thrown if the vectors are of different lengths. None of the arguments is modified.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      a new double vector
    • addD

      public static void addD(double[] a, double[] b)
      Adds the elements of the first double vector to the second vector (destructively). An exception is thrown if the vectors are of different lengths. The second vector is modified.
      Parameters:
      a - the first vector
      b - the second vector
    • add

      public static float[] add(float[] a, float[] b)
      Calculates and returns the sum of the specified float vectors (non-destructively). An exception is thrown if the vectors are of different lengths. None of the arguments is modified.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      a new float vector
    • addD

      public static void addD(float[] a, float[] b)
      Adds the elements of the first float vector to the second vector (destructively). An exception is thrown if the vectors are of different lengths. The second vector is modified.
      Parameters:
      a - the first vector
      b - the second vector
    • add

      public static double[][] add(double[][] A, double[][] B)
      Calculates and returns the sum of the specified double matrix (non-destructively). An exception is thrown if the matrices are of different size. None of the arguments is modified.
      Parameters:
      A - the first matrix
      B - the second matrix
      Returns:
      a new double matrix
    • addD

      public static void addD(double[][] A, double[][] B)
      Adds the elements of the first double matrix to the second vector (destructively). An exception is thrown if the matrices are of different size. The second matrix is modified.
      Parameters:
      A - the first matrix
      B - the second matrix
    • add

      public static float[][] add(float[][] A, float[][] B)
      Calculates and returns the sum of the specified float matrix (non-destructively). An exception is thrown if the matrices are of different size. None of the arguments is modified.
      Parameters:
      A - the first matrix
      B - the second matrix
      Returns:
      a new float matrix
    • addD

      public static void addD(float[][] A, float[][] B)
      Adds the elements of the first float matrix to the second vector (destructively). An exception is thrown if the matrices are of different size. The second matrix is modified.
      Parameters:
      A - the first matrix
      B - the second matrix
    • subtract

      public static double[] subtract(double[] a, double[] b)
      Calculates and returns the difference (a - b) of the specified double vectors (non-destructively). An exception is thrown if the vectors are of different lengths. None of the arguments is modified.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      a new double vector
    • subtract

      public static float[] subtract(float[] a, float[] b)
      Calculates and returns the difference (a - b) of the specified float vectors (non-destructively). An exception is thrown if the vectors are of different lengths. None of the arguments is modified.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      a new float vector
    • multiply

      public static double[] multiply(double s, double[] x)
      Multiplies a double[] vector by a scalar and returns a new double[] vector (non-destructive).
      Parameters:
      s - a scalar
      x - a vector
      Returns:
      a new vector
    • multiplyD

      public static void multiplyD(double s, double[] x)
      Multiplies a double[] vector by a scalar. Destructive, i.e., the specified vector is modified.
      Parameters:
      s - a scalar
      x - a vector
    • multiply

      public static double[][] multiply(double s, double[][] A)
      Multiplies a double[][] matrix by a scalar and returns a new double[][] matrix (non-destructive).
      Parameters:
      s - a scalar
      A - a matrix
      Returns:
      a new matrix
    • multiplyD

      public static void multiplyD(double s, double[][] A)
      Multiplies a double[][] matrix by a scalar. Destructive, i.e., the specified matrix is modified.
      Parameters:
      s - a scalar
      A - a matrix
    • multiply

      public static float[] multiply(float s, float[] x)
      Multiplies a float[] vector by a scalar and returns a new float[] vector (non-destructive).
      Parameters:
      s - a scalar
      x - a vector
      Returns:
      a new vector
    • multiplyD

      public static void multiplyD(float s, float[] x)
      Multiplies a float[] vector by a scalar. Destructive, i.e., the specified vector is modified.
      Parameters:
      s - a scalar
      x - a matrix
    • multiply

      public static float[][] multiply(float s, float[][] A)
      Multiplies a float[][] matrix by a scalar and returns a new float[][] matrix (non-destructive).
      Parameters:
      s - a scalar
      A - a matrix
      Returns:
      a new matrix
    • multiplyD

      public static void multiplyD(float s, float[][] A)
      Multiplies a float[][] matrix by a scalar. Destructive, i.e., the specified matrix is modified.
      Parameters:
      s - a scalar
      A - a matrix
    • multiply

      public static double[] multiply(double[] x, double[][] A)
      Multiplies a vector x with a matrix A "from the left", i.e., y = x * A, where x is treated as a row vector and the result y is also a row vector.
      Parameters:
      x - a (row) vector of length m
      A - a matrix of size (m,n)
      Returns:
      a (row) vector of length n
    • multiplyD

      public static void multiplyD(double[] x, double[][] A, double[] y)
      Destructive version of multiply(double[], double[][]).
      Parameters:
      x - a (row) vector of length m
      A - matrix of size (m,n)
      y - a (row) vector of length n
    • multiply

      public static double[] multiply(double[][] A, double[] x)
      Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
      Parameters:
      x - a (column) vector of length n
      A - a matrix of size (m,n)
      Returns:
      a (column) vector of length m
    • multiplyD

      public static void multiplyD(double[][] A, double[] x, double[] y)
      Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector. The result is placed in the vector passed as the last argument, which is modified. Destructive version of multiply(double[][], double[]). An exception is thrown if any matrix or vector dimensions do not match or if the two vectors are the same.
      Parameters:
      A - matrix of size (m,n)
      x - a (column) vector of length n
      y - a (column) vector of length m
    • multiply

      public static float[] multiply(float[][] A, float[] x)
      Multiplies a matrix A with a vector x from the right, i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector.
      Parameters:
      x - a (column) vector of length n
      A - a matrix of size (m,n)
      Returns:
      a (column) vector of length m
    • multiplyD

      public static void multiplyD(float[][] A, float[] x, float[] y)
      Multiplies a matrix A with a vector x "from the right", i.e., y = A * x, where x is treated as a column vector and the result y is also a column vector. The result is placed in the vector passed as the last argument, which is modified. Destructive version of multiply(float[][], float[]). An exception is thrown if any matrix or vector dimensions do not match or if the two vectors are the same.
      Parameters:
      A - matrix of size (m,n)
      x - a (column) vector of length n
      y - a (column) vector of length m
    • multiply

      public static double[][] multiply(double[][] A, double[][] B)
      Returns the product of two double[][] matrices A, B as a new double[][] matrix. Non-destructive, i.e., none of the arguments is modified.
      Parameters:
      A - first matrix
      B - second matrix
      Returns:
      the matrix product A * B
    • multiplyD

      public static void multiplyD(double[][] A, double[][] B, double[][] C) throws Matrix.SameSourceTargetException, Matrix.IncompatibleDimensionsException
      Calculates the product of two double[][] matrices A, B and places the results in the third double[][] matrix C, which is modified (destructively). An exception is thrown if any matrix dimensions do not match or if the target matrix is the same as one of the source matrices.
      Parameters:
      A - first matrix
      B - second matrix
      C - the result matrix
      Throws:
      Matrix.SameSourceTargetException - if the target matrix is the same as one of the source matrices
      Matrix.IncompatibleDimensionsException - if any matrix dimensions do not match
    • multiply

      public static float[][] multiply(float[][] A, float[][] B)
      Returns the product of two float[][] matrices A, B as a new float[][] matrix. Non-destructive, i.e., none of the arguments is modified.
      Parameters:
      A - first matrix
      B - second matrix
      Returns:
      the matrix product A * B
    • multiplyD

      public static void multiplyD(float[][] A, float[][] B, float[][] C)
      Calculates the product of two float[][] matrices A, B and places the results in the third float[][] matrix C, which is modified (destructively). An exception is thrown if any matrix dimensions do not match or if the target matrix is the same as one of the source matrices.
      Parameters:
      A - first matrix
      B - second matrix
      C - the result matrix
    • dotProduct

      public static double dotProduct(double[] a, double[] b)
      Calculates and returns the dot (inner or scalar) product of two vectors, which must have the same length. Throws an exceptions if vector dimensions do not match.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      the dot product
    • outerProduct

      public static double[][] outerProduct(double[] a, double[] b)
      Calculates and returns the outer product of two vectors, which is a matrix of size (m,n), where m is the length of the first vector and m is the length of the second vector.
      Parameters:
      a - first (column) vector (of length m)
      b - second (row) vector (of length n)
      Returns:
      the outer product (matrix)
    • normL1

      public static double normL1(double[] a)
      Calculates and returns the L1 norm of the given vector.
      Parameters:
      a - a vector
      Returns:
      the L1 norm of the vector
    • normL1

      public static float normL1(float[] a)
      Calculates and returns the L1 norm of the given vector.
      Parameters:
      a - a vector
      Returns:
      the L1 norm of the vector
    • normL2

      public static double normL2(double[] a)
      Calculates and returns the L2 norm of the given vector.
      Parameters:
      a - a vector
      Returns:
      the L2 norm of the vector
    • normL2squared

      public static double normL2squared(double[] a)
      Calculates and returns the squared L2 norm of the given vector. The squared norm is less costly to calculate (no square root needed) than the L2 norm and is thus often used for efficiency.
      Parameters:
      a - a vector
      Returns:
      the squared L2 norm of the vector
    • normL2

      public static float normL2(float[] x)
      Calculates and returns the L2 norm of the given vector.
      Parameters:
      x - a vector
      Returns:
      the L2 norm of the vector
    • normL2squared

      public static double normL2squared(float[] a)
      Calculates and returns the squared L2 norm of the given vector. The squared norm is less costly to calculate (no square root needed) than the L2 norm and is thus often used for efficiency.
      Parameters:
      a - a vector
      Returns:
      the squared L2 norm of the vector
    • normalize

      public static double[] normalize(double[] a)
      Normalizes the specified double[] vector to unit (L2) norm and returns the result as a new double[] vector.
      Parameters:
      a - a vector
      Returns:
      the normalized vector
    • normalizeD

      public static void normalizeD(double[] a)
      Normalizes the specified double[] vector to unit (L2) norm. The vector is modified (destructively).
      Parameters:
      a - a vector
    • normalize

      public static float[] normalize(float[] a)
      Normalizes the specified float[] vector to unit (L2) norm and returns the result as a new float[] vector.
      Parameters:
      a - a vector
      Returns:
      the normalized vector
    • normalizeD

      public static void normalizeD(float[] a)
      Normalizes the specified float[] vector to unit (L2) norm. The vector is modified (destructively).
      Parameters:
      a - a vector
    • distL2

      public static double distL2(double[] a, double[] b)
      Calculates the L2 distance between two vectors (points) in n-dimensional space. Both vectors must have the same number of elements.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      the distance
    • distL2squared

      public static double distL2squared(double[] a, double[] b)
      Calculates the squared L2 distance between two vectors (points) in n-dimensional space. Both vectors must have the same number of elements.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      the squared distance
    • distL2

      public static float distL2(float[] a, float[] b)
      Calculates the L2 distance between two vectors (points) in n-dimensional space. Both vectors must have the same number of elements.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      the distance
    • distL2squared

      public static float distL2squared(float[] a, float[] b)
      Calculates the squared L2 distance between two vectors (points) in n-dimensional space. Both vectors must have the same number of elements.
      Parameters:
      a - first vector
      b - second vector
      Returns:
      the squared distance
    • norm

      public static double norm(double[][] A)
      Calculates and returns the Froebenius norm of the given matrix.
      Parameters:
      A - a matrix
      Returns:
      the norm of the matrix
    • sum

      public static double sum(double[] a)
      Calculates and returns the sum of the elements in the specified double[] vector.
      Parameters:
      a - a vector
      Returns:
      the sum of vector elements
    • sum

      public static double sum(double[][] A)
      Calculates and returns the sum of the elements in the specified double[][] matrix.
      Parameters:
      A - a matrix
      Returns:
      the sum of matrix elements
    • sum

      public static double sum(float[] a)
      Calculates and returns the sum of the elements in the specified float[] vector.
      Parameters:
      a - a vector
      Returns:
      the sum of vector elements
    • sum

      public static double sum(float[][] A)
      Calculates and returns the sum of the elements in the specified float[][] matrix.
      Parameters:
      A - a matrix
      Returns:
      the sum of matrix elements
    • sumRow

      public static double sumRow(double[][] A, int row)
      Calculates the sum of the elements in the specified matrix row.
      Parameters:
      A - a matrix
      row - the row index
      Returns:
      the sum of the row's elements
    • sumRow

      public static double sumRow(float[][] A, int row)
      Calculates the sum of the elements in the specified matrix row.
      Parameters:
      A - a matrix
      row - the row index
      Returns:
      the sum of the row's elements
    • sumColumn

      public static double sumColumn(double[][] A, int col)
      Calculates the sum of the elements in the specified matrix column.
      Parameters:
      A - a matrix
      col - the column index
      Returns:
      the sum of the column's elements
    • sumColumn

      public static double sumColumn(float[][] A, int col)
      Calculates the sum of the elements in the specified matrix column.
      Parameters:
      A - a matrix
      col - the column index
      Returns:
      the sum of the column's elements
    • sumRows

      public static double[] sumRows(double[][] A)
      Calculates the sums of all matrix rows and returns them as a vector with one element per row.
      Parameters:
      A - a matrix of size (M,N)
      Returns:
      a vector of length M containing the sums of all matrix columns
    • sumRows

      public static float[] sumRows(float[][] A)
      Calculates the sums of all matrix rows and returns them as a vector with one element per row.
      Parameters:
      A - a matrix of size (M,N)
      Returns:
      a vector of length M containing the sums of all matrix columns
    • sumColumns

      public static double[] sumColumns(double[][] A)
      Calculates the sums of all matrix columns and returns them as a vector with one element per column.
      Parameters:
      A - a matrix of size (M,N)
      Returns:
      a vector of length N containing the sums of all matrix rows
    • sumColumns

      public static float[] sumColumns(float[][] A)
      Calculates the sums of all matrix columns and returns them as a vector with one element per column.
      Parameters:
      A - a matrix of size (M,N)
      Returns:
      a vector of length N containing the sums of all matrix rows
    • idxMin

      public static int idxMin(double[] a)
      Returns the index of the smallest element in the specified vector. If the smallest value is not unique, the lowest index is returned. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the index of the smallest value
    • idxMin

      public static int idxMin(float[] a)
      Returns the index of the smallest element in the specified vector. If the smallest value is not unique, the lowest index is returned. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the index of the smallest value
    • idxMax

      public static int idxMax(double[] a)
      Returns the index of the largest element in the specified vector. If the largest value is not unique, the lowest index is returned. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the index of the largest value
    • idxMax

      public static int idxMax(float[] a)
      Returns the index of the largest element in the specified vector. If the largest value is not unique, the lowest index is returned. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the index of the largest value
    • min

      public static float min(float[] a)
      Returns the smallest value in the specified vector. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the largest value
    • min

      public static double min(double[] a)
      Returns the smallest value in the specified vector. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the largest value
    • max

      public static double max(double[] a)
      Returns the largest value in the specified double[] vector. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the largest value
    • max

      public static float max(float[] a)
      Returns the largest value in the specified float[] vector. An exception is thrown if the vector has zero length.
      Parameters:
      a - a vector
      Returns:
      the largest value
    • max

      public static double max(double[][] A)
      Returns the largest value in the specified double[][] matrix.
      Parameters:
      A - a matrix
      Returns:
      the largest matrix value
    • max

      public static float max(float[][] A)
      Returns the largest value in the specified float[][] matrix.
      Parameters:
      A - a matrix
      Returns:
      the largest matrix value
    • join

      public static float[] join(float[]... as)
      Joins (concatenates) a sequence of vectors into a single vector.
      Parameters:
      as - a sequence of vectors (at least one vector)
      Returns:
      a vector containing all elements of the input vectors
    • join

      public static double[] join(double[]... as)
      Joins (concatenates) a sequence of vectors into a single vector.
      Parameters:
      as - a sequence of vectors (at least one vector)
      Returns:
      a vector containing all elements of the input vectors
    • lerp

      public static float[] lerp(float[] a, float[] b, float t)
      Performs linear interpolation between two vectors a and b, which must have the same length. Returns a new vector c = a + t * (b - a).
      Parameters:
      a - first vector (to be interpolated from)
      b - second vector (to be interpolated to)
      t - interpolation coefficient, expected to be in [0,1]
      Returns:
      the interpolated vector
    • lerp

      public static double[] lerp(double[] a, double[] b, double t)
      Performs linear interpolation between two vectors a and b, which must have the same length. Returns a new vector c = a + t * (b - a).
      Parameters:
      a - first vector (to be interpolated from)
      b - second vector (to be interpolated to)
      t - interpolation coefficient, expected to be in [0,1]
      Returns:
      the interpolated vector
    • toHomogeneous

      public static double[] toHomogeneous(double[] ac)
      Converts a Cartesian vector to an equivalent homogeneous vector by attaching an additional 1-element. The resulting homogeneous vector is one element longer than the specified Cartesian vector. See also toCartesian(double[]).
      Parameters:
      ac - a Cartesian vector
      Returns:
      an equivalent homogeneous vector
    • toCartesian

      public static double[] toCartesian(double[] ah) throws DivideByZeroException
      Converts a homogeneous vector to its equivalent Cartesian vector, which is one element shorter. See also toHomogeneous(double[]).
      Parameters:
      ah - a homogeneous vector
      Returns:
      the equivalent Cartesian vector
      Throws:
      DivideByZeroException - if the last vector element is zero
    • determinant

      public static double determinant(RealMatrix A)
      Calculates and returns the determinant of the given RealMatrix. Throws an exception if the matrix is non-square.
      Parameters:
      A - a square matrix
      Returns:
      the determinant
    • determinant

      public static double determinant(double[][] A)
      Calculates and returns the determinant of the given double[][] matrix. Throws an exception if the matrix is non-square.
      Parameters:
      A - a square matrix
      Returns:
      the determinant
    • determinant2x2

      public static double determinant2x2(double[][] A)
      Calculates and returns the determinant of the given 2x2 double[][] matrix. This method is hard-coded for the specific matrix size for better performance than the general method (determinant(double[][])). Throws an exception if the matrix is not 2x2.
      Parameters:
      A - a 2x2 matrix
      Returns:
      the determinant
    • determinant2x2

      public static float determinant2x2(float[][] A)
      Calculates and returns the determinant of the given 2x2 float[][] matrix. Throws an exception if the matrix is not 2x2.
      Parameters:
      A - a 2x2 matrix
      Returns:
      the determinant
    • determinant3x3

      public static double determinant3x3(double[][] A)
      Calculates and returns the determinant of the given 3x3 double[][] matrix. This method is hard-coded for the specific matrix size for better performance than the general method (determinant(double[][])). Throws an exception if the matrix is not 3x3.
      Parameters:
      A - a 3x3 matrix
      Returns:
      the determinant
    • determinant3x3

      public static float determinant3x3(float[][] A)
      Calculates and returns the determinant of the given 3x3 float[][] matrix. Throws an exception if the matrix is not 3x3.
      Parameters:
      A - a 3x3 matrix
      Returns:
      the determinant
    • trace

      public static double trace(double[][] A)
      Calculates and returns the trace of the given double[][] matrix. Throws an exception if the matrix is non-square.
      Parameters:
      A - a square matrix
      Returns:
      the trace
    • trace

      public static float trace(float[][] A)
      Calculates and returns the trace of the given float[][] matrix. Throws an exception if the matrix is non-square.
      Parameters:
      A - a square matrix
      Returns:
      the trace
    • transpose

      public static double[][] transpose(double[][] A)
      Returns the transpose of the given double[][] matrix. The original matrix is not modified.
      Parameters:
      A - a matrix
      Returns:
      the transpose of the matrix
    • transpose

      public static float[][] transpose(float[][] A)
      Returns the transpose of the given float[][] matrix. The original matrix is not modified.
      Parameters:
      A - a matrix
      Returns:
      the transpose of the matrix
    • isZero

      public static boolean isZero(double[] a, double tolerance)
      Checks if all elements of the specified double[] vector are zero using the specified tolerance value.
      Parameters:
      a - a vector
      tolerance - the tolerance value
      Returns:
      true if all vector elements are smaller than the tolerance
    • isZero

      public static boolean isZero(double[] a)
      Checks if all elements of the specified double[] vector are zero using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      a - a vector
      Returns:
      true if all vector elements are smaller than the tolerance
    • isZero

      public static boolean isZero(float[] a, float tolerance)
      Checks if all elements of the specified float[] vector are zero using the specified tolerance value.
      Parameters:
      a - a vector
      tolerance - the tolerance value
      Returns:
      true if all vector elements are smaller than the tolerance
    • isZero

      public static boolean isZero(float[] a)
      Checks if all elements of the specified float[] vector are zero using the default tolerance value (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      a - a vector
      Returns:
      true if all vector elements are smaller than the tolerance
    • isZero

      public static boolean isZero(double[][] A, double tolerance)
      Checks if all elements of the specified double[][] matrix are zero using the specified tolerance value.
      Parameters:
      A - a matrix
      tolerance - the tolerance value
      Returns:
      true if all matrix elements are smaller than the tolerance
    • isZero

      public static boolean isZero(double[][] A)
      Checks if all elements of the specified double[][] matrix are zero using default tolerance value (Arithmetic.EPSILON_DOUBLE).
      Parameters:
      A - a matrix
      Returns:
      true if all matrix elements are smaller than the tolerance
    • sort

      public static double[] sort(double[] a)
      Returns a sorted copy of the given double[] vector. Elements are sorted by increasing value (smallest first).
      Parameters:
      a - a double[] vector
      Returns:
      a sorted copy of the vector
    • sort

      public static float[] sort(float[] a)
      Returns a sorted copy of the given float[] vector. Elements are sorted by increasing value (smallest first).
      Parameters:
      a - a float[] vector
      Returns:
      a sorted copy of the vector
    • inverse

      public static double[][] inverse(double[][] A) throws Matrix.NonsquareMatrixException
      Calculates and returns the inverse of the given matrix, which must be square. Exceptions are thrown if the supplied matrix is not square or ill-conditioned (singular).
      Parameters:
      A - a square matrix
      Returns:
      the inverse matrix
      Throws:
      Matrix.NonsquareMatrixException - if the supplied matrix is not square
    • inverse

      public static float[][] inverse(float[][] A) throws Matrix.NonsquareMatrixException
      Calculates and returns the inverse of the given matrix, which must be square. Exceptions are thrown if the supplied matrix is not square or ill-conditioned (singular).
      Parameters:
      A - a square matrix
      Returns:
      the inverse matrix
      Throws:
      Matrix.NonsquareMatrixException - if the supplied matrix is not square
    • solve

      public static double[] solve(double[][] A, double[] b)
      Finds the exact solution x for the linear system of equations A * x = b. Returns the solution vector x or null if the supplied matrix is ill-conditioned (i.e., singular). Exceptions are thrown if A is not square or dimensions are incompatible. Calls solve(RealMatrix, RealVector).
      Parameters:
      A - a square matrix of size n x n
      b - a vector of length n
      Returns:
      the solution vector (x) of length n or null if no solution possible
    • solve

      public static RealVector solve(RealMatrix A, RealVector b)
      Finds the exact solution x for the linear system of equations A * x = b. Returns the solution vector x or null if the supplied matrix is ill-conditioned (i.e., singular). Exceptions are thrown if A is not square or dimensions are incompatible. Uses LUDecomposition from the Apache Commons Math library.
      Parameters:
      A - a square matrix of size n x n
      b - a vector of length n
      Returns:
      the solution vector (x) of length n or null if no solution possible
    • toString

      public static String toString(double[] a)
      Returns a string representation of the specified vector.
      Parameters:
      a - a vector
      Returns:
      the string representation
    • toString

      public static String toString(float[] a)
      Returns a string representation of the specified vector.
      Parameters:
      a - a vector
      Returns:
      the string representation
    • toString

      public static String toString(RealVector a)
      Returns a string representation of the specified vector.
      Parameters:
      a - a vector
      Returns:
      the string representation
    • toString

      public static String toString(double[][] A)
      Returns a string representation of the specified matrix.
      Parameters:
      A - a matrix
      Returns:
      the string representation
    • toString

      public static String toString(float[][] A)
      Returns a string representation of the specified matrix.
      Parameters:
      A - a matrix
      Returns:
      the string representation
    • toString

      public static String toString(long[][] A)
      Returns a string representation of the specified matrix with long elements.
      Parameters:
      A - a matrix
      Returns:
      the string representation
    • toString

      public static String toString(RealMatrix A)
      Returns a string representation of the specified matrix.
      Parameters:
      A - a matrix
      Returns:
      the string representation
    • printToStream

      public static void printToStream(double[] a, PrintStream strm)
      Outputs a string representation of the given vector to the specified output stream.
      Parameters:
      a - the vector
      strm - the output stream
      See Also:
    • printToStream

      public static void printToStream(double[][] A, PrintStream strm)
      Outputs a string representation of the given matrix to the specified output stream.
      Parameters:
      A - the matrix
      strm - the output stream
      See Also:
    • printToStream

      public static void printToStream(float[] a, PrintStream strm)
      Outputs a string representation of the given vector to the specified output stream.
      Parameters:
      a - the vector
      strm - the output stream
      See Also:
    • printToStream

      public static void printToStream(float[][] A, PrintStream strm)
      Outputs a string representation of the given matrix to the specified output stream.
      Parameters:
      A - the matrix
      strm - the output stream
      See Also:
    • printToStream

      public static void printToStream(long[][] A, PrintStream strm)