-
-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Display and Debug outputs concise and consistent #1119
base: dev
Are you sure you want to change the base?
Changes from all commits
831142d
f77f556
807e4c1
49e3633
65e90d5
97e2a8f
9dffc5b
004091a
b16a3ed
2dd76fd
699d57d
7bc91d3
6df81c2
76bc230
364302d
e70bcdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -195,9 +195,32 @@ pub struct Matrix<T, R, C, S> { | |||||||||||||||||||||||||||||||||||||||||||||
_phantoms: PhantomData<(T, R, C)>, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
impl<T, R: Dim, C: Dim, S: fmt::Debug> fmt::Debug for Matrix<T, R, C, S> { | ||||||||||||||||||||||||||||||||||||||||||||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||
self.data.fmt(formatter) | ||||||||||||||||||||||||||||||||||||||||||||||
impl<T: fmt::Debug, R: Dim, C: Dim, S: RawStorage<T, R, C> + fmt::Debug> fmt::Debug | ||||||||||||||||||||||||||||||||||||||||||||||
for Matrix<T, R, C, S> | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||
if f.alternate() { | ||||||||||||||||||||||||||||||||||||||||||||||
writeln!(f, "")?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "[")?; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
let row_separator = match f.alternate() { | ||||||||||||||||||||||||||||||||||||||||||||||
true => ";\n ", | ||||||||||||||||||||||||||||||||||||||||||||||
false => "; ", | ||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
for (i, row) in self.row_iter().enumerate() { | ||||||||||||||||||||||||||||||||||||||||||||||
if i > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "{row_separator}")?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
for (j, element) in row.iter().enumerate() { | ||||||||||||||||||||||||||||||||||||||||||||||
if j > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, ", ")?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
element.fmt(f)?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "]") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1855,68 +1878,94 @@ 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)?; // 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)])?, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
writeln!(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)? | ||||||||||||||||||||||||||||||||||||||||||||||
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, ", ")?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
None => write!(f, $fmt_str_without_precision, (*self)[(i, j)])?, | ||||||||||||||||||||||||||||||||||||||||||||||
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, "; ")?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
writeln!(f, " │")?; | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "]") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
writeln!( | ||||||||||||||||||||||||||||||||||||||||||||||
f, | ||||||||||||||||||||||||||||||||||||||||||||||
" └ {:>width$} ┘", | ||||||||||||||||||||||||||||||||||||||||||||||
"", | ||||||||||||||||||||||||||||||||||||||||||||||
width = max_length_with_space * ncols - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
)?; | ||||||||||||||||||||||||||||||||||||||||||||||
writeln!(f) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1930,6 +1979,28 @@ impl_fmt!(fmt::UpperHex, "{:X}", "{:1$X}"); | |||||||||||||||||||||||||||||||||||||||||||||
impl_fmt!(fmt::Binary, "{:b}", "{:.1$b}"); | ||||||||||||||||||||||||||||||||||||||||||||||
impl_fmt!(fmt::Pointer, "{:p}", "{:.1$p}"); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
/// Displays a vector using commas as the delimiter | ||||||||||||||||||||||||||||||||||||||||||||||
pub fn format_column_vec_as_row<T: Scalar + fmt::Display, D: crate::DimName>( | ||||||||||||||||||||||||||||||||||||||||||||||
vector: &OVector<T, D>, | ||||||||||||||||||||||||||||||||||||||||||||||
f: &mut fmt::Formatter<'_>, | ||||||||||||||||||||||||||||||||||||||||||||||
) -> fmt::Result | ||||||||||||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||||||||||||
DefaultAllocator: Allocator<T, D>, | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
if vector.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||||
return write!(f, "[ ]"); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "[")?; | ||||||||||||||||||||||||||||||||||||||||||||||
let mut it = vector.iter(); | ||||||||||||||||||||||||||||||||||||||||||||||
std::fmt::Display::fmt(it.next().unwrap(), f)?; | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we're fully-qualifying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was to disambiguate it from std::fmt::Debug::fmt. |
||||||||||||||||||||||||||||||||||||||||||||||
for comp in it { | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, ", ")?; | ||||||||||||||||||||||||||||||||||||||||||||||
std::fmt::Display::fmt(comp, f)?; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "]") | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1990
to
+2001
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this could be simpler/less unwrappy:
Suggested change
This also avoids the whitespace in empty vectors, which I think is more consistent. |
||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||||
mod tests { | ||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1948,9 +2019,7 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||||
┌ ┐ | ||||||||||||||||||||||||||||||||||||||||||||||
│ 1e6 2e5 │ | ||||||||||||||||||||||||||||||||||||||||||||||
│ 2e-5 1e0 │ | ||||||||||||||||||||||||||||||||||||||||||||||
└ ┘ | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
" | ||||||||||||||||||||||||||||||||||||||||||||||
└ ┘" | ||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice not to panic on 0-element vectors, unless we're really utterly certain that will never occur (but the signature doesn't suggest as much).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0-length vectors can easily occur in practice, so this is important!
Would be nice to have some unit tests that include 0-element vectors maybe..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a test for 0-element matrices, though I'm not sure how to add a test for this function directly. Since calling that function requires a
Formatter
struct, and I don't think I can construct a 0-length translation/point to test with.