Skip to content

Commit

Permalink
feat!: rename scalar associated type to field
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Nov 12, 2023
1 parent d15c996 commit 5d8c8e1
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 389 deletions.
12 changes: 6 additions & 6 deletions examples/rosenbrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ struct Rosenbrock {
}

impl Problem for Rosenbrock {
type Scalar = f64;
type Field = f64;

fn domain(&self) -> Domain<Self::Scalar> {
fn domain(&self) -> Domain<Self::Field> {
Domain::unconstrained(2)
}
}

impl System for Rosenbrock {
fn eval<Sx, Sfx>(
&self,
x: &na::Vector<Self::Scalar, Dynamic, Sx>,
fx: &mut na::Vector<Self::Scalar, Dynamic, Sfx>,
x: &na::Vector<Self::Field, Dynamic, Sx>,
fx: &mut na::Vector<Self::Field, Dynamic, Sfx>,
) where
Sx: na::storage::Storage<Self::Scalar, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Scalar, Dynamic>,
Sx: na::storage::Storage<Self::Field, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<Self::Field, Dynamic>,
{
fx[0] = (self.a - x[0]).powi(2);
fx[1] = self.b * (x[1] - x[0].powi(2)).powi(2);
Expand Down
30 changes: 15 additions & 15 deletions gomez-bench/benches/main.rs
Original file line number Diff line number Diff line change
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::Scalar>, &na::OVector<F::Scalar, Dynamic>) -> S,
GS: Fn(&F, &Domain<F::Field>, &na::OVector<F::Field, Dynamic>) -> S,
F: TestSystem,
S: Solver<F>,
{
Expand Down Expand Up @@ -203,8 +203,8 @@ where

fn with_trust_region<F>(
f: &F,
dom: &Domain<F::Scalar>,
_: &na::OVector<F::Scalar, Dynamic>,
dom: &Domain<F::Field>,
_: &na::OVector<F::Field, Dynamic>,
) -> TrustRegion<F>
where
F: Problem,
Expand All @@ -214,8 +214,8 @@ where

fn with_nelder_mead<F>(
f: &F,
dom: &Domain<F::Scalar>,
_: &na::OVector<F::Scalar, Dynamic>,
dom: &Domain<F::Field>,
_: &na::OVector<F::Field, Dynamic>,
) -> NelderMead<F>
where
F: Problem,
Expand All @@ -225,11 +225,11 @@ where

fn with_gsl_hybrids<F>(
f: &F,
_: &Domain<F::Scalar>,
x: &na::OVector<F::Scalar, Dynamic>,
_: &Domain<F::Field>,
x: &na::OVector<F::Field, Dynamic>,
) -> GslSolverWrapper<GslFunctionWrapper<F>>
where
F: TestSystem<Scalar = f64> + Clone,
F: TestSystem<Field = f64> + Clone,
{
GslSolverWrapper::new(GslFunctionWrapper::new(
f.clone(),
Expand All @@ -248,7 +248,7 @@ impl<F> GslFunctionWrapper<F> {
}
}

impl<F: TestSystem<Scalar = f64>> GslFunction for 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());
Expand Down Expand Up @@ -285,21 +285,21 @@ impl<F: GslFunction> GslSolverWrapper<F> {
}
}

impl<F: TestSystem<Scalar = f64>> Solver<F> for GslSolverWrapper<GslFunctionWrapper<F>> {
impl<F: TestSystem<Field = f64>> Solver<F> for GslSolverWrapper<GslFunctionWrapper<F>> {
const NAME: &'static str = "GSL hybrids";

type Error = String;

fn solve_next<Sx, Sfx>(
&mut self,
_f: &F,
_dom: &Domain<F::Scalar>,
x: &mut na::Vector<F::Scalar, Dynamic, Sx>,
fx: &mut na::Vector<F::Scalar, Dynamic, Sfx>,
_dom: &Domain<F::Field>,
x: &mut na::Vector<F::Field, Dynamic, Sx>,
fx: &mut na::Vector<F::Field, Dynamic, Sfx>,
) -> Result<(), Self::Error>
where
Sx: na::storage::StorageMut<F::Scalar, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<F::Scalar, Dynamic>,
Sx: na::storage::StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: na::storage::StorageMut<F::Field, Dynamic>,
{
let result = self.solver.step().to_result();
x.copy_from_slice(self.solver.root());
Expand Down
12 changes: 6 additions & 6 deletions src/analysis/initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ impl<F: System> InitialGuessAnalysis<F> {
/// Analyze the system in given point.
pub fn analyze<Sx, Sfx>(
f: &F,
dom: &Domain<F::Scalar>,
x: &mut Vector<F::Scalar, Dynamic, Sx>,
fx: &mut Vector<F::Scalar, Dynamic, Sfx>,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
fx: &mut Vector<F::Field, Dynamic, Sfx>,
) -> Self
where
Sx: StorageMut<F::Scalar, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Scalar, Dynamic>,
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Field, Dynamic>,
{
let dim = Dynamic::new(dom.dim());
let scale = dom
Expand All @@ -54,7 +54,7 @@ impl<F: System> InitialGuessAnalysis<F> {
qr.solve_mut(&mut p);

// Do Newton step.
p *= convert::<_, F::Scalar>(0.001);
p *= convert::<_, F::Field>(0.001);
*x += p;

// Compute F'(x) after one Newton step.
Expand Down
6 changes: 3 additions & 3 deletions src/core/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use super::domain::Domain;
/// The base trait for [`System`](super::system::System) and
/// [`Function`](super::function::Function).
pub trait Problem {
/// Type of the scalar, usually f32 or f64.
type Scalar: RealField + Copy;
/// Type of the field, usually f32 or f64.
type Field: RealField + Copy;

/// Get the domain (bound constraints) of the system. If not overridden, the
/// system is unconstrained.
fn domain(&self) -> Domain<Self::Scalar>;
fn domain(&self) -> Domain<Self::Field>;
}
18 changes: 9 additions & 9 deletions src/core/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{base::Problem, system::System};
/// ## Defining a function
///
/// A function is any type that implements [`Function`] and [`Problem`] traits.
/// There is one required associated type (scalar) and one required method
/// There is one required associated type (field) and one required method
/// ([`apply`](Function::apply)).
///
/// ```rust
Expand All @@ -24,19 +24,19 @@ use super::{base::Problem, system::System};
///
/// impl Problem for Rosenbrock {
/// // The numeric type. Usually f64 or f32.
/// type Scalar = f64;
/// type Field = f64;
///
/// // The domain of the problem (could be bound-constrained).
/// fn domain(&self) -> Domain<Self::Scalar> {
/// fn domain(&self) -> Domain<Self::Field> {
/// Domain::unconstrained(2)
/// }
/// }
///
/// impl Function for Rosenbrock {
/// // Apply trial values of variables to the function.
/// fn apply<Sx>(&self, x: &na::Vector<Self::Scalar, Dynamic, Sx>) -> Self::Scalar
/// fn apply<Sx>(&self, x: &na::Vector<Self::Field, Dynamic, Sx>) -> Self::Field
/// where
/// Sx: na::storage::Storage<Self::Scalar, Dynamic> + IsContiguous,
/// Sx: na::storage::Storage<Self::Field, Dynamic> + IsContiguous,
/// {
/// // Compute the function value.
/// (self.a - x[0]).powi(2) + self.b * (x[1] - x[0].powi(2)).powi(2)
Expand All @@ -45,18 +45,18 @@ use super::{base::Problem, system::System};
/// ```
pub trait Function: Problem {
/// Calculate the function value given values of the variables.
fn apply<Sx>(&self, x: &Vector<Self::Scalar, Dynamic, Sx>) -> Self::Scalar
fn apply<Sx>(&self, x: &Vector<Self::Field, Dynamic, Sx>) -> Self::Field
where
Sx: Storage<Self::Scalar, Dynamic> + IsContiguous;
Sx: Storage<Self::Field, Dynamic> + IsContiguous;
}

impl<F> Function for F
where
F: System,
{
fn apply<Sx>(&self, x: &Vector<Self::Scalar, Dynamic, Sx>) -> Self::Scalar
fn apply<Sx>(&self, x: &Vector<Self::Field, Dynamic, Sx>) -> Self::Field
where
Sx: Storage<Self::Scalar, Dynamic> + IsContiguous,
Sx: Storage<Self::Field, Dynamic> + IsContiguous,
{
self.norm(x)
}
Expand Down
20 changes: 10 additions & 10 deletions src/core/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ use super::{domain::Domain, function::Function};
///
/// impl<F: Function, R: Rng> Optimizer<F> for Random<R>
/// where
/// F::Scalar: SampleUniform,
/// Standard: Distribution<F::Scalar>,
/// F::Field: SampleUniform,
/// Standard: Distribution<F::Field>,
/// {
/// const NAME: &'static str = "Random";
/// type Error = std::convert::Infallible;
///
/// fn opt_next<Sx>(
/// &mut self,
/// f: &F,
/// dom: &Domain<F::Scalar>,
/// x: &mut Vector<F::Scalar, Dynamic, Sx>,
/// ) -> Result<F::Scalar, Self::Error>
/// dom: &Domain<F::Field>,
/// x: &mut Vector<F::Field, Dynamic, Sx>,
/// ) -> Result<F::Field, Self::Error>
/// where
/// Sx: StorageMut<F::Scalar, Dynamic> + IsContiguous,
/// Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
/// {
/// // Randomly sample in the domain.
/// dom.sample(x, &mut self.rng);
Expand Down Expand Up @@ -85,9 +85,9 @@ pub trait Optimizer<F: Function> {
fn opt_next<Sx>(
&mut self,
f: &F,
dom: &Domain<F::Scalar>,
x: &mut Vector<F::Scalar, Dynamic, Sx>,
) -> Result<F::Scalar, Self::Error>
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
) -> Result<F::Field, Self::Error>
where
Sx: StorageMut<F::Scalar, Dynamic> + IsContiguous;
Sx: StorageMut<F::Field, Dynamic> + IsContiguous;
}
24 changes: 12 additions & 12 deletions src/core/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ use super::{domain::Domain, system::System};
///
/// impl<F: System, R: Rng> Solver<F> for Random<R>
/// where
/// F::Scalar: SampleUniform,
/// Standard: Distribution<F::Scalar>,
/// F::Field: SampleUniform,
/// Standard: Distribution<F::Field>,
/// {
/// const NAME: &'static str = "Random";
/// type Error = std::convert::Infallible;
///
/// fn solve_next<Sx, Sfx>(
/// &mut self,
/// f: &F,
/// dom: &Domain<F::Scalar>,
/// x: &mut Vector<F::Scalar, Dynamic, Sx>,
/// fx: &mut Vector<F::Scalar, Dynamic, Sfx>,
/// dom: &Domain<F::Field>,
/// x: &mut Vector<F::Field, Dynamic, Sx>,
/// fx: &mut Vector<F::Field, Dynamic, Sfx>,
/// ) -> Result<(), Self::Error>
/// where
/// Sx: StorageMut<F::Scalar, Dynamic> + IsContiguous,
/// Sfx: StorageMut<F::Scalar, Dynamic>,
/// Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
/// Sfx: StorageMut<F::Field, Dynamic>,
/// {
/// // Randomly sample in the domain.
/// dom.sample(x, &mut self.rng);
Expand Down Expand Up @@ -89,11 +89,11 @@ pub trait Solver<F: System> {
fn solve_next<Sx, Sfx>(
&mut self,
f: &F,
dom: &Domain<F::Scalar>,
x: &mut Vector<F::Scalar, Dynamic, Sx>,
fx: &mut Vector<F::Scalar, Dynamic, Sfx>,
dom: &Domain<F::Field>,
x: &mut Vector<F::Field, Dynamic, Sx>,
fx: &mut Vector<F::Field, Dynamic, Sfx>,
) -> Result<(), Self::Error>
where
Sx: StorageMut<F::Scalar, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Scalar, Dynamic>;
Sx: StorageMut<F::Field, Dynamic> + IsContiguous,
Sfx: StorageMut<F::Field, Dynamic>;
}
Loading

0 comments on commit 5d8c8e1

Please sign in to comment.