2020-04-06 00:33:03 +08:00
|
|
|
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
|
2019-03-23 18:46:56 +08:00
|
|
|
|
use num::Zero;
|
2020-03-21 19:16:46 +08:00
|
|
|
|
use std::ops::Neg;
|
2018-12-09 18:21:24 +08:00
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::allocator::Allocator;
|
2021-04-11 17:00:38 +08:00
|
|
|
|
use crate::base::{DefaultAllocator, Dim, DimName, Matrix, Normed, OMatrix, OVector};
|
2020-03-18 00:58:36 +08:00
|
|
|
|
use crate::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::storage::{Storage, StorageMut};
|
2021-03-07 02:16:22 +08:00
|
|
|
|
use crate::{ComplexField, Scalar, SimdComplexField, Unit};
|
2020-03-21 19:16:46 +08:00
|
|
|
|
use simba::scalar::ClosedNeg;
|
2021-03-07 02:16:22 +08:00
|
|
|
|
use simba::simd::{SimdOption, SimdPartialOrd, SimdValue};
|
2018-12-09 18:21:24 +08:00
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: this should be be a trait on alga?
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// A trait for abstract matrix norms.
|
|
|
|
|
///
|
|
|
|
|
/// This may be moved to the alga crate in the future.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub trait Norm<T: SimdComplexField> {
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// Apply this norm to the given matrix.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm<R, C, S>(&self, m: &Matrix<T, R, C, S>) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R: Dim,
|
|
|
|
|
C: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S: Storage<T, R, C>;
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// Use the metric induced by this norm to compute the metric distance between the two given matrices.
|
2020-03-18 00:58:36 +08:00
|
|
|
|
fn metric_distance<R1, C1, S1, R2, C2, S2>(
|
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1: &Matrix<T, R1, C1, S1>,
|
|
|
|
|
m2: &Matrix<T, R2, C2, S2>,
|
|
|
|
|
) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R1: Dim,
|
|
|
|
|
C1: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S1: Storage<T, R1, C1>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>;
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Euclidean norm.
|
|
|
|
|
pub struct EuclideanNorm;
|
|
|
|
|
/// Lp norm.
|
|
|
|
|
pub struct LpNorm(pub i32);
|
|
|
|
|
/// L-infinite norm aka. Chebytchev norm aka. uniform norm aka. suppremum norm.
|
|
|
|
|
pub struct UniformNorm;
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdComplexField> Norm<T> for EuclideanNorm {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm<R, C, S>(&self, m: &Matrix<T, R, C, S>) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R: Dim,
|
|
|
|
|
C: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S: Storage<T, R, C>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
{
|
|
|
|
|
m.norm_squared().simd_sqrt()
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-18 00:58:36 +08:00
|
|
|
|
fn metric_distance<R1, C1, S1, R2, C2, S2>(
|
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1: &Matrix<T, R1, C1, S1>,
|
|
|
|
|
m2: &Matrix<T, R2, C2, S2>,
|
|
|
|
|
) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R1: Dim,
|
|
|
|
|
C1: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S1: Storage<T, R1, C1>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
let diff = a - b;
|
2020-03-18 00:58:36 +08:00
|
|
|
|
acc + diff.simd_modulus_squared()
|
|
|
|
|
})
|
|
|
|
|
.simd_sqrt()
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdComplexField> Norm<T> for LpNorm {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm<R, C, S>(&self, m: &Matrix<T, R, C, S>) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R: Dim,
|
|
|
|
|
C: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S: Storage<T, R, C>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m.fold(T::SimdRealField::zero(), |a, b| {
|
2020-03-18 00:58:36 +08:00
|
|
|
|
a + b.simd_modulus().simd_powi(self.0)
|
|
|
|
|
})
|
|
|
|
|
.simd_powf(crate::convert(1.0 / (self.0 as f64)))
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-18 00:58:36 +08:00
|
|
|
|
fn metric_distance<R1, C1, S1, R2, C2, S2>(
|
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1: &Matrix<T, R1, C1, S1>,
|
|
|
|
|
m2: &Matrix<T, R2, C2, S2>,
|
|
|
|
|
) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R1: Dim,
|
|
|
|
|
C1: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S1: Storage<T, R1, C1>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
let diff = a - b;
|
2020-03-18 00:58:36 +08:00
|
|
|
|
acc + diff.simd_modulus().simd_powi(self.0)
|
|
|
|
|
})
|
|
|
|
|
.simd_powf(crate::convert(1.0 / (self.0 as f64)))
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdComplexField> Norm<T> for UniformNorm {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm<R, C, S>(&self, m: &Matrix<T, R, C, S>) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R: Dim,
|
|
|
|
|
C: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S: Storage<T, R, C>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
{
|
2019-03-23 21:13:00 +08:00
|
|
|
|
// NOTE: we don't use `m.amax()` here because for the complex
|
|
|
|
|
// numbers this will return the max norm1 instead of the modulus.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m.fold(T::SimdRealField::zero(), |acc, a| {
|
2020-03-18 00:58:36 +08:00
|
|
|
|
acc.simd_max(a.simd_modulus())
|
|
|
|
|
})
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-18 00:58:36 +08:00
|
|
|
|
fn metric_distance<R1, C1, S1, R2, C2, S2>(
|
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1: &Matrix<T, R1, C1, S1>,
|
|
|
|
|
m2: &Matrix<T, R2, C2, S2>,
|
|
|
|
|
) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
|
|
|
|
R1: Dim,
|
|
|
|
|
C1: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S1: Storage<T, R1, C1>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| {
|
2020-03-18 00:58:36 +08:00
|
|
|
|
let val = (a - b).simd_modulus();
|
|
|
|
|
acc.simd_max(val)
|
2018-12-09 18:21:24 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
/// # Magnitude and norms
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
/// The squared L2 norm of this vector.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn norm_squared(&self) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
let mut res = T::SimdRealField::zero();
|
2018-12-09 18:21:24 +08:00
|
|
|
|
|
|
|
|
|
for i in 0..self.ncols() {
|
|
|
|
|
let col = self.column(i);
|
2020-03-18 00:58:36 +08:00
|
|
|
|
res += col.dotc(&col).simd_real()
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The L2 norm of this matrix.
|
2019-02-03 15:33:07 +08:00
|
|
|
|
///
|
|
|
|
|
/// Use `.apply_norm` to apply a custom norm.
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn norm(&self) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2020-03-18 00:58:36 +08:00
|
|
|
|
self.norm_squared().simd_sqrt()
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// Compute the distance between `self` and `rhs` using the metric induced by the euclidean norm.
|
|
|
|
|
///
|
|
|
|
|
/// Use `.apply_metric_distance` to apply a custom norm.
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn metric_distance<R2, C2, S2>(&self, rhs: &Matrix<T, R2, C2, S2>) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R, R2> + SameNumberOfColumns<C, C2>,
|
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
self.apply_metric_distance(rhs, &EuclideanNorm)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// Uses the given `norm` to compute the norm of `self`.
|
2019-02-03 18:29:10 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Vector3, UniformNorm, LpNorm, EuclideanNorm};
|
|
|
|
|
///
|
|
|
|
|
/// let v = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// assert_eq!(v.apply_norm(&UniformNorm), 3.0);
|
|
|
|
|
/// assert_eq!(v.apply_norm(&LpNorm(1)), 6.0);
|
|
|
|
|
/// assert_eq!(v.apply_norm(&EuclideanNorm), v.norm());
|
|
|
|
|
/// ```
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn apply_norm(&self, norm: &impl Norm<T>) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
norm.norm(self)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 15:33:07 +08:00
|
|
|
|
/// Uses the metric induced by the given `norm` to compute the metric distance between `self` and `rhs`.
|
2019-02-03 18:29:10 +08:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::{Vector3, UniformNorm, LpNorm, EuclideanNorm};
|
|
|
|
|
///
|
|
|
|
|
/// let v1 = Vector3::new(1.0, 2.0, 3.0);
|
|
|
|
|
/// let v2 = Vector3::new(10.0, 20.0, 30.0);
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(v1.apply_metric_distance(&v2, &UniformNorm), 27.0);
|
|
|
|
|
/// assert_eq!(v1.apply_metric_distance(&v2, &LpNorm(1)), 27.0 + 18.0 + 9.0);
|
|
|
|
|
/// assert_eq!(v1.apply_metric_distance(&v2, &EuclideanNorm), (v1 - v2).norm());
|
|
|
|
|
/// ```
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2020-03-18 00:58:36 +08:00
|
|
|
|
pub fn apply_metric_distance<R2, C2, S2>(
|
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
rhs: &Matrix<T, R2, C2, S2>,
|
|
|
|
|
norm: &impl Norm<T>,
|
|
|
|
|
) -> T::SimdRealField
|
2020-03-18 00:58:36 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
R2: Dim,
|
|
|
|
|
C2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
S2: Storage<T, R2, C2>,
|
2020-03-18 00:58:36 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<R, R2> + SameNumberOfColumns<C, C2>,
|
|
|
|
|
{
|
2019-02-23 18:24:07 +08:00
|
|
|
|
norm.metric_distance(self, rhs)
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A synonym for the norm of this matrix.
|
|
|
|
|
///
|
|
|
|
|
/// Aka the length.
|
|
|
|
|
///
|
|
|
|
|
/// This function is simply implemented as a call to `norm()`
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn magnitude(&self) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
self.norm()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A synonym for the squared norm of this matrix.
|
|
|
|
|
///
|
|
|
|
|
/// Aka the squared length.
|
|
|
|
|
///
|
|
|
|
|
/// This function is simply implemented as a call to `norm_squared()`
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn magnitude_squared(&self) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
self.norm_squared()
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 00:58:36 +08:00
|
|
|
|
/// Sets the magnitude of this vector.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn set_magnitude(&mut self, magnitude: T::SimdRealField)
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
|
|
|
|
S: StorageMut<T, R, C>,
|
2020-04-06 00:02:03 +08:00
|
|
|
|
{
|
2020-03-18 00:58:36 +08:00
|
|
|
|
let n = self.norm();
|
|
|
|
|
self.scale_mut(magnitude / n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a normalized version of this matrix.
|
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use = "Did you mean to use normalize_mut()?"]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn normalize(&self) -> OMatrix<T, R, C>
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C>,
|
2020-04-06 00:02:03 +08:00
|
|
|
|
{
|
2020-03-18 00:58:36 +08:00
|
|
|
|
self.unscale(self.norm())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The Lp norm of this matrix.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn lp_norm(&self, p: i32) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2020-03-18 00:58:36 +08:00
|
|
|
|
self.apply_norm(&LpNorm(p))
|
|
|
|
|
}
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
2020-04-06 00:02:03 +08:00
|
|
|
|
/// Attempts to normalize `self`.
|
|
|
|
|
///
|
|
|
|
|
/// The components of this matrix can be SIMD types.
|
2020-03-21 19:16:46 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use = "Did you mean to use simd_try_normalize_mut()?"]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn simd_try_normalize(&self, min_norm: T::SimdRealField) -> SimdOption<OMatrix<T, R, C>>
|
2020-03-21 19:16:46 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
|
|
|
|
T::Element: Scalar,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
|
{
|
|
|
|
|
let n = self.norm();
|
|
|
|
|
let le = n.simd_le(min_norm);
|
|
|
|
|
let val = self.unscale(n);
|
|
|
|
|
SimdOption::new(val, le)
|
|
|
|
|
}
|
2020-01-01 22:59:46 +08:00
|
|
|
|
|
|
|
|
|
/// Sets the magnitude of this vector unless it is smaller than `min_magnitude`.
|
|
|
|
|
///
|
|
|
|
|
/// If `self.magnitude()` is smaller than `min_magnitude`, it will be left unchanged.
|
|
|
|
|
/// Otherwise this is equivalent to: `*self = self.normalize() * magnitude.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn try_set_magnitude(&mut self, magnitude: T::RealField, min_magnitude: T::RealField)
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: ComplexField,
|
|
|
|
|
S: StorageMut<T, R, C>,
|
2020-04-06 00:02:03 +08:00
|
|
|
|
{
|
2020-01-01 22:59:46 +08:00
|
|
|
|
let n = self.norm();
|
|
|
|
|
|
2021-02-22 21:26:40 +08:00
|
|
|
|
if n > min_magnitude {
|
2020-01-01 22:59:46 +08:00
|
|
|
|
self.scale_mut(magnitude / n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 21:26:40 +08:00
|
|
|
|
/// Returns a new vector with the same magnitude as `self` clamped between `0.0` and `max`.
|
2021-03-24 23:28:14 +08:00
|
|
|
|
#[must_use = "This function does not mutate self but returns a new clamped version."]
|
2021-02-22 21:26:40 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn cap_magnitude(&self, max: T::RealField) -> OMatrix<T, R, C>
|
2021-02-22 21:26:40 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: ComplexField,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C>,
|
2021-02-22 21:26:40 +08:00
|
|
|
|
{
|
|
|
|
|
let n = self.norm();
|
|
|
|
|
|
|
|
|
|
if n > max {
|
|
|
|
|
self.scale(max / n)
|
|
|
|
|
} else {
|
|
|
|
|
self.clone_owned()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 02:16:22 +08:00
|
|
|
|
/// Returns a new vector with the same magnitude as `self` clamped between `0.0` and `max`.
|
2021-03-24 23:28:14 +08:00
|
|
|
|
#[must_use = "This function does not mutate self but returns a new clamped version."]
|
2021-03-07 02:16:22 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn simd_cap_magnitude(&self, max: T::SimdRealField) -> OMatrix<T, R, C>
|
2021-03-07 02:16:22 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
|
|
|
|
T::Element: Scalar,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2021-03-07 02:16:22 +08:00
|
|
|
|
{
|
|
|
|
|
let n = self.norm();
|
|
|
|
|
let scaled = self.scale(max / n);
|
|
|
|
|
let use_scaled = n.simd_gt(max);
|
|
|
|
|
scaled.select(use_scaled, self.clone_owned())
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 18:21:24 +08:00
|
|
|
|
/// Returns a normalized version of this matrix unless its norm as smaller or equal to `eps`.
|
2020-04-06 00:02:03 +08:00
|
|
|
|
///
|
|
|
|
|
/// The components of this matrix cannot be SIMD types (see `simd_try_normalize`) instead.
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2019-06-06 05:04:04 +08:00
|
|
|
|
#[must_use = "Did you mean to use try_normalize_mut()?"]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn try_normalize(&self, min_norm: T::RealField) -> Option<OMatrix<T, R, C>>
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: ComplexField,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C>,
|
2020-04-06 00:02:03 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
let n = self.norm();
|
|
|
|
|
|
|
|
|
|
if n <= min_norm {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2019-03-23 21:13:00 +08:00
|
|
|
|
Some(self.unscale(n))
|
2018-12-09 18:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
/// # In-place normalization
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: Scalar, R: Dim, C: Dim, S: StorageMut<T, R, C>> Matrix<T, R, C, S> {
|
2018-12-09 18:21:24 +08:00
|
|
|
|
/// Normalizes this matrix in-place and returns its norm.
|
2020-04-06 00:02:03 +08:00
|
|
|
|
///
|
|
|
|
|
/// The components of the matrix cannot be SIMD types (see `simd_try_normalize_mut` instead).
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn normalize_mut(&mut self) -> T::SimdRealField
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
let n = self.norm();
|
2019-03-23 21:13:00 +08:00
|
|
|
|
self.unscale_mut(n);
|
2018-12-09 18:21:24 +08:00
|
|
|
|
|
|
|
|
|
n
|
|
|
|
|
}
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
2020-04-06 00:02:03 +08:00
|
|
|
|
/// Normalizes this matrix in-place and return its norm.
|
|
|
|
|
///
|
|
|
|
|
/// The components of the matrix can be SIMD types.
|
2020-03-21 19:16:46 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use = "Did you mean to use simd_try_normalize_mut()?"]
|
|
|
|
|
pub fn simd_try_normalize_mut(
|
|
|
|
|
&mut self,
|
2021-04-11 17:00:38 +08:00
|
|
|
|
min_norm: T::SimdRealField,
|
|
|
|
|
) -> SimdOption<T::SimdRealField>
|
2020-03-21 19:16:46 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: SimdComplexField,
|
|
|
|
|
T::Element: Scalar,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
|
{
|
|
|
|
|
let n = self.norm();
|
|
|
|
|
let le = n.simd_le(min_norm);
|
|
|
|
|
self.apply(|e| e.simd_unscale(n).select(le, e));
|
|
|
|
|
SimdOption::new(n, le)
|
|
|
|
|
}
|
2018-12-09 18:21:24 +08:00
|
|
|
|
|
|
|
|
|
/// Normalizes this matrix in-place or does nothing if its norm is smaller or equal to `eps`.
|
|
|
|
|
///
|
2020-01-01 22:59:46 +08:00
|
|
|
|
/// If the normalization succeeded, returns the old norm of this matrix.
|
2018-12-09 18:21:24 +08:00
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn try_normalize_mut(&mut self, min_norm: T::RealField) -> Option<T::RealField>
|
2020-11-15 23:57:49 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: ComplexField,
|
2020-11-15 23:57:49 +08:00
|
|
|
|
{
|
2018-12-09 18:21:24 +08:00
|
|
|
|
let n = self.norm();
|
|
|
|
|
|
|
|
|
|
if n <= min_norm {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2019-03-23 21:13:00 +08:00
|
|
|
|
self.unscale_mut(n);
|
2018-12-09 18:21:24 +08:00
|
|
|
|
Some(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-03 17:56:30 +08:00
|
|
|
|
}
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: SimdComplexField, R: Dim, C: Dim> Normed for OMatrix<T, R, C>
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
DefaultAllocator: Allocator<T, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
type Norm = T::SimdRealField;
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm(&self) -> T::SimdRealField {
|
2020-03-21 19:16:46 +08:00
|
|
|
|
self.norm()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
fn norm_squared(&self) -> T::SimdRealField {
|
2020-03-21 19:16:46 +08:00
|
|
|
|
self.norm_squared()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn scale_mut(&mut self, n: Self::Norm) {
|
|
|
|
|
self.scale_mut(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn unscale_mut(&mut self, n: Self::Norm) {
|
|
|
|
|
self.unscale_mut(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: Scalar + ClosedNeg, R: Dim, C: Dim> Neg for Unit<OMatrix<T, R, C>>
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
DefaultAllocator: Allocator<T, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
type Output = Unit<OMatrix<T, R, C>>;
|
2020-03-21 19:16:46 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
|
Unit::new_unchecked(-self.value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-25 02:06:05 +08:00
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: specialization will greatly simplify this implementation in the future.
|
2020-03-25 02:06:05 +08:00
|
|
|
|
// In particular:
|
|
|
|
|
// − use `x()` instead of `::canonical_basis_element`
|
|
|
|
|
// − use `::new(x, y, z)` instead of `::from_slice`
|
2020-11-15 23:57:49 +08:00
|
|
|
|
/// # Basis and orthogonalization
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: ComplexField, D: DimName> OVector<T, D>
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
DefaultAllocator: Allocator<T, D>,
|
2020-03-25 02:06:05 +08:00
|
|
|
|
{
|
|
|
|
|
/// The i-the canonical basis element.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn canonical_basis_element(i: usize) -> Self {
|
|
|
|
|
assert!(i < D::dim(), "Index out of bound.");
|
|
|
|
|
|
|
|
|
|
let mut res = Self::zero();
|
|
|
|
|
unsafe {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
*res.data.get_unchecked_linear_mut(i) = T::one();
|
2020-03-25 02:06:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Orthonormalizes the given family of vectors. The largest free family of vectors is moved at
|
|
|
|
|
/// the beginning of the array and its size is returned. Vectors at an indices larger or equal to
|
|
|
|
|
/// this length can be modified to an arbitrary value.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn orthonormalize(vs: &mut [Self]) -> usize {
|
|
|
|
|
let mut nbasis_elements = 0;
|
|
|
|
|
|
|
|
|
|
for i in 0..vs.len() {
|
|
|
|
|
{
|
|
|
|
|
let (elt, basis) = vs[..i + 1].split_last_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
for basis_element in &basis[..nbasis_elements] {
|
|
|
|
|
*elt -= &*basis_element * elt.dot(basis_element)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if vs[i].try_normalize_mut(T::RealField::zero()).is_some() {
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: this will be efficient on dynamically-allocated vectors but for
|
2020-03-25 02:06:05 +08:00
|
|
|
|
// statically-allocated ones, `.clone_from` would be better.
|
|
|
|
|
vs.swap(nbasis_elements, i);
|
|
|
|
|
nbasis_elements += 1;
|
|
|
|
|
|
|
|
|
|
// All the other vectors will be dependent.
|
|
|
|
|
if nbasis_elements == D::dim() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nbasis_elements
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Applies the given closure to each element of the orthonormal basis of the subspace
|
|
|
|
|
/// orthogonal to free family of vectors `vs`. If `vs` is not a free family, the result is
|
|
|
|
|
/// unspecified.
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: return an iterator instead when `-> impl Iterator` will be supported by Rust.
|
2020-03-25 02:06:05 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn orthonormal_subspace_basis<F>(vs: &[Self], mut f: F)
|
2020-04-06 00:02:03 +08:00
|
|
|
|
where
|
|
|
|
|
F: FnMut(&Self) -> bool,
|
|
|
|
|
{
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: is this necessary?
|
2020-03-25 02:06:05 +08:00
|
|
|
|
assert!(
|
|
|
|
|
vs.len() <= D::dim(),
|
|
|
|
|
"The given set of vectors has no chance of being a free family."
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match D::dim() {
|
|
|
|
|
1 => {
|
2020-11-16 19:11:24 +08:00
|
|
|
|
if vs.is_empty() {
|
2020-03-25 02:06:05 +08:00
|
|
|
|
let _ = f(&Self::canonical_basis_element(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
2 => {
|
2020-11-16 19:11:24 +08:00
|
|
|
|
if vs.is_empty() {
|
2020-03-25 02:06:05 +08:00
|
|
|
|
let _ = f(&Self::canonical_basis_element(0))
|
|
|
|
|
&& f(&Self::canonical_basis_element(1));
|
|
|
|
|
} else if vs.len() == 1 {
|
|
|
|
|
let v = &vs[0];
|
|
|
|
|
let res = Self::from_column_slice(&[-v[1], v[0]]);
|
|
|
|
|
|
|
|
|
|
let _ = f(&res.normalize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, nothing.
|
|
|
|
|
}
|
|
|
|
|
3 => {
|
2020-11-16 19:11:24 +08:00
|
|
|
|
if vs.is_empty() {
|
2020-03-25 02:06:05 +08:00
|
|
|
|
let _ = f(&Self::canonical_basis_element(0))
|
|
|
|
|
&& f(&Self::canonical_basis_element(1))
|
|
|
|
|
&& f(&Self::canonical_basis_element(2));
|
|
|
|
|
} else if vs.len() == 1 {
|
|
|
|
|
let v = &vs[0];
|
|
|
|
|
let mut a;
|
|
|
|
|
|
|
|
|
|
if v[0].norm1() > v[1].norm1() {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
a = Self::from_column_slice(&[v[2], T::zero(), -v[0]]);
|
2020-03-25 02:06:05 +08:00
|
|
|
|
} else {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
a = Self::from_column_slice(&[T::zero(), -v[2], v[1]]);
|
2020-03-25 02:06:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let _ = a.normalize_mut();
|
|
|
|
|
|
|
|
|
|
if f(&a.cross(v)) {
|
|
|
|
|
let _ = f(&a);
|
|
|
|
|
}
|
|
|
|
|
} else if vs.len() == 2 {
|
|
|
|
|
let _ = f(&vs[0].cross(&vs[1]).normalize());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
|
{
|
|
|
|
|
// XXX: use a GenericArray instead.
|
|
|
|
|
let mut known_basis = Vec::new();
|
|
|
|
|
|
|
|
|
|
for v in vs.iter() {
|
|
|
|
|
known_basis.push(v.normalize())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i in 0..D::dim() - vs.len() {
|
|
|
|
|
let mut elt = Self::canonical_basis_element(i);
|
|
|
|
|
|
|
|
|
|
for v in &known_basis {
|
|
|
|
|
elt -= v * elt.dot(v)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if let Some(subsp_elt) = elt.try_normalize(T::RealField::zero()) {
|
2020-03-25 02:06:05 +08:00
|
|
|
|
if !f(&subsp_elt) {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
known_basis.push(subsp_elt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
|
|
|
|
|
{
|
|
|
|
|
panic!("Cannot compute the orthogonal subspace basis of a vector with a dimension greater than 3 \
|
|
|
|
|
if #![no_std] is enabled and the 'alloc' feature is not enabled.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|