--- a/src/bisection_tree/support.rs Sat Dec 14 09:31:27 2024 -0500 +++ b/src/bisection_tree/support.rs Tue Dec 31 08:30:02 2024 -0500 @@ -6,7 +6,9 @@ use std::ops::{MulAssign,DivAssign,Neg}; use crate::types::{Float, Num}; use crate::maputil::map2; -use crate::mapping::{Apply, Differentiable}; +use crate::mapping::{ + Instance, Mapping, DifferentiableImpl, DifferentiableMapping, Space +}; use crate::sets::Cube; use crate::loc::Loc; use super::aggregator::Bounds; @@ -127,39 +129,23 @@ base_fn : T, } -impl<'a, T, V, F : Float, const N : usize> Apply<&'a Loc<F, N>> for Shift<T,F,N> -where T : Apply<Loc<F, N>, Output=V> { - type Output = V; +impl<'a, T, V : Space, F : Float, const N : usize> Mapping<Loc<F, N>> for Shift<T,F,N> +where T : Mapping<Loc<F, N>, Codomain=V> { + type Codomain = V; + #[inline] - fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { - self.base_fn.apply(x - &self.shift) + fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { + self.base_fn.apply(x.own() - &self.shift) } } -impl<'a, T, V, F : Float, const N : usize> Apply<Loc<F, N>> for Shift<T,F,N> -where T : Apply<Loc<F, N>, Output=V> { - type Output = V; - #[inline] - fn apply(&self, x : Loc<F, N>) -> Self::Output { - self.base_fn.apply(x - &self.shift) - } -} - -impl<'a, T, V, F : Float, const N : usize> Differentiable<&'a Loc<F, N>> for Shift<T,F,N> -where T : Differentiable<Loc<F, N>, Derivative=V> { +impl<'a, T, V : Space, F : Float, const N : usize> DifferentiableImpl<Loc<F, N>> for Shift<T,F,N> +where T : DifferentiableMapping<Loc<F, N>, DerivativeDomain=V> { type Derivative = V; + #[inline] - fn differential(&self, x : &'a Loc<F, N>) -> Self::Derivative { - self.base_fn.differential(x - &self.shift) - } -} - -impl<'a, T, V, F : Float, const N : usize> Differentiable<Loc<F, N>> for Shift<T,F,N> -where T : Differentiable<Loc<F, N>, Derivative=V> { - type Derivative = V; - #[inline] - fn differential(&self, x : Loc<F, N>) -> Self::Derivative { - self.base_fn.differential(x - &self.shift) + fn differential_impl<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Derivative { + self.base_fn.differential(x.own() - &self.shift) } } @@ -228,46 +214,26 @@ pub base_fn : T, } -impl<'a, T, V, F : Float, C, const N : usize> Apply<&'a Loc<F, N>> for Weighted<T, C> -where T : for<'b> Apply<&'b Loc<F, N>, Output=V>, - V : std::ops::Mul<F,Output=V>, +impl<'a, T, V, F : Float, C, const N : usize> Mapping<Loc<F, N>> for Weighted<T, C> +where T : Mapping<Loc<F, N>, Codomain=V>, + V : Space + std::ops::Mul<F,Output=V>, C : Constant<Type=F> { - type Output = V; + type Codomain = V; + #[inline] - fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { + fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { self.base_fn.apply(x) * self.weight.value() } } -impl<'a, T, V, F : Float, C, const N : usize> Apply<Loc<F, N>> for Weighted<T, C> -where T : Apply<Loc<F, N>, Output=V>, - V : std::ops::Mul<F,Output=V>, - C : Constant<Type=F> { - type Output = V; - #[inline] - fn apply(&self, x : Loc<F, N>) -> Self::Output { - self.base_fn.apply(x) * self.weight.value() - } -} - -impl<'a, T, V, F : Float, C, const N : usize> Differentiable<&'a Loc<F, N>> for Weighted<T, C> -where T : for<'b> Differentiable<&'b Loc<F, N>, Derivative=V>, - V : std::ops::Mul<F, Output=V>, +impl<'a, T, V, F : Float, C, const N : usize> DifferentiableImpl<Loc<F, N>> for Weighted<T, C> +where T : DifferentiableMapping<Loc<F, N>, DerivativeDomain=V>, + V : Space + std::ops::Mul<F, Output=V>, C : Constant<Type=F> { type Derivative = V; + #[inline] - fn differential(&self, x : &'a Loc<F, N>) -> Self::Derivative { - self.base_fn.differential(x) * self.weight.value() - } -} - -impl<'a, T, V, F : Float, C, const N : usize> Differentiable<Loc<F, N>> for Weighted<T, C> -where T : Differentiable<Loc<F, N>, Derivative=V>, - V : std::ops::Mul<F, Output=V>, - C : Constant<Type=F> { - type Derivative = V; - #[inline] - fn differential(&self, x : Loc<F, N>) -> Self::Derivative { + fn differential_impl<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Derivative { self.base_fn.differential(x) * self.weight.value() } } @@ -380,21 +346,12 @@ pub T ); -impl<'a, T, F : Float, const N : usize> Apply<&'a Loc<F, N>> for Normalised<T> -where T : Norm<F, L1> + for<'b> Apply<&'b Loc<F, N>, Output=F> { - type Output = F; +impl<'a, T, F : Float, const N : usize> Mapping<Loc<F, N>> for Normalised<T> +where T : Norm<F, L1> + Mapping<Loc<F,N>, Codomain=F> { + type Codomain = F; + #[inline] - fn apply(&self, x : &'a Loc<F, N>) -> 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<Loc<F, N>> for Normalised<T> -where T : Norm<F, L1> + Apply<Loc<F,N>, Output=F> { - type Output = F; - #[inline] - fn apply(&self, x : Loc<F, N>) -> Self::Output { + fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { let w = self.0.norm(L1); if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } }