aboutsummaryrefslogtreecommitdiff
path: root/libpsi-core/src/maths/matrix.rs
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2024-10-03 21:48:56 +0200
committerhachem <im@hachem.wtf>2024-10-03 21:48:56 +0200
commitca9fc67ccf7899244ba484c9c99a55985f1f36ac (patch)
tree8e11a7eea1581084585aad43e48c036ef571a191 /libpsi-core/src/maths/matrix.rs
parentd442a1159d9d29e70e7f80eccf4902655a0df8f4 (diff)
Better printing
Diffstat (limited to 'libpsi-core/src/maths/matrix.rs')
-rw-r--r--libpsi-core/src/maths/matrix.rs27
1 files changed, 24 insertions, 3 deletions
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<T: Float + fmt::Debug> fmt::Debug for Matrix<T> {
impl<T: Float + fmt::Display> fmt::Display for Matrix<T> {
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")?;
}