Skip to main content

ComplexMatrix

Struct ComplexMatrix 

Source
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: usize

Number of rows.

§cols: usize

Number of columns.

Implementations§

Source§

impl ComplexMatrix

Source

pub fn new(rows: usize, cols: usize) -> Self

Create a new complex matrix filled with zeros.

Source

pub fn from_data(data: Vec<Complex>, rows: usize, cols: usize) -> Self

Create matrix from data.

§Panics

If data.len() != rows * cols.

Source

pub fn get(&self, row: usize, col: usize) -> Complex

Get element at (row, col).

§Panics

If the index is out of bounds. Use try_get for a checked variant.

Source

pub fn set(&mut self, row: usize, col: usize, value: Complex)

Set element at (row, col).

§Panics

If the index is out of bounds. Use try_set for a checked variant.

Source

pub fn try_get(&self, row: usize, col: usize) -> Option<Complex>

Checked element access: None when (row, col) is out of bounds.

Source

pub fn try_set( &mut self, row: usize, col: usize, value: Complex, ) -> MathResult<()>

Checked element write: Err when (row, col) is out of bounds.

Source

pub fn zeros(rows: usize, cols: usize) -> Self

Create zero matrix.

Source

pub fn identity(n: usize) -> Self

Create identity matrix.

Source

pub fn add(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>

Matrix addition.

§Errors

MathError::DimensionMismatch if the shapes differ.

Source

pub fn sub(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>

Matrix subtraction.

§Errors

MathError::DimensionMismatch if the shapes differ.

Source

pub fn scale(&self, scalar: Complex) -> ComplexMatrix

Scalar multiplication.

Source

pub fn mul(&self, other: &ComplexMatrix) -> MathResult<ComplexMatrix>

Matrix multiplication.

§Errors

MathError::DimensionMismatch if self.cols != other.rows.

Source

pub fn transpose(&self) -> ComplexMatrix

Matrix transpose.

Source

pub fn hermitian(&self) -> ComplexMatrix

Conjugate transpose (Hermitian transpose).

Source

pub fn trace(&self) -> Complex

Matrix trace.

§Panics

If the matrix is not square.

Source

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.

Source

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.

Source

pub fn solve(&self, b: &[Complex]) -> Option<Vec<Complex>>

Solve linear system Ax = b using LU decomposition.

Source

pub fn inverse(&self) -> Option<ComplexMatrix>

Matrix inverse.

Source

pub fn is_hermitian(&self, tolerance: f64) -> bool

Check if matrix is Hermitian (equal to its conjugate transpose).

Source

pub fn is_unitary(&self, tolerance: f64) -> bool

Check if matrix is unitary (A · Aᴴ = I).

Source

pub fn frobenius_norm(&self) -> f64

Frobenius norm.

Source

pub fn power(&self, n: usize) -> ComplexMatrix

Matrix power A^n.

§Panics

If the matrix is not square.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for ComplexMatrix

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ComplexMatrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Index<(usize, usize)> for ComplexMatrix

Source§

type Output = Complex

The returned type after indexing.
Source§

fn index(&self, (row, col): (usize, usize)) -> &Complex

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<(usize, usize)> for ComplexMatrix

Source§

fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut Complex

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.