From 639382ea49c855c8736cc61df3185edd5cd7b170 Mon Sep 17 00:00:00 2001 From: "nathan.eckert" Date: Thu, 8 Jul 2021 11:23:41 +0200 Subject: [PATCH 1/3] Add failing test higlighting the issue --- tests/core/matrix.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 7befd351..60007846 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1108,3 +1108,30 @@ fn partial_eq_different_types() { // assert_ne!(static_mat, typenum_static_mat); //assert_ne!(typenum_static_mat, static_mat); } + + + +fn generic_omatrix_to_string( + vector: &nalgebra::OVector, + matrix: &nalgebra::OMatrix) -> (String, String) +where + D: nalgebra::Dim, + nalgebra::DefaultAllocator: nalgebra::base::allocator::Allocator, + nalgebra::DefaultAllocator: nalgebra::base::allocator::Allocator, +{ + (vector.to_string(), matrix.to_string()) +} + +#[test] +fn omatrix_to_string() { + let dvec: nalgebra::DVector = nalgebra::dvector![1.0, 2.0]; + let dmatr: nalgebra::DMatrix = nalgebra::dmatrix![1.0, 2.0; 3.0, 4.0]; + let svec: nalgebra::SVector = nalgebra::vector![1.0, 2.0]; + let smatr: nalgebra::SMatrix = nalgebra::matrix![1.0, 2.0; 3.0, 4.0]; + assert_eq!( + generic_omatrix_to_string(&dvec, &dmatr), + (dvec.to_string(), dmatr.to_string())); + assert_eq!( + generic_omatrix_to_string(&svec, &smatr), + (svec.to_string(), smatr.to_string())); +} From 92a51662bf5bdb748d32ce46c3b18b59359e8ed3 Mon Sep 17 00:00:00 2001 From: "nathan.eckert" Date: Thu, 8 Jul 2021 14:18:22 +0200 Subject: [PATCH 2/3] Remove Allocator from display --- src/base/matrix.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 7e5c0ce9..319e8eb9 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1819,7 +1819,6 @@ macro_rules! impl_fmt { where T: Scalar + $trait, S: Storage, - DefaultAllocator: Allocator, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[cfg(feature = "std")] @@ -1837,20 +1836,17 @@ macro_rules! impl_fmt { 4 } - let (nrows, ncols) = self.data.shape(); + let (nrows, ncols) = self.shape(); - if nrows.value() == 0 || ncols.value() == 0 { + if nrows == 0 || ncols == 0 { return write!(f, "[ ]"); } let mut max_length = 0; - let mut lengths: OMatrix = Matrix::zeros_generic(nrows, ncols); - let (nrows, ncols) = self.shape(); for i in 0..nrows { for j in 0..ncols { - lengths[(i, j)] = val_width(&self[(i, j)], f); - max_length = crate::max(max_length, lengths[(i, j)]); + max_length = crate::max(max_length, val_width(&self[(i, j)], f)); } } @@ -1867,7 +1863,7 @@ macro_rules! impl_fmt { for i in 0..nrows { write!(f, " │")?; for j in 0..ncols { - let number_length = lengths[(i, j)] + 1; + 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() { @@ -1900,19 +1896,29 @@ impl_fmt!(fmt::UpperHex, "{:X}", "{:1$X}"); impl_fmt!(fmt::Binary, "{:b}", "{:.1$b}"); impl_fmt!(fmt::Pointer, "{:p}", "{:.1$p}"); -#[test] -fn lower_exp() { - let test = crate::Matrix2::new(1e6, 2e5, 2e-5, 1.); - assert_eq!( - format!("{:e}", test), - r" +#[cfg(test)] +mod tests { + #[test] + fn empty_display() { + let vec: Vec = Vec::new(); + let dvector = crate::DVector::from_vec(vec); + assert_eq!(format!("{}", dvector), "[ ]") + } + + #[test] + fn lower_exp() { + let test = crate::Matrix2::new(1e6, 2e5, 2e-5, 1.); + assert_eq!( + format!("{:e}", test), + r" ┌ ┐ │ 1e6 2e5 │ │ 2e-5 1e0 │ └ ┘ " - ) + ) + } } /// # Cross product From dd1530adc3970c243e7215d6191251545a1b0c06 Mon Sep 17 00:00:00 2001 From: "nathan.eckert" Date: Thu, 8 Jul 2021 14:20:35 +0200 Subject: [PATCH 3/3] Cargo fmt --- tests/core/matrix.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 60007846..eaa252db 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1109,11 +1109,10 @@ fn partial_eq_different_types() { //assert_ne!(typenum_static_mat, static_mat); } - - fn generic_omatrix_to_string( vector: &nalgebra::OVector, - matrix: &nalgebra::OMatrix) -> (String, String) + matrix: &nalgebra::OMatrix, +) -> (String, String) where D: nalgebra::Dim, nalgebra::DefaultAllocator: nalgebra::base::allocator::Allocator, @@ -1130,8 +1129,10 @@ fn omatrix_to_string() { let smatr: nalgebra::SMatrix = nalgebra::matrix![1.0, 2.0; 3.0, 4.0]; assert_eq!( generic_omatrix_to_string(&dvec, &dmatr), - (dvec.to_string(), dmatr.to_string())); + (dvec.to_string(), dmatr.to_string()) + ); assert_eq!( generic_omatrix_to_string(&svec, &smatr), - (svec.to_string(), smatr.to_string())); + (svec.to_string(), smatr.to_string()) + ); }