Add property method `is_hermitian` and `is_unitary`

This commit is contained in:
Xin Hao 2024-06-19 23:41:48 +08:00
parent d1edb4fd7b
commit 448e45ffbe
2 changed files with 37 additions and 0 deletions

View File

@ -119,4 +119,23 @@ where
// TODO: improve this? // TODO: improve this?
self.clone_owned().try_inverse().is_some() 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)
}
} }

View File

@ -201,6 +201,24 @@ fn identity() {
assert!(!not_id3.is_identity(0.0)); 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] #[test]
fn coordinates() { fn coordinates() {
let a = Matrix3x4::new(11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34); let a = Matrix3x4::new(11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34);