Skip to main content

geo_nd/
farray2.rs

1//a Imports
2use serde::{Deserialize, Serialize};
3
4use crate::{matrix, vector, FArray};
5use crate::{Float, SqMatrix, Vector};
6
7//a FArray2
8//tp FArray2
9/// The [FArray2] is a wrapper around a `D2 = D`^2` sized array of [Float]s.
10///
11/// It provides implementations of the traits required for a [SqMatrix]
12/// trait operating on an [FArray] of dimesion D.
13#[derive(Clone, Copy, Debug, PartialEq)]
14#[repr(transparent)]
15pub struct FArray2<F: Float, const D: usize, const D2: usize> {
16    data: [F; D2],
17}
18
19//a Macros
20//mi farray2_basic_traits!
21macro_rules! farray2_basic_traits {
22    { $f:ty,  $d:expr,  $d2:expr, $ty:ty } => {
23
24        //ip Default for FArray
25        impl std::default::Default for $ty {
26            fn default() -> Self {
27                Self {
28                    data: vector::zero(),
29                }
30            }
31        }
32
33        //ip Display for FArray
34        impl std::fmt::Display for $ty {
35            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36                vector::fmt(f, &self.data)
37            }
38        }
39    }
40}
41
42//mi farray2_mul_traits!
43macro_rules! farray2_mul_traits {
44    { $f:ty,  $d:expr,  $d2:expr, $ty:ty } => {
45        //ip FArray2
46        //ip Mul, MulAssign <Self> for FArray2
47        impl std::ops::Mul<Self> for $ty {
48            type Output = Self;
49            fn mul(self, other: Self) -> Self {
50                matrix::multiply::<$f, $d2, $d2, $d2, $d, $d, $d>(&self.data, &other.data).into()
51            }
52        }
53        impl std::ops::MulAssign<Self> for $ty {
54            fn mul_assign(&mut self, other: Self) {
55                *self = *self * other;
56            }
57        }
58    }
59}
60
61//ip SqMatrix<F,2,4> for FArray2
62//mi farray2_sqmatrix_trait!
63macro_rules! farray2_sqmatrix_trait {
64    { $f:ty,  $d:expr,  $d2:expr, $ty:ty, $det_fn:expr, $inv_fn:expr } => {
65
66        impl SqMatrix<$f, $d, $d2> for $ty
67        where
68            FArray<$f, $d>: Vector<$f, $d>,
69        {
70            fn transpose(&self) -> Self {
71                matrix::transpose::<$f, $d2, $d, $d>(self.data).into()
72            }
73            fn transform<T>(&self, v: &T) -> T
74            where
75                T: std::ops::Deref<Target = [$f; $d]>,
76                T: From<[$f; $d]>
77            {
78                matrix::multiply::<$f, $d2, $d, $d, $d, $d, 1>(&self.data, v).into()
79            }
80            fn determinant(&self) -> $f {
81                $det_fn (&self.data)
82            }
83            fn inverse(&self) -> Self {
84                $inv_fn (&self.data).into()
85            }
86        }
87    }
88}
89
90//mi farray2_traits
91macro_rules! farray2_traits {
92    { $f:ty,  $d:expr,  $d2:expr, $ty:ty, $det_fn:expr, $inv_fn:expr } => {
93        farray2_basic_traits! {$f, $d, $d2, $ty}
94
95        crate::ref_traits!{$f, $d2, $ty}
96        crate::convert_traits!{$f, $d2, $ty}
97        crate::serialize_traits!{$f, $d2, $ty}
98        crate::unary_traits!{$f, $d2, $ty}
99        crate::elementwise_traits!{$f, $d2, $ty, Add, add, +, AddAssign, add_assign, +=}
100        crate::elementwise_traits!{$f, $d2, $ty, Sub, sub, -, SubAssign, sub_assign, -=}
101        crate::scale_by_f_traits!{$f, $ty, Mul, mul, *, MulAssign, mul_assign, *=}
102        crate::scale_by_f_traits!{$f, $ty, Div, div, /, DivAssign, div_assign, /=}
103
104        farray2_mul_traits! {$f, $d, $d2, $ty}
105
106        farray2_sqmatrix_trait! {$f, $d, $d2, $ty, $det_fn, $inv_fn}
107    }
108}
109
110//a Use macros
111farray2_traits! {f32,2,4,FArray2<f32,2,4>, matrix::determinant2, matrix::inverse2}
112farray2_traits! {f32,3,9,FArray2<f32,3,9>, matrix::determinant3, matrix::inverse3}
113farray2_traits! {f32,4,16,FArray2<f32,4,16>, matrix::determinant4, matrix::inverse4}
114
115farray2_traits! {f64,2,4,FArray2<f64,2,4>, matrix::determinant2, matrix::inverse2}
116farray2_traits! {f64,3,9,FArray2<f64,3,9>, matrix::determinant3, matrix::inverse3}
117farray2_traits! {f64,4,16,FArray2<f64,4,16>, matrix::determinant4, matrix::inverse4}