Class ComplexMatrix

java.lang.Object
jline.util.matrix.ComplexMatrix

public class ComplexMatrix extends Object
A matrix class for handling complex-valued matrices using separate real and imaginary components.

ComplexMatrix provides a comprehensive implementation for complex matrix operations by storing the real and imaginary parts as separate Matrix objects. This approach allows for efficient manipulation of complex numbers in matrix computations while leveraging the existing Matrix infrastructure.

Key features:

  • Separate storage of real and imaginary components
  • Support for all standard complex matrix operations
  • Integration with Apache Commons Math for complex number operations
  • Efficient determinant calculation using LU decomposition
  • Row and column extraction operations
Since:
1.0
Author:
QORE Lab, Imperial College London
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The imaginary component of the complex matrix
    The real component of the complex matrix
  • Constructor Summary

    Constructors
    Constructor
    Description
    ComplexMatrix(int i, int j)
    Creates a new complex matrix with the specified dimensions.
    Creates a complex matrix from a real matrix with zero imaginary component.
    Creates a complex matrix from separate real and imaginary component matrices.
    ComplexMatrix(org.ejml.data.DMatrixSparseCSC matrix_real, org.ejml.data.DMatrixSparseCSC matrix_im)
    Creates a complex matrix from EJML sparse matrices representing real and imaginary components.
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds another complex matrix to this one, returning a new matrix.
    Concatenates two complex matrices vertically (row-wise).
    Returns the conjugate transpose (Hermitian transpose) of this complex matrix.
    Creates a deep copy of this complex matrix.
    org.apache.commons.math3.complex.Complex
    det()
    Computes the determinant of this complex matrix using LU decomposition.
    extractRows(ComplexMatrix A, int row0, int row1, ComplexMatrix out)
    Extracts a range of rows from a complex matrix.
    eye(int n)
    Creates a complex identity matrix of the specified size.
    org.apache.commons.math3.complex.Complex
    get(int idx)
    Gets the complex element at the specified linear index.
    org.apache.commons.math3.complex.Complex
    get(int i, int j)
    Gets the complex element at the specified position.
    int
    Returns the number of columns in this complex matrix.
    int
    Returns the total number of elements in this complex matrix.
    int
    Returns the number of rows in this complex matrix.
    boolean
    Checks if this complex matrix is empty (both real and imaginary parts have no elements).
    Solves the complex linear system A*x = b.
    Multiplies this complex matrix by another complex matrix.
    void
    scale(double a)
    Scales this complex matrix by a real scalar value.
    scaleComplex(org.apache.commons.math3.complex.Complex z)
    Scales this complex matrix by a complex scalar, returning a new matrix.
    void
    set(int i, int j, double val)
    Sets the element at the specified position to a real value (imaginary part becomes zero).
    void
    set(int row, int col, int val)
    Sets the element at the specified position to an integer value (real part only).
    void
    set(int i, int j, org.apache.commons.math3.complex.Complex val)
    Sets the element at the specified position to a complex value.
    void
    set(int idx, org.apache.commons.math3.complex.Complex val)
    Sets the element at the specified linear index to a complex value.
    Subtracts another complex matrix from this one, returning a new matrix.
    Computes the sum of each row, returning a complex matrix with one column.
    org.apache.commons.math3.linear.FieldMatrix<org.apache.commons.math3.complex.Complex>
    Converts this ComplexMatrix to an Apache Commons Math3 FieldMatrix.
    Returns the transpose of this complex matrix.
    void
    Sets all elements of this complex matrix to zero (both real and imaginary parts).
    zeros(int rows, int cols)
    Creates a complex zero matrix of the specified dimensions.

    Methods inherited from class java.lang.Object

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

    • real

      public Matrix real
      The real component of the complex matrix
    • im

      public Matrix im
      The imaginary component of the complex matrix
  • Constructor Details

    • ComplexMatrix

      public ComplexMatrix(int i, int j)
      Creates a new complex matrix with the specified dimensions. All elements are initialized to zero (both real and imaginary parts).
      Parameters:
      i - the number of rows
      j - the number of columns
    • ComplexMatrix

      public ComplexMatrix(Matrix real, Matrix im)
      Creates a complex matrix from separate real and imaginary component matrices.
      Parameters:
      real - the real component matrix
      im - the imaginary component matrix
      Throws:
      AssertionError - if the dimensions of real and imaginary matrices don't match
    • ComplexMatrix

      public ComplexMatrix(org.ejml.data.DMatrixSparseCSC matrix_real, org.ejml.data.DMatrixSparseCSC matrix_im)
      Creates a complex matrix from EJML sparse matrices representing real and imaginary components.
      Parameters:
      matrix_real - the real component as DMatrixSparseCSC
      matrix_im - the imaginary component as DMatrixSparseCSC
    • ComplexMatrix

      public ComplexMatrix(Matrix real)
      Creates a complex matrix from a real matrix with zero imaginary component.
      Parameters:
      real - the real component matrix (imaginary part will be set to zero)
  • Method Details

    • concatRows

      public static ComplexMatrix concatRows(ComplexMatrix top, ComplexMatrix bottom, ComplexMatrix out)
      Concatenates two complex matrices vertically (row-wise).
      Parameters:
      top - the upper complex matrix
      bottom - the lower complex matrix
      out - the output matrix, or null to create a new one
      Returns:
      a complex matrix with the concatenated rows
    • extractRows

      public static ComplexMatrix extractRows(ComplexMatrix A, int row0, int row1, ComplexMatrix out)
      Extracts a range of rows from a complex matrix.
      Parameters:
      A - the source complex matrix
      row0 - the first row to extract (inclusive)
      row1 - the last row to extract (exclusive)
      out - the output matrix, or null to create a new one
      Returns:
      a complex matrix containing the extracted rows
    • copy

      public ComplexMatrix copy()
      Creates a deep copy of this complex matrix.
      Returns:
      a new ComplexMatrix that is a copy of this instance
    • det

      public org.apache.commons.math3.complex.Complex det()
      Computes the determinant of this complex matrix using LU decomposition.
      Returns:
      the complex determinant of the matrix
    • get

      public org.apache.commons.math3.complex.Complex get(int i, int j)
      Gets the complex element at the specified position.
      Parameters:
      i - the row index
      j - the column index
      Returns:
      the complex value at the specified position
    • get

      public org.apache.commons.math3.complex.Complex get(int idx)
      Gets the complex element at the specified linear index.
      Parameters:
      idx - the linear index
      Returns:
      the complex value at the specified index
    • getNumCols

      public int getNumCols()
      Returns the number of columns in this complex matrix.
      Returns:
      the number of columns
    • getNumElements

      public int getNumElements()
      Returns the total number of elements in this complex matrix.
      Returns:
      the total number of elements (rows * columns)
    • getNumRows

      public int getNumRows()
      Returns the number of rows in this complex matrix.
      Returns:
      the number of rows
    • isEmpty

      public boolean isEmpty()
      Checks if this complex matrix is empty (both real and imaginary parts have no elements).
      Returns:
      true if the matrix is empty, false otherwise
    • scale

      public void scale(double a)
      Scales this complex matrix by a real scalar value. Both real and imaginary components are multiplied by the scalar.
      Parameters:
      a - the scalar value to multiply by
    • set

      public void set(int i, int j, org.apache.commons.math3.complex.Complex val)
      Sets the element at the specified position to a complex value.
      Parameters:
      i - the row index
      j - the column index
      val - the complex value to set
    • set

      public void set(int row, int col, int val)
      Sets the element at the specified position to an integer value (real part only). Special handling for Integer.MAX_VALUE and Integer.MIN_VALUE as positive and negative infinity.
      Parameters:
      row - the row index
      col - the column index
      val - the integer value to set
    • set

      public void set(int i, int j, double val)
      Sets the element at the specified position to a real value (imaginary part becomes zero).
      Parameters:
      i - the row index
      j - the column index
      val - the real value to set
    • set

      public void set(int idx, org.apache.commons.math3.complex.Complex val)
      Sets the element at the specified linear index to a complex value.
      Parameters:
      idx - the linear index
      val - the complex value to set
    • sumRows

      public ComplexMatrix sumRows()
      Computes the sum of each row, returning a complex matrix with one column.
      Returns:
      a complex matrix containing the row sums
    • zero

      public void zero()
      Sets all elements of this complex matrix to zero (both real and imaginary parts).
    • eye

      public static ComplexMatrix eye(int n)
      Creates a complex identity matrix of the specified size.
      Parameters:
      n - the size of the identity matrix
      Returns:
      an n x n complex identity matrix
    • zeros

      public static ComplexMatrix zeros(int rows, int cols)
      Creates a complex zero matrix of the specified dimensions.
      Parameters:
      rows - the number of rows
      cols - the number of columns
      Returns:
      a rows x cols complex zero matrix
    • mult

      public ComplexMatrix mult(ComplexMatrix other)
      Multiplies this complex matrix by another complex matrix. (A+iB)(C+iD) = (AC-BD) + i(AD+BC)
      Parameters:
      other - the matrix to multiply by
      Returns:
      the product of the two complex matrices
    • add

      public ComplexMatrix add(ComplexMatrix other)
      Adds another complex matrix to this one, returning a new matrix.
      Parameters:
      other - the matrix to add
      Returns:
      a new ComplexMatrix that is the sum of this and other
    • sub

      public ComplexMatrix sub(ComplexMatrix other)
      Subtracts another complex matrix from this one, returning a new matrix.
      Parameters:
      other - the matrix to subtract
      Returns:
      a new ComplexMatrix that is this minus other
    • transpose

      public ComplexMatrix transpose()
      Returns the transpose of this complex matrix.
      Returns:
      the transpose
    • conjugateTranspose

      public ComplexMatrix conjugateTranspose()
      Returns the conjugate transpose (Hermitian transpose) of this complex matrix. For (A+iB), the conjugate transpose is (A^T - iB^T).
      Returns:
      the conjugate transpose
    • scaleComplex

      public ComplexMatrix scaleComplex(org.apache.commons.math3.complex.Complex z)
      Scales this complex matrix by a complex scalar, returning a new matrix. (a+ib)(C+iD) = (aC-bD) + i(aD+bC)
      Parameters:
      z - the complex scalar
      Returns:
      a new ComplexMatrix scaled by z
    • leftMatrixDivide

      public ComplexMatrix leftMatrixDivide(ComplexMatrix b)
      Solves the complex linear system A*x = b. For square systems, uses LU decomposition. For overdetermined systems (more rows than columns), uses the real-embedding approach with SVD-based pseudo-inverse, which correctly handles rank-deficient systems.

      The complex system (A_r + i*A_i)(x_r + i*x_i) = (b_r + i*b_i) is converted to the equivalent real system:

         [A_r, -A_i] [x_r]   [b_r]
         [A_i,  A_r] [x_i] = [b_i]
       
      This 2m x 2n real system is solved via the real Matrix.leftMatrixDivide (SVD-based).

      Parameters:
      b - the right-hand side complex column vector or matrix
      Returns:
      the solution vector x
    • toFieldMatrix

      public org.apache.commons.math3.linear.FieldMatrix<org.apache.commons.math3.complex.Complex> toFieldMatrix()
      Converts this ComplexMatrix to an Apache Commons Math3 FieldMatrix.
      Returns:
      the equivalent FieldMatrix