Class Matrix

All Implemented Interfaces:
Serializable, TensorOverField<Matrix,Matrix,double[],Double>, MatrixMixin<Matrix,Matrix,Vector,Double>, TensorOverRing<Matrix,Matrix,double[],Double>, TensorOverSemiring<Matrix,Matrix,double[],Double>

public class Matrix extends AbstractDenseDoubleTensor<Matrix> implements MatrixMixin<Matrix,Matrix,Vector,Double>

Instances of this class represents a complex dense matrix backed by a double[] array. The Matrix class provides functionality for real dense matrix operations, supporting mutable data with a fixed shape. This class extends AbstractDenseDoubleTensor and offers additional methods optimized for complex arithmetic and matrix computations.

A Matrix is essentially equivalent to a rank-2 tensor but includes extended functionality and may offer improved performance for certain operations compared to general rank-n tensors.

Key Features:

  • Support for standard matrix operations like addition, subtraction, multiplication, and exponentiation.
  • Conversion methods to other matrix representations, such as COO (Coordinate) and CSR (Compressed Sparse Row) formats.
  • Utility methods for checking properties like being orthogonal, symmetric, etc.

Example Usage:


 // Constructing a complex matrix from a 2D array of complex numbers
 double[][] data = {
     { 1, 2, 3 },
     { 4, 5, 6 },
     { 7, 8, 9,}};
 Matrix matrix = new Matrix(data);

 // Performing matrix multiplication.
 Matrix result = matrix.mult(matrix);

 // Performing matrix transpose.
 Matrix transpose = matrix.T();

 // Performing matrix transpose.
 Matrix conjugateTranspose = matrix.T();

 // Checking if the matrix is orthogonal.
 boolean isUnitary = matrix.isOrthogonal();
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
    The number of columns in this matrix.
    final int
    The number of rows in this matrix.

    Fields inherited from class org.flag4j.arrays.backend.AbstractTensor

    data, rank, shape
  • Constructor Summary

    Constructors
    Constructor
    Description
    Matrix(double[][] data)
    Creates a real dense matrix whose data are specified by a double array.
    Matrix(int size)
    Constructs a square real dense matrix of a specified size.
    Matrix(int[][] data)
    Creates a real dense matrix whose data are specified by a double array.
    Matrix(int size, double value)
    Creates a square real dense matrix with a specified fill value.
    Matrix(int rows, int cols)
    Creates a real dense matrix of a specified shape filled with zeros.
    Matrix(int rows, int cols, double value)
    Creates a real dense matrix with a specified shape and fills the matrix with the specified value.
    Matrix(int numRows, int numCols, double... data)
    Constructs a matrix with specified shape and data.
    Matrix(Double[][] data)
    Creates a real dense matrix whose data are specified by a double array.
    Matrix(Integer[][] data)
    Creates a real dense matrix whose data are specified by a double array.
    Creates a real dense matrix which is a copy of a specified matrix.
    Matrix(Shape shape)
    Creates a real dense matrix with specified shape filled with zeros.
    Matrix(Shape shape, double value)
    Creates a real dense matrix with specified shape filled with a specific value.
    Matrix(Shape shape, double... entries)
    Creates a tensor with the specified data and shape.
  • Method Summary

    Modifier and Type
    Method
    Description
    <R> R
    accept(MatrixVisitor<R> visitor)
    Accepts a visitor that implements the MatrixVisitor interface.
    Adds a complex-valued scalar to each entry of this matrix.
    Sums this matrix with a complex dense matrix.
    Sums this matrix with a real sparse COO matrix.
    Sums this matrix with a real sparse COO matrix.
    Sums this matrix with a real sparse CSR matrix.
    Sums this matrix with a real sparse CSR matrix.
    Stacks matrices along rows.
    Augments a vector to this matrix.
    int
    Gets the length of the data array which backs this matrix.
    det()
    Computes the determinant of a square matrix.
    static Matrix
    diag(double... data)
    Constructs a diagonal matrix from an array specifying the diagonal elements of the matrix.
    static Matrix
    diag(Vector vec)
    Constructs a diagonal matrix from a vector specifying the diagonal elements of the matrix.
    Computes the scalar division of this matrix with a complex number.
    Computes the element-wise division between two tensors.
    Computes the element-wise product of two matrices.
    Computes the element-wise product of two matrices.
    Computes the element-wise product of two matrices.
    boolean
    equals(Object object)
    Checks if an object is equal to this matrix object.
    Computes the Frobenius inner product of two matrices.
    Flattens this matrix to a row vector.
    flatten(int axis)
    Flattens this matrix along the specified axis.
    get(int row, int col)
    Gets the element of this matrix at this specified row and col.
    getCol(int colIdx, int rowStart, int rowEnd)
    Gets a specified column of this matrix between rowStart (inclusive) and rowEnd (exclusive).
    Extracts the diagonal elements of this matrix and returns them as a vector.
    getDiag(int diagOffset)
    Gets the elements of this matrix along the specified diagonal.
    getRow(int rowIdx, int colStart, int colEnd)
    Gets a specified row of this matrix between colStart (inclusive) and colEnd (exclusive).
    getSlice(int rowStart, int rowEnd, int colStart, int colEnd)
    Gets a specified slice of this matrix.
    getTriL(int diagOffset)
    Extracts the lower-triangular portion of this matrix with a specified diagonal offset.
    getTriU(int diagOffset)
    Extracts the upper-triangular portion of this matrix with a specified diagonal offset.
    H()
    Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values.
    int
     
    static Matrix
    I(int size)
    Constructs an identity matrix of the specified size.
    static Matrix
    I(int numRows, int numCols)
    Constructs an identity-like matrix of the specified shape.
    static Matrix
    I(Shape shape)
    Constructs an identity-like matrix of the specified shape.
    boolean
    Checks that this matrix is close to the identity matrix.
    boolean
    Checks if a matrix is Hermitian.
    boolean
    isI()
    Checks if this matrix is the identity matrix.
    boolean
    Checks if this matrix is orthogonal.
    boolean
    Checks if a matrix is symmetric.
    boolean
    Checks if this matrix is lower triangular.
    boolean
    Checks if this matrix is upper triangular.
    makeLikeTensor(Shape shape, double[] data)
    Constructs a tensor of the same type as this tensor with the given the shape and data.
    int
    Computes the rank of this matrix (i.e. the number of linearly independent rows/columns in this matrix).
    Computes the scalar multiplication of this matrix with a complex number.
    Computes the matrix multiplication between this matrix and a complex dense matrix.
    Computes the matrix-vector product of this matrix and a dense complex vector.
    Computes the matrix multiplication between two matrices.
    Computes matrix-vector multiplication.
    Computes the matrix multiplication between this matrix and a complex sparse COO matrix.
    Computes the matrix-vector product of this matrix and a complex sparse vector.
    Computes the matrix multiplication between this matrix and a real sparse COO matrix.
    Computes the matrix-vector product of this matrix and a real sparse vector.
    Computes the matrix multiplication between this matrix and a complex sparse CSR matrix.
    Computes the matrix multiplication between this matrix and a real sparse CSR matrix.
    Multiplies this matrix with the transpose of the b tensor as if by this.mult(b.T()).
    int
    Gets the number of columns in this matrix.
    int
    Gets the number of rows in this matrix.
    pow(int n)
    Computes the matrix multiplication of this matrix with itself n times.
    removeCol(int colIndex)
    Removes a specified column from this matrix.
    removeCols(int... colIndices)
    Removes a specified set of columns from this matrix.
    removeRow(int rowIndex)
    Removes a specified row from this matrix.
    removeRows(int... rowIndices)
    Removes a specified set of rows from this matrix.
    set(Double value, int row, int col)
    Sets an index of this matrix to the specified value.
    setCol(double[] values, int colIndex)
    Sets a column of this matrix at the given index to the specified values.
    setCol(Double[] values, int colIndex)
    Sets a column of this matrix at the given index to the specified values.
    setCol(Vector values, int colIndex)
    Sets a column of this matrix at the given index to the specified values.
    setRow(double[] values, int rowIndex)
    Sets a row of this matrix at the given index to the specified values.
    setRow(Double[] values, int rowIndex)
    Sets a row of this matrix at the given index to the specified values.
    setRow(Vector values, int rowIndex)
    Sets a row of this matrix at the given index to the specified values.
    setSlice(double[][] values, int rowStart, int colStart)
    Sets a slice of this matrix to the specified values.
    setSlice(Double[][] values, int rowStart, int colStart)
    Sets a slice of this matrix to the specified values.
    setSlice(Matrix values, int rowStart, int colStart)
    Sets a slice of this matrix to the specified values.
    setSliceCopy(Matrix values, int rowStart, int colStart)
    Creates a copy of this matrix and sets a slice of the copy to the specified values.
    setValues(double[][] values)
    Sets the value of this matrix using a 2D array.
    setValues(int[][] values)
    Sets the value of this matrix using a 2D array.
    setValues(Double[][] values)
    Sets the value of this matrix using a 2D array.
    setValues(Integer[][] values)
    Sets the value of this matrix using a 2D array.
    setValues(Matrix values)
    Sets the value of this matrix using another matrix.
    Computes the element-wise square root of a tensor.
    Stacks matrices along columns.
    Subtracts a complex-valued scalar from each entry of this matrix.
    Computes the difference of this matrix with a complex dense matrix.
    Computes the difference of this matrix with a real sparse COO matrix.
    Computes the difference of this matrix with a real sparse COO matrix.
    Computes the difference of this matrix with a real sparse CSR matrix.
    Computes the difference of this matrix with a real sparse CSR matrix.
    swapCols(int colIndex1, int colIndex2)
    Swaps specified columns in the matrix.
    swapRows(int rowIndex1, int rowIndex2)
    Swaps specified rows in the matrix.
    T()
    Computes the transpose of a tensor by exchanging the first and last axes of this tensor.
    T(int... axes)
    Computes the transpose of this tensor.
    T(int axis1, int axis2)
    Computes the transpose of a tensor by exchanging axis1 and axis2.
    Converts this matrix to an equivalent complex matrix.
    Converts this dense tensor to an equivalent sparse COO tensor.
    Converts this dense matrix to sparse CSR matrix.
    Generates a human-readable string representing this matrix.
    Converts this matrix to an equivalent Tensor.
    Converts this matrix to an equivalent vector.
    tr()
    Computes the trace of this matrix.

    Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor

    add, addEq, allClose, allClose, argmax, argmaxAbs, argmin, argminAbs, div, divEq, elemMult, elemMultEq, get, reshape, round, round, roundToZero, set, sub, subEq, tensorDot, tensorTr

    Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor

    abs, add, add, addEq, addEq, conj, copy, div, div, divEq, divEq, H, H, isFinite, isInfinite, isNaN, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, prod, recip, roundToZero, sqrt, sub, sub, subEq, subEq, sum

    Methods inherited from class org.flag4j.arrays.backend.AbstractTensor

    getData, getRank, getShape, reshape, sameShape, totalEntries

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.flag4j.arrays.backend.MatrixMixin

    add, copy, div, elemMult, getCol, getRow, getShape, getTriL, getTriU, isDiag, isSquare, isTri, isVector, stack, sub, trace, vectorType

    Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring

    getData, getRank, getShape, tensorDot, tensorDot, tensorDot, tensorTr
  • Field Details

    • numRows

      public final int numRows
      The number of rows in this matrix.
    • numCols

      public final int numCols
      The number of columns in this matrix.
  • Constructor Details

    • Matrix

      public Matrix(Shape shape, double... entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor. If this tensor is dense, this specifies all data within the tensor. If this tensor is sparse, this specifies only the non-zero data of the tensor.
    • Matrix

      public Matrix(int size)
      Constructs a square real dense matrix of a specified size. The data of the matrix will default to zero.
      Parameters:
      size - Size of the square matrix.
      Throws:
      IllegalArgumentException - if size negative.
    • Matrix

      public Matrix(int size, double value)
      Creates a square real dense matrix with a specified fill value.
      Parameters:
      size - Size of the square matrix.
      value - Value to fill this matrix with.
      Throws:
      IllegalArgumentException - if size negative.
    • Matrix

      public Matrix(int rows, int cols)
      Creates a real dense matrix of a specified shape filled with zeros.
      Parameters:
      rows - The number of rows in the matrix.
      cols - The number of columns in the matrix.
      Throws:
      IllegalArgumentException - if either m or n is negative.
    • Matrix

      public Matrix(int rows, int cols, double value)
      Creates a real dense matrix with a specified shape and fills the matrix with the specified value.
      Parameters:
      rows - Number of rows in the matrix.
      cols - Number of columns in the matrix.
      value - Value to fill this matrix with.
      Throws:
      IllegalArgumentException - if either m or n is negative.
    • Matrix

      public Matrix(Double[][] data)
      Creates a real dense matrix whose data are specified by a double array.
      Parameters:
      data - Entries of the real dense matrix.
    • Matrix

      public Matrix(Integer[][] data)
      Creates a real dense matrix whose data are specified by a double array.
      Parameters:
      data - Entries of the real dense matrix.
    • Matrix

      public Matrix(double[][] data)
      Creates a real dense matrix whose data are specified by a double array.
      Parameters:
      data - Entries of the real dense matrix.
    • Matrix

      public Matrix(int[][] data)
      Creates a real dense matrix whose data are specified by a double array.
      Parameters:
      data - Entries of the real dense matrix.
    • Matrix

      public Matrix(Matrix A)
      Creates a real dense matrix which is a copy of a specified matrix.
      Parameters:
      A - The matrix defining the data for this matrix.
    • Matrix

      public Matrix(Shape shape)
      Creates a real dense matrix with specified shape filled with zeros.
      Parameters:
      shape - Shape of matrix.
      Throws:
      IllegalArgumentException - If the shape is not of rank 2.
    • Matrix

      public Matrix(Shape shape, double value)
      Creates a real dense matrix with specified shape filled with a specific value.
      Parameters:
      shape - Shape of matrix.
      value - Value to fill matrix with.
      Throws:
      IllegalArgumentException - If the shape is not of rank 2.
    • Matrix

      public Matrix(int numRows, int numCols, double... data)
      Constructs a matrix with specified shape and data.
      Parameters:
      numRows - Number of rows in this matrix.
      numCols - Number of columns in this matrix.
      data - Entries of the matrix.
  • Method Details

    • diag

      public static Matrix diag(double... data)
      Constructs a diagonal matrix from an array specifying the diagonal elements of the matrix.
      Parameters:
      data - Diagonal elements of the matrix. All other values will be zero.
      Returns:
      A diagonal matrix whose diagonal elements are equal to data.
      See Also:
    • diag

      public static Matrix diag(Vector vec)
      Constructs a diagonal matrix from a vector specifying the diagonal elements of the matrix.
      Parameters:
      vec - Diagonal elements of the matrix. All other values will be zero.
      Returns:
      A diagonal matrix whose diagonal elements are equal to the entries of vec.
      See Also:
    • makeLikeTensor

      public Matrix makeLikeTensor(Shape shape, double[] data)
      Constructs a tensor of the same type as this tensor with the given the shape and data.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<Matrix,Matrix,double[],Double>
      Specified by:
      makeLikeTensor in class AbstractTensor<Matrix,double[],Double>
      Parameters:
      shape - Shape of the tensor to construct.
      data - Entries of the tensor to construct.
      Returns:
      A tensor of the same type as this tensor with the given the shape and data.
    • dataLength

      public int dataLength()
      Gets the length of the data array which backs this matrix.
      Specified by:
      dataLength in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      The length of the data array which backs this matrix.
    • flatten

      public Matrix flatten()
      Flattens this matrix to a row vector.
      Specified by:
      flatten in class AbstractTensor<Matrix,double[],Double>
      Returns:
      The flattened matrix.
      See Also:
    • flatten

      public Matrix flatten(int axis)
      Flattens this matrix along the specified axis.
      Specified by:
      flatten in class AbstractTensor<Matrix,double[],Double>
      Parameters:
      axis - Axis along which to flatten tensor.
      Returns:
      A new matrix containing the same data as this matrix but flattened along the specified axis.
      • If axis == 0 a matrix with the shape (this.numRows*this.numCols, 1) is returned.
      • If axis == 1 a matrix with the shape (1, this.numRows*this.numCols) is returned.
      Throws:
      ArrayIndexOutOfBoundsException - If the axis is negative or larger than this.{@link #getRank()}-1.
      See Also:
    • I

      public static Matrix I(int size)
      Constructs an identity matrix of the specified size.
      Parameters:
      size - Size of the identity matrix.
      Returns:
      An identity matrix of specified size.
      Throws:
      IllegalArgumentException - If the specified size is less than 1.
      See Also:
    • I

      public static Matrix I(int numRows, int numCols)
      Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.
      Parameters:
      numRows - Number of rows in the identity-like matrix.
      numCols - Number of columns in the identity-like matrix.
      Returns:
      An identity matrix of specified shape.
      Throws:
      IllegalArgumentException - If the specified number of rows or columns is less than 1.
      See Also:
    • I

      public static Matrix I(Shape shape)
      Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.
      Parameters:
      shape - Shape of the identity-like matrix.
      Returns:
      An identity matrix of specified size.
      Throws:
      IllegalArgumentException - If the specified shape is not rank 2.
      See Also:
    • numRows

      public int numRows()
      Gets the number of rows in this matrix.
      Specified by:
      numRows in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      The number of rows in this matrix.
    • numCols

      public int numCols()
      Gets the number of columns in this matrix.
      Specified by:
      numCols in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      The number of columns in this matrix.
    • get

      public Double get(int row, int col)
      Gets the element of this matrix at this specified row and col.
      Specified by:
      get in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      row - Row index of the item to get from this matrix.
      col - Column index of the item to get from this matrix.
      Returns:
      The element of this matrix at the specified index.
    • tr

      public Double tr()

      Computes the trace of this matrix. That is, the sum of elements along the principle diagonal of this matrix.

      Same as MatrixMixin.trace().

      Specified by:
      tr in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      The trace of this matrix.
      Throws:
      IllegalArgumentException - If this matrix is not square.
    • isTriU

      public boolean isTriU()
      Checks if this matrix is upper triangular.
      Specified by:
      isTriU in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true is this matrix is upper triangular; false otherwise.
      See Also:
    • isTriL

      public boolean isTriL()
      Checks if this matrix is lower triangular.
      Specified by:
      isTriL in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true is this matrix is lower triangular; false otherwise.
      See Also:
    • isI

      public boolean isI()
      Checks if this matrix is the identity matrix. That is, checks if this matrix is square and contains only ones along the principle diagonal and zeros everywhere else.
      Specified by:
      isI in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true if this matrix is the identity matrix; false otherwise.
    • isCloseToI

      public boolean isCloseToI()
      Checks that this matrix is close to the identity matrix.
      Returns:
      True if this matrix is approximately the identity matrix.
      See Also:
    • det

      public Double det()
      Computes the determinant of a square matrix.
      Returns:
      The determinant of this matrix.
      Throws:
      LinearAlgebraException - If this matrix is not square.
    • matrixRank

      public int matrixRank()

      Computes the rank of this matrix (i.e. the number of linearly independent rows/columns in this matrix).

      This is computed as the number of singular values greater than tol where:

      double tol = 2.0*Math.max(rows, cols)*Flag4jConstants.EPS_F64*Math.min(this.numRows, this.numCols);

      Note the "matrix rank" is NOT related to the "tensor rank" which is number of indices needed to uniquely specify an entry in the tensor.

      Returns:
      The matrix rank of this matrix.
    • mult

      public Matrix mult(Matrix b)
      Computes the matrix multiplication between two matrices.
      Specified by:
      mult in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - Second matrix in the matrix multiplication.
      Returns:
      The result of matrix multiplying this matrix with matrix B.
      Throws:
      LinearAlgebraException - If the number of columns in this matrix do not equal the number of rows in matrix B.
    • multTranspose

      public Matrix multTranspose(Matrix b)
      Multiplies this matrix with the transpose of the b tensor as if by this.mult(b.T()). For large matrices, this method may be significantly faster than directly computing the transpose followed by the multiplication as this.mult(b.T()).
      Specified by:
      multTranspose in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - The second matrix in the multiplication and the matrix to transpose/
      Returns:
      The result of multiplying this matrix with the transpose of b.
    • fib

      public Double fib(Matrix b)
      Computes the Frobenius inner product of two matrices.
      Specified by:
      fib in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - Second matrix in the Frobenius inner product
      Returns:
      The Frobenius inner product of this matrix and matrix B.
      Throws:
      IllegalArgumentException - If this matrix and B have different shapes.
    • stack

      public Matrix stack(Matrix b)
      Stacks matrices along columns.
      Specified by:
      stack in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - Matrix to stack to this matrix.
      Returns:
      The result of stacking this matrix on top of the matrix b.
      Throws:
      IllegalArgumentException - If this matrix and matrix b have a different number of columns.
      See Also:
    • augment

      public Matrix augment(Matrix b)
      Stacks matrices along rows.
      Specified by:
      augment in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - Matrix to stack to this matrix.
      Returns:
      The result of stacking b to the right of this matrix.
      Throws:
      IllegalArgumentException - If this matrix and matrix b have a different number of rows.
      See Also:
    • augment

      public Matrix augment(Vector b)
      Augments a vector to this matrix.
      Specified by:
      augment in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - The vector to augment to this matrix.
      Returns:
      The result of augmenting b to this matrix.
    • swapRows

      public Matrix swapRows(int rowIndex1, int rowIndex2)
      Swaps specified rows in the matrix. This is done in place.
      Specified by:
      swapRows in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      rowIndex1 - Index of the first row to swap.
      rowIndex2 - Index of the second row to swap.
      Returns:
      A reference to this matrix.
      Throws:
      ArrayIndexOutOfBoundsException - If either index is outside the matrix bounds.
    • swapCols

      public Matrix swapCols(int colIndex1, int colIndex2)
      Swaps specified columns in the matrix. This is done in place.
      Specified by:
      swapCols in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      colIndex1 - Index of the first column to swap.
      colIndex2 - Index of the second column to swap.
      Returns:
      A reference to this matrix.
      Throws:
      ArrayIndexOutOfBoundsException - If either index is outside the matrix bounds.
    • isSymmetric

      public boolean isSymmetric()
      Checks if a matrix is symmetric. That is, if the matrix is square and equal to its transpose.
      Specified by:
      isSymmetric in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true if this matrix is symmetric; false otherwise.
    • isHermitian

      public boolean isHermitian()
      Checks if a matrix is Hermitian. That is, if the matrix is square and equal to its conjugate transpose.
      Specified by:
      isHermitian in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true if this matrix is Hermitian; false otherwise.
    • isOrthogonal

      public boolean isOrthogonal()
      Checks if this matrix is orthogonal. That is, if the inverse of this matrix is equal to its transpose.
      Specified by:
      isOrthogonal in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      true if this matrix it is orthogonal; false otherwise.
    • removeRow

      public Matrix removeRow(int rowIndex)
      Removes a specified row from this matrix.
      Specified by:
      removeRow in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      rowIndex - Index of the row to remove from this matrix.
      Returns:
      A copy of this matrix with the specified column removed.
    • removeRows

      public Matrix removeRows(int... rowIndices)
      Removes a specified set of rows from this matrix.
      Specified by:
      removeRows in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      rowIndices - The indices of the rows to remove from this matrix. Assumed to contain unique values.
      Returns:
      a copy of this matrix with the specified column removed.
    • removeCol

      public Matrix removeCol(int colIndex)
      Removes a specified column from this matrix.
      Specified by:
      removeCol in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      colIndex - Index of the column to remove from this matrix.
      Returns:
      a copy of this matrix with the specified column removed.
    • removeCols

      public Matrix removeCols(int... colIndices)
      Removes a specified set of columns from this matrix.
      Specified by:
      removeCols in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      colIndices - Indices of the columns to remove from this matrix. Assumed to contain unique values.
      Returns:
      a copy of this matrix with the specified column removed.
    • setSliceCopy

      public Matrix setSliceCopy(Matrix values, int rowStart, int colStart)
      Creates a copy of this matrix and sets a slice of the copy to the specified values. The rowStart and colStart parameters specify the upper left index location of the slice to set.
      Specified by:
      setSliceCopy in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      values - New values for the specified slice.
      rowStart - Starting row index for the slice (inclusive).
      colStart - Starting column index for the slice (inclusive).
      Returns:
      A copy of this matrix with the given slice set to the specified values.
      Throws:
      IndexOutOfBoundsException - If rowStart or colStart are not within the matrix.
      IllegalArgumentException - If the values slice, with upper left corner at the specified location, does not fit completely within this matrix.
    • setSlice

      public Matrix setSlice(Matrix values, int rowStart, int colStart)
      Sets a slice of this matrix to the specified values. The rowStart and colStart parameters specify the upper left index location of the slice to set within this matrix.
      Parameters:
      values - New values for the specified slice.
      rowStart - Starting row index for the slice (inclusive).
      colStart - Starting column index for the slice (inclusive).
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If rowStart or colStart are not within the matrix.
      IllegalArgumentException - If the values slice, with upper left corner at the specified location, does not fit completely within this matrix.
    • setSlice

      public Matrix setSlice(double[][] values, int rowStart, int colStart)
      Sets a slice of this matrix to the specified values. The rowStart and colStart parameters specify the upper left index location of the slice to set within this matrix.
      Parameters:
      values - New values for the specified slice.
      rowStart - Starting row index for the slice (inclusive).
      colStart - Starting column index for the slice (inclusive).
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If rowStart or colStart are not within the matrix.
      IllegalArgumentException - If the values slice, with upper left corner at the specified location, does not fit completely within this matrix.
    • setSlice

      public Matrix setSlice(Double[][] values, int rowStart, int colStart)
      Sets a slice of this matrix to the specified values. The rowStart and colStart parameters specify the upper left index location of the slice to set within this matrix.
      Parameters:
      values - New values for the specified slice.
      rowStart - Starting row index for the slice (inclusive).
      colStart - Starting column index for the slice (inclusive).
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If rowStart or colStart are not within the matrix.
      IllegalArgumentException - If the values slice, with upper left corner at the specified location, does not fit completely within this matrix.
    • getSlice

      public Matrix getSlice(int rowStart, int rowEnd, int colStart, int colEnd)
      Gets a specified slice of this matrix.
      Specified by:
      getSlice in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      rowStart - Starting row index of slice (inclusive).
      rowEnd - Ending row index of slice (exclusive).
      colStart - Starting column index of slice (inclusive).
      colEnd - Ending row index of slice (exclusive).
      Returns:
      The specified slice of this matrix. This is a completely new matrix and NOT a view into the matrix.
      Throws:
      ArrayIndexOutOfBoundsException - If any of the indices are out of bounds of this matrix.
      IllegalArgumentException - If rowEnd is not greater than rowStart or if colEnd is not greater than colStart.
    • set

      public Matrix set(Double value, int row, int col)
      Sets an index of this matrix to the specified value.
      Specified by:
      set in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      value - Value to set.
      row - Row index to set.
      col - Column index to set.
      Returns:
      A reference to this matrix.
    • setValues

      public Matrix setValues(Double[][] values)
      Sets the value of this matrix using a 2D array.
      Parameters:
      values - New values of the matrix.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different shape then this matrix.
    • setValues

      public Matrix setValues(double[][] values)
      Sets the value of this matrix using a 2D array.
      Parameters:
      values - New values of the matrix.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different shape then this matrix.
    • setValues

      public Matrix setValues(Integer[][] values)
      Sets the value of this matrix using a 2D array.
      Parameters:
      values - New values of the matrix.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different shape then this matrix.
    • setValues

      public Matrix setValues(int[][] values)
      Sets the value of this matrix using a 2D array.
      Parameters:
      values - New values of the matrix.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different shape then this matrix.
    • setValues

      public Matrix setValues(Matrix values)
      Sets the value of this matrix using another matrix.
      Parameters:
      values - New values of the matrix.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values matrix has a different shape then this matrix.
    • setCol

      public Matrix setCol(Vector values, int colIndex)
      Sets a column of this matrix at the given index to the specified values.
      Specified by:
      setCol in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      values - New values for the column.
      colIndex - The index of the column which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values vector has a different length than the number of rows of this matrix.
    • setCol

      public Matrix setCol(Double[] values, int colIndex)
      Sets a column of this matrix at the given index to the specified values.
      Parameters:
      values - New values for the column.
      colIndex - The index of the column which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different length than the number of rows of this matrix.
    • setCol

      public Matrix setCol(double[] values, int colIndex)
      Sets a column of this matrix at the given index to the specified values.
      Parameters:
      values - New values for the column.
      colIndex - The index of the column which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different length than the number of rows of this matrix.
    • setRow

      public Matrix setRow(Vector values, int rowIndex)
      Sets a row of this matrix at the given index to the specified values.
      Specified by:
      setRow in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      values - New values for the row.
      rowIndex - The index of the row which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values vector has a different length than the number of rows of this matrix.
    • setRow

      public Matrix setRow(double[] values, int rowIndex)
      Sets a row of this matrix at the given index to the specified values.
      Parameters:
      values - New values for the row.
      rowIndex - The index of the row which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different length than the number of rows of this matrix.
    • setRow

      public Matrix setRow(Double[] values, int rowIndex)
      Sets a row of this matrix at the given index to the specified values.
      Parameters:
      values - New values for the row.
      rowIndex - The index of the row which is to be set.
      Returns:
      A reference to this matrix.
      Throws:
      IllegalArgumentException - If the values array has a different length than the number of rows of this matrix.
    • sqrtComplex

      public CMatrix sqrtComplex()
      Computes the element-wise square root of a tensor.
      Returns:
      The result of applying an element-wise square root to this tensor. Note, this method will compute the principle square root i.e. the square root with positive real part.
    • getTriU

      public Matrix getTriU(int diagOffset)
      Extracts the upper-triangular portion of this matrix with a specified diagonal offset. All other data of the resulting matrix will be zero.
      Specified by:
      getTriU in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      diagOffset - Diagonal offset for upper-triangular portion to extract:
      • If zero, then all data at and above the principle diagonal of this matrix are extracted.
      • If positive, then all data at and above the equivalent super-diagonal are extracted.
      • If negative, then all data at and above the equivalent sub-diagonal are extracted.
      Returns:
      The upper-triangular portion of this matrix with a specified diagonal offset. All other data of the returned matrix will be zero.
      Throws:
      IllegalArgumentException - If diagOffset is not in the range (-numRows, numCols).
    • getTriL

      public Matrix getTriL(int diagOffset)
      Extracts the lower-triangular portion of this matrix with a specified diagonal offset. All other data of the resulting matrix will be zero.
      Specified by:
      getTriL in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      diagOffset - Diagonal offset for lower-triangular portion to extract:
      • If zero, then all data at and above the principle diagonal of this matrix are extracted.
      • If positive, then all data at and above the equivalent super-diagonal are extracted.
      • If negative, then all data at and above the equivalent sub-diagonal are extracted.
      Returns:
      The lower-triangular portion of this matrix with a specified diagonal offset. All other data of the returned matrix will be zero.
      Throws:
      IllegalArgumentException - If diagOffset is not in the range (-numRows, numCols).
    • mult

      public Vector mult(Vector b)
      Computes matrix-vector multiplication.
      Specified by:
      mult in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      b - Vector in the matrix-vector multiplication.
      Returns:
      The result of matrix multiplying this matrix with vector b.
      Throws:
      IllegalArgumentException - If the number of columns in this matrix do not equal the number of data in the vector b.
    • toVector

      public Vector toVector()

      Converts this matrix to an equivalent vector.

      If this matrix is not shaped as a row/column vector, it will first be flattened then converted to a vector.

      Specified by:
      toVector in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      A vector equivalent to this matrix.
    • toTensor

      public Tensor toTensor()
      Converts this matrix to an equivalent Tensor.
      Returns:
      A Tensor which is equivalent to this matrix.
    • getRow

      public Vector getRow(int rowIdx, int colStart, int colEnd)
      Gets a specified row of this matrix between colStart (inclusive) and colEnd (exclusive).
      Specified by:
      getRow in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      rowIdx - Index of the row of this matrix to get.
      colStart - Starting column of the row (inclusive).
      colEnd - Ending column of the row (exclusive).
      Returns:
      The row at index rowIdx of this matrix between the colStart and colEnd indices.
      Throws:
      IndexOutOfBoundsException - If either colEnd are colStart out of bounds for the shape of this matrix.
      IllegalArgumentException - If colEnd is less than colStart.
    • getCol

      public Vector getCol(int colIdx, int rowStart, int rowEnd)
      Gets a specified column of this matrix between rowStart (inclusive) and rowEnd (exclusive).
      Specified by:
      getCol in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      colIdx - Index of the column of this matrix to get.
      rowStart - Starting row of the column (inclusive).
      rowEnd - Ending row of the column (exclusive).
      Returns:
      The column at index colIdx of this matrix between the rowStart and rowEnd indices.
      Throws:
      IndexOutOfBoundsException - If either colEnd are colStart out of bounds for the shape of this matrix.
      IllegalArgumentException - If rowEnd is less than rowStart.
    • getDiag

      public Vector getDiag()
      Extracts the diagonal elements of this matrix and returns them as a vector.
      Specified by:
      getDiag in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Returns:
      A vector containing the diagonal data of this matrix.
    • getDiag

      public Vector getDiag(int diagOffset)
      Gets the elements of this matrix along the specified diagonal.
      Specified by:
      getDiag in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Parameters:
      diagOffset - The diagonal to get within this matrix.
      • If diagOffset == 0: Then the elements of the principle diagonal are collected.
      • If diagOffset < 0: Then the elements of the sub-diagonal diagOffset below the principle diagonal are collected.
      • If diagOffset > 0: Then the elements of the super-diagonal diagOffset above the principle diagonal are collected.
      Returns:
      The elements of the specified diagonal as a vector.
    • T

      public Matrix T()
      Computes the transpose of a tensor by exchanging the first and last axes of this tensor.
      Specified by:
      T in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Overrides:
      T in class AbstractTensor<Matrix,double[],Double>
      Returns:
      The transpose of this tensor.
      See Also:
    • T

      public Matrix T(int axis1, int axis2)
      Computes the transpose of a tensor by exchanging axis1 and axis2.
      Overrides:
      T in class AbstractDenseDoubleTensor<Matrix>
      Parameters:
      axis1 - First axis to exchange.
      axis2 - Second axis to exchange.
      Returns:
      The transpose of this tensor according to the specified axes.
      Throws:
      IndexOutOfBoundsException - If either axis1 or axis2 are out of bounds for the rank of this tensor.
      See Also:
    • T

      public Matrix T(int... axes)
      Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified by axes.
      Overrides:
      T in class AbstractDenseDoubleTensor<Matrix>
      Parameters:
      axes - Permutation of tensor axis. If the tensor has rank N, then this must be an array of length N which is a permutation of {0, 1, 2, ..., N-1}.
      Returns:
      The transpose of this tensor with its axes permuted by the axes array.
      Throws:
      IndexOutOfBoundsException - If any element of axes is out of bounds for the rank of this tensor.
      IllegalArgumentException - If axes is not a permutation of {1, 2, 3, ... N-1}.
      See Also:
    • toCoo

      public CooMatrix toCoo()
      Converts this dense tensor to an equivalent sparse COO tensor.
      Returns:
      A sparse COO tensor equivalent to this dense tensor.
      See Also:
    • toCsr

      public CsrMatrix toCsr()
      Converts this dense matrix to sparse CSR matrix.
      Returns:
      A sparse CSR matrix equivalent to this dense matrix.
      See Also:
    • toComplex

      public CMatrix toComplex()
      Converts this matrix to an equivalent complex matrix.
      Returns:
      A complex matrix with real components equal to the data of this matrix and imaginary components set to zero.
    • add

      public CMatrix add(CMatrix b)
      Sums this matrix with a complex dense matrix.
      Parameters:
      b - Complex dense matrix in the sum.
      Returns:
      The element-wise sum of this matrix with b.
    • add

      public Matrix add(CsrMatrix b)
      Sums this matrix with a real sparse CSR matrix.
      Parameters:
      b - real sparse CSR matrix in the sum.
      Returns:
      The element-wise sum of this matrix and b
    • add

      public Matrix add(CooMatrix b)
      Sums this matrix with a real sparse COO matrix.
      Parameters:
      b - real sparse CSR matrix in the sum.
      Returns:
      The element-wise sum of this matrix and b
    • add

      public CMatrix add(CsrCMatrix b)
      Sums this matrix with a real sparse CSR matrix.
      Parameters:
      b - real sparse CSR matrix in the sum.
      Returns:
      The element-wise sum of this matrix and b
    • add

      public CMatrix add(CooCMatrix b)
      Sums this matrix with a real sparse COO matrix.
      Parameters:
      b - real sparse CSR matrix in the sum.
      Returns:
      The element-wise sum of this matrix and b
    • add

      public CMatrix add(Complex128 b)
      Adds a complex-valued scalar to each entry of this matrix.
      Parameters:
      b - Scalar to add to this matrix.
      Returns:
      A matrix containing the sum of each entry in this matrix with b.
    • sub

      public CMatrix sub(CMatrix b)
      Computes the difference of this matrix with a complex dense matrix.
      Parameters:
      b - Complex dense matrix in the difference.
      Returns:
      The difference of this matrix with b.
    • sub

      public Matrix sub(CsrMatrix b)
      Computes the difference of this matrix with a real sparse CSR matrix.
      Parameters:
      b - real sparse CSR matrix in the difference.
      Returns:
      The element-wise difference of this matrix and b
    • sub

      public Matrix sub(CooMatrix b)
      Computes the difference of this matrix with a real sparse COO matrix.
      Parameters:
      b - real sparse CSR matrix in the difference.
      Returns:
      The element-wise difference of this matrix and b
    • sub

      public CMatrix sub(CsrCMatrix b)
      Computes the difference of this matrix with a real sparse CSR matrix.
      Parameters:
      b - real sparse CSR matrix in the difference.
      Returns:
      The element-wise difference of this matrix and b
    • sub

      public CMatrix sub(CooCMatrix b)
      Computes the difference of this matrix with a real sparse COO matrix.
      Parameters:
      b - real sparse CSR matrix in the difference.
      Returns:
      The element-wise difference of this matrix and b
    • sub

      public CMatrix sub(Complex128 b)
      Subtracts a complex-valued scalar from each entry of this matrix.
      Parameters:
      b - Scalar to subtract from each entry of this matrix.
      Returns:
      A matrix containing the difference of each entry in this matrix with b.
    • mult

      public CMatrix mult(CMatrix b)
      Computes the matrix multiplication between this matrix and a complex dense matrix.
      Parameters:
      b - The complex dense matrix in the matrix multiplication.
      Returns:
      The matrix product between this matrix and b.
      Throws:
      TensorShapeException - If this.numCols != b.numRows.
    • mult

      public Matrix mult(CsrMatrix b)
      Computes the matrix multiplication between this matrix and a real sparse CSR matrix.
      Parameters:
      b - The real sparse matrix in the matrix multiplication.
      Returns:
      The matrix product between this matrix and b.
      Throws:
      TensorShapeException - If this.numCols != b.numRows.
    • mult

      public CMatrix mult(CsrCMatrix b)
      Computes the matrix multiplication between this matrix and a complex sparse CSR matrix.
      Parameters:
      b - The complex sparse matrix in the matrix multiplication.
      Returns:
      The matrix product between this matrix and b.
      Throws:
      TensorShapeException - If this.numCols != b.numRows.
    • mult

      public Matrix mult(CooMatrix b)
      Computes the matrix multiplication between this matrix and a real sparse COO matrix.
      Parameters:
      b - The real sparse matrix in the matrix multiplication.
      Returns:
      The matrix product between this matrix and b.
      Throws:
      TensorShapeException - If this.numCols != b.numRows.
    • mult

      public CMatrix mult(CooCMatrix b)
      Computes the matrix multiplication between this matrix and a complex sparse COO matrix.
      Parameters:
      b - The complex sparse matrix in the matrix multiplication.
      Returns:
      The matrix product between this matrix and b.
      Throws:
      TensorShapeException - If this.numCols != b.numRows.
    • mult

      public CVector mult(CVector b)
      Computes the matrix-vector product of this matrix and a dense complex vector.
      Parameters:
      b - Vector in the matrix-vector product.
      Returns:
      The matrix-vector product of this matrix and the vector b.
    • mult

      public Vector mult(CooVector b)
      Computes the matrix-vector product of this matrix and a real sparse vector.
      Parameters:
      b - Vector in the matrix-vector product.
      Returns:
      The matrix-vector product of this matrix and the vector b.
    • mult

      public CVector mult(CooCVector b)
      Computes the matrix-vector product of this matrix and a complex sparse vector.
      Parameters:
      b - Vector in the matrix-vector product.
      Returns:
      The matrix-vector product of this matrix and the vector b.
    • mult

      public CMatrix mult(Complex128 b)
      Computes the scalar multiplication of this matrix with a complex number.
      Parameters:
      b - Complex valued scalar to multiply with this matrix.
      Returns:
      The matrix-scalar product of this matrix and b.
    • div

      public CMatrix div(CMatrix b)
      Computes the element-wise division between two tensors.
      Parameters:
      b - The denominator tensor in the element-wise quotient.
      Returns:
      The element-wise quotient of this tensor and b.
      Throws:
      TensorShapeException - If this tensor and b's shape are not equal.
    • div

      public CMatrix div(Complex128 b)
      Computes the scalar division of this matrix with a complex number.
      Parameters:
      b - Complex valued scalar to divide this matrix by.
      Returns:
      The matrix-scalar quotient of this matrix and b.
    • pow

      public Matrix pow(int n)

      Computes the matrix multiplication of this matrix with itself n times. This matrix must be square.

      For large n values, this method may be significantly more efficient than calling this.mult(this) a total of n times.

      Parameters:
      n - Number of times to multiply this matrix with itself. Must be non-negative.
      Returns:
      If n=0, then the identity
    • elemMult

      public CMatrix elemMult(CMatrix b)
      Computes the element-wise product of two matrices.
      Parameters:
      b - The second matrix in the element-wise product.
      Returns:
      The element-wise product of this matrix and b.
    • elemMult

      public CooMatrix elemMult(CooMatrix b)
      Computes the element-wise product of two matrices.
      Parameters:
      b - The second matrix in the element-wise product.
      Returns:
      The element-wise product of this matrix and b.
    • elemMult

      public CooCMatrix elemMult(CooCMatrix b)
      Computes the element-wise product of two matrices.
      Parameters:
      b - The second matrix in the element-wise product.
      Returns:
      The element-wise product of this matrix and b.
    • H

      public Matrix H()
      Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values.
      Specified by:
      H in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Specified by:
      H in interface TensorOverRing<Matrix,Matrix,double[],Double>
      Returns:
      The conjugate transpose of this tensor.
      See Also:
    • accept

      public <R> R accept(MatrixVisitor<R> visitor)
      Accepts a visitor that implements the MatrixVisitor interface. This method is part of the "Visitor Pattern" and allows operations to be performed on the matrix without modifying the matrix's class directly.
      Specified by:
      accept in interface MatrixMixin<Matrix,Matrix,Vector,Double>
      Type Parameters:
      R - The return type of the visitor's operation.
      Parameters:
      visitor - The visitor implementing the operation to be performed.
      Returns:
      The result of the visitor's operation, typically another matrix or a scalar value.
      Throws:
      NullPointerException - if the visitor is null.
    • equals

      public boolean equals(Object object)
      Checks if an object is equal to this matrix object.
      Overrides:
      equals in class Object
      Parameters:
      object - Object to check equality with this vector.
      Returns:
      True if the two matrices have the same shape, are numerically equivalent, and are of type Matrix. False otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Generates a human-readable string representing this matrix.
      Overrides:
      toString in class Object
      Returns:
      A human-readable string representing this matrix.