Sun, 11 May 2025 02:03:45 -0500
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
--- a/src/mapping.rs Thu May 08 22:53:31 2025 -0500 +++ b/src/mapping.rs Sun May 11 02:03:45 2025 -0500 @@ -3,12 +3,12 @@ */ use crate::error::DynResult; +use crate::instance::MyCow; pub use crate::instance::{BasicDecomposition, Decomposition, Instance, Space}; use crate::loc::Loc; use crate::norms::{Norm, NormExponent}; use crate::operator_arithmetic::{Constant, Weighted}; use crate::types::{ClosedMul, Float, Num}; -use std::borrow::Cow; use std::marker::PhantomData; use std::ops::Mul; @@ -124,7 +124,7 @@ impl<T, Domain> DifferentiableMapping<Domain> for T where Domain: Space, - T: Clone + Mapping<Domain> + DifferentiableImpl<Domain>, + T: Mapping<Domain> + DifferentiableImpl<Domain>, { type DerivativeDomain = T::Derivative; type Differential<'b> @@ -139,26 +139,26 @@ fn diff(self) -> Differential<'static, Domain, Self> { Differential { - g: Cow::Owned(self), + g: MyCow::Owned(self), _space: PhantomData, } } fn diff_ref(&self) -> Differential<'_, Domain, Self> { Differential { - g: Cow::Borrowed(self), + g: MyCow::Borrowed(self), _space: PhantomData, } } } /// Container for the differential [`Mapping`] of a [`DifferentiableMapping`]. -pub struct Differential<'a, X, G: Clone> { - g: Cow<'a, G>, +pub struct Differential<'a, X, G> { + g: MyCow<'a, G>, _space: PhantomData<X>, } -impl<'a, X, G: Clone> Differential<'a, X, G> { +impl<'a, X, G> Differential<'a, X, G> { pub fn base_fn(&self) -> &G { &self.g } @@ -167,7 +167,7 @@ impl<'a, X, G> Mapping<X> for Differential<'a, X, G> where X: Space, - G: Clone + DifferentiableMapping<X>, + G: DifferentiableMapping<X>, { type Codomain = G::DerivativeDomain; @@ -211,8 +211,8 @@ impl<X: Space, F, G: Sized + Mapping<X, Codomain = Loc<1, F>>> FlattenCodomain<X, F> for G {} /// Container for dimensional slicing [`Loc`]`<N, F>` codomain of a [`Mapping`] to `F`. -pub struct SlicedCodomain<'a, X, F, G: Clone, const N: usize> { - g: Cow<'a, G>, +pub struct SlicedCodomain<'a, X, F, G, const N: usize> { + g: MyCow<'a, G>, slice: usize, _phantoms: PhantomData<(X, F)>, } @@ -221,7 +221,7 @@ where X: Space, F: Copy + Space, - G: Mapping<X, Codomain = Loc<N, F>> + Clone, + G: Mapping<X, Codomain = Loc<N, F>>, { type Codomain = F; @@ -236,13 +236,13 @@ /// An auto-trait for constructing a [`FlattenCodomain`] structure for /// flattening the codomain of a [`Mapping`] from [`Loc`]`<F, 1>` to `F`. pub trait SliceCodomain<X: Space, const N: usize, F: Copy = f64>: - Mapping<X, Codomain = Loc<N, F>> + Clone + Sized + Mapping<X, Codomain = Loc<N, F>> + Sized { /// Flatten the codomain from [`Loc`]`<F, 1>` to `F`. fn slice_codomain(self, slice: usize) -> SlicedCodomain<'static, X, F, Self, N> { assert!(slice < N); SlicedCodomain { - g: Cow::Owned(self), + g: MyCow::Owned(self), slice, _phantoms: PhantomData, } @@ -252,14 +252,14 @@ fn slice_codomain_ref(&self, slice: usize) -> SlicedCodomain<'_, X, F, Self, N> { assert!(slice < N); SlicedCodomain { - g: Cow::Borrowed(self), + g: MyCow::Borrowed(self), slice, _phantoms: PhantomData, } } } -impl<X: Space, F: Copy, G: Sized + Mapping<X, Codomain = Loc<N, F>> + Clone, const N: usize> +impl<X: Space, F: Copy, G: Sized + Mapping<X, Codomain = Loc<N, F>>, const N: usize> SliceCodomain<X, N, F> for G { } @@ -337,7 +337,7 @@ impl<'b, M, X, A> Lipschitz<M> for Differential<'b, X, A> where X: Space, - A: LipschitzDifferentiableImpl<X, M> + Clone, + A: LipschitzDifferentiableImpl<X, M>, { type FloatType = A::FloatType;
--- a/src/mapping/dataterm.rs Thu May 08 22:53:31 2025 -0500 +++ b/src/mapping/dataterm.rs Sun May 11 02:03:45 2025 -0500 @@ -128,9 +128,9 @@ impl<'a, F, X, Y, A, G> LipschitzDifferentiableImpl<X, X::NormExp> for DataTerm<F, X, A, G> where F: Float, - X: Normed<F> + Clone, + X: Normed<F>, Y: Normed<F>, - A: Clone + BoundedLinear<X, X::NormExp, L2, F, Codomain = Y>, + A: BoundedLinear<X, X::NormExp, L2, F, Codomain = Y>, G: Mapping<Y, Codomain = F> + LipschitzDifferentiableImpl<Y, Y::NormExp>, Self: DifferentiableImpl<X>, {
--- a/src/operator_arithmetic.rs Thu May 08 22:53:31 2025 -0500 +++ b/src/operator_arithmetic.rs Sun May 11 02:03:45 2025 -0500 @@ -2,72 +2,74 @@ Arithmetic of [`Mapping`]s. */ -use serde::Serialize; +use crate::instance::{Instance, Space}; +use crate::mapping::{DifferentiableImpl, DifferentiableMapping, Mapping}; use crate::types::*; -use crate::instance::{Space, Instance}; -use crate::mapping::{Mapping, DifferentiableImpl, DifferentiableMapping}; +use serde::Serialize; /// A trait for encoding constant [`Float`] values -pub trait Constant : Copy + Sync + Send + 'static + std::fmt::Debug + Into<Self::Type> { +pub trait Constant: Copy + Sync + Send + 'static + std::fmt::Debug + Into<Self::Type> { /// The type of the value - type Type : Float; + type Type: Float; /// Returns the value of the constant fn value(&self) -> Self::Type; } -impl<F : Float> Constant for F { +impl<F: Float> Constant for F { type Type = F; #[inline] - fn value(&self) -> F { *self } + fn value(&self) -> F { + *self + } } /// Weighting of a [`Mapping`] by scalar multiplication. -#[derive(Copy,Clone,Debug,Serialize)] -pub struct Weighted<T, C : Constant> { +#[derive(Copy, Clone, Debug, Serialize)] +pub struct Weighted<T, C: Constant> { /// The weight - pub weight : C, + pub weight: C, /// The base [`Mapping`] being weighted. - pub base_fn : T, + pub base_fn: T, } impl<T, C> Weighted<T, C> where - C : Constant, + C: Constant, { /// Construct from an iterator. - pub fn new(weight : C, base_fn : T) -> Self { - Weighted{ weight, base_fn } + pub fn new(weight: C, base_fn: T) -> Self { + Weighted { weight, base_fn } } } impl<'a, T, V, D, F, C> Mapping<D> for Weighted<T, C> where - F : Float, - D : Space, - T : Mapping<D, Codomain=V>, - V : Space + ClosedMul<F>, - C : Constant<Type=F> + F: Float, + D: Space, + T: Mapping<D, Codomain = V>, + V: Space + ClosedMul<F>, + C: Constant<Type = F>, { type Codomain = V; #[inline] - fn apply<I : Instance<D>>(&self, x : I) -> Self::Codomain { + fn apply<I: Instance<D>>(&self, x: I) -> Self::Codomain { self.base_fn.apply(x) * self.weight.value() } } impl<'a, T, V, D, F, C> DifferentiableImpl<D> for Weighted<T, C> where - F : Float, - D : Space, - T : DifferentiableMapping<D, DerivativeDomain=V>, - V : Space + std::ops::Mul<F, Output=V>, - C : Constant<Type=F> + F: Float, + D: Space, + T: DifferentiableMapping<D, DerivativeDomain = V>, + V: Space + std::ops::Mul<F, Output = V>, + C: Constant<Type = F>, { type Derivative = V; #[inline] - fn differential_impl<I : Instance<D>>(&self, x : I) -> Self::Derivative { + fn differential_impl<I: Instance<D>>(&self, x: I) -> Self::Derivative { self.base_fn.differential(x) * self.weight.value() } } @@ -76,9 +78,9 @@ #[derive(Serialize, Debug, Clone)] pub struct MappingSum<M>(Vec<M>); -impl< M> MappingSum<M> { +impl<M> MappingSum<M> { /// Construct from an iterator. - pub fn new<I : IntoIterator<Item = M>>(iter : I) -> Self { + pub fn new<I: IntoIterator<Item = M>>(iter: I) -> Self { MappingSum(iter.into_iter().collect()) } @@ -90,27 +92,27 @@ impl<Domain, M> Mapping<Domain> for MappingSum<M> where - Domain : Space + Clone, - M : Mapping<Domain>, - M::Codomain : std::iter::Sum + Clone + Domain: Space + Clone, + M: Mapping<Domain>, + M::Codomain: std::iter::Sum + Clone, { type Codomain = M::Codomain; - fn apply<I : Instance<Domain>>(&self, x : I) -> Self::Codomain { + fn apply<I: Instance<Domain>>(&self, x: I) -> Self::Codomain { let xr = x.ref_instance(); self.0.iter().map(|c| c.apply(xr)).sum() } } -impl<Domain, M> DifferentiableImpl<Domain> for MappingSum< M> +impl<Domain, M> DifferentiableImpl<Domain> for MappingSum<M> where - Domain : Space + Clone, - M : DifferentiableMapping<Domain>, - M :: DerivativeDomain : std::iter::Sum + Domain: Space, + M: DifferentiableMapping<Domain>, + M::DerivativeDomain: std::iter::Sum, { type Derivative = M::DerivativeDomain; - fn differential_impl<I : Instance<Domain>>(&self, x : I) -> Self::Derivative { + fn differential_impl<I: Instance<Domain>>(&self, x: I) -> Self::Derivative { let xr = x.ref_instance(); self.0.iter().map(|c| c.differential(xr)).sum() }