      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240

    use num::Zero;

    use crate::allocator::Allocator;
    use crate::{RealField, ComplexField};
    use crate::storage::{Storage, StorageMut};
    use crate::base::{DefaultAllocator, Matrix, Dim, MatrixMN};
    use crate::constraint::{SameNumberOfRows, SameNumberOfColumns, ShapeConstraint};

    // FIXME: this should be be a trait on alga?
    /// A trait for abstract matrix norms.
    ///
    /// This may be moved to the alga crate in the future.
    pub trait Norm<N: ComplexField> {
        /// Apply this norm to the given matrix.
        fn norm<R, C, S>(&self, m: &Matrix<N, R, C, S>) -> N::RealField
            where R: Dim, C: Dim, S: Storage<N, R, C>;
        /// Use the metric induced by this norm to compute the metric distance between the two given matrices.
        fn metric_distance<R1, C1, S1, R2, C2, S2>(&self, m1: &Matrix<N, R1, C1, S1>, m2: &Matrix<N, R2, C2, S2>) -> N::RealField
            where R1: Dim, C1: Dim, S1: Storage<N, R1, C1>,
                  R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>;
    }

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

    impl<N: ComplexField> Norm<N> for EuclideanNorm {
        #[inline]
        fn norm<R, C, S>(&self, m: &Matrix<N, R, C, S>) -> N::RealField
            where R: Dim, C: Dim, S: Storage<N, R, C> {
            m.norm_squared().sqrt()
        }

        #[inline]
        fn metric_distance<R1, C1, S1, R2, C2, S2>(&self, m1: &Matrix<N, R1, C1, S1>, m2: &Matrix<N, R2, C2, S2>) -> N::RealField
            where R1: Dim, C1: Dim, S1: Storage<N, R1, C1>,
                  R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {
            m1.zip_fold(m2, N::RealField::zero(), |acc, a, b| {
                let diff = a - b;
                acc + diff.modulus_squared()
            }).sqrt()
        }
    }

    impl<N: ComplexField> Norm<N> for LpNorm {
        #[inline]
        fn norm<R, C, S>(&self, m: &Matrix<N, R, C, S>) -> N::RealField
            where R: Dim, C: Dim, S: Storage<N, R, C> {
            m.fold(N::RealField::zero(), |a, b| {
                a + b.modulus().powi(self.0)
            }).powf(crate::convert(1.0 / (self.0 as f64)))
        }

        #[inline]
        fn metric_distance<R1, C1, S1, R2, C2, S2>(&self, m1: &Matrix<N, R1, C1, S1>, m2: &Matrix<N, R2, C2, S2>) -> N::RealField
            where R1: Dim, C1: Dim, S1: Storage<N, R1, C1>,
                  R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {
            m1.zip_fold(m2, N::RealField::zero(), |acc, a, b| {
                let diff = a - b;
                acc + diff.modulus().powi(self.0)
            }).powf(crate::convert(1.0 / (self.0 as f64)))
        }
    }

    impl<N: ComplexField> Norm<N> for UniformNorm {
        #[inline]
        fn norm<R, C, S>(&self, m: &Matrix<N, R, C, S>) -> N::RealField
            where R: Dim, C: Dim, S: Storage<N, R, C> {
            // NOTE: we don't use `m.amax()` here because for the complex
            // numbers this will return the max norm1 instead of the modulus.
            m.fold(N::RealField::zero(), |acc, a| acc.max(a.modulus()))
        }

        #[inline]
        fn metric_distance<R1, C1, S1, R2, C2, S2>(&self, m1: &Matrix<N, R1, C1, S1>, m2: &Matrix<N, R2, C2, S2>) -> N::RealField
            where R1: Dim, C1: Dim, S1: Storage<N, R1, C1>,
                  R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {
            m1.zip_fold(m2, N::RealField::zero(), |acc, a, b| {
                let val = (a - b).modulus();
                if val > acc {
                    val
                } else {
                    acc
                }
            })
        }
    }

    impl<N: ComplexField, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> {
        /// The squared L2 norm of this vector.
        #[inline]
        pub fn norm_squared(&self) -> N::RealField {
            let mut res = N::RealField::zero();

            for i in 0..self.ncols() {
                let col = self.column(i);
                res += col.dotc(&col).real()
            }

            res
        }

        /// The L2 norm of this matrix.
        ///
        /// Use `.apply_norm` to apply a custom norm.
        #[inline]
        pub fn norm(&self) -> N::RealField {
            self.norm_squared().sqrt()
        }

        /// Compute the distance between `self` and `rhs` using the metric induced by the euclidean norm.
        ///
        /// Use `.apply_metric_distance` to apply a custom norm.
        #[inline]
        pub fn metric_distance<R2, C2, S2>(&self, rhs: &Matrix<N, R2, C2, S2>) -> N::RealField
            where R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R, R2> + SameNumberOfColumns<C, C2> {
            self.apply_metric_distance(rhs, &EuclideanNorm)
        }

        /// Uses the given `norm` to compute the norm of `self`.
        ///
        /// # 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());
        /// ```
        #[inline]
        pub fn apply_norm(&self, norm: &impl Norm<N>) -> N::RealField {
            norm.norm(self)
        }

        /// Uses the metric induced by the given `norm` to compute the metric distance between `self` and `rhs`.
        ///
        /// # 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());
        /// ```
        #[inline]
        pub fn apply_metric_distance<R2, C2, S2>(&self, rhs: &Matrix<N, R2, C2, S2>, norm: &impl Norm<N>) -> N::RealField
            where R2: Dim, C2: Dim, S2: Storage<N, R2, C2>,
                  ShapeConstraint: SameNumberOfRows<R, R2> + SameNumberOfColumns<C, C2> {
            norm.metric_distance(self, rhs)
        }

        /// A synonym for the norm of this matrix.
        ///
        /// Aka the length.
        ///
        /// This function is simply implemented as a call to `norm()`
        #[inline]
        pub fn magnitude(&self) -> N::RealField {
            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]
        pub fn magnitude_squared(&self) -> N::RealField {
            self.norm_squared()
        }

        /// Returns a normalized version of this matrix.
        #[inline]
        pub fn normalize(&self) -> MatrixMN<N, R, C>
            where DefaultAllocator: Allocator<N, R, C> {
            self.unscale(self.norm())
        }

        /// Returns a normalized version of this matrix unless its norm as smaller or equal to `eps`.
        #[inline]
        pub fn try_normalize(&self, min_norm: N::RealField) -> Option<MatrixMN<N, R, C>>
            where DefaultAllocator: Allocator<N, R, C> {
            let n = self.norm();

            if n <= min_norm {
                None
            } else {
                Some(self.unscale(n))
            }
        }

        /// The Lp norm of this matrix.
        #[inline]
        pub fn lp_norm(&self, p: i32) -> N::RealField {
            self.apply_norm(&LpNorm(p))
        }
    }

    impl<N: ComplexField, R: Dim, C: Dim, S: StorageMut<N, R, C>> Matrix<N, R, C, S> {
        /// Normalizes this matrix in-place and returns its norm.
        #[inline]
        pub fn normalize_mut(&mut self) -> N::RealField {
            let n = self.norm();
            self.unscale_mut(n);

            n
        }

        /// Normalizes this matrix in-place or does nothing if its norm is smaller or equal to `eps`.
        ///
        /// If the normalization succeeded, returns the old normal of this matrix.
        #[inline]
        pub fn try_normalize_mut(&mut self, min_norm: N::RealField) -> Option<N::RealField> {
            let n = self.norm();

            if n <= min_norm {
                None
            } else {
                self.unscale_mut(n);
                Some(n)
            }
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)