From ca9fc67ccf7899244ba484c9c99a55985f1f36ac Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 3 Oct 2024 21:48:56 +0200 Subject: Better printing --- libpsi-core/src/maths/matrix.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'libpsi-core/src/maths/matrix.rs') diff --git a/libpsi-core/src/maths/matrix.rs b/libpsi-core/src/maths/matrix.rs index 975c92d..bc91edd 100644 --- a/libpsi-core/src/maths/matrix.rs +++ b/libpsi-core/src/maths/matrix.rs @@ -172,15 +172,36 @@ impl fmt::Debug for Matrix { impl fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let max_width = (0..self.rows) + .flat_map(|i| (0..self.cols).map(move |j| self.get(i, j))) + .map(|x| format!("{:.2}", x).split('.').next().unwrap().len()) // Length of integer part + .max() + .unwrap_or(0); + for i in 0..self.rows { - write!(f, "[")?; + if i == 0 { + write!(f, "┌")?; + } else if i == self.rows - 1 { + write!(f, "└")?; + } else { + write!(f, "│")?; + } + for j in 0..self.cols { - write!(f, "{}", self.get(i, j))?; + write!(f, "{:>width$.2}", self.get(i, j), width = max_width + 3)?; if j != self.cols - 1 { write!(f, " ")?; } } - write!(f, "]")?; + + if i == 0 { + write!(f, "┐")?; + } else if i == self.rows - 1 { + write!(f, "┘")?; + } else { + write!(f, "│")?; + } + if i != self.rows - 1 { write!(f, "\n")?; } -- cgit v1.3