Sat, 30 Aug 2025 22:43:37 -0500
Bump pyo3
| 146 | 1 | /*! |
| 2 | Wrappers for implemention [`Euclidean`] operations. | |
| 3 | */ | |
| 4 | ||
| 147 | 5 | pub trait Wrapped { |
| 6 | type Unwrapped; | |
| 7 | type UnwrappedMut; | |
| 8 | type UnwrappedOutput; | |
| 9 | fn get_view(&self) -> Self::Unwrapped; | |
| 10 | fn get_view_mut(&mut self) -> Self::UnwrappedMut; | |
| 11 | fn wrap(output: Self::UnwrappedOutput) -> Self; | |
| 12 | } | |
| 146 | 13 | |
| 147 | 14 | #[macro_export] |
| 15 | macro_rules! wrap { | |
| 16 | (impl_unary<$($qual:tt)*> $type:ty, $trait:path, $fn:ident) => { | |
| 17 | impl<$($qual)*> $trait for $type { | |
| 146 | 18 | type Output = Self::WrappedOutput; |
| 19 | fn $fn(self) -> Self::Output { | |
| 147 | 20 | Self::wrap(self.get_view().$fn()) |
| 146 | 21 | } |
| 22 | } | |
| 23 | }; | |
| 147 | 24 | (impl_binary<$($qual:tt)*> $type:ty, $trait:path, $fn:ident) => { |
| 25 | impl<$($qual)*> $trait<$type> for $type { | |
| 146 | 26 | type Output = Self::WrappedOutput; |
| 27 | fn $fn(self, other: $type) -> Self::Output { | |
| 147 | 28 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 29 | } |
| 30 | } | |
| 31 | ||
| 147 | 32 | impl<'a, $($qual)*> $trait<$type> for &'a $type { |
| 146 | 33 | type Output = Self::WrappedOutput; |
| 34 | fn $fn(self, other: $type) -> Self::Output { | |
| 147 | 35 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 36 | } |
| 37 | } | |
| 38 | ||
| 147 | 39 | impl<'a, 'b, $($qual)*> $trait<&'b $type> for &'a $type { |
| 146 | 40 | type Output = Self::WrappedOutput; |
| 41 | fn $fn(self, other: $type) -> Self::Output { | |
| 147 | 42 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 43 | } |
| 44 | } | |
| 45 | ||
| 147 | 46 | impl<'b, $($qual)*> $trait<&'b $type> for $type { |
| 146 | 47 | type Output = Self::WrappedOutput; |
| 48 | fn $fn(self, other: $type) -> Self::Output { | |
| 147 | 49 | Self::wrap(self.get_view().$fn(other.get_view())) |
| 146 | 50 | } |
| 51 | } | |
| 52 | }; | |
| 147 | 53 | (impl_scalar<$($qual:tt)*> $type:ty, $trait:path, $fn:ident) => { |
| 54 | impl<$($qual)*, F: Num> $trait<F> for $type, | |
| 146 | 55 | where |
| 56 | $type::Unwrapped: $trait<F>, | |
| 57 | { | |
| 58 | type Output = Self::WrappedOutput; | |
| 59 | fn $fn(self, t: F) -> Self::Output { | |
| 147 | 60 | Self::wrap(self.get_view().$fn(t)) |
| 146 | 61 | } |
| 62 | } | |
| 63 | ||
| 147 | 64 | impl<'a, $($qual)*, F: Num> $trait<F> for &'a $type, |
| 146 | 65 | where |
| 66 | $type::Unwrapped: $trait<F>, | |
| 67 | { | |
| 68 | type Output = Self::WrappedOutput; | |
| 69 | fn $fn(self, t: F) -> Self::Output { | |
| 147 | 70 | Self::wrap(self.get_view().$fn(t)) |
| 146 | 71 | } |
| 72 | } | |
| 73 | ||
| 74 | }; | |
| 147 | 75 | (impl_scalar_lhs<$($qual:tt)*> $type:ty, $trait:path, $fn:ident, $F:ty) => { |
| 76 | impl<$($qual)*> $trait<$type> for $F | |
| 146 | 77 | where |
| 78 | $F: $type::Unwrapped, | |
| 79 | { | |
| 80 | type Output = Self::WrappedOutput; | |
| 81 | fn $fn(self, rhs: $type) -> Self::Output { | |
| 147 | 82 | Self::wrap(self.$fn(rhs.get_view())) |
| 146 | 83 | } |
| 84 | } | |
| 85 | }; | |
| 147 | 86 | (impl_binary_mut<$($qual:tt)*> $type:ty, $trait:path, $fn:ident) => { |
| 87 | impl<$($qual)*> $trait<$type> for $type { | |
| 146 | 88 | fn $fn(&mut self, rhs: $type) { |
| 89 | self.get_view_mut().$fn(rhs.get_view()) | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 147 | 93 | impl<'b, $($qual)*> $trait<&'b $type> for $type { |
| 146 | 94 | fn $fn(&mut self, rhs: $type) { |
| 95 | self.get_view_mut().$fn(rhs.get_view()) | |
| 96 | } | |
| 97 | } | |
| 98 | }; | |
| 147 | 99 | (impl_scalar_mut<$($qual:tt)*> $type:ty, $trait:path, $fn:ident) => { |
| 100 | impl<$($qual)*> $trait<F> for $type | |
| 146 | 101 | where |
| 102 | $type::UnwrappedMut: $trait<F>, | |
| 103 | { | |
| 104 | fn $fn(&mut self, t: F) { | |
| 105 | self.unwrap_mut().$fn(t) | |
| 106 | } | |
| 107 | } | |
| 108 | }; | |
| 109 | ($type:ty) => { | |
| 147 | 110 | $crate::wrap!(imp<> do $type); |
| 111 | }; | |
| 112 | (imp<$($qual:tt)*> $type:ty) => { | |
| 113 | $crate::wrap!(impl_unary<$($qual)*> $type, std::ops::Neg, neg); | |
| 114 | $crate::wrap!(impl_binary<$($qual)*> $type, std::ops::Add, add); | |
| 115 | $crate::wrap!(impl_binary<$($qual)*> $type, std::ops::Sub, sub); | |
| 116 | $crate::wrap!(impl_scalar<$($qual)*> $type, std::ops::Mul, mul); | |
| 117 | $crate::wrap!(impl_scalar<$($qual)*> $type, std::ops::Div, div); | |
| 118 | $crate::wrap!(impl_scalar_lhs<$($qual)*> $type, std::ops::Mul, mul, f32); | |
| 119 | $crate::wrap!(impl_scalar_lhs<$($qual)*> $type, std::ops::Mul, mul, f64); | |
| 120 | $crate::wrap!(impl_scalar_lhs<$($qual)*> $type, std::ops::Div, div, f32); | |
| 121 | $crate::wrap!(impl_scalar_lhs<$($qual)*> $type, std::ops::Div, div, f64); | |
| 122 | $crate::wrap!(impl_binary_mut<$($qual)*> $type, std::ops::AddAssign, add_assign); | |
| 123 | $crate::wrap!(impl_binary_mut<$($qual)*> $type, std::ops::SubAssign, sub_assign); | |
| 124 | $crate::wrap!(impl_scalar_mut<$($qual)*> $type, std::ops::MulAssign, mul_assign); | |
| 125 | $crate::wrap!(impl_scalar_mut<$($qual)*> $type, std::ops::DivAssign, div_assign); | |
| 146 | 126 | |
| 127 | /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause | |
| 128 | /// compiler overflows. | |
| 147 | 129 | impl<$($qual)* F: $crate::types::Float> $crate::euclidean::Euclidean<F> for $type |
| 146 | 130 | where |
| 131 | //Pair<A, B>: Euclidean<F>, | |
| 147 | 132 | Self: $crate::euclidean::wrap::Wrapped |
| 133 | + Sized | |
| 134 | + std::ops::Mul<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 135 | + std::ops::MulAssign<F> | |
| 136 | + std::ops::Div<F, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 137 | + std::ops::DivAssign<F> | |
| 138 | + std::ops::Add<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 139 | + std::ops::Sub<Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 140 | + for<'b> std::ops::Add<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 141 | + for<'b> std::ops::Sub<&'b Self, Output = <Self as $crate::linops::AXPY>::Owned> | |
| 142 | + std::ops::AddAssign<Self> | |
| 143 | + for<'b> std::ops::AddAssign<&'b Self> | |
| 144 | + std::ops::SubAssign<Self> | |
| 145 | + for<'b> std::ops::SubAssign<&'b Self> | |
| 146 | + std::ops::Neg<Output = <Self as $crate::linops::AXPY>::Owned>, | |
| 146 | 147 | { |
| 147 | 148 | fn dot<I: $crate::instance::Instance<Self>>(&self, other: I) -> F { |
| 146 | 149 | other.eval_decompose(|x| self.get_view().dot(x.get_view())) |
| 150 | } | |
| 151 | ||
| 152 | fn norm2_squared(&self) -> F { | |
| 153 | self.get_view().norm2_squared() | |
| 154 | } | |
| 155 | ||
| 147 | 156 | fn dist2_squared<I: $crate::instance::Instance<Self>>(&self, other: I) -> F { |
| 146 | 157 | other.eval_decompose(|x| self.get_view().dist2_squared(x.get_view())) |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 147 | 161 | impl<$($qual)* F : $crate::types::Float> $crate::linops::AXPY for $type |
| 146 | 162 | where |
| 147 | 163 | Self : $crate::euclidean::wrap::Wrapped, |
| 164 | Self::Unwrapped : $crate::linops::AXPY<Field = F>, | |
| 165 | Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 166 | Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>, | |
| 146 | 167 | { |
| 168 | type Field = F; | |
| 147 | 169 | type Owned = Self; |
| 146 | 170 | |
| 147 | 171 | fn axpy<I: $crate::instance::Instance<Self>>(&mut self, α: F, x: I, β: F) { |
| 146 | 172 | x.eval_decompose(|v| { |
| 173 | self.get_mut_view().axpy(α, v.get_view(), β) | |
| 174 | }) | |
| 175 | } | |
| 176 | ||
| 147 | 177 | fn copy_from<I: $crate::instance::Instance<Self>>(&mut self, x: I) { |
| 146 | 178 | x.eval_decompose(|Pair(u, v)| { |
| 179 | self.get_mut_view().copy_from(v.get_view()) | |
| 180 | }) | |
| 181 | } | |
| 182 | ||
| 147 | 183 | fn scale_from<I: $crate::instance::Instance<Self>>(&mut self, α: F, x: I) { |
| 146 | 184 | x.eval_decompose(|v| { |
| 185 | self.get_mut_view().scale_from(α, v.get_view()) | |
| 186 | }) | |
| 187 | } | |
| 188 | ||
| 189 | /// Return a similar zero as `self`. | |
| 190 | fn similar_origin(&self) -> Self::Owned { | |
| 147 | 191 | Self::wrap(self.get_view().similar_origin()) |
| 146 | 192 | } |
| 193 | ||
| 194 | /// Set self to zero. | |
| 195 | fn set_zero(&mut self) { | |
| 196 | self.get_mut_view().set_zero() | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 147 | 200 | impl<$($qual)*> $crate::instance::Space for $type |
| 201 | where Self : $crate::euclidean::wrap::Wrapped { | |
| 202 | type Decomp = Self::Unwrapped::Decomp; | |
| 146 | 203 | } |
| 204 | }; | |
| 205 | } |