diff -r 123f7f38e161 -r 1f19c6bbf07b src/direct_product.rs --- a/src/direct_product.rs Tue Apr 08 13:30:12 2025 -0500 +++ b/src/direct_product.rs Sun Apr 27 20:29:43 2025 -0500 @@ -5,33 +5,38 @@ operations on references. */ -use core::ops::{Mul,MulAssign,Div,DivAssign,Add,AddAssign,Sub,SubAssign,Neg}; -use std::clone::Clone; -use serde::{Serialize, Deserialize}; -use crate::types::{Num, Float}; -use crate::{maybe_lifetime, maybe_ref}; use crate::euclidean::Euclidean; -use crate::instance::{Instance, InstanceMut, Decomposition, DecompositionMut, MyCow}; -use crate::mapping::Space; +use crate::instance::{Decomposition, DecompositionMut, Instance, InstanceMut, MyCow}; use crate::linops::AXPY; use crate::loc::Loc; -use crate::norms::{Norm, PairNorm, NormExponent, Normed, HasDual, L2}; +use crate::mapping::Space; +use crate::norms::{HasDual, Norm, NormExponent, Normed, PairNorm, L2}; +use crate::types::{Float, Num}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use serde::{Deserialize, Serialize}; +use std::clone::Clone; -#[derive(Debug,Clone,Copy,PartialEq,Eq,Serialize,Deserialize)] -pub struct Pair (pub A, pub B); +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct Pair(pub A, pub B); -impl Pair { - pub fn new(a : A, b : B) -> Pair { Pair(a, b) } +impl Pair { + pub fn new(a: A, b: B) -> Pair { + Pair(a, b) + } } -impl From<(A,B)> for Pair { +impl From<(A, B)> for Pair { #[inline] - fn from((a, b) : (A, B)) -> Pair { Pair(a, b) } + fn from((a, b): (A, B)) -> Pair { + Pair(a, b) + } } -impl From> for (A,B) { +impl From> for (A, B) { #[inline] - fn from(Pair(a, b) : Pair) -> (A,B) { (a, b) } + fn from(Pair(a, b): Pair) -> (A, B) { + (a, b) + } } macro_rules! impl_binop { @@ -152,16 +157,18 @@ macro_rules! impl_scalar_assignop { (($a : ty, $b : ty), $field : ty, $trait : ident, $fn : ident) => { - impl<'r> $trait<$field> - for Pair<$a, $b> - where $a: $trait<$field>, $b: $trait<$field> { + impl<'r> $trait<$field> for Pair<$a, $b> + where + $a: $trait<$field>, + $b: $trait<$field>, + { #[inline] - fn $fn(&mut self, a : $field) -> () { + fn $fn(&mut self, a: $field) -> () { self.0.$fn(a); self.1.$fn(a); } } - } + }; } macro_rules! impl_unaryop { @@ -241,24 +248,28 @@ impl Euclidean for Pair where - A : Euclidean, - B : Euclidean, - F : Float, - PairOutput : Euclidean, - Self : Sized - + Mul> + MulAssign - + Div> + DivAssign - + Add> - + Sub> - + for<'b> Add<&'b Self, Output=PairOutput> - + for<'b> Sub<&'b Self, Output=PairOutput> - + AddAssign + for<'b> AddAssign<&'b Self> - + SubAssign + for<'b> SubAssign<&'b Self> - + Neg> + A: Euclidean, + B: Euclidean, + F: Float, + PairOutput: Euclidean, + Self: Sized + + Mul> + + MulAssign + + Div> + + DivAssign + + Add> + + Sub> + + for<'b> Add<&'b Self, Output = PairOutput> + + for<'b> Sub<&'b Self, Output = PairOutput> + + AddAssign + + for<'b> AddAssign<&'b Self> + + SubAssign + + for<'b> SubAssign<&'b Self> + + Neg>, { type Output = PairOutput; - fn dot>(&self, other : I) -> F { + fn dot>(&self, other: I) -> F { let Pair(u, v) = other.decompose(); self.0.dot(u) + self.1.dot(v) } @@ -267,7 +278,7 @@ self.0.norm2_squared() + self.1.norm2_squared() } - fn dist2_squared>(&self, other : I) -> F { + fn dist2_squared>(&self, other: I) -> F { let Pair(u, v) = other.decompose(); self.0.dist2_squared(u) + self.1.dist2_squared(v) } @@ -275,31 +286,30 @@ impl AXPY> for Pair where - U : Space, - V : Space, - A : AXPY, - B : AXPY, - F : Num, - Self : MulAssign, - Pair : MulAssign, - Pair : AXPY>, + U: Space, + V: Space, + A: AXPY, + B: AXPY, + F: Num, + Self: MulAssign, + Pair: MulAssign, + Pair: AXPY>, { - type Owned = Pair; - fn axpy>>(&mut self, α : F, x : I, β : F) { + fn axpy>>(&mut self, α: F, x: I, β: F) { let Pair(u, v) = x.decompose(); self.0.axpy(α, u, β); self.1.axpy(α, v, β); } - fn copy_from>>(&mut self, x : I) { + fn copy_from>>(&mut self, x: I) { let Pair(u, v) = x.decompose(); self.0.copy_from(u); self.1.copy_from(v); } - fn scale_from>>(&mut self, α : F, x : I) { + fn scale_from>>(&mut self, α: F, x: I) { let Pair(u, v) = x.decompose(); self.0.scale_from(α, u); self.1.scale_from(α, v); @@ -321,52 +331,64 @@ #[derive(Copy, Clone, Debug)] pub struct PairDecomposition(D, Q); -impl Space for Pair { +impl Space for Pair { type Decomp = PairDecomposition; } -impl Decomposition> for PairDecomposition +impl Decomposition> for PairDecomposition where - A : Space, - B : Space, - D : Decomposition, - Q : Decomposition, + A: Space, + B: Space, + D: Decomposition, + Q: Decomposition, { - type Decomposition<'b> = Pair, Q::Decomposition<'b>> where Pair : 'b; - type Reference<'b> = Pair, Q::Reference<'b>> where Pair : 'b; + type Decomposition<'b> + = Pair, Q::Decomposition<'b>> + where + Pair: 'b; + type Reference<'b> + = Pair, Q::Reference<'b>> + where + Pair: 'b; #[inline] - fn lift<'b>(Pair(u, v) : Self::Reference<'b>) -> Self::Decomposition<'b> { + fn lift<'b>(Pair(u, v): Self::Reference<'b>) -> Self::Decomposition<'b> { Pair(D::lift(u), Q::lift(v)) } } impl Instance, PairDecomposition> for Pair where - A : Space, - B : Space, - D : Decomposition, - Q : Decomposition, - U : Instance, - V : Instance, + A: Space, + B: Space, + D: Decomposition, + Q: Decomposition, + U: Instance, + V: Instance, { #[inline] - fn decompose<'b>(self) - -> as Decomposition>>::Decomposition<'b> - where Self : 'b, Pair : 'b + fn decompose<'b>( + self, + ) -> as Decomposition>>::Decomposition<'b> + where + Self: 'b, + Pair: 'b, { Pair(self.0.decompose(), self.1.decompose()) } #[inline] - fn ref_instance(&self) - -> as Decomposition>>::Reference<'_> - { + fn ref_instance( + &self, + ) -> as Decomposition>>::Reference<'_> { Pair(self.0.ref_instance(), self.1.ref_instance()) } #[inline] - fn cow<'b>(self) -> MyCow<'b, Pair> where Self : 'b{ + fn cow<'b>(self) -> MyCow<'b, Pair> + where + Self: 'b, + { MyCow::Owned(Pair(self.0.own(), self.1.own())) } @@ -376,35 +398,43 @@ } } - impl<'a, A, B, U, V, D, Q> Instance, PairDecomposition> for &'a Pair where - A : Space, - B : Space, - D : Decomposition, - Q : Decomposition, - U : Instance, - V : Instance, - &'a U : Instance, - &'a V : Instance, + A: Space, + B: Space, + D: Decomposition, + Q: Decomposition, + U: Instance, + V: Instance, + &'a U: Instance, + &'a V: Instance, { #[inline] - fn decompose<'b>(self) - -> as Decomposition>>::Decomposition<'b> - where Self : 'b, Pair : 'b + fn decompose<'b>( + self, + ) -> as Decomposition>>::Decomposition<'b> + where + Self: 'b, + Pair: 'b, { - Pair(D::lift(self.0.ref_instance()), Q::lift(self.1.ref_instance())) + Pair( + D::lift(self.0.ref_instance()), + Q::lift(self.1.ref_instance()), + ) } #[inline] - fn ref_instance(&self) - -> as Decomposition>>::Reference<'_> - { + fn ref_instance( + &self, + ) -> as Decomposition>>::Reference<'_> { Pair(self.0.ref_instance(), self.1.ref_instance()) } #[inline] - fn cow<'b>(self) -> MyCow<'b, Pair> where Self : 'b { + fn cow<'b>(self) -> MyCow<'b, Pair> + where + Self: 'b, + { MyCow::Owned(self.own()) } @@ -413,75 +443,74 @@ let Pair(ref u, ref v) = self; Pair(u.own(), v.own()) } - } -impl DecompositionMut> for PairDecomposition +impl DecompositionMut> for PairDecomposition where - A : Space, - B : Space, - D : DecompositionMut, - Q : DecompositionMut, + A: Space, + B: Space, + D: DecompositionMut, + Q: DecompositionMut, { - type ReferenceMut<'b> = Pair, Q::ReferenceMut<'b>> where Pair : 'b; + type ReferenceMut<'b> + = Pair, Q::ReferenceMut<'b>> + where + Pair: 'b; } impl InstanceMut, PairDecomposition> for Pair where - A : Space, - B : Space, - D : DecompositionMut, - Q : DecompositionMut, - U : InstanceMut, - V : InstanceMut, + A: Space, + B: Space, + D: DecompositionMut, + Q: DecompositionMut, + U: InstanceMut, + V: InstanceMut, { #[inline] - fn ref_instance_mut(&mut self) - -> as DecompositionMut>>::ReferenceMut<'_> - { + fn ref_instance_mut( + &mut self, + ) -> as DecompositionMut>>::ReferenceMut<'_> { Pair(self.0.ref_instance_mut(), self.1.ref_instance_mut()) } } impl<'a, A, B, U, V, D, Q> InstanceMut, PairDecomposition> for &'a mut Pair where - A : Space, - B : Space, - D : DecompositionMut, - Q : DecompositionMut, - U : InstanceMut, - V : InstanceMut, + A: Space, + B: Space, + D: DecompositionMut, + Q: DecompositionMut, + U: InstanceMut, + V: InstanceMut, { #[inline] - fn ref_instance_mut(&mut self) - -> as DecompositionMut>>::ReferenceMut<'_> - { + fn ref_instance_mut( + &mut self, + ) -> as DecompositionMut>>::ReferenceMut<'_> { Pair(self.0.ref_instance_mut(), self.1.ref_instance_mut()) } } - -impl Norm> -for Pair +impl Norm> for Pair where - F : Num, - ExpA : NormExponent, - ExpB : NormExponent, - ExpJ : NormExponent, - A : Norm, - B : Norm, - Loc : Norm, + F: Num, + ExpA: NormExponent, + ExpB: NormExponent, + ExpJ: NormExponent, + A: Norm, + B: Norm, + Loc: Norm, { - fn norm(&self, PairNorm(expa, expb, expj) : PairNorm) -> F { + fn norm(&self, PairNorm(expa, expb, expj): PairNorm) -> F { Loc([self.0.norm(expa), self.1.norm(expb)]).norm(expj) } } - -impl Normed for Pair +impl Normed for Pair where - A : Normed, - B : Normed, + A: Normed, + B: Normed, { type NormExp = PairNorm; @@ -496,11 +525,10 @@ } } -impl HasDual for Pair +impl HasDual for Pair where - A : HasDual, - B : HasDual, - + A: HasDual, + B: HasDual, { type DualSpace = Pair; }