# HG changeset patch # User Tuomo Valkonen # Date 1735652930 18000 # Node ID c5f70e76751139511c3d501caf4f6220bf4eb5cb # Parent d7c0f431cbd6a949773dc78984e2f8ca76ce1d66 Split out and generalise Weighted diff -r d7c0f431cbd6 -r c5f70e767511 src/bisection_tree/support.rs --- a/src/bisection_tree/support.rs Mon Dec 30 09:49:08 2024 -0500 +++ b/src/bisection_tree/support.rs Tue Dec 31 08:48:50 2024 -0500 @@ -13,21 +13,7 @@ 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; -} - -impl Constant for F { - type Type = F; - #[inline] - fn value(&self) -> F { *self } -} - +pub use crate::operator_arithmetic::{Weighted, Constant}; /// A trait for working with the supports of [`Apply`]s. /// @@ -64,12 +50,6 @@ 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`]. @@ -204,40 +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> Mapping> for Weighted -where T : Mapping, Codomain=V>, - V : Space + std::ops::Mul, - C : Constant { - type Codomain = V; - - #[inline] - fn apply>>(&self, x : I) -> Self::Codomain { - self.base_fn.apply(x) * self.weight.value() - } -} - -impl<'a, T, V, F : Float, C, const N : usize> DifferentiableImpl> for Weighted -where T : DifferentiableMapping, DerivativeDomain=V>, - V : Space + std::ops::Mul, - C : Constant { - type Derivative = V; - - #[inline] - fn differential_impl>>(&self, x : I) -> Self::Derivative { - self.base_fn.differential(x) * self.weight.value() - } -} - impl<'a, T, F : Float, C, const N : usize> Support for Weighted where T : Support, C : Constant { diff -r d7c0f431cbd6 -r c5f70e767511 src/lib.rs --- a/src/lib.rs Mon Dec 30 09:49:08 2024 -0500 +++ b/src/lib.rs Tue Dec 31 08:48:50 2024 -0500 @@ -42,5 +42,7 @@ pub(crate) mod metaprogramming; pub mod direct_product; pub mod convex; +pub mod discrete_gradient; +pub mod operator_arithmetic; pub use types::*; diff -r d7c0f431cbd6 -r c5f70e767511 src/mapping.rs --- a/src/mapping.rs Mon Dec 30 09:49:08 2024 -0500 +++ b/src/mapping.rs Tue Dec 31 08:48:50 2024 -0500 @@ -4,8 +4,7 @@ use std::marker::PhantomData; use std::borrow::Cow; -use crate::types::{Num, Float}; -use serde::Serialize; +use crate::types::{Num, Float, ClosedMul}; use crate::loc::Loc; pub use crate::instance::{Instance, Decomposition, BasicDecomposition, Space}; use crate::norms::{Norm, NormExponent}; @@ -45,6 +44,17 @@ { Composition{ outer : self, inner : other, intermediate_norm_exponent : norm } } + + /// Multiply `self` by the scalar `a`. + #[inline] + fn weigh(self, a : C) -> Weighted + where + Self : Sized, + C : Constant, + Self::Codomain : ClosedMul, + { + Weighted { weight : a, base_fn : self } + } } /// Automatically implemented shorthand for referring to [`Mapping`]s from [`Loc`] to `F`. diff -r d7c0f431cbd6 -r c5f70e767511 src/operator_arithmetic.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/operator_arithmetic.rs Tue Dec 31 08:48:50 2024 -0500 @@ -0,0 +1,64 @@ +/*! +Arithmetic of [`Mapping`]s. + */ + +use serde::Serialize; +use crate::types::*; +use crate::instance::{Space, Instance}; +use crate::mapping::{Mapping, DifferentiableImpl, DifferentiableMapping}; + +/// 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; +} + +impl Constant for F { + type Type = F; + #[inline] + fn value(&self) -> F { *self } +} + +/// 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, D, F, C> Mapping for Weighted +where + F : Float, + D : Space, + T : Mapping, + V : Space + ClosedMul, + C : Constant +{ + type Codomain = V; + + #[inline] + fn apply>(&self, x : I) -> Self::Codomain { + self.base_fn.apply(x) * self.weight.value() + } +} + +impl<'a, T, V, D, F, C> DifferentiableImpl for Weighted +where + F : Float, + D : Space, + T : DifferentiableMapping, + V : Space + std::ops::Mul, + C : Constant +{ + type Derivative = V; + + #[inline] + fn differential_impl>(&self, x : I) -> Self::Derivative { + self.base_fn.differential(x) * self.weight.value() + } +}