2016-12-05 05:44:42 +08:00
|
|
|
|
// Matrix properties checks.
|
2018-05-19 21:41:58 +08:00
|
|
|
|
use approx::RelativeEq;
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use num::{One, Zero};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
|
use simba::scalar::{ClosedAdd, ClosedMul, ComplexField, RealField};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
|
use crate::base::dimension::{Dim, DimMin};
|
|
|
|
|
use crate::base::storage::Storage;
|
|
|
|
|
use crate::base::{DefaultAllocator, Matrix, Scalar, SquareMatrix};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
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> {
|
2020-11-19 19:24:26 +08:00
|
|
|
|
/// The total number of elements of this matrix.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::Matrix3x4;
|
|
|
|
|
/// let mat = Matrix3x4::<f32>::zeros();
|
|
|
|
|
/// assert_eq!(mat.len(), 12);
|
|
|
|
|
/// ```
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[inline]
|
2020-11-19 19:24:26 +08:00
|
|
|
|
pub fn len(&self) -> usize {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let (nrows, ncols) = self.shape();
|
2020-11-19 19:24:26 +08:00
|
|
|
|
nrows * ncols
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if the matrix contains no elements.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use nalgebra::Matrix3x4;
|
|
|
|
|
/// let mat = Matrix3x4::<f32>::zeros();
|
|
|
|
|
/// assert!(!mat.is_empty());
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.len() == 0
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Indicates if this is a square matrix.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_square(&self) -> bool {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let (nrows, ncols) = self.shape();
|
|
|
|
|
nrows == ncols
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: RelativeEq prevents us from using those methods on integer matrices…
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Indicated if this is the identity matrix within a relative error of `eps`.
|
|
|
|
|
///
|
|
|
|
|
/// If the matrix is diagonal, this checks that diagonal elements (i.e. at coordinates `(i, i)`
|
|
|
|
|
/// for i from `0` to `min(R, C)`) are equal one; and that all other elements are zero.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn is_identity(&self, eps: T::Epsilon) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: Zero + One + RelativeEq,
|
|
|
|
|
T::Epsilon: Copy,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
let (nrows, ncols) = self.shape();
|
|
|
|
|
let d;
|
|
|
|
|
|
|
|
|
|
if nrows > ncols {
|
|
|
|
|
d = ncols;
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
for i in d..nrows {
|
|
|
|
|
for j in 0..ncols {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if !relative_eq!(self[(i, j)], T::zero(), epsilon = eps) {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
|
|
|
|
// nrows <= ncols
|
2016-12-05 05:44:42 +08:00
|
|
|
|
d = nrows;
|
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
|
for i in 0..nrows {
|
|
|
|
|
for j in d..ncols {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if !relative_eq!(self[(i, j)], T::zero(), epsilon = eps) {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Off-diagonal elements of the sub-square matrix.
|
2018-02-02 19:26:35 +08:00
|
|
|
|
for i in 1..d {
|
|
|
|
|
for j in 0..i {
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: use unsafe indexing.
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if !relative_eq!(self[(i, j)], T::zero(), epsilon = eps)
|
|
|
|
|
|| !relative_eq!(self[(j, i)], T::zero(), epsilon = eps)
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Diagonal elements of the sub-square matrix.
|
2018-02-02 19:26:35 +08:00
|
|
|
|
for i in 0..d {
|
2021-04-11 17:00:38 +08:00
|
|
|
|
if !relative_eq!(self[(i, i)], T::one(), epsilon = eps) {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
}
|
2019-03-03 02:33:49 +08:00
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: ComplexField, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
/// Checks that `Mᵀ × M = Id`.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
///
|
|
|
|
|
/// In this definition `Id` is approximately equal to the identity matrix with a relative error
|
|
|
|
|
/// equal to `eps`.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn is_orthogonal(&self, eps: T::Epsilon) -> bool
|
2020-03-21 19:16:46 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
T: Zero + One + ClosedAdd + ClosedMul + RelativeEq,
|
|
|
|
|
S: Storage<T, R, C>,
|
|
|
|
|
T::Epsilon: Copy,
|
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T, C, C>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2019-03-23 18:48:12 +08:00
|
|
|
|
(self.ad_mul(self)).is_identity(eps)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
|
impl<T: RealField, D: Dim, S: Storage<T, D, D>> SquareMatrix<T, D, S>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/// Checks that this matrix is orthogonal and has a determinant equal to 1.
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
|
pub fn is_special_orthogonal(&self, eps: T) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
D: DimMin<D, Output = D>,
|
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
|
self.is_square() && self.is_orthogonal(eps) && self.determinant() > T::zero()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` if this matrix is invertible.
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
pub fn is_invertible(&self) -> bool {
|
2020-11-15 23:57:49 +08:00
|
|
|
|
// TODO: improve this?
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.clone_owned().try_inverse().is_some()
|
|
|
|
|
}
|
|
|
|
|
}
|