pub struct ComplexMatrix {
pub data: Vec<Complex>,
pub rows: usize,
pub cols: usize,
}Expand description
Complex matrix with row-major storage.
Fields§
§data: Vec<Complex>Row-major flat storage.
rows: usizeNumber of rows.
cols: usizeNumber of columns.
Implementations§
Source§impl ComplexMatrix
impl ComplexMatrix
Sourcepub fn try_get(&self, row: usize, col: usize) -> Option<Complex>
pub fn try_get(&self, row: usize, col: usize) -> Option<Complex>
Checked element access: None when (row, col) is out of bounds.
Sourcepub fn try_set(
&mut self,
row: usize,
col: usize,
value: Complex,
) -> MathResult<()>
pub fn try_set( &mut self, row: usize, col: usize, value: Complex, ) -> MathResult<()>
Checked element write: Err when (row, col) is out of bounds.
Sourcepub fn add(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
pub fn add(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
Sourcepub fn sub(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
pub fn sub(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
Sourcepub fn scale(&self, scalar: Complex) -> ComplexMatrix
pub fn scale(&self, scalar: Complex) -> ComplexMatrix
Scalar multiplication.
Sourcepub fn mul(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
pub fn mul(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>
Sourcepub fn transpose(&self) -> ComplexMatrix
pub fn transpose(&self) -> ComplexMatrix
Matrix transpose.
Sourcepub fn hermitian(&self) -> ComplexMatrix
pub fn hermitian(&self) -> ComplexMatrix
Conjugate transpose (Hermitian transpose).
Sourcepub fn determinant(&self) -> Complex
pub fn determinant(&self) -> Complex
Matrix determinant (1×1, 2×2, 3×3 closed forms, larger via LU).
§Panics
If the matrix is not square.
Sourcepub fn lu_decomposition(
&self,
) -> Option<(ComplexMatrix, ComplexMatrix, Vec<usize>)>
pub fn lu_decomposition( &self, ) -> Option<(ComplexMatrix, ComplexMatrix, Vec<usize>)>
LU decomposition with partial pivoting.
Singularity is decided with a relative threshold: a pivot is
considered zero only when it is below 1e-15 relative to the largest
entry of the matrix, so matrices whose entries are all tiny (e.g.
1e-20-scaled physics data) are not falsely rejected.
Sourcepub fn solve(&self, b: &[Complex]) -> Option<Vec<Complex>>
pub fn solve(&self, b: &[Complex]) -> Option<Vec<Complex>>
Solve linear system Ax = b using LU decomposition.
Sourcepub fn inverse(&self) -> Option<ComplexMatrix>
pub fn inverse(&self) -> Option<ComplexMatrix>
Matrix inverse.
Sourcepub fn is_hermitian(&self, tolerance: f64) -> bool
pub fn is_hermitian(&self, tolerance: f64) -> bool
Check if matrix is Hermitian (equal to its conjugate transpose).
Sourcepub fn is_unitary(&self, tolerance: f64) -> bool
pub fn is_unitary(&self, tolerance: f64) -> bool
Check if matrix is unitary (A · Aᴴ = I).
Sourcepub fn frobenius_norm(&self) -> f64
pub fn frobenius_norm(&self) -> f64
Frobenius norm.
Sourcepub fn power(&self, n: usize) -> ComplexMatrix
pub fn power(&self, n: usize) -> ComplexMatrix
Sourcepub fn exp(&self, iterations: usize) -> ComplexMatrix
pub fn exp(&self, iterations: usize) -> ComplexMatrix
Matrix exponential e^A using Taylor series with scaling-and-squaring:
compute e^(A/2^k) then square k times. The series terminates early
once a Taylor term falls below 1e-20 in magnitude (so the iteration
count is a safety cap rather than the accuracy control).
§Panics
If the matrix is not square.
Sourcepub fn ln(&self, iterations: usize) -> Option<ComplexMatrix>
pub fn ln(&self, iterations: usize) -> Option<ComplexMatrix>
Matrix logarithm ln(A) using Taylor series around identity.
Returns None if the series cannot converge, i.e. if any eigenvalue
of A is outside the disk |λ − 1| < 1 (checked via a Frobenius-norm
bound on A − I, which is conservative but safe).
§Panics
If the matrix is not square.
Sourcepub fn qr_decomposition(&self) -> MathResult<(ComplexMatrix, ComplexMatrix)>
pub fn qr_decomposition(&self) -> MathResult<(ComplexMatrix, ComplexMatrix)>
QR decomposition via modified Gram–Schmidt: A = Q·R with Q
unitary and R upper triangular. Rank-deficient columns are completed
with orthonormal basis vectors (QR always exists), so R may have
zero diagonal entries for singular input.
§Errors
MathError::DimensionMismatch for non-square input.
Sourcepub fn eigenvalues(
&self,
max_iterations: usize,
tolerance: f64,
) -> MathResult<Vec<Complex>>
pub fn eigenvalues( &self, max_iterations: usize, tolerance: f64, ) -> MathResult<Vec<Complex>>
Eigenvalues of a square matrix via the QR algorithm with Wilkinson shifts. Returns them in arbitrary order; convergence is judged by the magnitude of the subdiagonal entries.
use mathverse_complex::ComplexMatrix;
use mathverse_complex::Complex;
let mut m = ComplexMatrix::new(2, 2);
m.set(0, 0, Complex::real(2.0));
m.set(1, 1, Complex::real(3.0));
let e = m.eigenvalues(1000, 1e-12).unwrap();
assert_eq!(e.len(), 2);§Errors
MathError::DimensionMismatch for non-square input;
MathError::NotConverged when max_iterations is exhausted.