2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::ComplexField;
|
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::DimMin;
|
|
|
|
use crate::base::storage::Storage;
|
|
|
|
use crate::base::{DefaultAllocator, SquareMatrix};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::linalg::LU;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: ComplexField, D: DimMin<D, Output = D>, S: Storage<T, D, D>> SquareMatrix<T, D, S> {
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Computes the matrix determinant.
|
|
|
|
///
|
|
|
|
/// If the matrix has a dimension larger than 3, an LU decomposition is used.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn determinant(&self) -> T
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<(usize, usize), D>,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
self.is_square(),
|
|
|
|
"Unable to compute the determinant of a non-square matrix."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
let dim = self.shape().0;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
match dim {
|
2021-04-11 17:00:38 +08:00
|
|
|
0 => T::one(),
|
2021-08-04 23:34:25 +08:00
|
|
|
1 => self.get_unchecked((0, 0)).clone(),
|
2016-12-05 05:44:42 +08:00
|
|
|
2 => {
|
2021-08-04 23:34:25 +08:00
|
|
|
let m11 = self.get_unchecked((0, 0)).clone();
|
|
|
|
let m12 = self.get_unchecked((0, 1)).clone();
|
|
|
|
let m21 = self.get_unchecked((1, 0)).clone();
|
|
|
|
let m22 = self.get_unchecked((1, 1)).clone();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
m11 * m22 - m21 * m12
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
3 => {
|
2021-08-04 23:34:25 +08:00
|
|
|
let m11 = self.get_unchecked((0, 0)).clone();
|
|
|
|
let m12 = self.get_unchecked((0, 1)).clone();
|
|
|
|
let m13 = self.get_unchecked((0, 2)).clone();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
let m21 = self.get_unchecked((1, 0)).clone();
|
|
|
|
let m22 = self.get_unchecked((1, 1)).clone();
|
|
|
|
let m23 = self.get_unchecked((1, 2)).clone();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
let m31 = self.get_unchecked((2, 0)).clone();
|
|
|
|
let m32 = self.get_unchecked((2, 1)).clone();
|
|
|
|
let m33 = self.get_unchecked((2, 2)).clone();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
let minor_m12_m23 = m22.clone() * m33.clone() - m32.clone() * m23.clone();
|
|
|
|
let minor_m11_m23 = m21.clone() * m33.clone() - m31.clone() * m23.clone();
|
2016-12-05 05:44:42 +08:00
|
|
|
let minor_m11_m22 = m21 * m32 - m31 * m22;
|
|
|
|
|
|
|
|
m11 * minor_m12_m23 - m12 * minor_m11_m23 + m13 * minor_m11_m22
|
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
_ => LU::new(self.clone_owned()).determinant(),
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|