src/euclidean.rs

Mon, 01 Sep 2025 13:51:03 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 01 Sep 2025 13:51:03 -0500
branch
dev
changeset 150
c4e394a9c84c
parent 146
3f9a03f95457
child 151
402d717bb5c0
permissions
-rw-r--r--

fubar

/*!
Euclidean spaces.
*/

use crate::instance::Instance;
use crate::linops::{VectorSpace, AXPY};
use crate::norms::{HasDual, Reflexive};
use crate::types::*;
use std::ops::{Add, AddAssign, Sub, SubAssign};

pub mod wrap;

// TODO: Euclidean & EuclideanMut
//
/// Space (type) with Euclidean and vector space structure
///
/// 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.
// TODO: remove F parameter, use AXPY::Field
pub trait Euclidean<F: Float = f64>:
    HasDual<F, DualSpace = Self>
    + VectorSpace<Field = F>
    + Reflexive<F>
    // TODO: move the following to AXPY
    + for<'b> Add<&'b Self, Output = <Self as VectorSpace>::Owned>
    + for<'b> Sub<&'b Self, Output = <Self as VectorSpace>::Owned>
    + for<'b> AddAssign<&'b Self>
    + for<'b> SubAssign<&'b Self>
{
    // Inner product
    fn dot<I: Instance<Self>>(&self, other: I) -> F;

    /// 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<Self>` trait bound bloat.
    fn norm2_squared(&self) -> F;

    /// 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
    }

    /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$.
    #[inline]
    fn norm2(&self) -> F {
        self.norm2_squared().sqrt()
    }

    /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$.
    fn dist2_squared<I: Instance<Self>>(&self, y: I) -> F;

    /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$.
    #[inline]
    fn dist2<I: Instance<Self>>(&self, y: I) -> F {
        self.dist2_squared(y).sqrt()
    }

    /// Projection to the 2-ball.
    #[inline]
    fn proj_ball2(self, ρ: F) -> Self::Owned {
        let r = self.norm2();
        if r > ρ {
            self * (ρ / r)
        } else {
            self.into_owned()
        }
    }
}

// TODO: remove F parameter, use AXPY::Field
pub trait EuclideanMut<F: Float = f64>:
    Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self>
{
    /// In-place projection to the 2-ball.
    #[inline]
    fn proj_ball2_mut(&mut self, ρ: F) {
        let r = self.norm2();
        if r > ρ {
            *self *= ρ / r
        }
    }
}

impl<X, F: Float> EuclideanMut<F> for X where
    X: Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self>
{
}

/// Trait for [`Euclidean`] spaces with dimensions known at compile time.
pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> {
    /// Returns the origin
    fn origin() -> <Self as VectorSpace>::Owned;
}

mercurial