Merge pull request #938 from Nateckert/omatrix_to_string

Make display generical for Omatrix
This commit is contained in:
Sébastien Crozet 2021-07-11 17:45:02 +02:00 committed by GitHub
commit b007e192d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 15 deletions

View File

@ -1819,7 +1819,6 @@ macro_rules! impl_fmt {
where
T: Scalar + $trait,
S: Storage<T, R, C>,
DefaultAllocator: Allocator<usize, R, C>,
{
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<usize, R, C> = 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,6 +1896,15 @@ impl_fmt!(fmt::UpperHex, "{:X}", "{:1$X}");
impl_fmt!(fmt::Binary, "{:b}", "{:.1$b}");
impl_fmt!(fmt::Pointer, "{:p}", "{:.1$p}");
#[cfg(test)]
mod tests {
#[test]
fn empty_display() {
let vec: Vec<f64> = 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.);
@ -1914,6 +1919,7 @@ fn lower_exp() {
"
)
}
}
/// # Cross product
impl<T: Scalar + ClosedAdd + ClosedSub + ClosedMul, R: Dim, C: Dim, S: Storage<T, R, C>>

View File

@ -1108,3 +1108,31 @@ fn partial_eq_different_types() {
// assert_ne!(static_mat, typenum_static_mat);
//assert_ne!(typenum_static_mat, static_mat);
}
fn generic_omatrix_to_string<D>(
vector: &nalgebra::OVector<f64, D>,
matrix: &nalgebra::OMatrix<f64, D, D>,
) -> (String, String)
where
D: nalgebra::Dim,
nalgebra::DefaultAllocator: nalgebra::base::allocator::Allocator<f64, D>,
nalgebra::DefaultAllocator: nalgebra::base::allocator::Allocator<f64, D, D>,
{
(vector.to_string(), matrix.to_string())
}
#[test]
fn omatrix_to_string() {
let dvec: nalgebra::DVector<f64> = nalgebra::dvector![1.0, 2.0];
let dmatr: nalgebra::DMatrix<f64> = nalgebra::dmatrix![1.0, 2.0; 3.0, 4.0];
let svec: nalgebra::SVector<f64, 2> = nalgebra::vector![1.0, 2.0];
let smatr: nalgebra::SMatrix<f64, 2, 2> = 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())
);
}