Skip to content

Commit

Permalink
refactor(tensor/validation): create is_symmetric, update inner checks…
Browse files Browse the repository at this point in the history
… & docs
  • Loading branch information
drewxs committed Dec 18, 2023
1 parent 24c9c81 commit 10068b7
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/linalg/tensor/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,45 @@ impl Tensor {
}
}

/// Validates that the tensor is symmetric.
/// Returns true if the tensor is symmetric, i.e. a square matrix with equal elements across the diagonal.
///
/// # Examples
/// ```
/// # use engram::*;
/// let t = tensor![[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [3.0, 2.0, 1]];
/// t.validate_symmetric("op");
/// assert(t.is_symmetric());
/// ```
pub fn validate_symmetric(&self, op: &str) {
self.validate_square(op);
pub fn is_symmetric(&self) -> bool {
if !self.is_square() {
return false;
}

for i in 0..self.rows {
for j in 0..self.cols {
if self.data[i][j] != self.data[j][i] {
panic!("Tensor.{} invoked with non-symmetric matrix", op);
return false;
}
}
}

true
}

/// Returns true if the tensor is positive definite.
/// Validates that the tensor is symmetric, i.e. a square matrix with equal elements across the diagonal.
///
/// # Examples
/// ```
/// # use engram::*;
/// let t = tensor![[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [3.0, 2.0, 1]];
/// t.validate_symmetric("op");
/// ```
pub fn validate_symmetric(&self, op: &str) {
if !self.is_symmetric() {
panic!("Tensor.{} invoked with non-symmetric matrix", op);
}
}

/// Returns true if the tensor is positive definite, i.e. symmetric and all eigenvalues are positive.
///
/// # Examples
/// ```
Expand All @@ -123,7 +141,7 @@ impl Tensor {
/// assert!(t.is_positive_definite());
/// ```
pub fn is_positive_definite(&self) -> bool {
if self.rows != self.cols {
if !self.is_square() {
return false;
}

Expand All @@ -143,7 +161,7 @@ impl Tensor {
false
}

/// Validates that the tensor is positive definite.
/// Validates that the tensor is positive definite, i.e. symmetric and all eigenvalues are positive.
///
/// # Examples
/// ```
Expand Down

0 comments on commit 10068b7

Please sign in to comment.