Skip to content

Commit

Permalink
Add alternate Display for concise matrix printing
Browse files Browse the repository at this point in the history
Add format option for concise printing of matrices
as semicolon-delimited rows. e.g. println!("{:#}", matrix);
  • Loading branch information
owenbrooks committed Jun 15, 2022
1 parent 807e4c1 commit 49e3633
Showing 1 changed file with 74 additions and 50 deletions.
124 changes: 74 additions & 50 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1893,68 +1893,92 @@ macro_rules! impl_fmt {
S: RawStorage<T, R, C>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(feature = "std")]
fn val_width<T: Scalar + $trait>(val: &T, f: &mut fmt::Formatter<'_>) -> usize {
match f.precision() {
Some(precision) => format!($fmt_str_with_precision, val, precision)
.chars()
.count(),
None => format!($fmt_str_without_precision, val).chars().count(),
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() {
Some(precision) => format!($fmt_str_with_precision, val, precision)
.chars()
.count(),
None => format!($fmt_str_without_precision, val).chars().count(),
}
}
}

#[cfg(not(feature = "std"))]
fn val_width<T: Scalar + $trait>(_: &T, _: &mut fmt::Formatter<'_>) -> usize {
4
}
#[cfg(not(feature = "std"))]
fn val_width<T: Scalar + $trait>(_: &T, _: &mut fmt::Formatter<'_>) -> usize {
4
}

let (nrows, ncols) = self.shape();
let (nrows, ncols) = self.shape();

if nrows == 0 || ncols == 0 {
return write!(f, "[ ]");
}
if nrows == 0 || ncols == 0 {
return write!(f, "[ ]");
}

let mut max_length = 0;
let mut max_length = 0;

for i in 0..nrows {
for j in 0..ncols {
max_length = crate::max(max_length, val_width(&self[(i, j)], f));
for i in 0..nrows {
for j in 0..ncols {
max_length = crate::max(max_length, val_width(&self[(i, j)], f));
}
}
}

let max_length_with_space = max_length + 1;

writeln!(f)?;
writeln!(
f,
" ┌ {:>width$} ┐",
"",
width = max_length_with_space * ncols - 1
)?;

for i in 0..nrows {
write!(f, " │")?;
for j in 0..ncols {
let number_length = val_width(&self[(i, j)], f) + 1;
let pad = max_length_with_space - number_length;
write!(f, " {:>thepad$}", "", thepad = pad)?;
match f.precision() {
Some(precision) => {
write!(f, $fmt_str_with_precision, (*self)[(i, j)], precision)?
let max_length_with_space = max_length + 1;

writeln!(f)?; // leading newline to ensure no offset from previous prints
writeln!(
f,
" ┌ {:>width$} ┐",
"",
width = max_length_with_space * ncols - 1
)?;

for i in 0..nrows {
write!(f, " │")?;
for j in 0..ncols {
let number_length = val_width(&self[(i, j)], f) + 1;
let pad = max_length_with_space - number_length;
write!(f, " {:>thepad$}", "", thepad = pad)?;
match f.precision() {
Some(precision) => {
write!(f, $fmt_str_with_precision, (*self)[(i, j)], precision)?
}
None => write!(f, $fmt_str_without_precision, (*self)[(i, j)])?,
}
None => write!(f, $fmt_str_without_precision, (*self)[(i, j)])?,
}
writeln!(f, " │")?;
}
writeln!(f, " │")?;
}

writeln!(
f,
" └ {:>width$} ┘",
"",
width = max_length_with_space * ncols - 1
)?;
writeln!(f)
write!(
f,
" └ {:>width$} ┘",
"",
width = max_length_with_space * ncols - 1
)
} 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, "]")
}
}
}
};
Expand Down

0 comments on commit 49e3633

Please sign in to comment.