Skip to content

Commit

Permalink
feat!: Rename Error to ProblemError
Browse files Browse the repository at this point in the history
The previous name was too generic and had high chance for clashing (with
std::error::Error at least).
  • Loading branch information
pnevyk committed Apr 12, 2022
1 parent b63317d commit 0aa2397
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/rosenbrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl System for Rosenbrock {
&self,
x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
) -> Result<(), ProblemError>
where
Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nalgebra::{
use thiserror::Error;

use crate::{
core::{Domain, Error, Problem, System},
core::{Domain, Problem, ProblemError, System},
derivatives::{Jacobian, JacobianError, EPSILON_SQRT},
};

Expand All @@ -25,7 +25,7 @@ use crate::{
pub enum InitialGuessAnalysisError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
System(#[from] Error),
System(#[from] ProblemError),
/// Error that occurred when computing the Jacobian matrix.
#[error("{0}")]
Jacobian(#[from] JacobianError),
Expand Down
4 changes: 2 additions & 2 deletions src/core/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub trait Problem {
}
}

/// Error encountered while applying variables to the function.
/// Error encountered while applying variables to the problem.
#[derive(Debug, Error)]
pub enum Error {
pub enum ProblemError {
/// The number of variables does not match the dimensionality
/// ([`Problem::dim`]) of the problem.
#[error("invalid dimensionality")]
Expand Down
26 changes: 16 additions & 10 deletions src/core/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nalgebra::{
use num_traits::Zero;

use super::{
base::{Error, Problem},
base::{Problem, ProblemError},
system::System,
};

Expand Down Expand Up @@ -45,7 +45,7 @@ use super::{
/// fn apply<Sx>(
/// &self,
/// x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
/// ) -> Result<Self::Scalar, Error>
/// ) -> Result<Self::Scalar, ProblemError>
/// where
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
/// {
Expand All @@ -56,7 +56,10 @@ use super::{
/// ```
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>
fn apply<Sx>(
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
) -> Result<Self::Scalar, ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous;

Expand All @@ -70,7 +73,7 @@ pub trait Function: Problem {
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<Self::Scalar, Error>
) -> Result<Self::Scalar, ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
Expand All @@ -86,7 +89,10 @@ impl<F: System> Function for F
where
DefaultAllocator: Allocator<F::Scalar, F::Dim>,
{
fn apply<Sx>(&self, x: &Vector<Self::Scalar, Self::Dim, Sx>) -> Result<Self::Scalar, Error>
fn apply<Sx>(
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
) -> Result<Self::Scalar, ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
{
Expand All @@ -98,7 +104,7 @@ where
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<Self::Scalar, Error>
) -> Result<Self::Scalar, ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
Expand All @@ -111,16 +117,16 @@ where

/// Extension trait for `Result<F::Scalar, Error>`.
pub trait FunctionResultExt<T> {
/// If the result is [`Error::InvalidValue`], `Ok(default)` is returned
/// instead. The original result is returned otherwise.
/// If the result is [`ProblemError::InvalidValue`], `Ok(default)` is
/// returned instead. The original result is returned otherwise.
fn ignore_invalid_value(self, replace_with: T) -> Self;
}

impl<T> FunctionResultExt<T> for Result<T, Error> {
impl<T> FunctionResultExt<T> for Result<T, ProblemError> {
fn ignore_invalid_value(self, replace_with: T) -> Self {
match self {
Ok(value) => Ok(value),
Err(Error::InvalidValue) => Ok(replace_with),
Err(ProblemError::InvalidValue) => Ok(replace_with),
Err(error) => Err(error),
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use super::{domain::Domain, function::Function};
/// F::Scalar: SampleUniform,
/// {
/// const NAME: &'static str = "Random";
/// type Error = Error;
/// type Error = ProblemError;
///
/// fn next<Sx>(
/// &mut self,
Expand Down Expand Up @@ -69,7 +69,8 @@ pub trait Optimizer<F: Function> {

/// Error type of the iteration. Represents an invalid operation during
/// computing the next step. It is usual that one of the error kinds is
/// propagation of the [`Error`](super::Error) from the function.
/// propagation of the [`ProblemError`](super::ProblemError) from the
/// function.
type Error;

/// Computes the next step in the optimization process.
Expand Down
5 changes: 3 additions & 2 deletions src/core/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use super::{domain::Domain, system::System};
/// F::Scalar: SampleUniform,
/// {
/// const NAME: &'static str = "Random";
/// type Error = Error;
/// type Error = ProblemError;
///
/// fn next<Sx, Sfx>(
/// &mut self,
Expand Down Expand Up @@ -72,7 +72,8 @@ pub trait Solver<F: System> {

/// Error type of the iteration. Represents an invalid operation during
/// computing the next step. It is usual that one of the error kinds is
/// propagation of the [`Error`](super::Error) from the system.
/// propagation of the [`ProblemError`](super::ProblemError) from the
/// system.
type Error;

/// Computes the next step in the solving process.
Expand Down
8 changes: 4 additions & 4 deletions src/core/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nalgebra::{
};

use super::{
base::{Error, Problem},
base::{Problem, ProblemError},
domain::Domain,
};

Expand Down Expand Up @@ -46,7 +46,7 @@ use super::{
/// &self,
/// x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
/// fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
/// ) -> Result<(), Error>
/// ) -> Result<(), ProblemError>
/// where
/// Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
/// Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
Expand All @@ -65,7 +65,7 @@ pub trait System: Problem {
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
) -> Result<(), ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>;
Expand Down Expand Up @@ -148,7 +148,7 @@ where
&self,
x: &Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), Error>
) -> Result<(), ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
Expand Down
4 changes: 2 additions & 2 deletions src/derivatives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use nalgebra::{
use num_traits::{One, Zero};
use thiserror::Error;

use crate::core::{Error, Problem, System};
use crate::core::{Problem, ProblemError, System};

/// Square root of double precision machine epsilon. This value is a standard
/// constant for epsilons in approximating derivate-based concepts.
Expand All @@ -22,7 +22,7 @@ pub const EPSILON_SQRT: f64 = 0.000000014901161193847656;
pub enum JacobianError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
Problem(#[from] Error),
Problem(#[from] ProblemError),
}

/// Jacobian matrix of a system.
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
//! &self,
//! x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
//! fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
//! ) -> Result<(), Error>
//! ) -> Result<(), ProblemError>
//! where
//! Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
//! Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
Expand Down Expand Up @@ -182,7 +182,7 @@
//! # &self,
//! # x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
//! # fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
//! # ) -> Result<(), Error>
//! # ) -> Result<(), ProblemError>
//! # where
//! # Sx: na::storage::Storage<Self::Scalar, Self::Dim> + IsContiguous,
//! # Sfx: na::storage::StorageMut<Self::Scalar, Self::Dim>,
Expand Down Expand Up @@ -262,7 +262,8 @@ pub use nalgebra;
pub mod prelude {
pub use crate::{
core::{
Domain, Error, Function, Optimizer, Problem, Solver, System, Variable, VariableBuilder,
Domain, Function, Optimizer, Problem, ProblemError, Solver, System, Variable,
VariableBuilder,
},
var,
};
Expand Down
4 changes: 2 additions & 2 deletions src/population.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use num_traits::Zero;
use rand::Rng;
use rand_distr::{uniform::SampleUniform, Distribution, Uniform};

use crate::core::{Domain, Error, Function, Problem};
use crate::core::{Domain, Function, Problem, ProblemError};

/// Population in a population-based solving algorithm.
///
Expand Down Expand Up @@ -373,7 +373,7 @@ where
&self,
f: &F,
fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
) -> Result<F::Scalar, Error>
) -> Result<F::Scalar, ProblemError>
where
Sfx: StorageMut<F::Scalar, F::Dim>,
F: Function,
Expand Down
4 changes: 2 additions & 2 deletions src/solver/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rand_distr::{uniform::SampleUniform, Distribution, StandardNormal, Uniform};
use thiserror::Error;

use crate::{
core::{Domain, Error, Function, Optimizer, Problem, Solver, System},
core::{Domain, Function, Optimizer, Problem, ProblemError, Solver, System},
population::{Population, PopulationInit, PopulationSize, UniformInit},
};

Expand Down Expand Up @@ -193,7 +193,7 @@ where
pub enum CuckooError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
Problem(#[from] Error),
Problem(#[from] ProblemError),
}

impl<F: Function, I: PopulationInit<F>, R: Rng> Cuckoo<F, I, R>
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 @@ -30,7 +30,7 @@ use thiserror::Error;

use crate::{
core::{
Domain, Error, Function, FunctionResultExt, Optimizer, Problem, Solver, System,
Domain, Function, FunctionResultExt, Optimizer, Problem, ProblemError, Solver, System,
VectorDomainExt,
},
derivatives::EPSILON_SQRT,
Expand Down Expand Up @@ -182,7 +182,7 @@ where
pub enum NelderMeadError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
Problem(#[from] Error),
Problem(#[from] ProblemError),
/// Simplex collapsed so it is impossible to make any progress.
#[error("simplex collapsed")]
SimplexCollapsed,
Expand Down Expand Up @@ -249,7 +249,7 @@ where
// hope that it gets replaced. The exception is the very
// point provided by the caller as it is expected to be
// valid and it is erroneous situation if it is not.
Err(Error::InvalidValue) => inf,
Err(ProblemError::InvalidValue) => inf,
Err(error) => {
// Clear the simplex so the solver is not in invalid
// state.
Expand All @@ -274,7 +274,7 @@ where
// The simplex is too degenerate.
simplex.clear();
errors.clear();
return Err(NelderMeadError::Problem(Error::InvalidValue));
return Err(NelderMeadError::Problem(ProblemError::InvalidValue));
}

sort_perm.extend(0..=n);
Expand Down
8 changes: 5 additions & 3 deletions src/solver/steffensen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use getset::{CopyGetters, Setters};
use nalgebra::{storage::StorageMut, Dim, IsContiguous, Vector};
use thiserror::Error;

use crate::core::{Domain, Error, Problem, Solver, System, VectorDomainExt};
use crate::core::{Domain, Problem, ProblemError, Solver, System, VectorDomainExt};

/// Variant of the Steffenen's method.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<F: Problem> Steffensen<F> {
pub enum SteffensenError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
Problem(#[from] Error),
Problem(#[from] ProblemError),
}

impl<F: System> Solver<F> for Steffensen<F> {
Expand All @@ -96,7 +96,9 @@ impl<F: System> Solver<F> for Steffensen<F> {
Sfx: StorageMut<<F>::Scalar, <F>::Dim>,
{
if f.dim().value() != 1 {
return Err(SteffensenError::Problem(Error::InvalidDimensionality));
return Err(SteffensenError::Problem(
ProblemError::InvalidDimensionality,
));
}

let SteffensenOptions { variant, .. } = self.options;
Expand Down
4 changes: 2 additions & 2 deletions src/solver/trust_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use num_traits::{One, Zero};
use thiserror::Error;

use crate::{
core::{Domain, Error, Problem, Solver, System, VectorDomainExt},
core::{Domain, Problem, ProblemError, Solver, System, VectorDomainExt},
derivatives::{Jacobian, JacobianError, EPSILON_SQRT},
};

Expand Down Expand Up @@ -175,7 +175,7 @@ where
pub enum TrustRegionError {
/// Error that occurred when evaluating the system.
#[error("{0}")]
Problem(#[from] Error),
Problem(#[from] ProblemError),
/// Error that occurred when computing the Jacobian matrix.
#[error("{0}")]
Jacobian(#[from] JacobianError),
Expand Down
Loading

0 comments on commit 0aa2397

Please sign in to comment.