diff -r d14c877e14b7 -r b3c35d16affe src/bisection_tree/support.rs --- a/src/bisection_tree/support.rs Tue Feb 20 12:33:16 2024 -0500 +++ b/src/bisection_tree/support.rs Mon Feb 03 19:22:16 2025 -0500 @@ -1,35 +1,23 @@ /*! -Traits for representing the support of a [`Apply`], and analysing the mapping on a [`Cube`]. +Traits for representing the support of a [`Mapping`], and analysing the mapping on a [`Cube`]. */ use serde::Serialize; use std::ops::{MulAssign,DivAssign,Neg}; use crate::types::{Float, Num}; use crate::maputil::map2; -use crate::mapping::Apply; +use crate::mapping::{ + Instance, Mapping, DifferentiableImpl, DifferentiableMapping, Space +}; use crate::sets::Cube; use crate::loc::Loc; use super::aggregator::Bounds; use crate::norms::{Norm, L1, L2, Linfinity}; - -/// A trait for encoding constant [`Float`] values -pub trait Constant : Copy + Sync + Send + 'static + std::fmt::Debug + Into { - /// The type of the value - type Type : Float; - /// Returns the value of the constant - fn value(&self) -> Self::Type; -} +pub use crate::operator_arithmetic::{Weighted, Constant}; -impl Constant for F { - type Type = F; - #[inline] - fn value(&self) -> F { *self } -} - - -/// A trait for working with the supports of [`Apply`]s. +/// A trait for working with the supports of [`Mapping`]s. /// -/// Apply is not a super-trait to allow more general use. +/// `Mapping` is not a super-trait to allow more general use. pub trait Support : Sized + Sync + Send + 'static { /// Return a cube containing the support of the function represented by `self`. /// @@ -62,15 +50,9 @@ fn shift(self, x : Loc) -> Shift { Shift { shift : x, base_fn : self } } - - /// Multiply `self` by the scalar `a`. - #[inline] - fn weigh>(self, a : C) -> Weighted { - Weighted { weight : a, base_fn : self } - } } -/// Trait for globally analysing a property `A` of a [`Apply`]. +/// Trait for globally analysing a property `A` of a [`Mapping`]. /// /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as /// [`Bounds`][super::aggregator::Bounds]. @@ -91,7 +73,7 @@ // } // } -/// Trait for locally analysing a property `A` of a [`Apply`] (implementing [`Support`]) +/// Trait for locally analysing a property `A` of a [`Mapping`] (implementing [`Support`]) /// within a [`Cube`]. /// /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as @@ -105,10 +87,10 @@ fn local_analysis(&self, cube : &Cube) -> A; } -/// Trait for determining the upper and lower bounds of an float-valued [`Apply`]. +/// Trait for determining the upper and lower bounds of an float-valued [`Mapping`]. /// /// This is a blanket-implemented alias for [`GlobalAnalysis`]`>` -/// [`Apply`] is not a supertrait to allow flexibility in the implementation of either +/// [`Mapping`] is not a supertrait to allow flexibility in the implementation of either /// reference or non-reference arguments. pub trait Bounded : GlobalAnalysis> { /// Return lower and upper bounds for the values of of `self`. @@ -120,28 +102,30 @@ impl>> Bounded for T { } -/// Shift of [`Support`] and [`Apply`]; output of [`Support::shift`]. +/// Shift of [`Support`] and [`Mapping`]; output of [`Support::shift`]. #[derive(Copy,Clone,Debug,Serialize)] // Serialize! but not implemented by Loc. pub struct Shift { shift : Loc, base_fn : T, } -impl<'a, T, V, F : Float, const N : usize> Apply<&'a Loc> for Shift -where T : Apply, Output=V> { - type Output = V; +impl<'a, T, V : Space, F : Float, const N : usize> Mapping> for Shift +where T : Mapping, Codomain=V> { + type Codomain = V; + #[inline] - fn apply(&self, x : &'a Loc) -> Self::Output { - self.base_fn.apply(x - &self.shift) + fn apply>>(&self, x : I) -> Self::Codomain { + self.base_fn.apply(x.own() - &self.shift) } } -impl<'a, T, V, F : Float, const N : usize> Apply> for Shift -where T : Apply, Output=V> { - type Output = V; +impl<'a, T, V : Space, F : Float, const N : usize> DifferentiableImpl> for Shift +where T : DifferentiableMapping, DerivativeDomain=V> { + type Derivative = V; + #[inline] - fn apply(&self, x : Loc) -> Self::Output { - self.base_fn.apply(x - &self.shift) + fn differential_impl>>(&self, x : I) -> Self::Derivative { + self.base_fn.differential(x.own() - &self.shift) } } @@ -200,38 +184,6 @@ impl_shift_norm!(L1 L2 Linfinity); -/// Weighting of a [`Support`] and [`Apply`] by scalar multiplication; -/// output of [`Support::weigh`]. -#[derive(Copy,Clone,Debug,Serialize)] -pub struct Weighted { - /// The weight - pub weight : C, - /// The base [`Support`] or [`Apply`] being weighted. - pub base_fn : T, -} - -impl<'a, T, V, F : Float, C, const N : usize> Apply<&'a Loc> for Weighted -where T : for<'b> Apply<&'b Loc, Output=V>, - V : std::ops::Mul, - C : Constant { - type Output = V; - #[inline] - fn apply(&self, x : &'a Loc) -> Self::Output { - self.base_fn.apply(x) * self.weight.value() - } -} - -impl<'a, T, V, F : Float, C, const N : usize> Apply> for Weighted -where T : Apply, Output=V>, - V : std::ops::Mul, - C : Constant { - type Output = V; - #[inline] - fn apply(&self, x : Loc) -> Self::Output { - self.base_fn.apply(x) * self.weight.value() - } -} - impl<'a, T, F : Float, C, const N : usize> Support for Weighted where T : Support, C : Constant { @@ -331,30 +283,21 @@ impl_weighted_norm!(L1 L2 Linfinity); -/// Normalisation of [`Support`] and [`Apply`] to L¹ norm 1. +/// Normalisation of [`Support`] and [`Mapping`] to L¹ norm 1. /// /// Currently only scalar-valued functions are supported. #[derive(Copy, Clone, Debug, Serialize, PartialEq)] pub struct Normalised( - /// The base [`Support`] or [`Apply`]. + /// The base [`Support`] or [`Mapping`]. pub T ); -impl<'a, T, F : Float, const N : usize> Apply<&'a Loc> for Normalised -where T : Norm + for<'b> Apply<&'b Loc, Output=F> { - type Output = F; +impl<'a, T, F : Float, const N : usize> Mapping> for Normalised +where T : Norm + Mapping, Codomain=F> { + type Codomain = F; + #[inline] - fn apply(&self, x : &'a Loc) -> Self::Output { - let w = self.0.norm(L1); - if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } - } -} - -impl<'a, T, F : Float, const N : usize> Apply> for Normalised -where T : Norm + Apply, Output=F> { - type Output = F; - #[inline] - fn apply(&self, x : Loc) -> Self::Output { + fn apply>>(&self, x : I) -> Self::Codomain { let w = self.0.norm(L1); if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } } @@ -449,7 +392,7 @@ : MulAssign + DivAssign + Neg + Clone + Sync + Send + 'static { /// The identification type type Id : 'static + Copy; - /// The type of the [`Support`] (often also a [`Apply`]). + /// The type of the [`Support`] (often also a [`Mapping`]). type SupportType : 'static + Support; /// An iterator over all the [`Support`]s of the generator. type AllDataIter<'a> : Iterator where Self : 'a;