diff -r edb95d2b83cc -r d2acaaddd9af src/bisection_tree/support.rs --- a/src/bisection_tree/support.rs Sun Nov 10 09:02:57 2024 -0500 +++ b/src/bisection_tree/support.rs Tue Dec 31 09:12:43 2024 -0500 @@ -10,18 +10,17 @@ use crate::sets::Cube; use crate::loc::Loc; use super::aggregator::Bounds; -use crate::norms::{Norm, L1, L2, Linfinity}; +use crate::norms::{Norm, L1, L2, Linfinity, HasScalarField, HasRealField}; /// 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; +pub trait Constant : Copy + Sync + Send + 'static + + std::fmt::Debug + HasRealField + + Into { /// Returns the value of the constant - fn value(&self) -> Self::Type; + fn value(&self) -> Self::Field; } impl Constant for F { - type Type = F; #[inline] fn value(&self) -> F { *self } } @@ -30,17 +29,17 @@ /// A trait for working with the supports of [`Apply`]s. /// /// Apply is not a super-trait to allow more general use. -pub trait Support : Sized + Sync + Send + 'static { +pub trait Support : Sized + Sync + Send + 'static + HasRealField { /// Return a cube containing the support of the function represented by `self`. /// /// The hint may be larger than the actual support, but must contain it. - fn support_hint(&self) -> Cube; + fn support_hint(&self) -> Cube; /// Indicate whether `x` is in the support of the function represented by `self`. - fn in_support(&self, x : &Loc) -> bool; + fn in_support(&self, x : &Loc) -> bool; // Indicate whether `cube` is fully in the support of the function represented by `self`. - //fn fully_in_support(&self, cube : &Cube) -> bool; + //fn fully_in_support(&self, cube : &Cube) -> bool; /// Return an optional hint for bisecting the support. /// @@ -53,19 +52,19 @@ /// The default implementation returns `[None; N]`. #[inline] #[allow(unused_variables)] - fn bisection_hint(&self, cube : &Cube) -> [Option; N] { + fn bisection_hint(&self, cube : &Cube) -> [Option; N] { [None; N] } /// Translate `self` by `x`. #[inline] - fn shift(self, x : Loc) -> Shift { + 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 { + fn weigh>(self, a : C) -> Weighted { Weighted { weight : a, base_fn : self } } } @@ -74,7 +73,7 @@ /// /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as /// [`Bounds`][super::aggregator::Bounds]. -pub trait GlobalAnalysis { +pub trait GlobalAnalysis { /// Perform global analysis of the property `A` of `Self`. /// /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], @@ -83,26 +82,18 @@ fn global_analysis(&self) -> A; } -// default impl GlobalAnalysis for L -// where L : LocalAnalysis { -// #[inline] -// fn global_analysis(&self) -> Bounds { -// self.local_analysis(&self.support_hint()) -// } -// } - /// Trait for locally analysing a property `A` of a [`Apply`] (implementing [`Support`]) /// within a [`Cube`]. /// /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as /// [`Bounds`][super::aggregator::Bounds]. -pub trait LocalAnalysis : GlobalAnalysis + Support { +pub trait LocalAnalysis : GlobalAnalysis { /// Perform local analysis of the property `A` of `Self`. /// /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], /// this function will return upper and lower bounds within `cube` for the mapping /// represented by `self`. - fn local_analysis(&self, cube : &Cube) -> A; + fn local_analysis(&self, cube : &LocalSet) -> A; } /// Trait for determining the upper and lower bounds of an float-valued [`Apply`]. @@ -110,15 +101,15 @@ /// This is a blanket-implemented alias for [`GlobalAnalysis`]`>` /// [`Apply`] is not a supertrait to allow flexibility in the implementation of either /// reference or non-reference arguments. -pub trait Bounded : GlobalAnalysis> { +pub trait Bounded : GlobalAnalysis> + HasScalarField { /// Return lower and upper bounds for the values of of `self`. #[inline] - fn bounds(&self) -> Bounds { + fn bounds(&self) -> Bounds { self.global_analysis() } } -impl>> Bounded for T { } +impl> + HasScalarField> Bounded for T { } /// Shift of [`Support`] and [`Apply`]; output of [`Support::shift`]. #[derive(Copy,Clone,Debug,Serialize)] // Serialize! but not implemented by Loc. @@ -127,6 +118,10 @@ base_fn : T, } +impl HasScalarField for Shift { + type Field = F; +} + impl<'a, T, V, F : Float, const N : usize> Apply<&'a Loc> for Shift where T : Apply, Output=V> { type Output = V; @@ -145,7 +140,8 @@ } } -impl<'a, T, V, F : Float, const N : usize> Differentiable<&'a Loc> for Shift +impl<'a, T, V : HasRealField, F : Float, const N : usize> +Differentiable<&'a Loc> for Shift where T : Differentiable, Derivative=V> { type Derivative = V; #[inline] @@ -154,7 +150,8 @@ } } -impl<'a, T, V, F : Float, const N : usize> Differentiable> for Shift +impl<'a, T, V : HasRealField, F : Float, const N : usize> +Differentiable> for Shift where T : Differentiable, Derivative=V> { type Derivative = V; #[inline] @@ -163,8 +160,8 @@ } } -impl<'a, T, F : Float, const N : usize> Support for Shift -where T : Support { +impl<'a, T, F : Float, const N : usize> Support for Shift +where T : Support { #[inline] fn support_hint(&self) -> Cube { self.base_fn.support_hint().shift(&self.shift) @@ -188,16 +185,17 @@ } -impl<'a, T, F : Float, const N : usize> GlobalAnalysis> for Shift -where T : LocalAnalysis, N> { +impl<'a, T, F : Float, const N : usize> GlobalAnalysis> for Shift +where T : GlobalAnalysis> { #[inline] fn global_analysis(&self) -> Bounds { self.base_fn.global_analysis() } } -impl<'a, T, F : Float, const N : usize> LocalAnalysis, N> for Shift -where T : LocalAnalysis, N> { +impl<'a, T, F : Float, const N : usize> LocalAnalysis, Cube> +for Shift +where T : LocalAnalysis, Cube> { #[inline] fn local_analysis(&self, cube : &Cube) -> Bounds { self.base_fn.local_analysis(&cube.shift(&(-self.shift))) @@ -206,10 +204,10 @@ macro_rules! impl_shift_norm { ($($norm:ident)*) => { $( - impl<'a, T, F : Float, const N : usize> Norm for Shift - where T : Norm { + impl<'a, T, const N : usize> Norm<$norm> for Shift + where T : Norm<$norm> { #[inline] - fn norm(&self, n : $norm) -> F { + fn norm(&self, n : $norm) -> T::Field { self.base_fn.norm(n) } } @@ -228,10 +226,16 @@ pub base_fn : T, } +impl HasScalarField for Weighted +where T : HasRealField, + C : Constant { + type Field = F; +} + 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 { + C : Constant { type Output = V; #[inline] fn apply(&self, x : &'a Loc) -> Self::Output { @@ -242,7 +246,7 @@ impl<'a, T, V, F : Float, C, const N : usize> Apply> for Weighted where T : Apply, Output=V>, V : std::ops::Mul, - C : Constant { + C : Constant { type Output = V; #[inline] fn apply(&self, x : Loc) -> Self::Output { @@ -250,10 +254,11 @@ } } -impl<'a, T, V, F : Float, C, const N : usize> Differentiable<&'a Loc> for Weighted -where T : for<'b> Differentiable<&'b Loc, Derivative=V>, +impl<'a, T, V : HasRealField, F : Float, C, const N : usize> +Differentiable<&'a Loc> for Weighted +where T : for<'b> Differentiable<&'b Loc, Derivative=V, RealField=F>, V : std::ops::Mul, - C : Constant { + C : Constant { type Derivative = V; #[inline] fn differential(&self, x : &'a Loc) -> Self::Derivative { @@ -261,10 +266,11 @@ } } -impl<'a, T, V, F : Float, C, const N : usize> Differentiable> for Weighted -where T : Differentiable, Derivative=V>, +impl<'a, T, V : HasRealField, F : Float, C, const N : usize> +Differentiable> for Weighted +where T : Differentiable, Derivative=V, RealField = F>, V : std::ops::Mul, - C : Constant { + C : Constant { type Derivative = V; #[inline] fn differential(&self, x : Loc) -> Self::Derivative { @@ -272,9 +278,9 @@ } } -impl<'a, T, F : Float, C, const N : usize> Support for Weighted -where T : Support, - C : Constant { +impl<'a, T, F : Float, C, const N : usize> Support for Weighted +where T : Support, + C : Constant { #[inline] fn support_hint(&self) -> Cube { @@ -296,9 +302,9 @@ } } -impl<'a, T, F : Float, C> GlobalAnalysis> for Weighted -where T : GlobalAnalysis>, - C : Constant { +impl<'a, T, F : Float, C> GlobalAnalysis> for Weighted +where T : GlobalAnalysis>, + C : Constant { #[inline] fn global_analysis(&self) -> Bounds { let Bounds(lower, upper) = self.base_fn.global_analysis(); @@ -310,9 +316,10 @@ } } -impl<'a, T, F : Float, C, const N : usize> LocalAnalysis, N> for Weighted -where T : LocalAnalysis, N>, - C : Constant { +impl<'a, T, F : Float, C, const N : usize> LocalAnalysis, Cube> +for Weighted +where T : LocalAnalysis, Cube>, + C : Constant { #[inline] fn local_analysis(&self, cube : &Cube) -> Bounds { let Bounds(lower, upper) = self.base_fn.local_analysis(cube); @@ -358,8 +365,9 @@ macro_rules! impl_weighted_norm { ($($norm:ident)*) => { $( - impl<'a, T, F : Float> Norm for Weighted - where T : Norm { + impl<'a, T, F : Float> Norm<$norm> for Weighted + where T : Norm<$norm, Field = F> { + #[inline] fn norm(&self, n : $norm) -> F { self.base_fn.norm(n) * self.weight.abs() @@ -381,7 +389,7 @@ ); impl<'a, T, F : Float, const N : usize> Apply<&'a Loc> for Normalised -where T : Norm + for<'b> Apply<&'b Loc, Output=F> { +where T : Norm + for<'b> Apply<&'b Loc, Output=F> { type Output = F; #[inline] fn apply(&self, x : &'a Loc) -> Self::Output { @@ -391,7 +399,7 @@ } impl<'a, T, F : Float, const N : usize> Apply> for Normalised -where T : Norm + Apply, Output=F> { +where T : Norm + Apply, Output=F> { type Output = F; #[inline] fn apply(&self, x : Loc) -> Self::Output { @@ -400,8 +408,8 @@ } } -impl<'a, T, F : Float, const N : usize> Support for Normalised -where T : Norm + Support { +impl<'a, T, F : Float, const N : usize> Support for Normalised +where T : Norm + Support { #[inline] fn support_hint(&self) -> Cube { @@ -423,8 +431,8 @@ } } -impl<'a, T, F : Float> GlobalAnalysis> for Normalised -where T : Norm + GlobalAnalysis> { +impl<'a, T, F : Float> GlobalAnalysis> for Normalised +where T : Norm + GlobalAnalysis> { #[inline] fn global_analysis(&self) -> Bounds { let Bounds(lower, upper) = self.0.global_analysis(); @@ -435,8 +443,9 @@ } } -impl<'a, T, F : Float, const N : usize> LocalAnalysis, N> for Normalised -where T : Norm + LocalAnalysis, N> { +impl<'a, T, F : Float, const N : usize> LocalAnalysis, Cube> +for Normalised +where T : Norm + LocalAnalysis, Cube> { #[inline] fn local_analysis(&self, cube : &Cube) -> Bounds { let Bounds(lower, upper) = self.0.local_analysis(cube); @@ -447,23 +456,26 @@ } } -impl<'a, T, F : Float> Norm for Normalised -where T : Norm { +impl<'a, T> HasScalarField for Normalised where T : HasScalarField { + type Field = T::Field; +} + +impl<'a, T> Norm for Normalised where T : Norm { #[inline] - fn norm(&self, _ : L1) -> F { + fn norm(&self, _ : L1) -> Self::Field { let w = self.0.norm(L1); - if w == F::ZERO { F::ZERO } else { F::ONE } + if w == Self::Field::ZERO { Self::Field::ZERO } else { Self::Field::ONE } } } macro_rules! impl_normalised_norm { ($($norm:ident)*) => { $( - impl<'a, T, F : Float> Norm for Normalised - where T : Norm + Norm { + impl<'a, T> Norm<$norm> for Normalised where T : Norm<$norm> + Norm { + #[inline] - fn norm(&self, n : $norm) -> F { + fn norm(&self, n : $norm) -> Self::Field { let w = self.0.norm(L1); - if w == F::ZERO { F::ZERO } else { self.0.norm(n) / w } + if w == Self::Field::ZERO { Self::Field::ZERO } else { self.0.norm(n) / w } } } )* } @@ -471,26 +483,16 @@ impl_normalised_norm!(L2 Linfinity); -/* -impl, const N : usize> LocalAnalysis for S { - fn local_analysis(&self, _cube : &Cube) -> NullAggregator { NullAggregator } -} - -impl, const N : usize> LocalAnalysis, N> for S { - #[inline] - fn local_analysis(&self, cube : &Cube) -> Bounds { - self.bounds(cube) - } -}*/ /// Generator of [`Support`]-implementing component functions based on low storage requirement /// [ids][`Self::Id`]. -pub trait SupportGenerator -: MulAssign + DivAssign + Neg + Clone + Sync + Send + 'static { +pub trait SupportGenerator +: MulAssign + DivAssign + Neg + HasRealField + + Clone + Sync + Send + 'static { /// The identification type type Id : 'static + Copy; /// The type of the [`Support`] (often also a [`Apply`]). - type SupportType : 'static + Support; + type SupportType : 'static + Support; /// An iterator over all the [`Support`]s of the generator. type AllDataIter<'a> : Iterator where Self : 'a;