implement determinant on 3x3 and 4x4 matrices
This commit is contained in:
parent
d6ad12e344
commit
762cd45f63
@ -1,5 +1,5 @@
|
||||
use std::fmt;
|
||||
use std::ops::{Add, Div, Index, Mul, Neg, Sub};
|
||||
use std::ops::{Index, Mul};
|
||||
|
||||
use crate::tuples::Tuple;
|
||||
|
||||
@ -86,7 +86,7 @@ impl Matrix3x3 {
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use rtchallenge::matrices::{Matrix2x2, Matrix3x3};
|
||||
/// use rtchallenge::matrices::Matrix3x3;
|
||||
///
|
||||
/// let a = Matrix3x3::new([3., 5., 0.], [2., -1., -7.], [6., -1., 5.]);
|
||||
/// let b = a.submatrix(1, 0);
|
||||
@ -101,7 +101,7 @@ impl Matrix3x3 {
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use rtchallenge::matrices::{Matrix2x2, Matrix3x3};
|
||||
/// use rtchallenge::matrices::Matrix3x3;
|
||||
///
|
||||
/// let a = Matrix3x3::new([3., 5., 0.], [2., -1., -7.], [6., -1., 5.]);
|
||||
/// assert_eq!(a.minor(0, 0), -12.);
|
||||
@ -113,6 +113,22 @@ impl Matrix3x3 {
|
||||
let negate = if (row + col) % 2 == 0 { 1. } else { -1. };
|
||||
self.submatrix(row, col).determinant() * negate
|
||||
}
|
||||
|
||||
/// Compute determinant of a 3x3 matrix.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use rtchallenge::matrices::Matrix3x3;
|
||||
///
|
||||
/// let a = Matrix3x3::new([1., 2., 6.], [-5., 8., -4.], [2., 6., 4.]);
|
||||
/// assert_eq!(a.cofactor(0, 0), 56.);
|
||||
/// assert_eq!(a.cofactor(0, 1), 12.);
|
||||
/// assert_eq!(a.cofactor(0, 2), -46.);
|
||||
/// assert_eq!(a.determinant(), -196.);
|
||||
/// ```
|
||||
pub fn determinant(&self) -> f32 {
|
||||
(0..3).map(|i| self.cofactor(0, i) * self[(0, i)]).sum()
|
||||
}
|
||||
}
|
||||
impl Index<(usize, usize)> for Matrix3x3 {
|
||||
type Output = f32;
|
||||
@ -333,6 +349,37 @@ impl Matrix4x4 {
|
||||
];
|
||||
Matrix3x3 { m }
|
||||
}
|
||||
|
||||
/// Compute minor of a 4x4 matrix.
|
||||
pub fn minor(&self, row: usize, col: usize) -> f32 {
|
||||
self.submatrix(row, col).determinant()
|
||||
}
|
||||
/// Compute cofactor of a 4x4 matrix.
|
||||
pub fn cofactor(&self, row: usize, col: usize) -> f32 {
|
||||
let negate = if (row + col) % 2 == 0 { 1. } else { -1. };
|
||||
self.submatrix(row, col).determinant() * negate
|
||||
}
|
||||
/// Compute determinant of a 4x4 matrix.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use rtchallenge::matrices::Matrix4x4;
|
||||
///
|
||||
/// let a = Matrix4x4::new(
|
||||
/// [-2., -8., 3., 5.],
|
||||
/// [-3., 1., 7., 3.],
|
||||
/// [1., 2., -9., 6.],
|
||||
/// [-6., 7., 7., -9.],
|
||||
/// );
|
||||
/// assert_eq!(a.cofactor(0, 0), 690.);
|
||||
/// assert_eq!(a.cofactor(0, 1), 447.);
|
||||
/// assert_eq!(a.cofactor(0, 2), 210.);
|
||||
/// assert_eq!(a.cofactor(0, 3), 51.);
|
||||
/// assert_eq!(a.determinant(), -4071.);
|
||||
/// ```
|
||||
pub fn determinant(&self) -> f32 {
|
||||
(0..4).map(|i| self.cofactor(0, i) * self[(0, i)]).sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Matrix4x4 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user