nalgebra/base/
properties.rs

1// Matrix properties checks.
2use approx::RelativeEq;
3use num::{One, Zero};
4
5use simba::scalar::{ClosedAddAssign, ClosedMulAssign, ComplexField, RealField};
6
7use crate::base::allocator::Allocator;
8use crate::base::dimension::{Dim, DimMin};
9use crate::base::storage::Storage;
10use crate::base::{DefaultAllocator, Matrix, SquareMatrix};
11use crate::RawStorage;
12
13impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
14    /// The total number of elements of this matrix.
15    ///
16    /// # Examples:
17    ///
18    /// ```
19    /// # use nalgebra::Matrix3x4;
20    /// let mat = Matrix3x4::<f32>::zeros();
21    /// assert_eq!(mat.len(), 12);
22    /// ```
23    #[inline]
24    #[must_use]
25    pub fn len(&self) -> usize {
26        let (nrows, ncols) = self.shape();
27        nrows * ncols
28    }
29
30    /// Returns true if the matrix contains no elements.
31    ///
32    /// # Examples:
33    ///
34    /// ```
35    /// # use nalgebra::Matrix3x4;
36    /// let mat = Matrix3x4::<f32>::zeros();
37    /// assert!(!mat.is_empty());
38    /// ```
39    #[inline]
40    #[must_use]
41    pub fn is_empty(&self) -> bool {
42        self.len() == 0
43    }
44
45    /// Indicates if this is a square matrix.
46    #[inline]
47    #[must_use]
48    pub fn is_square(&self) -> bool {
49        let (nrows, ncols) = self.shape();
50        nrows == ncols
51    }
52
53    // TODO: RelativeEq prevents us from using those methods on integer matrices…
54    /// Indicated if this is the identity matrix within a relative error of `eps`.
55    ///
56    /// If the matrix is diagonal, this checks that diagonal elements (i.e. at coordinates `(i, i)`
57    /// for i from `0` to `min(R, C)`) are equal one; and that all other elements are zero.
58    #[inline]
59    #[must_use]
60    pub fn is_identity(&self, eps: T::Epsilon) -> bool
61    where
62        T: Zero + One + RelativeEq,
63        T::Epsilon: Clone,
64    {
65        let (nrows, ncols) = self.shape();
66
67        for j in 0..ncols {
68            for i in 0..nrows {
69                let el = unsafe { self.get_unchecked((i, j)) };
70                if (i == j && !relative_eq!(*el, T::one(), epsilon = eps.clone()))
71                    || (i != j && !relative_eq!(*el, T::zero(), epsilon = eps.clone()))
72                {
73                    return false;
74                }
75            }
76        }
77
78        true
79    }
80}
81
82impl<T: ComplexField, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> {
83    /// Checks that `Mᵀ × M = Id`.
84    ///
85    /// In this definition `Id` is approximately equal to the identity matrix with a relative error
86    /// equal to `eps`.
87    #[inline]
88    #[must_use]
89    pub fn is_orthogonal(&self, eps: T::Epsilon) -> bool
90    where
91        T: Zero + One + ClosedAddAssign + ClosedMulAssign + RelativeEq,
92        S: Storage<T, R, C>,
93        T::Epsilon: Clone,
94        DefaultAllocator: Allocator<R, C> + Allocator<C, C>,
95    {
96        (self.ad_mul(self)).is_identity(eps)
97    }
98}
99
100impl<T: RealField, D: Dim, S: Storage<T, D, D>> SquareMatrix<T, D, S>
101where
102    DefaultAllocator: Allocator<D, D>,
103{
104    /// Checks that this matrix is orthogonal and has a determinant equal to 1.
105    #[inline]
106    #[must_use]
107    pub fn is_special_orthogonal(&self, eps: T) -> bool
108    where
109        D: DimMin<D, Output = D>,
110        DefaultAllocator: Allocator<D>,
111    {
112        self.is_square() && self.is_orthogonal(eps) && self.determinant() > T::zero()
113    }
114
115    /// Returns `true` if this matrix is invertible.
116    #[inline]
117    #[must_use]
118    pub fn is_invertible(&self) -> bool {
119        // TODO: improve this?
120        self.clone_owned().try_inverse().is_some()
121    }
122}