//! Test functionality for generating random Monge matrices.
// The code is put here so we can reuse it in different integration // tests, without Cargo finding it when `cargo test` is run. See the // section on "Submodules in Integration Tests" in // https://doc.rust-lang.org/book/ch11-03-test-organization.html
use ndarray::{s, Array2}; use num_traits::PrimInt; use rand::distributions::{Distribution, Standard}; use rand::Rng;
/// A Monge matrix can be decomposed into one of these primitive /// building blocks. #[derive(Copy, Clone)] pubenum MongePrim {
ConstantRows,
ConstantCols,
UpperRightOnes,
LowerLeftOnes,
}
impl MongePrim { /// Generate a Monge matrix from a primitive. pubfn to_matrix<T: PrimInt, R: Rng>(&self, m: usize, n: usize, rng: &an style='color:red'>mut R) -> Array2<T> where
Standard: Distribution<T>,
{ letmut matrix = Array2::from_elem((m, n), T::zero()); // Avoid panic in UpperRightOnes and LowerLeftOnes below. if m == 0 || n == 0 { return matrix;
}
match *self {
MongePrim::ConstantRows => { formut row in matrix.rows_mut() { if rng.gen::<bool>() {
row.fill(T::one())
}
}
}
MongePrim::ConstantCols => { formut col in matrix.columns_mut() { if rng.gen::<bool>() {
col.fill(T::one())
}
}
}
MongePrim::UpperRightOnes => { let i = rng.gen_range(0..(m + 1) as isize); let j = rng.gen_range(0..(n + 1) as isize);
matrix.slice_mut(s![..i, -j..]).fill(T::one());
}
MongePrim::LowerLeftOnes => { let i = rng.gen_range(0..(m + 1) as isize); let j = rng.gen_range(0..(n + 1) as isize);
matrix.slice_mut(s![-i.., ..j]).fill(T::one());
}
}
matrix
}
}
/// Generate a random Monge matrix. pubfn random_monge_matrix<R: Rng, T: PrimInt>(m: usize, n: usize, rng: &mut R) -> Array2<T> where
Standard: Distribution<T>,
{ let monge_primitives = [
MongePrim::ConstantRows,
MongePrim::ConstantCols,
MongePrim::LowerLeftOnes,
MongePrim::UpperRightOnes,
]; letmut matrix = Array2::from_elem((m, n), T::zero()); for _ in0..(m + n) { let monge = monge_primitives[rng.gen_range(0..monge_primitives.len())];
matrix = matrix + monge.to_matrix(m, n, rng);
}
matrix
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-22)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.