diff -r bd13c2ae3450 -r 56c8adc32b09 src/kernels/base.rs --- a/src/kernels/base.rs Fri Apr 28 13:15:19 2023 +0300 +++ b/src/kernels/base.rs Tue Dec 31 09:34:24 2024 -0500 @@ -14,11 +14,12 @@ GlobalAnalysis, Bounded, }; -use alg_tools::mapping::Apply; -use alg_tools::maputil::{array_init, map2}; +use alg_tools::mapping::{Apply, Differentiable}; +use alg_tools::maputil::{array_init, map2, map1_indexed}; use alg_tools::sets::SetOrd; use crate::fourier::Fourier; +use crate::types::Lipschitz; /// Representation of the product of two kernels. /// @@ -55,6 +56,33 @@ } } +impl Differentiable> +for SupportProductFirst +where A : for<'a> Apply<&'a Loc, Output=F> + + for<'a> Differentiable<&'a Loc, Output=Loc>, + B : for<'a> Apply<&'a Loc, Output=F> + + for<'a> Differentiable<&'a Loc, Output=Loc> { + type Output = Loc; + #[inline] + fn differential(&self, x : Loc) -> Self::Output { + self.0.differential(&x) * self.1.apply(&x) + self.1.differential(&x) * self.0.apply(&x) + } +} + +impl<'a, A, B, F : Float, const N : usize> Differentiable<&'a Loc> +for SupportProductFirst +where A : Apply<&'a Loc, Output=F> + + Differentiable<&'a Loc, Output=Loc>, + B : Apply<&'a Loc, Output=F> + + Differentiable<&'a Loc, Output=Loc> { + type Output = Loc; + #[inline] + fn differential(&self, x : &'a Loc) -> Self::Output { + self.0.differential(&x) * self.1.apply(&x) + self.1.differential(&x) * self.0.apply(&x) + } +} + + impl<'a, A, B, F : Float, const N : usize> Support for SupportProductFirst where A : Support, @@ -130,6 +158,28 @@ } } +impl<'a, A, B, F : Float, const N : usize> Differentiable<&'a Loc> +for SupportSum +where A : Differentiable<&'a Loc, Output=Loc>, + B : Differentiable<&'a Loc, Output=Loc> { + type Output = Loc; + #[inline] + fn differential(&self, x : &'a Loc) -> Self::Output { + self.0.differential(x) + self.1.differential(x) + } +} + +impl Differentiable> +for SupportSum +where A : for<'a> Differentiable<&'a Loc, Output=Loc>, + B : for<'a> Differentiable<&'a Loc, Output=Loc> { + type Output = Loc; + #[inline] + fn differential(&self, x : Loc) -> Self::Output { + self.0.differential(&x) + self.1.differential(&x) + } +} + impl<'a, A, B, F : Float, const N : usize> Support for SupportSum where A : Support, @@ -174,6 +224,20 @@ } } +impl Lipschitz for SupportSum +where A : Lipschitz, + B : Lipschitz { + type FloatType = F; + + fn lipschitz_factor(&self, m : M) -> Option { + match (self.0.lipschitz_factor(m), self.1.lipschitz_factor(m)) { + (Some(l0), Some(l1)) => Some(l0 + l1), + _ => None + } + } +} + + /// Representation of the convolution of two kernels. /// /// The kernels typically implement [`Support`]s and [`Mapping`][alg_tools::mapping::Mapping]. @@ -187,6 +251,16 @@ pub B ); +impl Lipschitz for Convolution +where A : Bounded , + B : Lipschitz { + type FloatType = F; + + fn lipschitz_factor(&self, m : M) -> Option { + self.1.lipschitz_factor(m).map(|l| l * self.0.bounds().uniform()) + } +} + /// Representation of the autoconvolution of a kernel. /// /// The kernel typically implements [`Support`] and [`Mapping`][alg_tools::mapping::Mapping]. @@ -198,6 +272,16 @@ pub A ); +impl Lipschitz for AutoConvolution +where C : Lipschitz + Bounded { + type FloatType = F; + + fn lipschitz_factor(&self, m : M) -> Option { + self.0.lipschitz_factor(m).map(|l| l * self.0.bounds().uniform()) + } +} + + /// Representation a multi-dimensional product of a one-dimensional kernel. /// /// For $G: ℝ → ℝ$, this is the function $F(x\_1, …, x\_n) := \prod_{i=1}^n G(x\_i)$. @@ -229,6 +313,45 @@ } } +impl<'a, G, F : Float, const N : usize> Differentiable<&'a Loc> +for UniformProduct +where G : Apply, Output=F> + Differentiable, Output=F> { + type Output = Loc; + #[inline] + fn differential(&self, x : &'a Loc) -> Loc { + let vs = x.map(|y| self.0.apply(Loc([y]))); + product_differential(x, &vs, |y| self.0.differential(Loc([y]))) + } +} + +/// Helper function to calulate the differential of $f(x)=∏_{i=1}^N g(x_i)$. +/// +/// The vector `x` is the location, `vs` consists of the values `g(x_i)`, and +/// `gd` calculates the derivative `g'`. +#[inline] +pub(crate) fn product_differential F, const N : usize>( + x : &Loc, + vs : &Loc, + gd : G +) -> Loc { + map1_indexed(x, |i, &y| { + gd(y) * vs.iter() + .zip(0..) + .filter_map(|(v, j)| (j != i).then_some(*v)) + .product() + }).into() +} + +impl Differentiable> +for UniformProduct +where G : Apply, Output=F> + Differentiable, Output=F> { + type Output = Loc; + #[inline] + fn differential(&self, x : Loc) -> Loc { + self.differential(&x) + } +} + impl Support for UniformProduct where G : Support {