# HG changeset patch # User Tuomo Valkonen # Date 1747087839 18000 # Node ID 8264d72aa34796e42e69ccfb625808e7a74d2655 # Parent 0a689881b0f1d353078fbcc04cd170e9b644c8b6 euclidean simplify fail diff -r 0a689881b0f1 -r 8264d72aa347 src/convex.rs --- a/src/convex.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/convex.rs Mon May 12 17:10:39 2025 -0500 @@ -359,7 +359,7 @@ } } -impl, F: Float> Mapping for Norm222 { +impl, F: Float> Mapping for Norm222 { type Codomain = F; /// Compute the value of `self` at `x`. @@ -368,13 +368,13 @@ } } -impl, F: Float> ConvexMapping for Norm222 { +impl, F: Float> ConvexMapping for Norm222 { fn factor_of_strong_convexity(&self) -> F { F::ONE } } -impl, F: Float> Conjugable for Norm222 { +impl, F: Float> Conjugable for Norm222 { type Conjugate<'a> = Self where @@ -386,7 +386,7 @@ } } -impl, F: Float> Preconjugable for Norm222 { +impl, F: Float> Preconjugable for Norm222 { type Preconjugate<'a> = Self where @@ -401,7 +401,7 @@ impl Prox for Norm222 where F: Float, - X: Euclidean, + X: Euclidean, { type Prox<'a> = Scaled @@ -416,7 +416,7 @@ impl DifferentiableImpl for Norm222 where F: Float, - X: Euclidean, + X: Euclidean, { type Derivative = X; @@ -428,7 +428,7 @@ impl LipschitzDifferentiableImpl for Norm222 where F: Float, - X: Euclidean, + X: Euclidean, { type FloatType = F; diff -r 0a689881b0f1 -r 8264d72aa347 src/direct_product.rs --- a/src/direct_product.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/direct_product.rs Mon May 12 17:10:39 2025 -0500 @@ -264,10 +264,10 @@ /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause /// compiler overflows. -impl Euclidean for Pair +impl Euclidean for Pair where - A: Euclidean, - B: Euclidean, + A: Euclidean, + B: Euclidean, //Pair: Euclidean, Self: Sized + Mul> @@ -284,8 +284,6 @@ + for<'b> SubAssign<&'b Self> + Neg>, { - type Output = Pair; - fn dot>(&self, other: I) -> F { let Pair(u, v) = other.decompose(); self.0.dot(u) + self.1.dot(v) diff -r 0a689881b0f1 -r 8264d72aa347 src/euclidean.rs --- a/src/euclidean.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/euclidean.rs Mon May 12 17:10:39 2025 -0500 @@ -3,7 +3,8 @@ */ use crate::instance::Instance; -use crate::norms::{HasDual, Reflexive}; +use crate::linops::AXPY; +use crate::norms::{HasDual, NormExponent, Normed, Reflexive, L2}; use crate::types::*; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; @@ -12,66 +13,68 @@ /// The type should implement vector space operations (addition, subtraction, scalar /// multiplication and scalar division) along with their assignment versions, as well /// as an inner product. -pub trait Euclidean: - HasDual - + Reflexive - + Mul>::Output> - + MulAssign - + Div>::Output> - + DivAssign - + Add>::Output> - + Sub>::Output> - + for<'b> Add<&'b Self, Output = >::Output> - + for<'b> Sub<&'b Self, Output = >::Output> +pub trait Euclidean: + AXPY + + HasDual + //+ Normed + + Reflexive + + Mul::Owned> + + MulAssign + + Div::Owned> + + DivAssign + + Add::Owned> + + Sub::Owned> + + for<'b> Add<&'b Self, Output = ::Owned> + + for<'b> Sub<&'b Self, Output = ::Owned> + AddAssign + for<'b> AddAssign<&'b Self> + SubAssign + for<'b> SubAssign<&'b Self> - + Neg>::Output> + + Neg::Owned> { - type Output: Euclidean; + type Owned_: Euclidean; // Inner product - fn dot>(&self, other: I) -> F; + fn dot>(&self, other: I) -> Self::Field; /// Calculate the square of the 2-norm, $\frac{1}{2}\\|x\\|_2^2$, where `self` is $x$. /// /// This is not automatically implemented to avoid imposing /// `for <'a> &'a Self : Instance` trait bound bloat. - fn norm2_squared(&self) -> F; + fn norm2_squared(&self) -> Self::Field; /// Calculate the square of the 2-norm divided by 2, $\frac{1}{2}\\|x\\|_2^2$, /// where `self` is $x$. #[inline] - fn norm2_squared_div2(&self) -> F { - self.norm2_squared() / F::TWO + fn norm2_squared_div2(&self) -> Self::Field { + self.norm2_squared() / Self::Field::TWO } /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$. #[inline] - fn norm2(&self) -> F { + fn norm2(&self) -> Self::Field { self.norm2_squared().sqrt() } /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$. - fn dist2_squared>(&self, y: I) -> F; + fn dist2_squared>(&self, y: I) -> Self::Field; /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$. #[inline] - fn dist2>(&self, y: I) -> F { + fn dist2>(&self, y: I) -> Self::Field { self.dist2_squared(y).sqrt() } /// Projection to the 2-ball. #[inline] - fn proj_ball2(mut self, ρ: F) -> Self { + fn proj_ball2(mut self, ρ: Self::Field) -> Self { self.proj_ball2_mut(ρ); self } /// In-place projection to the 2-ball. #[inline] - fn proj_ball2_mut(&mut self, ρ: F) { + fn proj_ball2_mut(&mut self, ρ: Self::Field) { let r = self.norm2(); if r > ρ { *self *= ρ / r @@ -80,7 +83,7 @@ } /// Trait for [`Euclidean`] spaces with dimensions known at compile time. -pub trait StaticEuclidean: Euclidean { +pub trait StaticEuclidean: Euclidean { /// Returns the origin - fn origin() -> >::Output; + fn origin() -> ::Owned; } diff -r 0a689881b0f1 -r 8264d72aa347 src/linops.rs --- a/src/linops.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/linops.rs Mon May 12 17:10:39 2025 -0500 @@ -29,12 +29,12 @@ #[replace_float_literals(Self::Field::cast_from(literal))] pub trait AXPY: Space - + MulAssign - + DivAssign - + AddAssign - + AddAssign - + SubAssign - + SubAssign + //+ MulAssign + //+ DivAssign + //+ AddAssign + //+ AddAssign + //+ SubAssign + //+ SubAssign + Mul + Div + Add @@ -45,7 +45,7 @@ where X: Space, { - type Field: Num; + type Field: Float; type Owned: AXPY; // type OriginGen<'a>: OriginGenerator // where diff -r 0a689881b0f1 -r 8264d72aa347 src/loc.rs --- a/src/loc.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/loc.rs Mon May 12 17:10:39 2025 -0500 @@ -432,9 +432,7 @@ domination!(Linfinity, L2); domination!(L2, L1); -impl Euclidean for Loc { - type Output = Self; - +impl Euclidean for Loc { /// This implementation is not stabilised as it's meant to be used for very small vectors. /// Use [`nalgebra`] for larger vectors. #[inline] @@ -565,7 +563,7 @@ } } -impl StaticEuclidean for Loc { +impl StaticEuclidean for Loc { #[inline] fn origin() -> Self { Self::ORIGIN diff -r 0a689881b0f1 -r 8264d72aa347 src/nalgebra_support.rs --- a/src/nalgebra_support.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/nalgebra_support.rs Mon May 12 17:10:39 2025 -0500 @@ -215,15 +215,13 @@ // TODO: should allow different input storages in `Euclidean`. -impl Euclidean for Vector +impl Euclidean for Vector where M: Dim, S: StorageMut + Clone, E: Float + Scalar + Zero + One + RealField, DefaultAllocator: Allocator, { - type Output = OVector; - #[inline] fn dot>(&self, other: I) -> E { Vector::::dot(self, other.ref_instance()) @@ -240,7 +238,7 @@ } } -impl StaticEuclidean for Vector +impl StaticEuclidean for Vector where M: DimName, S: StorageMut + Clone, diff -r 0a689881b0f1 -r 8264d72aa347 src/norms.rs --- a/src/norms.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/norms.rs Mon May 12 17:10:39 2025 -0500 @@ -146,7 +146,7 @@ fn dist(&self, other : &Self, _p : L2) -> F { self.dist2(other) } }*/ -impl + Norm> Projection for E { +impl + Norm> Projection for E { #[inline] fn proj_ball(self, ρ: F, _p: L2) -> Self { self.proj_ball2(ρ) @@ -176,13 +176,13 @@ } } -impl + Normed> Norm, F> for E { +impl + Normed> Norm, F> for E { fn norm(&self, huber: HuberL1) -> F { huber.apply(self.norm2_squared()) } } -impl + Normed> Dist> for E { +impl + Normed> Dist> for E { fn dist>(&self, other: I, huber: HuberL1) -> F { huber.apply(self.dist2_squared(other)) } diff -r 0a689881b0f1 -r 8264d72aa347 src/sets.rs --- a/src/sets.rs Mon May 12 16:28:50 2025 -0500 +++ b/src/sets.rs Mon May 12 17:10:39 2025 -0500 @@ -72,22 +72,20 @@ /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal /// vector and $t$ the offset. #[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] -pub struct Halfspace +pub struct Halfspace where - A: Euclidean, - F: Float, + A: Euclidean, { pub orthogonal: A, - pub offset: F, + pub offset: A::Field, } -impl Halfspace +impl Halfspace where - A: Euclidean, - F: Float, + A: Euclidean, { #[inline] - pub fn new(orthogonal: A, offset: F) -> Self { + pub fn new(orthogonal: A, offset: A::Field) -> Self { Halfspace { orthogonal: orthogonal, offset: offset, @@ -96,29 +94,26 @@ } /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. -pub trait SpannedHalfspace -where - F: Float, -{ +pub trait SpannedHalfspace { /// Type of the orthogonal vector describing the halfspace. - type A: Euclidean; + type A: Euclidean; /// Returns the halfspace spanned by this set. - fn spanned_halfspace(&self) -> Halfspace; + fn spanned_halfspace(&self) -> Halfspace; } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace for [Loc<1, F>; 2] { +impl SpannedHalfspace for [Loc<1, F>; 2] { type A = Loc<1, F>; - fn spanned_halfspace(&self) -> Halfspace { + fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (self[0], self[1]); Halfspace::new(x1 - x0, x0[0]) } } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace for [Loc<2, F>; 2] { +impl SpannedHalfspace for [Loc<2, F>; 2] { type A = Loc<2, F>; - fn spanned_halfspace(&self) -> Halfspace { + fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (&self[0], &self[1]); let d = x1 - x0; let orthog = loc![d[1], -d[0]]; // We don't normalise for efficiency @@ -127,10 +122,9 @@ } } -impl Set for Halfspace +impl Set for Halfspace where - A: Euclidean, - F: Float, + A: Euclidean, { #[inline] fn contains>(&self, item: I) -> bool { @@ -139,17 +133,10 @@ } /// Polygons defined by `N` `Halfspace`s. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct NPolygon(pub [Halfspace; N]) -where - A: Euclidean, - F: Float; +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct NPolygon(pub [Halfspace; N]); -impl Set for NPolygon -where - A: Euclidean, - F: Float, -{ +impl Set for NPolygon { fn contains>(&self, item: I) -> bool { let r = item.ref_instance(); self.0.iter().all(|halfspace| halfspace.contains(r))