Skip to content

Commit

Permalink
feat!: Require contiguous storage for x values
Browse files Browse the repository at this point in the history
In my code, I often need to pass x to an evalution procedure that takes
&[f64] slice. Therefore I had to collect the values to a newly allocated
Vec in order to achieve that. To me, requiring contiguous storage for x
is reasonable limitation as it usually is a vector.

For fx, contiguous storage is not required at the moment as I haven't
encountered a situation where I needed it.
  • Loading branch information
pnevyk committed Apr 12, 2022
1 parent e31c75a commit b63317d
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 53 deletions.
5 changes: 3 additions & 2 deletions benches/solvers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use gomez::{
nalgebra as na,
prelude::*,
solver::{NelderMead, TrustRegion},
testing::*,
Expand All @@ -9,7 +10,7 @@ use gsl_wrapper::{
multiroot::{Solver as GslSolver, SolverType::HybridScaled},
prelude::*,
};
use nalgebra as na;
use na::IsContiguous;

const MAX_ITERS: usize = 1_000_000;
const TOLERANCE: f64 = 1e-12;
Expand Down Expand Up @@ -275,7 +276,7 @@ where
fx: &mut na::Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<(), Self::Error>
where
Sx: na::storage::StorageMut<F::Scalar, F::Dim>,
Sx: na::storage::StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sfx: na::storage::StorageMut<F::Scalar, F::Dim>,
{
let result = self.solver.step().to_result();
Expand Down
4 changes: 2 additions & 2 deletions examples/rosenbrock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use gomez::nalgebra as na;
use gomez::prelude::*;
use gomez::solver::TrustRegion;
use na::{Dim, DimName};
use na::{Dim, DimName, IsContiguous};

// https://en.wikipedia.org/wiki/Rosenbrock_function
struct Rosenbrock {
Expand All @@ -25,7 +25,7 @@ impl System for Rosenbrock {
fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
where
Sx: na::storage::Storage<Self::Scalar, Self::Dim>,
Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
{
fx[0] = (self.a - x[0]).powi(2);
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::marker::PhantomData;

use nalgebra::{
allocator::Allocator, convert, storage::StorageMut, ComplexField, DefaultAllocator, DimMin,
DimName, OVector, Vector, U1,
DimName, IsContiguous, OVector, Vector, U1,
};
use thiserror::Error;

Expand Down Expand Up @@ -52,7 +52,7 @@ where
fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<Self, InitialGuessAnalysisError>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sfx: StorageMut<F::Scalar, F::Dim>,
{
let scale_iter = dom.vars().iter().map(|var| var.scale());
Expand Down
15 changes: 8 additions & 7 deletions src/core/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use nalgebra::{
allocator::Allocator, storage::Storage, storage::StorageMut, DefaultAllocator, Vector,
allocator::Allocator, storage::Storage, storage::StorageMut, DefaultAllocator, IsContiguous,
Vector,
};
use num_traits::Zero;

Expand All @@ -19,7 +20,7 @@ use super::{
/// ```rust
/// use gomez::nalgebra as na;
/// use gomez::prelude::*;
/// use na::{Dim, DimName};
/// use na::{Dim, DimName, IsContiguous};
///
/// // A problem is represented by a type.
/// struct Rosenbrock {
Expand All @@ -46,7 +47,7 @@ use super::{
/// x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
/// ) -> Result<Self::Scalar, Error>
/// where
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim>,
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
/// {
/// // Compute the function value.
/// Ok((self.a - x[0]).powi(2) + self.b * (x[1] - x[0].powi(2)).powi(2))
Expand All @@ -57,7 +58,7 @@ pub trait Function: Problem {
/// Calculate the function value given values of the variables.
fn apply<Sx>(&self, x: &Vector<Self::Scalar, Self::Dim, Sx>) -> Result<Self::Scalar, Error>
where
Sx: Storage<Self::Scalar, Self::Dim>;
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous;

/// Calculate the norm of residuals of the system given values of the
/// variable for cases when the function is actually a system of equations.
Expand All @@ -71,7 +72,7 @@ pub trait Function: Problem {
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<Self::Scalar, Error>
where
Sx: Storage<Self::Scalar, Self::Dim>,
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
{
let norm = self.apply(x)?;
Expand All @@ -87,7 +88,7 @@ where
{
fn apply<Sx>(&self, x: &Vector<Self::Scalar, Self::Dim, Sx>) -> Result<Self::Scalar, Error>
where
Sx: Storage<Self::Scalar, Self::Dim>,
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
{
let mut fx = x.clone_owned();
self.apply_eval(x, &mut fx)
Expand All @@ -99,7 +100,7 @@ where
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<Self::Scalar, Error>
where
Sx: Storage<Self::Scalar, Self::Dim>,
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
{
self.eval(x, fx)?;
Expand Down
8 changes: 4 additions & 4 deletions src/core/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nalgebra::{storage::StorageMut, Vector};
use nalgebra::{storage::StorageMut, IsContiguous, Vector};

use super::{domain::Domain, function::Function};

Expand All @@ -22,7 +22,7 @@ use super::{domain::Domain, function::Function};
/// ```rust
/// use gomez::nalgebra as na;
/// use gomez::core::*;
/// use na::{storage::StorageMut, Vector};
/// use na::{storage::StorageMut, IsContiguous, Vector};
/// use rand::Rng;
/// use rand_distr::{uniform::SampleUniform, Distribution, Uniform};
///
Expand Down Expand Up @@ -50,7 +50,7 @@ use super::{domain::Domain, function::Function};
/// x: &mut Vector<F::Scalar, F::Dim, Sx>,
/// ) -> Result<F::Scalar, Self::Error>
/// where
/// Sx: StorageMut<F::Scalar, F::Dim>,
/// Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
/// {
/// // Randomly sample within the bounds.
/// x.iter_mut().zip(dom.vars().iter()).for_each(|(xi, vi)| {
Expand Down Expand Up @@ -92,5 +92,5 @@ pub trait Optimizer<F: Function> {
x: &mut Vector<F::Scalar, F::Dim, Sx>,
) -> Result<F::Scalar, Self::Error>
where
Sx: StorageMut<F::Scalar, F::Dim>;
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous;
}
8 changes: 4 additions & 4 deletions src/core/solver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nalgebra::{storage::StorageMut, Vector};
use nalgebra::{storage::StorageMut, IsContiguous, Vector};

use super::{domain::Domain, system::System};

Expand All @@ -22,7 +22,7 @@ use super::{domain::Domain, system::System};
/// ```rust
/// use gomez::nalgebra as na;
/// use gomez::core::*;
/// use na::{storage::StorageMut, Vector};
/// use na::{storage::StorageMut, IsContiguous, Vector};
/// use rand::Rng;
/// use rand_distr::{uniform::SampleUniform, Distribution, Uniform};
///
Expand Down Expand Up @@ -51,7 +51,7 @@ use super::{domain::Domain, system::System};
/// fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
/// ) -> Result<(), Self::Error>
/// where
/// Sx: StorageMut<F::Scalar, F::Dim>,
/// Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
/// Sfx: StorageMut<F::Scalar, F::Dim>,
/// {
/// // Randomly sample within the bounds.
Expand Down Expand Up @@ -96,6 +96,6 @@ pub trait Solver<F: System> {
fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sfx: StorageMut<F::Scalar, F::Dim>;
}
10 changes: 5 additions & 5 deletions src/core/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nalgebra::{
allocator::Allocator,
storage::{Storage, StorageMut},
DefaultAllocator, OVector, Vector,
DefaultAllocator, IsContiguous, OVector, Vector,
};

use super::{
Expand All @@ -20,7 +20,7 @@ use super::{
/// ```rust
/// use gomez::nalgebra as na;
/// use gomez::prelude::*;
/// use na::{Dim, DimName};
/// use na::{Dim, DimName, IsContiguous};
///
/// // A problem is represented by a type.
/// struct Rosenbrock {
Expand Down Expand Up @@ -48,7 +48,7 @@ use super::{
/// fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
/// ) -> Result<(), Error>
/// where
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim>,
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
/// Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
/// {
/// // Compute the residuals of all equations.
Expand All @@ -67,7 +67,7 @@ pub trait System: Problem {
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
where
Sx: Storage<Self::Scalar, Self::Dim>,
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>;
}

Expand Down Expand Up @@ -150,7 +150,7 @@ where
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
where
Sx: Storage<Self::Scalar, Self::Dim>,
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
{
// TODO: RepulsiveSystem should adjust the residuals of the inner system
Expand Down
6 changes: 3 additions & 3 deletions src/derivatives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use nalgebra::{
allocator::Allocator,
convert,
storage::{Storage, StorageMut},
ComplexField, DefaultAllocator, OMatrix, RealField, Vector,
ComplexField, DefaultAllocator, IsContiguous, OMatrix, RealField, Vector,
};
use num_traits::{One, Zero};
use thiserror::Error;
Expand Down Expand Up @@ -60,7 +60,7 @@ where
fx: &Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<Self, JacobianError>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sscale: Storage<F::Scalar, F::Dim>,
Sfx: Storage<F::Scalar, F::Dim>,
{
Expand All @@ -86,7 +86,7 @@ where
fx: &Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<&mut Self, JacobianError>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sscale: Storage<F::Scalar, F::Dim>,
Sfx: Storage<F::Scalar, F::Dim>,
{
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
//! // Gomez is based on `nalgebra` crate.
//! use gomez::nalgebra as na;
//! use gomez::prelude::*;
//! use na::{Dim, DimName};
//! use na::{Dim, DimName, IsContiguous};
//!
//! // A problem is represented by a type.
//! struct Rosenbrock {
Expand Down Expand Up @@ -98,7 +98,7 @@
//! fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
//! ) -> Result<(), Error>
//! where
//! Sx: na::storage::Storage<Self::Scalar, Self::Dim>,
//! Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
//! Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
//! {
//! // Compute the residuals of all equations.
Expand Down Expand Up @@ -161,7 +161,7 @@
//! use gomez::prelude::*;
//! // Pick your solver.
//! use gomez::solver::TrustRegion;
//! # use na::{Dim, DimName};
//! # use na::{Dim, DimName, IsContiguous};
//! #
//! # struct Rosenbrock {
//! # a: f64,
Expand All @@ -184,7 +184,7 @@
//! # fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
//! # ) -> Result<(), Error>
//! # where
//! # Sx: na::storage::Storage<Self::Scalar, Self::Dim>,
//! # Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
//! # Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
//! # {
//! # fx[0] = (self.a - x[0]).powi(2);
Expand Down
8 changes: 4 additions & 4 deletions src/solver/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use nalgebra::{
allocator::{Allocator, Reallocator},
convert,
storage::StorageMut,
DefaultAllocator, DimMin, DimName, Dynamic, OVector, Vector, U1,
DefaultAllocator, DimMin, DimName, Dynamic, IsContiguous, OVector, Vector, U1,
};
use num_traits::{Float, Zero};
use rand::Rng;
Expand Down Expand Up @@ -213,7 +213,7 @@ where
x: &mut Vector<F::Scalar, F::Dim, Sx>,
) -> Result<F::Scalar, CuckooError>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
{
let CuckooOptions {
scale_factor,
Expand Down Expand Up @@ -360,7 +360,7 @@ where
x: &mut Vector<F::Scalar, F::Dim, Sx>,
) -> Result<F::Scalar, Self::Error>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
{
self.next_inner(f, dom, x)
}
Expand All @@ -387,7 +387,7 @@ where
fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
Sfx: StorageMut<F::Scalar, F::Dim>,
{
self.next_inner(f, dom, x)
Expand Down
8 changes: 4 additions & 4 deletions src/solver/nelder_mead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use nalgebra::{
allocator::Allocator,
convert,
storage::{Storage, StorageMut},
DefaultAllocator, Dim, DimName, OVector, RealField, Vector, U1,
DefaultAllocator, Dim, DimName, IsContiguous, OVector, RealField, Vector, U1,
};
use num_traits::{One, Zero};
use thiserror::Error;
Expand Down Expand Up @@ -200,7 +200,7 @@ where
x: &mut Vector<<F>::Scalar, <F>::Dim, Sx>,
) -> Result<F::Scalar, NelderMeadError>
where
Sx: StorageMut<<F>::Scalar, <F>::Dim>,
Sx: StorageMut<<F>::Scalar, <F>::Dim> + IsContiguous,
{
let NelderMeadOptions {
reflection_coeff,
Expand Down Expand Up @@ -488,7 +488,7 @@ where
x: &mut Vector<F::Scalar, F::Dim, Sx>,
) -> Result<F::Scalar, Self::Error>
where
Sx: StorageMut<F::Scalar, F::Dim>,
Sx: StorageMut<F::Scalar, F::Dim> + IsContiguous,
{
self.next_inner(f, dom, x)
}
Expand All @@ -511,7 +511,7 @@ where
fx: &mut Vector<<F>::Scalar, <F>::Dim, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<<F>::Scalar, <F>::Dim>,
Sx: StorageMut<<F>::Scalar, <F>::Dim> + IsContiguous,
Sfx: StorageMut<<F>::Scalar, <F>::Dim>,
{
self.next_inner(f, dom, x).map(|_| {
Expand Down
4 changes: 2 additions & 2 deletions src/solver/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::{storage::StorageMut, Dim, Vector};
use nalgebra::{storage::StorageMut, Dim, IsContiguous, Vector};
use thiserror::Error;

use crate::core::{Domain, Error, Problem, Solver, System, VectorDomainExt};
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<F: System> Solver<F> for Steffensen<F> {
fx: &mut Vector<<F>::Scalar, <F>::Dim, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<<F>::Scalar, <F>::Dim>,
Sx: StorageMut<<F>::Scalar, <F>::Dim> + IsContiguous,
Sfx: StorageMut<<F>::Scalar, <F>::Dim>,
{
if f.dim().value() != 1 {
Expand Down
Loading

0 comments on commit b63317d

Please sign in to comment.