Skip to content

Commit

Permalink
feat!: Update nalgebra to v0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Apr 12, 2022
1 parent c84446a commit e31c75a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["science", "mathematics"]
edition = "2021"

[dependencies]
nalgebra = "0.28"
nalgebra = "0.30"
rand = "0.8"
rand_distr = "0.4"
approx = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion src/core/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::domain::Domain;
/// [`Function`](super::function::Function).
pub trait Problem {
/// Type of the scalar, usually f32 or f64.
type Scalar: RealField;
type Scalar: RealField + Copy;

/// Dimension of the system. Can be fixed
/// ([`Const`](nalgebra::base::dimension::Const)) or dynamic
Expand Down
26 changes: 13 additions & 13 deletions src/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use nalgebra::{storage::StorageMut, Dim, RealField, Vector};

/// [`Variable`] builder type.
#[derive(Debug, Clone, Copy)]
pub struct VariableBuilder<T: RealField>(Variable<T>);
pub struct VariableBuilder<T: RealField + Copy>(Variable<T>);

impl<T: RealField> VariableBuilder<T> {
impl<T: RealField + Copy> VariableBuilder<T> {
fn new() -> Self {
Self(Variable::new())
}
Expand Down Expand Up @@ -41,12 +41,12 @@ impl<T: RealField> VariableBuilder<T> {
/// * [`Magnitude`](Variable::set_magnitude) is used to compensate scaling
/// discrepancies between different variables.
#[derive(Debug, Clone, Copy)]
pub struct Variable<T: RealField> {
pub struct Variable<T: RealField + Copy> {
bounds: (T, T),
magnitude: T,
}

impl<T: RealField> Variable<T> {
impl<T: RealField + Copy> Variable<T> {
/// Creates new unconstrained variable with magnitude 1.
pub fn new() -> Self {
let inf = T::from_subset(&f64::INFINITY);
Expand Down Expand Up @@ -129,19 +129,19 @@ impl<T: RealField> Variable<T> {
}
}

impl<T: RealField> Default for Variable<T> {
impl<T: RealField + Copy> Default for Variable<T> {
fn default() -> Self {
Self::new()
}
}

impl<T: RealField> From<VariableBuilder<T>> for Variable<T> {
impl<T: RealField + Copy> From<VariableBuilder<T>> for Variable<T> {
fn from(def: VariableBuilder<T>) -> Self {
def.finalize()
}
}

fn estimate_magnitude<T: RealField>(lower: T, upper: T) -> T {
fn estimate_magnitude<T: RealField + Copy>(lower: T, upper: T) -> T {
let ten = T::from_subset(&10.0);
let half = T::from_subset(&0.5);

Expand Down Expand Up @@ -198,11 +198,11 @@ macro_rules! var {

/// A set of [`Variable`] definitions.
// TODO: Add generic type for nalgebra dimension?
pub struct Domain<T: RealField> {
pub struct Domain<T: RealField + Copy> {
vars: Vec<Variable<T>>,
}

impl<T: RealField> Domain<T> {
impl<T: RealField + Copy> Domain<T> {
/// Creates unconstrained domain with given dimension.
pub fn with_dim(n: usize) -> Self {
(0..n).map(|_| Variable::default()).collect()
Expand All @@ -225,28 +225,28 @@ impl<T: RealField> Domain<T> {
}
}

impl<T: RealField> FromIterator<Variable<T>> for Domain<T> {
impl<T: RealField + Copy> FromIterator<Variable<T>> for Domain<T> {
fn from_iter<I: IntoIterator<Item = Variable<T>>>(iter: I) -> Self {
Self::with_vars(iter.into_iter().collect())
}
}

impl<T: RealField> From<Vec<Variable<T>>> for Domain<T> {
impl<T: RealField + Copy> From<Vec<Variable<T>>> for Domain<T> {
fn from(vars: Vec<Variable<T>>) -> Self {
Self::with_vars(vars)
}
}

/// Domain-related extension methods for [`Vector`], which is a common storage
/// for variable values.
pub trait VectorDomainExt<T: RealField, D: Dim> {
pub trait VectorDomainExt<T: RealField + Copy, D: Dim> {
/// Clamp all values within corresponding bounds and returns if the original
/// value was outside of bounds (in other bounds, the point was not
/// feasible).
fn project(&mut self, dom: &Domain<T>) -> bool;
}

impl<T: RealField, D: Dim, S> VectorDomainExt<T, D> for Vector<T, D, S>
impl<T: RealField + Copy, D: Dim, S> VectorDomainExt<T, D> for Vector<T, D, S>
where
S: StorageMut<T, D>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/population.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ where
/// # let mut population = Population::zeros(&f, PopulationSize::Adaptive);
/// #
/// for mut x in population.iter_mut() {
/// x.apply(|_| rng.sample(StandardNormal));
/// x.apply(|xi| *xi = rng.sample(StandardNormal));
/// let error = x.eval(&f, &mut fx).unwrap();
/// // Without the following line the code would panic!
/// // Because `x.apply` mutably borrows `x`.
Expand Down
6 changes: 3 additions & 3 deletions src/solver/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ where
}

*next *= scale_factor;
next.apply(|uj| uj * rng.sample(StandardNormal));
next.apply(|uj| *uj *= rng.sample(StandardNormal));
*next += &*temp;

// Make sure that the candidate is in domain.
Expand Down Expand Up @@ -289,10 +289,10 @@ where
temp.copy_from(&next_gen.get(rand_perm2[i]).unwrap());
*next -= &*temp;
next.apply(|uj| {
if rng.gen_bool(abandon_prob) {
*uj = if rng.gen_bool(abandon_prob) {
F::Scalar::zero()
} else {
uj * convert(rng.gen_range(0f64..=1.0))
*uj * convert(rng.gen_range(0f64..=1.0))
}
});
temp.copy_from(&x);
Expand Down
2 changes: 1 addition & 1 deletion src/solver/trust_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ where
// Compute D^(-2) (use p for storage).
scale_inv2 = p;
scale_inv2.copy_from(scale);
scale_inv2.apply(|s| F::Scalar::one() / (s * s));
scale_inv2.apply(|s| *s = F::Scalar::one() / (*s * *s));

// Compute g = -D^(-2) grad F, the steepest descent direction in
// scaled space (use cauchy for storage).
Expand Down

0 comments on commit e31c75a

Please sign in to comment.