Package jline.util.matrix
Class ComplexMatrix
java.lang.Object
jline.util.matrix.ComplexMatrix
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 -
Constructor Summary
ConstructorsConstructorDescriptionComplexMatrix(int i, int j) Creates a new complex matrix with the specified dimensions.ComplexMatrix(Matrix real) Creates a complex matrix from a real matrix with zero imaginary component.ComplexMatrix(Matrix real, Matrix im) 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 TypeMethodDescriptionadd(ComplexMatrix other) Adds another complex matrix to this one, returning a new matrix.static ComplexMatrixconcatRows(ComplexMatrix top, ComplexMatrix bottom, ComplexMatrix out) Concatenates two complex matrices vertically (row-wise).Returns the conjugate transpose (Hermitian transpose) of this complex matrix.copy()Creates a deep copy of this complex matrix.org.apache.commons.math3.complex.Complexdet()Computes the determinant of this complex matrix using LU decomposition.static ComplexMatrixextractRows(ComplexMatrix A, int row0, int row1, ComplexMatrix out) Extracts a range of rows from a complex matrix.static ComplexMatrixeye(int n) Creates a complex identity matrix of the specified size.org.apache.commons.math3.complex.Complexget(int idx) Gets the complex element at the specified linear index.org.apache.commons.math3.complex.Complexget(int i, int j) Gets the complex element at the specified position.intReturns the number of columns in this complex matrix.intReturns the total number of elements in this complex matrix.intReturns the number of rows in this complex matrix.booleanisEmpty()Checks if this complex matrix is empty (both real and imaginary parts have no elements).Solves the complex linear system A*x = b.mult(ComplexMatrix other) Multiplies this complex matrix by another complex matrix.voidscale(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.voidset(int i, int j, double val) Sets the element at the specified position to a real value (imaginary part becomes zero).voidset(int row, int col, int val) Sets the element at the specified position to an integer value (real part only).voidset(int i, int j, org.apache.commons.math3.complex.Complex val) Sets the element at the specified position to a complex value.voidset(int idx, org.apache.commons.math3.complex.Complex val) Sets the element at the specified linear index to a complex value.sub(ComplexMatrix other) Subtracts another complex matrix from this one, returning a new matrix.sumRows()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.voidzero()Sets all elements of this complex matrix to zero (both real and imaginary parts).static ComplexMatrixzeros(int rows, int cols) Creates a complex zero matrix of the specified dimensions.
-
Field Details
-
real
The real component of the complex 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 rowsj- the number of columns
-
ComplexMatrix
Creates a complex matrix from separate real and imaginary component matrices.- Parameters:
real- the real component matrixim- 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 DMatrixSparseCSCmatrix_im- the imaginary component as DMatrixSparseCSC
-
ComplexMatrix
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
Concatenates two complex matrices vertically (row-wise).- Parameters:
top- the upper complex matrixbottom- the lower complex matrixout- the output matrix, or null to create a new one- Returns:
- a complex matrix with the concatenated rows
-
extractRows
Extracts a range of rows from a complex matrix.- Parameters:
A- the source complex matrixrow0- 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
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 indexj- 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 indexj- the column indexval- 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 indexcol- the column indexval- 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 indexj- the column indexval- 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 indexval- the complex value to set
-
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
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
Creates a complex zero matrix of the specified dimensions.- Parameters:
rows- the number of rowscols- the number of columns- Returns:
- a rows x cols complex zero matrix
-
mult
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
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
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
Returns the transpose of this complex matrix.- Returns:
- the transpose
-
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
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
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
-