1use num::Float;
5
6use crate::v::{TVector, v3::Vector3};
7use crate::m::TMatrix;
8
9pub type Matrix3<F> = [[F; 3]; 3];
11
12impl<F: Float + std::fmt::Debug + std::iter::Sum> TMatrix<F> for Matrix3<F> {
14 fn colmajor3(m: Vec<Vector3<F>>) -> Self where Self: Sized {
16 (0..3).into_iter().map(|i|
17 (0..3).into_iter().map(|j|
18 m[j][i]
19 ).collect::<Vec<_>>().try_into().unwrap()
20 ).collect::<Vec<_>>().try_into().unwrap()
21 }
22 fn rowmajor3(m: Vec<Vector3<F>>) -> Self where Self: Sized {
24 (0..3).into_iter().map(|j|
25 m[j]
26 ).collect::<Vec<_>>().try_into().unwrap()
27 }
28 fn col_major(m: &Vec<Vec<F>>) -> Self {
30 (0..3).into_iter().map(|i|
31 (0..3).into_iter().map(|j|
32 m[j][i]
33 ).collect::<Vec<_>>().try_into().unwrap()
34 ).collect::<Vec<_>>().try_into().unwrap()
35 }
36 fn row_major(m: &Vec<Vec<F>>) -> Self {
38 (0..3).into_iter().map(|j|
39 (0..3).into_iter().map(|i|
40 m[j][i]
41 ).collect::<Vec<_>>().try_into().unwrap()
42 ).collect::<Vec<_>>().try_into().unwrap()
43 }
44 fn new(m: &Vec<Vec<F>>) -> Self {
46 Self::row_major(m)
47 }
48 fn identity() -> Self {
50 let o = <F>::from(0).unwrap();
51 let l = <F>::from(1).unwrap();
52 Self::new(&vec![
53 vec![l, o, o],
54 vec![o, l, o],
55 vec![o, o, l]])
56 }
57 fn prec_eq(&self, e: F, m: &impl TMatrix<F>) -> bool {
59 let m = m.mev3();
60 for j in 0..3 {
61 for i in 0..3 {
62 if (self[j][i] - m[j][i]).abs() >= e { return false; }
63 }
64 }
65 true
66 }
67 fn mev3(&self) -> &[Vector3<F>] {
69 self
70 }
71 fn dot_m(&self, m: &impl TMatrix<F>) -> Self {
73 Matrix3::<F>::colmajor3((0..3).into_iter().map(|i| {
74 self.colv3(i).dot_mv(m) }).collect())
76 }
77 fn rowv3(&self, j: usize) -> Vector3<F> {
79 self[j]
80}
82 fn colv3(&self, i: usize) -> Vector3<F> {
84 Vector3::<F>::new(&(0..3).into_iter().map(|j| self[j][i]).collect())
85 }
86 fn to_vec(&self) -> Vec<Vec<F>> {
88 self.iter().map(|r| r.to_vec()).collect::<Vec<_>>()
89 }
90}