Skip to content

Commit

Permalink
Add property method is_hermitian and is_unitary
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxins committed Jun 19, 2024
1 parent d1edb4f commit 448e45f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/base/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,23 @@ where
// TODO: improve this?
self.clone_owned().try_inverse().is_some()
}

/// Returns `true` if this matrix is hermitian.
#[inline]
#[must_use]
pub fn is_hermitian(&self) -> bool {
self.is_square() && self.transpose().conjugate().eq(self)
}

/// Returns `true` if this matrix is unitary.
#[inline]
#[must_use]
pub fn is_unitary(&self, eps: T::Epsilon) -> bool
where
T: Zero + One + RelativeEq,
T::Epsilon: Clone,
{
let r = self.transpose().conjugate() * self;
self.is_square() && r.is_identity(eps)
}
}
18 changes: 18 additions & 0 deletions tests/core/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ fn identity() {
assert!(!not_id3.is_identity(0.0));
}

#[test]
fn is_hermitian() {
let a = Matrix2::new(1.0, 2.0, 3.0, 4.0);
let b = Matrix2::new(1.0, 2.0, 2.0, 1.0);

assert!(!a.is_hermitian());
assert!(b.is_hermitian());
}

#[test]
fn is_unitary() {
let a = Matrix2::new(1.0, 2.0, 3.0, 4.0);
let b = Matrix2::new(0.0, 1.0, 1.0, 0.0);

assert!(!a.is_unitary(1.0e-7));
assert!(b.is_unitary(1.0e-7));
}

#[test]
fn coordinates() {
let a = Matrix3x4::new(11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34);
Expand Down

0 comments on commit 448e45f

Please sign in to comment.