Skip to content

Commit

Permalink
implemented LowerExp
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Avital authored and sebcrozet committed Sep 8, 2019
1 parent 04eb817 commit 04e322f
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,89 @@ where
}
}

impl<N, R: Dim, C: Dim, S> fmt::LowerExp for Matrix<N, R, C, S>
where
N: Scalar + fmt::LowerExp,
S: Storage<N, R, C>,
DefaultAllocator: Allocator<usize, R, C>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(feature = "std")]
fn val_width<N: Scalar + fmt::LowerExp>(val: N, f: &mut fmt::Formatter) -> usize {
match f.precision() {
Some(precision) => format!("{:.1$e}", val, precision).chars().count(),
None => format!("{:e}", val).chars().count(),
}
}

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

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

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

let mut max_length = 0;
let mut lengths: MatrixMN<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)]);
}
}

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 = lengths[(i, j)] + 1;
let pad = max_length_with_space - number_length;
write!(f, " {:>thepad$}", "", thepad = pad)?;
match f.precision() {
Some(precision) => write!(f, "{:.1$e}", (*self)[(i, j)], precision)?,
None => write!(f, "{:e}", (*self)[(i, j)])?,
}
}
writeln!(f, " │")?;
}

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

#[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 │
└ ┘
")
}

impl<N: Scalar + Ring, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> {
/// The perpendicular product between two 2D column vectors, i.e. `a.x * b.y - a.y * b.x`.
#[inline]
Expand Down

0 comments on commit 04e322f

Please sign in to comment.