From 7cc3c78a24b51a58d44e1baff20b86292662390e Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 13 Oct 2024 15:13:17 +0200 Subject: Mass refactor --- libpsi-core/src/maths/matrix.rs | 2 +- libpsi-core/src/maths/mod.rs | 2 +- libpsi-core/src/maths/vector.rs | 32 +++++--------------------------- 3 files changed, 7 insertions(+), 29 deletions(-) (limited to 'libpsi-core/src/maths') diff --git a/libpsi-core/src/maths/matrix.rs b/libpsi-core/src/maths/matrix.rs index c374a11..58492f9 100644 --- a/libpsi-core/src/maths/matrix.rs +++ b/libpsi-core/src/maths/matrix.rs @@ -230,7 +230,7 @@ impl fmt::Display for Matrix { .data .iter() .map(|element| { - let element_string = format!("{}", element); + let element_string = element.to_string(); if is_complex { let element_string = element_string.trim_end_matches('i').trim(); diff --git a/libpsi-core/src/maths/mod.rs b/libpsi-core/src/maths/mod.rs index 22fb558..5bc7f6d 100644 --- a/libpsi-core/src/maths/mod.rs +++ b/libpsi-core/src/maths/mod.rs @@ -1,10 +1,10 @@ pub mod complex; pub mod matrix; pub mod numeric; - pub mod vector; pub mod vector_ops; pub use complex::*; pub use matrix::*; pub use numeric::*; +pub use vector::*; diff --git a/libpsi-core/src/maths/vector.rs b/libpsi-core/src/maths/vector.rs index cb25ecd..87b5353 100644 --- a/libpsi-core/src/maths/vector.rs +++ b/libpsi-core/src/maths/vector.rs @@ -33,10 +33,11 @@ pub trait Vector { fn max(&self) -> T; fn min(&self) -> T; fn sum(&self) -> T; + + fn from_matrix(matrix: &Matrix) -> Self; } pub trait VectorMatrix { - fn from_matrix(matrix: &Matrix) -> Self; fn to_matrix(&self) -> Matrix; } @@ -97,23 +98,19 @@ impl VectorMatrix for RowVector { fn to_matrix(&self) -> Matrix { Matrix::new(1, self.size(), self.0.clone()) } - - fn from_matrix(matrix: &Matrix) -> Self { - Self::new(matrix.data.clone()) - } } impl VectorMatrix for ColumnVector { fn to_matrix(&self) -> Matrix { Matrix::new(self.size(), 1, self.0.clone()) } +} +impl Vector for VectorImpl { fn from_matrix(matrix: &Matrix) -> Self { Self::new(matrix.data.clone()) } -} -impl Vector for VectorImpl { fn new(data: Vec) -> Self { Self(data) } @@ -256,25 +253,6 @@ impl fmt::Display for RowVector { impl fmt::Display for ColumnVector { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let max_width = self - .0 - .iter() - .map(|x| format!("{:.2}", x).split('.').next().unwrap().len()) - .max() - .unwrap_or(0); - - for (i, x) in self.0.iter().enumerate() { - let formatted_x = format!("{:>width$.2}", x, width = max_width + 3); - - if i == 0 { - write!(f, "┌{}┐\n", formatted_x)?; - } else if i == self.0.len() - 1 { - write!(f, "└{}┘\n", formatted_x)?; - } else { - write!(f, "│{}│\n", formatted_x)?; - } - } - - Ok(()) + write!(f, "{}", self.to_matrix()) } } -- cgit v1.3