Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/structs/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ impl Matrix {
}
}

/// Constructs a Matrix with shape, optional name and every element set to
/// `value`.
///
/// The column padding stays zeroed, as with [`Matrix::new`].
pub fn fill(n_rows: usize, n_cols: usize, value: f64, name: Option<impl Into<String>>) -> Self {
let mut out = Self::new(n_rows, n_cols, name);
let stride = out.stride;
let data = Arc::make_mut(&mut out.data).as_mut_slice();
for j in 0..n_cols {
data[j * stride..j * stride + n_rows].fill(value);
}
out
}

/// Constructs a Matrix from a pre-padded Vec64 buffer.
/// The buffer must already have `stride * n_cols` elements with the correct
/// stride layout. Use `from_f64_unaligned` if your data is unpadded.
Expand Down Expand Up @@ -1430,6 +1444,18 @@ mod buffer_sharing_tests {
);
}

#[test]
fn fill_sets_every_element_and_leaves_padding_zeroed() {
let m = Matrix::fill(3, 2, f64::NAN, Some("filled"));
assert_eq!((m.n_rows, m.n_cols, m.stride), (3, 2, 8));
assert_eq!(m.name.as_deref(), Some("filled"));
for j in 0..2 {
assert!(m.col(j).iter().all(|v| v.is_nan()));
assert!(m.as_slice()[j * m.stride + 3..(j + 1) * m.stride].iter().all(|&v| v == 0.0));
}
assert_eq!(Matrix::fill(2, 2, 0.0, None::<String>), Matrix::new(2, 2, None::<String>));
}

#[test]
fn as_tuple_describes_the_whole_matrix() {
let m = mat![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
Expand Down
Loading