#[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_matrix { () => { impl Matrix{ #[inline(always)] pub const fn new(array:[[T;Y];X])->Self{ Self{array} } #[inline(always)] pub fn to_array(self)->[[T;Y];X]{ self.array } #[inline] pub fn map(self,f:F)->Matrix where F:Fn(T)->U { Matrix{ array:self.array.map(|inner|inner.map(&f)), } } #[inline] pub fn transpose(self)->Matrix{ //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() ) ) } } #[inline] // MatX.MatY = MatX pub fn dot(self,rhs:Matrix)->Matrix where T:core::ops::Mul+Copy, V:core::iter::Sum, U:Clone, { let mut array_of_iterators=rhs.array.map(|axis|axis.into_iter().cycle()); Matrix{ array:self.array.map(|axis| core::array::from_fn(|_| // axis dot product with transposed rhs array axis.iter().zip( array_of_iterators.each_mut().map(|iter| iter.next().unwrap() ) ).map(|(&a,b)| a*b ).sum() ) ) } } } impl Matrix where T:Copy { pub const fn from_value(value:T)->Self{ Self{ array:[[value;Y];X] } } } impl Default for Matrix{ fn default()->Self{ Self{ array:core::array::from_fn(|_|core::array::from_fn(|_|Default::default())) } } } } } #[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 core::ops::Deref for Matrix<$size_outer,$size_inner,T>{ type Target=$struct_outer<$struct_inner>; fn deref(&self)->&Self::Target{ unsafe{core::mem::transmute(&self.array)} } } impl 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),+); } }