Fri, 05 Sep 2025 00:56:59 -0500
wrap
| 146 | 1 | /*! |
| 2 | Wrappers for implemention [`Euclidean`] operations. | |
| 3 | */ | |
| 4 | ||
| 149 | 5 | use crate::euclidean::Euclidean; |
| 6 | use crate::instance::Space; | |
| 7 | use crate::types::Float; | |
| 8 | ||
| 9 | pub trait Wrapped: Space { | |
| 10 | type WrappedField: Float; | |
| 11 | type Unwrapped: Euclidean<Self::WrappedField>; | |
| 12 | type UnwrappedMut: Euclidean<Self::WrappedField>; | |
| 13 | type UnwrappedOutput: Euclidean<Self::WrappedField>; | |
| 14 | type WrappedOutput; | |
| 147 | 15 | fn get_view(&self) -> Self::Unwrapped; |
| 16 | fn get_view_mut(&mut self) -> Self::UnwrappedMut; | |
| 149 | 17 | fn wrap(output: Self::UnwrappedOutput) -> Self::WrappedOutput; |
| 147 | 18 | } |
| 146 | 19 | |
| 147 | 20 | #[macro_export] |
| 21 | macro_rules! wrap { | |
| 149 | 22 | // Rust macros are totally fucked up. $trait:path does not work, have to |
| 23 | // manually code paths through $($trait:ident)::+. | |
| 24 | (impl_unary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { | |
| 25 | impl<$($qual)*> $($trait)::+ for $type { | |
| 26 | type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; | |
| 146 | 27 | fn $fn(self) -> Self::Output { |
| 147 | 28 | Self::wrap(self.get_view().$fn()) |
| 146 | 29 | } |
| 30 | } | |
| 31 | }; | |
| 149 | 32 | (impl_binary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { |
| 33 | impl<$($qual)*> $($trait)::+<$type> for $type { | |
| 34 | type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; | |
| 146 | 35 | fn $fn(self, other: $type) -> Self::Output { |
| 147 | 36 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 37 | } |
| 38 | } | |
| 39 | ||
| 149 | 40 | impl<'a, $($qual)*> $($trait)::+<$type> for &'a $type { |
| 41 | type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; | |
| 146 | 42 | fn $fn(self, other: $type) -> Self::Output { |
| 149 | 43 | <$type>::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 44 | } |
| 45 | } | |
| 46 | ||
| 149 | 47 | impl<'a, 'b, $($qual)*> $($trait)::+<&'b $type> for &'a $type { |
| 48 | type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; | |
| 49 | fn $fn(self, other: &'b $type) -> Self::Output { | |
| 50 | <$type>::wrap(self.get_view().$fn(other.get_view())) | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type { | |
| 55 | type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; | |
| 56 | fn $fn(self, other: &'b $type) -> Self::Output { | |
| 147 | 57 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 58 | } |
| 59 | } | |
| 60 | }; | |
| 149 | 61 | (impl_scalar $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { |
| 62 | impl<$($qual)*> $($trait)::+<$F> for $type | |
| 63 | // where | |
| 64 | // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, | |
| 65 | // //$type::Unwrapped: $($trait)::+<F>, | |
| 146 | 66 | { |
| 149 | 67 | type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; |
| 68 | fn $fn(self, t: $F) -> Self::Output { | |
| 147 | 69 | Self::wrap(self.get_view().$fn(t)) |
| 146 | 70 | } |
| 71 | } | |
| 72 | ||
| 149 | 73 | impl<'a, $($qual)*> $($trait)::+<$F> for &'a $type |
| 74 | // where | |
| 75 | // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, | |
| 76 | // //$type::Unwrapped: $($trait)::+<F>, | |
| 146 | 77 | { |
| 149 | 78 | type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; |
| 79 | fn $fn(self, t: $F) -> Self::Output { | |
| 80 | <$type>::wrap(self.get_view().$fn(t)) | |
| 146 | 81 | } |
| 82 | } | |
| 83 | ||
| 84 | }; | |
| 149 | 85 | (impl_scalar_lhs $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { |
| 86 | impl<$($qual)*> $($trait)::+<$type> for $F | |
| 87 | // where | |
| 88 | // $type: $crate::euclidean::wrap::Wrapped<WrappedField = $F>, | |
| 89 | // // where | |
| 90 | // // $F: $($trait)::+<$type::Unwrapped>, | |
| 146 | 91 | { |
| 149 | 92 | type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; |
| 146 | 93 | fn $fn(self, rhs: $type) -> Self::Output { |
| 149 | 94 | <$type>::wrap(self.$fn(rhs.get_view())) |
| 146 | 95 | } |
| 96 | } | |
| 97 | }; | |
| 149 | 98 | (impl_binary_mut $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { |
| 99 | impl<$($qual)*> $($trait)::+<$type> for $type { | |
| 146 | 100 | fn $fn(&mut self, rhs: $type) { |
| 101 | self.get_view_mut().$fn(rhs.get_view()) | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 149 | 105 | impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type { |
| 106 | fn $fn(&mut self, rhs: &'b $type) { | |
| 146 | 107 | self.get_view_mut().$fn(rhs.get_view()) |
| 108 | } | |
| 109 | } | |
| 110 | }; | |
| 149 | 111 | (impl_scalar_mut $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { |
| 112 | impl<$($qual)*> $($trait)::+<$F> for $type | |
| 113 | // where | |
| 114 | // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, | |
| 115 | // // where | |
| 116 | // // $type::UnwrappedMut: $($trait)::+<$($trait)::+<F>>, | |
| 146 | 117 | { |
| 149 | 118 | fn $fn(&mut self, t: $F) { |
| 119 | self.get_view_mut().$fn(t) | |
| 146 | 120 | } |
| 121 | } | |
| 122 | }; | |
| 149 | 123 | // ($type:ty) => { |
| 124 | // $crate::wrap!(imp<> do $type); | |
| 125 | // }; | |
| 126 | ($F:ty; $type:ty where $($qual:tt)*) => { | |
| 176 | 127 | |
| 149 | 128 | $crate::wrap!(impl_unary $type, std::ops::Neg, neg where $($qual)*); |
| 129 | $crate::wrap!(impl_binary $type, std::ops::Add, add where $($qual)*); | |
| 130 | $crate::wrap!(impl_binary $type, std::ops::Sub, sub where $($qual)*); | |
| 131 | $crate::wrap!(impl_scalar $F, $type, std::ops::Mul, mul where $($qual)*); | |
| 132 | $crate::wrap!(impl_scalar $F, $type, std::ops::Div, div where $($qual)*); | |
| 133 | $crate::wrap!(impl_scalar_lhs $F, $type, std::ops::Mul, mul where $($qual)*); | |
| 134 | $crate::wrap!(impl_binary_mut $type, std::ops::AddAssign, add_assign where $($qual)*); | |
| 135 | $crate::wrap!(impl_binary_mut $type, std::ops::SubAssign, sub_assign where $($qual)*); | |
| 136 | $crate::wrap!(impl_scalar_mut $F, $type, std::ops::MulAssign, mul_assign where $($qual)*); | |
| 137 | $crate::wrap!(impl_scalar_mut $F, $type, std::ops::DivAssign, div_assign where $($qual)*); | |
| 146 | 138 | |
| 174 | 139 | $crate::self_ownable!($type where $($qual)*); |
| 140 | ||
| 175 | 141 | impl<$($qual)*> $crate::norms::Norm<$crate::norms::L2, $F> for $type |
| 142 | { | |
| 143 | fn norm(&self, p : $crate::norms::L2) -> $F { | |
| 176 | 144 | $crate::norms::Norm::norm(&self.get_view(), p) |
| 175 | 145 | } |
| 146 | } | |
| 147 | ||
| 148 | impl<$($qual)*> $crate::norms::Dist<$crate::norms::L2, $F> for $type | |
| 149 | { | |
| 150 | fn dist<I: $crate::instance::Instance<Self>>(&self, other : I, p : $crate::norms::L2) -> $F { | |
|
177
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
151 | other.eval_ref(|x| self.get_view().dist(x.get_view(), p)) |
| 175 | 152 | } |
| 153 | } | |
| 154 | ||
| 155 | impl<$($qual)*> $crate::norms::Normed<$F> for $type { | |
| 156 | type NormExp = $crate::norms::L2; | |
| 157 | ||
| 158 | fn norm_exponent(&self) -> Self::NormExp { | |
| 159 | $crate::norms::L2 | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | impl<$($qual)*> $crate::norms::HasDual<$F> for $type { | |
| 164 | type DualSpace = Self; | |
| 165 | ||
| 166 | fn dual_origin(&self) -> Self { | |
| 176 | 167 | $crate::linops::VectorSpace::similar_origin(self) |
| 175 | 168 | } |
| 169 | } | |
| 170 | ||
| 149 | 171 | impl<$($qual)*> $crate::euclidean::Euclidean<$F> for $type |
| 172 | // where | |
| 173 | // Self: $crate::euclidean::wrap::Wrapped<WrappedField = $F> | |
| 174 | // + Sized | |
| 175 | // + std::ops::Mul<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 176 | // + std::ops::MulAssign<F> | |
| 177 | // + std::ops::Div<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 178 | // + std::ops::DivAssign<F> | |
| 179 | // + std::ops::Add<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 180 | // + std::ops::Sub<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 181 | // + for<'b> std::ops::Add<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 182 | // + for<'b> std::ops::Sub<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 183 | // + std::ops::AddAssign<Self> | |
| 184 | // + for<'b> std::ops::AddAssign<&'b Self> | |
| 185 | // + std::ops::SubAssign<Self> | |
| 186 | // + for<'b> std::ops::SubAssign<&'b Self> | |
| 187 | // + std::ops::Neg<Output = <Self as $crate::linops::AXPY>::Owned>, | |
| 146 | 188 | { |
| 174 | 189 | type PrincipalE = Self; |
| 190 | ||
| 149 | 191 | fn dot<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { |
| 192 | other.eval_decompose(|x| self.get_view().dot(&x.get_view())) | |
| 146 | 193 | } |
| 194 | ||
| 149 | 195 | fn norm2_squared(&self) -> $F { |
| 146 | 196 | self.get_view().norm2_squared() |
| 197 | } | |
| 198 | ||
| 149 | 199 | fn dist2_squared<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { |
| 146 | 200 | other.eval_decompose(|x| self.get_view().dist2_squared(x.get_view())) |
| 201 | } | |
| 202 | } | |
| 203 | ||
| 174 | 204 | impl<$($qual)*> $crate::linops::VectorSpace for $type |
| 205 | // where | |
| 206 | // Self : $crate::euclidean::wrap::Wrapped<WrappedField = $F>, | |
| 207 | // Self::Unwrapped : $crate::linops::AXPY<Field = F>, | |
| 208 | // Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 209 | // Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 210 | { | |
| 211 | type Field = $F; | |
| 212 | type PrincipalV = Self; | |
| 213 | ||
| 214 | /// Return a similar zero as `self`. | |
| 215 | fn similar_origin(&self) -> Self::PrincipalV { | |
| 216 | Self::wrap(self.get_view().similar_origin()) | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 149 | 220 | impl<$($qual)*> $crate::linops::AXPY for $type |
| 221 | // where | |
| 222 | // Self : $crate::euclidean::wrap::Wrapped<WrappedField = $F>, | |
| 223 | // Self::Unwrapped : $crate::linops::AXPY<Field = F>, | |
| 224 | // Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 225 | // Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 146 | 226 | { |
| 174 | 227 | fn axpy<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I, β: $F) { |
| 146 | 228 | x.eval_decompose(|v| { |
| 179 | 229 | $crate::linops::AXPY::axpy(&mut self.get_view_mut(), α, v.get_view(), β) |
| 146 | 230 | }) |
| 231 | } | |
| 232 | ||
| 147 | 233 | fn copy_from<I: $crate::instance::Instance<Self>>(&mut self, x: I) { |
| 149 | 234 | x.eval_decompose(|v| { |
| 179 | 235 | $crate::linops::AXPY::copy_from(&mut self.get_view_mut(), v.get_view()) |
| 146 | 236 | }) |
| 237 | } | |
| 238 | ||
| 149 | 239 | fn scale_from<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I) { |
| 146 | 240 | x.eval_decompose(|v| { |
| 179 | 241 | $crate::linops::AXPY::scale_from(&mut self.get_view_mut(), α, v.get_view()) |
| 146 | 242 | }) |
| 243 | } | |
| 244 | ||
| 245 | /// Set self to zero. | |
| 246 | fn set_zero(&mut self) { | |
| 175 | 247 | self.get_view_mut().set_zero() |
| 146 | 248 | } |
| 249 | } | |
| 250 | ||
| 149 | 251 | impl<$($qual)*> $crate::instance::Space for $type { |
| 176 | 252 | type Decomp = $crate::instance::BasicDecomposition; |
| 174 | 253 | type Principal = Self; |
| 146 | 254 | } |
| 255 | }; | |
| 256 | } |