Skip to content

Commit

Permalink
feat!: update nalgebra
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Nov 14, 2023
1 parent f9fba37 commit d2c229b
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 290 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2021"
testing = []

[dependencies]
nalgebra = "0.31"
nalgebra = "0.32.3"
approx = "0.5"
num-traits = "0.2"
thiserror = "1"
Expand Down
10 changes: 5 additions & 5 deletions examples/rosenbrock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gomez::nalgebra as na;
use gomez::{Domain, Problem, SolverDriver, System};
use na::{Dynamic, IsContiguous};
use na::{Dyn, IsContiguous};

// https://en.wikipedia.org/wiki/Rosenbrock_function
struct Rosenbrock {
Expand All @@ -19,11 +19,11 @@ impl Problem for Rosenbrock {
impl System for Rosenbrock {
fn eval<Sx, Sfx>(
&self,
x: &na::Vector<Self::Field, Dynamic, Sx>,
fx: &mut na::Vector<Self::Field, Dynamic, Sfx>,
x: &na::Vector<Self::Field, Dyn, Sx>,
fx: &mut na::Vector<Self::Field, Dyn, Sfx>,
) where
Sx: na::storage::Storage<Self::Field, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Field, Dynamic>,
Sx: na::storage::Storage<Self::Field, Dyn> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Field, Dyn>,
{
fx[0] = (self.a - x[0]).powi(2);
fx[1] = self.b * (x[1] - x[0].powi(2)).powi(2);
Expand Down
24 changes: 12 additions & 12 deletions gomez-bench/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
use gomez::{
algo::{NelderMead, TrustRegion},
nalgebra as na,
nalgebra::Dynamic,
nalgebra::Dyn,
testing::*,
Domain, Problem, Solver,
};
Expand Down Expand Up @@ -168,7 +168,7 @@ mod bullard_biegler {
fn bench_solve<F, S, GF, GS>(bencher: divan::Bencher, with_system: GF, with_solver: GS)
where
GF: Fn() -> (F, usize),
GS: Fn(&F, &Domain<F::Field>, &na::OVector<F::Field, Dynamic>) -> S,
GS: Fn(&F, &Domain<F::Field>, &na::OVector<F::Field, Dyn>) -> S,
F: TestSystem,
S: Solver<F>,
{
Expand Down Expand Up @@ -204,7 +204,7 @@ where
fn with_trust_region<F>(
f: &F,
dom: &Domain<F::Field>,
_: &na::OVector<F::Field, Dynamic>,
_: &na::OVector<F::Field, Dyn>,
) -> TrustRegion<F>
where
F: Problem,
Expand All @@ -215,7 +215,7 @@ where
fn with_nelder_mead<F>(
f: &F,
dom: &Domain<F::Field>,
_: &na::OVector<F::Field, Dynamic>,
_: &na::OVector<F::Field, Dyn>,
) -> NelderMead<F>
where
F: Problem,
Expand All @@ -226,7 +226,7 @@ where
fn with_gsl_hybrids<F>(
f: &F,
_: &Domain<F::Field>,
x: &na::OVector<F::Field, Dynamic>,
x: &na::OVector<F::Field, Dyn>,
) -> GslSolverWrapper<GslFunctionWrapper<F>>
where
F: TestSystem<Field = f64> + Clone,
Expand All @@ -251,14 +251,14 @@ impl<F> GslFunctionWrapper<F> {
impl<F: TestSystem<Field = f64>> GslFunction for GslFunctionWrapper<F> {
fn eval(&self, x: &GslVec, f: &mut GslVec) -> GslStatus {
use na::DimName;
let dim = Dynamic::new(x.len());
let dim = Dyn(x.len());

let x = na::MatrixSlice::<f64, Dynamic, na::U1>::from_slice_generic(
let x = na::MatrixView::<f64, Dyn, na::U1>::from_slice_generic(
x.as_slice(),
dim,
na::U1::name(),
);
let mut fx = na::MatrixSliceMut::<f64, Dynamic, na::U1>::from_slice_generic(
let mut fx = na::MatrixViewMut::<f64, Dyn, na::U1>::from_slice_generic(
f.as_mut_slice(),
dim,
na::U1::name(),
Expand Down Expand Up @@ -294,12 +294,12 @@ impl<F: TestSystem<Field = f64>> Solver<F> for GslSolverWrapper<GslFunctionWrapp
&mut self,
_f: &F,
_dom: &Domain<F::Field>,
x: &mut na::Vector<F::Field, Dynamic, Sx>,
fx: &mut na::Vector<F::Field, Dynamic, Sfx>,
x: &mut na::Vector<F::Field, Dyn, Sx>,
fx: &mut na::Vector<F::Field, Dyn, Sfx>,
) -> Result<(), Self::Error>
where
Sx: na::storage::StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<F::Field, Dynamic>,
Sx: na::storage::StorageMut<F::Field, Dyn> + IsContiguous,
Sfx: na::storage::StorageMut<F::Field, Dyn>,
{
let result = self.solver.step().to_result();
x.copy_from_slice(self.solver.root());
Expand Down
29 changes: 14 additions & 15 deletions src/algo/lipo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use fastrand::Rng;
use getset::{CopyGetters, Setters};
use log::{debug, trace};
use nalgebra::{
convert, try_convert, ComplexField, DimName, Dynamic, IsContiguous, OVector, StorageMut,
Vector, U1,
convert, try_convert, ComplexField, DimName, Dyn, IsContiguous, OVector, StorageMut, Vector, U1,
};
use num_traits::{One, Zero};
use thiserror::Error;
Expand Down Expand Up @@ -77,15 +76,15 @@ impl<F: Problem> Default for LipoOptions<F> {
pub struct Lipo<F: Problem> {
options: LipoOptions<F>,
alpha: F::Field,
xs: Vec<OVector<F::Field, Dynamic>>,
xs: Vec<OVector<F::Field, Dyn>>,
ys: Vec<F::Field>,
best: usize,
k: F::Field,
k_inf: F::Field,
rng: Rng,
p_explore: f64,
tmp: OVector<F::Field, Dynamic>,
x_tmp: OVector<F::Field, Dynamic>,
tmp: OVector<F::Field, Dyn>,
x_tmp: OVector<F::Field, Dyn>,
local_optimizer: NelderMead<F>,
iter: usize,
}
Expand All @@ -98,7 +97,7 @@ impl<F: Problem> Lipo<F> {

/// Initializes LIPO solver with given options.
pub fn with_options(f: &F, dom: &Domain<F::Field>, options: LipoOptions<F>, rng: Rng) -> Self {
let dim = Dynamic::new(dom.dim());
let dim = Dyn(dom.dim());

let p_explore = options.p_explore.clamp(0.0, 1.0);

Expand Down Expand Up @@ -141,7 +140,7 @@ impl<F: Problem> Lipo<F> {
/// the LIPO solver gives extra information for free.
pub fn add_evaluation(
&mut self,
x: OVector<F::Field, Dynamic>,
x: OVector<F::Field, Dyn>,
y: F::Field,
) -> Result<(), LipoError> {
let alpha = self.alpha;
Expand Down Expand Up @@ -216,10 +215,10 @@ where
&mut self,
f: &F,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
x: &mut Vector<F::Field, Dyn, Sx>,
) -> Result<F::Field, LipoError>
where
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
{
let LipoOptions {
sampling_trials,
Expand Down Expand Up @@ -388,10 +387,10 @@ where
&mut self,
f: &F,
dom: &Domain<<F>::Field>,
x: &mut Vector<<F>::Field, Dynamic, Sx>,
x: &mut Vector<<F>::Field, Dyn, Sx>,
) -> Result<<F>::Field, Self::Error>
where
Sx: StorageMut<<F>::Field, Dynamic> + IsContiguous,
Sx: StorageMut<<F>::Field, Dyn> + IsContiguous,
{
self.next_inner(f, dom, x)
}
Expand All @@ -409,12 +408,12 @@ where
&mut self,
f: &F,
dom: &Domain<<F>::Field>,
x: &mut Vector<<F>::Field, Dynamic, Sx>,
fx: &mut Vector<<F>::Field, Dynamic, Sfx>,
x: &mut Vector<<F>::Field, Dyn, Sx>,
fx: &mut Vector<<F>::Field, Dyn, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<<F>::Field, Dynamic> + IsContiguous,
Sfx: StorageMut<<F>::Field, Dynamic>,
Sx: StorageMut<<F>::Field, Dyn> + IsContiguous,
Sfx: StorageMut<<F>::Field, Dyn>,
{
self.next_inner(f, dom, x)?;
f.eval(x, fx);
Expand Down
32 changes: 16 additions & 16 deletions src/algo/nelder_mead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use log::debug;
use nalgebra::{
convert,
storage::{Storage, StorageMut},
ComplexField, Dim, DimName, Dynamic, IsContiguous, OVector, RealField, Vector, U1,
ComplexField, Dim, DimName, Dyn, IsContiguous, OVector, RealField, Vector, U1,
};
use num_traits::{One, Zero};
use thiserror::Error;
Expand Down Expand Up @@ -129,12 +129,12 @@ impl<F: Problem> NelderMeadOptions<F> {
/// Nelder-Mead solver. See [module](self) documentation for more details.
pub struct NelderMead<F: Problem> {
options: NelderMeadOptions<F>,
scale: OVector<F::Field, Dynamic>,
centroid: OVector<F::Field, Dynamic>,
reflection: OVector<F::Field, Dynamic>,
expansion: OVector<F::Field, Dynamic>,
contraction: OVector<F::Field, Dynamic>,
simplex: Vec<OVector<F::Field, Dynamic>>,
scale: OVector<F::Field, Dyn>,
centroid: OVector<F::Field, Dyn>,
reflection: OVector<F::Field, Dyn>,
expansion: OVector<F::Field, Dyn>,
contraction: OVector<F::Field, Dyn>,
simplex: Vec<OVector<F::Field, Dyn>>,
errors: Vec<F::Field>,
sort_perm: Vec<usize>,
}
Expand All @@ -147,7 +147,7 @@ impl<F: Problem> NelderMead<F> {

/// Initializes Nelder-Mead solver with given options.
pub fn with_options(_: &F, dom: &Domain<F::Field>, mut options: NelderMeadOptions<F>) -> Self {
let dim = Dynamic::new(dom.dim());
let dim = Dyn(dom.dim());

options.overwrite_coeffs(dom);

Expand Down Expand Up @@ -194,10 +194,10 @@ impl<F: Function> NelderMead<F> {
&mut self,
f: &F,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
x: &mut Vector<F::Field, Dyn, Sx>,
) -> Result<F::Field, NelderMeadError>
where
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
{
let NelderMeadOptions {
reflection_coeff,
Expand Down Expand Up @@ -458,10 +458,10 @@ impl<F: Function> Optimizer<F> for NelderMead<F> {
&mut self,
f: &F,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
x: &mut Vector<F::Field, Dyn, Sx>,
) -> Result<F::Field, Self::Error>
where
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
{
self.next_inner(f, dom, x)
}
Expand All @@ -476,12 +476,12 @@ impl<F: System + Function> Solver<F> for NelderMead<F> {
&mut self,
f: &F,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
fx: &mut Vector<F::Field, Dynamic, Sfx>,
x: &mut Vector<F::Field, Dyn, Sx>,
fx: &mut Vector<F::Field, Dyn, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Field, Dynamic>,
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sfx: StorageMut<F::Field, Dyn>,
{
self.next_inner(f, dom, x)?;
f.eval(x, fx);
Expand Down
12 changes: 6 additions & 6 deletions src/algo/steffensen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::marker::PhantomData;

use getset::{CopyGetters, Setters};
use nalgebra::{convert, storage::StorageMut, Dynamic, IsContiguous, Vector};
use nalgebra::{convert, storage::StorageMut, Dyn, IsContiguous, Vector};
use thiserror::Error;

use crate::core::{Domain, Problem, Solver, System};
Expand Down Expand Up @@ -88,12 +88,12 @@ impl<F: System> Solver<F> for Steffensen<F> {
&mut self,
f: &F,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
fx: &mut Vector<F::Field, Dynamic, Sfx>,
x: &mut Vector<F::Field, Dyn, Sx>,
fx: &mut Vector<F::Field, Dyn, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Field, Dynamic>,
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sfx: StorageMut<F::Field, Dyn>,
{
if dom.dim() != 1 {
return Err(SteffensenError::InvalidDimensionality);
Expand Down Expand Up @@ -214,7 +214,7 @@ mod tests {
let eps = convert(1e-12);
let solver = Steffensen::new(&f, &dom);

let x = OVector::from_element_generic(Dynamic::new(dom.dim()), U1::name(), 0.0);
let x = OVector::from_element_generic(Dyn(dom.dim()), U1::name(), 0.0);
assert!(f.is_root(&solve(&f, &dom, solver, x, 40, eps).unwrap(), eps));
}
}
Loading

0 comments on commit d2c229b

Please sign in to comment.