Skip to content

Commit

Permalink
feat!: use std vectors for in public API of the domain
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Nov 14, 2023
1 parent 42da442 commit 16de444
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::analysis::estimate_magnitude_from_bounds;
use crate::core::Sample;

/// Domain for a problem.
#[derive(Clone)]
pub struct Domain<T: RealField + Copy> {
lower: OVector<T, na::Dynamic>,
upper: OVector<T, na::Dynamic>,
Expand Down Expand Up @@ -38,23 +39,25 @@ impl<T: RealField + Copy> Domain<T> {
/// Positive and negative infinity can be used to indicate value unbounded
/// in that dimension and direction. If the entire domain is unconstrained,
/// use [`Domain::unconstrained`] instead.
pub fn rect(lower: OVector<T, na::Dynamic>, upper: OVector<T, na::Dynamic>) -> Self {
assert!(lower.ncols() == 1, "lower is not a column vector");
assert!(upper.ncols() == 1, "upper is not a column vector");
pub fn rect(lower: Vec<T>, upper: Vec<T>) -> Self {
assert!(
lower.ncols() == upper.ncols(),
lower.len() == upper.len(),
"lower and upper have different size"
);

let dim = lower.nrows();
let dim = lower.len();
assert!(dim > 0, "empty domain");

let scale = lower
.iter()
.copied()
.zip(upper.iter().copied())
.map(|(l, u)| estimate_magnitude_from_bounds(l, u));
let scale = OVector::from_iterator_generic(na::Dynamic::new(dim), na::Const::<1>, scale);

let dim = na::Dynamic::new(dim);
let scale = OVector::from_iterator_generic(dim, na::U1::name(), scale);
let lower = OVector::from_iterator_generic(dim, na::U1::name(), lower);
let upper = OVector::from_iterator_generic(dim, na::U1::name(), upper);

Self {
lower,
Expand All @@ -67,13 +70,15 @@ impl<T: RealField + Copy> Domain<T> {
///
/// Scale value of a variable is the inverse of the expected magnitude of
/// that variable.
pub fn with_scale(mut self, scale: OVector<T, na::Dynamic>) -> Self {
assert!(scale.ncols() == 1, "scale is not a column vector");
pub fn with_scale(mut self, scale: Vec<T>) -> Self {
assert!(
scale.ncols() == self.lower.ncols(),
scale.len() == self.lower.nrows(),
"scale has invalid dimension"
);

let dim = na::Dynamic::new(self.lower.nrows());
let scale = OVector::from_iterator_generic(dim, na::U1::name(), scale);

self.scale = Some(scale);
self
}
Expand Down Expand Up @@ -168,12 +173,6 @@ impl<T: RealField + Copy> Domain<T> {
impl<T: RealField + Copy> FromIterator<(T, T)> for Domain<T> {
fn from_iter<I: IntoIterator<Item = (T, T)>>(iter: I) -> Self {
let (lower, upper): (Vec<_>, Vec<_>) = iter.into_iter().unzip();

let n = na::Dynamic::new(lower.len());

let lower = OVector::from_vec_generic(n, na::U1::name(), lower);
let upper = OVector::from_vec_generic(n, na::U1::name(), upper);

Self::rect(lower, upper)
}
}
Expand All @@ -186,11 +185,6 @@ impl<T: RealField + Copy> FromIterator<T> for Domain<T> {
.map(|magnitude| one / magnitude)
.collect::<Vec<_>>();

let dim = scale.len();
let n = na::Dynamic::new(dim);

let scale = OVector::from_vec_generic(n, na::U1::name(), scale);

Self::unconstrained(dim).with_scale(scale)
Self::unconstrained(scale.len()).with_scale(scale)
}
}

0 comments on commit 16de444

Please sign in to comment.