123 lines
2.8 KiB
Rust
Raw Normal View History

2024-09-05 13:36:38 -07:00
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix {
() => {
2024-09-05 13:52:54 -07:00
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>{
2024-09-05 15:41:39 -07:00
#[inline(always)]
2024-09-05 13:52:54 -07:00
pub const fn new(array:[[T;Y];X])->Self{
Self{array}
}
2024-09-05 15:41:39 -07:00
#[inline(always)]
pub fn to_array(self)->[[T;Y];X]{
self.array
}
#[inline]
pub fn map<F,U>(self,f:F)->Matrix<X,Y,U>
where
F:Fn(T)->U
{
Matrix{
array:self.array.map(|inner|inner.map(&f)),
}
}
#[inline]
pub fn transpose(self)->Matrix<Y,X,T>{
//how did I think of this
let mut array_of_iterators=self.array.map(|axis|axis.into_iter());
Matrix{
array:core::array::from_fn(|_|
array_of_iterators.each_mut().map(|iter|
iter.next().unwrap()
)
)
}
}
2024-09-05 17:36:37 -07:00
#[inline]
// MatX<VecY>.MatY<VecZ> = MatX<VecZ>
pub fn dot<const Z:usize,U,V>(self,rhs:Matrix<Y,Z,U>)->Matrix<X,Z,V>
where
T:core::ops::Mul<U,Output=V>+Copy,
V:core::iter::Sum,
2024-09-05 17:56:04 -07:00
U:Clone,
2024-09-05 17:36:37 -07:00
{
2024-09-05 17:56:04 -07:00
let mut array_of_iterators=rhs.array.map(|axis|axis.into_iter().cycle());
2024-09-05 17:36:37 -07:00
Matrix{
array:self.array.map(|axis|
core::array::from_fn(|_|
// axis dot product with transposed rhs array
2024-09-05 17:44:44 -07:00
axis.iter().zip(
2024-09-05 17:36:37 -07:00
array_of_iterators.each_mut().map(|iter|
iter.next().unwrap()
)
2024-09-05 17:44:44 -07:00
).map(|(&a,b)|
2024-09-05 17:36:37 -07:00
a*b
).sum()
)
)
}
}
2024-09-05 15:41:39 -07:00
}
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>
where
T:Copy
{
pub const fn from_value(value:T)->Self{
Self{
array:[[value;Y];X]
}
}
}
impl<const X:usize,const Y:usize,T:Default> Default for Matrix<X,Y,T>{
fn default()->Self{
Self{
array:core::array::from_fn(|_|core::array::from_fn(|_|Default::default()))
}
}
2024-09-05 13:52:54 -07:00
}
2024-09-05 13:36:38 -07:00
}
}
2024-08-30 12:06:33 -07:00
2024-09-05 13:36:38 -07:00
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_named_fields_shape {
(
($struct_outer:ident, $size_outer: expr),
($struct_inner:ident, $size_inner: expr)
) => {
impl<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{
type Target=$struct_outer<$struct_inner<T>>;
fn deref(&self)->&Self::Target{
unsafe{core::mem::transmute(&self.array)}
}
}
impl<T> core::ops::DerefMut for Matrix<$size_outer,$size_inner,T>{
fn deref_mut(&mut self)->&mut Self::Target{
unsafe{core::mem::transmute(&mut self.array)}
}
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_named_fields_shape_shim {
(
($($vector_info:tt),+),
$matrix_info:tt
) => {
$crate::macro_repeated!(impl_matrix_named_fields_shape,$matrix_info,$($vector_info),+);
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_named_fields {
(
($($matrix_info:tt),+),
$vector_infos:tt
) => {
$crate::macro_repeated!(impl_matrix_named_fields_shape_shim,$vector_infos,$($matrix_info),+);
}
}