Mon, 01 Sep 2025 00:04:22 -0500
wrap hacks
| 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)*) => { | |
| 127 | $crate::wrap!(impl_unary $type, std::ops::Neg, neg where $($qual)*); | |
| 128 | $crate::wrap!(impl_binary $type, std::ops::Add, add where $($qual)*); | |
| 129 | $crate::wrap!(impl_binary $type, std::ops::Sub, sub where $($qual)*); | |
| 130 | $crate::wrap!(impl_scalar $F, $type, std::ops::Mul, mul where $($qual)*); | |
| 131 | $crate::wrap!(impl_scalar $F, $type, std::ops::Div, div where $($qual)*); | |
| 132 | $crate::wrap!(impl_scalar_lhs $F, $type, std::ops::Mul, mul where $($qual)*); | |
| 133 | $crate::wrap!(impl_binary_mut $type, std::ops::AddAssign, add_assign where $($qual)*); | |
| 134 | $crate::wrap!(impl_binary_mut $type, std::ops::SubAssign, sub_assign where $($qual)*); | |
| 135 | $crate::wrap!(impl_scalar_mut $F, $type, std::ops::MulAssign, mul_assign where $($qual)*); | |
| 136 | $crate::wrap!(impl_scalar_mut $F, $type, std::ops::DivAssign, div_assign where $($qual)*); | |
| 146 | 137 | |
| 149 | 138 | impl<$($qual)*> $crate::euclidean::Euclidean<$F> for $type |
| 139 | // where | |
| 140 | // Self: $crate::euclidean::wrap::Wrapped<WrappedField = $F> | |
| 141 | // + Sized | |
| 142 | // + std::ops::Mul<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 143 | // + std::ops::MulAssign<F> | |
| 144 | // + std::ops::Div<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 145 | // + std::ops::DivAssign<F> | |
| 146 | // + std::ops::Add<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 147 | // + std::ops::Sub<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 148 | // + for<'b> std::ops::Add<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 149 | // + for<'b> std::ops::Sub<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 150 | // + std::ops::AddAssign<Self> | |
| 151 | // + for<'b> std::ops::AddAssign<&'b Self> | |
| 152 | // + std::ops::SubAssign<Self> | |
| 153 | // + for<'b> std::ops::SubAssign<&'b Self> | |
| 154 | // + std::ops::Neg<Output = <Self as $crate::linops::AXPY>::Owned>, | |
| 146 | 155 | { |
| 149 | 156 | fn dot<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { |
| 157 | other.eval_decompose(|x| self.get_view().dot(&x.get_view())) | |
| 146 | 158 | } |
| 159 | ||
| 149 | 160 | fn norm2_squared(&self) -> $F { |
| 146 | 161 | self.get_view().norm2_squared() |
| 162 | } | |
| 163 | ||
| 149 | 164 | fn dist2_squared<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { |
| 146 | 165 | other.eval_decompose(|x| self.get_view().dist2_squared(x.get_view())) |
| 166 | } | |
| 167 | } | |
| 168 | ||
| 149 | 169 | impl<$($qual)*> $crate::linops::AXPY for $type |
| 170 | // where | |
| 171 | // Self : $crate::euclidean::wrap::Wrapped<WrappedField = $F>, | |
| 172 | // Self::Unwrapped : $crate::linops::AXPY<Field = F>, | |
| 173 | // Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 174 | // Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 146 | 175 | { |
| 149 | 176 | type Field = $F; |
| 147 | 177 | type Owned = Self; |
| 146 | 178 | |
| 149 | 179 | fn axpy<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I, β: $F) { |
| 146 | 180 | x.eval_decompose(|v| { |
| 149 | 181 | self.get_view_mut().axpy(α, v.get_view(), β) |
| 146 | 182 | }) |
| 183 | } | |
| 184 | ||
| 147 | 185 | fn copy_from<I: $crate::instance::Instance<Self>>(&mut self, x: I) { |
| 149 | 186 | x.eval_decompose(|v| { |
| 187 | self.get_view_mut().copy_from(v.get_view()) | |
| 146 | 188 | }) |
| 189 | } | |
| 190 | ||
| 149 | 191 | fn scale_from<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I) { |
| 146 | 192 | x.eval_decompose(|v| { |
| 193 | self.get_mut_view().scale_from(α, v.get_view()) | |
| 194 | }) | |
| 195 | } | |
| 196 | ||
| 197 | /// Return a similar zero as `self`. | |
| 198 | fn similar_origin(&self) -> Self::Owned { | |
| 147 | 199 | Self::wrap(self.get_view().similar_origin()) |
| 146 | 200 | } |
| 201 | ||
| 202 | /// Set self to zero. | |
| 203 | fn set_zero(&mut self) { | |
| 204 | self.get_mut_view().set_zero() | |
| 205 | } | |
| 206 | } | |
| 207 | ||
| 149 | 208 | impl<$($qual)*> $crate::instance::Space for $type { |
| 209 | type Decomp = <<Self as $crate::euclidean::wrap::Wrapped>::Unwrapped as $crate::instance::Space>::Decomp; | |
| 146 | 210 | } |
| 211 | }; | |
| 212 | } |