Add alternate Display for concise matrix printing

Add format option for concise printing of matrices
as semicolon-delimited rows. e.g. println!("{:#}", matrix);
This commit is contained in:
Owen Brooks 2022-06-15 13:48:46 +10:00
parent 807e4c153a
commit 49e3633c1c
1 changed files with 82 additions and 58 deletions

View File

@ -1893,6 +1893,7 @@ macro_rules! impl_fmt {
S: RawStorage<T, R, C>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !f.alternate() { // pretty print in 2D layout
#[cfg(feature = "std")]
fn val_width<T: Scalar + $trait>(val: &T, f: &mut fmt::Formatter<'_>) -> usize {
match f.precision() {
@ -1924,7 +1925,7 @@ macro_rules! impl_fmt {
let max_length_with_space = max_length + 1;
writeln!(f)?;
writeln!(f)?; // leading newline to ensure no offset from previous prints
writeln!(
f,
" ┌ {:>width$} ┐",
@ -1948,13 +1949,36 @@ macro_rules! impl_fmt {
writeln!(f, "")?;
}
writeln!(
write!(
f,
" └ {:>width$} ┘",
"",
width = max_length_with_space * ncols - 1
)?;
writeln!(f)
)
} else { // print on single line with semicolon-delimited rows and comma-delimited columns
let (nrows, ncols) = self.shape();
write!(f, "[")?;
for row in 0..nrows {
for col in 0..ncols {
if col != 0 {
write!(f, ", ")?;
}
match f.precision() {
Some(precision) => write!(
f,
$fmt_str_with_precision,
(*self)[(row, col)],
precision
)?,
None => write!(f, $fmt_str_without_precision, (*self)[(row, col)])?,
}
}
if row != nrows - 1 {
write!(f, "; ")?;
}
}
write!(f, "]")
}
}
}
};