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-06 10:52:17 -07:00
|
|
|
pub const fn new(array:[[T;X];Y])->Self{
|
2024-09-05 13:52:54 -07:00
|
|
|
Self{array}
|
|
|
|
}
|
2024-09-05 15:41:39 -07:00
|
|
|
#[inline(always)]
|
2024-09-06 10:52:17 -07:00
|
|
|
pub fn to_array(self)->[[T;X];Y]{
|
2024-09-05 15:41:39 -07:00
|
|
|
self.array
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
pub fn map<F,U>(self,f:F)->Matrix<X,Y,U>
|
|
|
|
where
|
|
|
|
F:Fn(T)->U
|
|
|
|
{
|
2024-09-06 11:44:43 -07:00
|
|
|
Matrix::new(
|
|
|
|
self.array.map(|inner|inner.map(&f)),
|
|
|
|
)
|
2024-09-05 15:41:39 -07:00
|
|
|
}
|
2024-09-05 17:15:41 -07:00
|
|
|
#[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());
|
2024-09-06 11:44:43 -07:00
|
|
|
Matrix::new(
|
|
|
|
core::array::from_fn(|_|
|
2024-09-05 17:15:41 -07:00
|
|
|
array_of_iterators.each_mut().map(|iter|
|
|
|
|
iter.next().unwrap()
|
|
|
|
)
|
|
|
|
)
|
2024-09-06 11:44:43 -07:00
|
|
|
)
|
2024-09-05 17:15:41 -07:00
|
|
|
}
|
2024-09-05 17:36:37 -07:00
|
|
|
#[inline]
|
2024-09-06 10:52:17 -07:00
|
|
|
// MatY<VecX>.MatX<VecZ> = MatY<VecZ>
|
|
|
|
pub fn dot<const Z:usize,U,V>(self,rhs:Matrix<Z,X,U>)->Matrix<Z,Y,V>
|
2024-09-05 17:36:37 -07:00
|
|
|
where
|
|
|
|
T:core::ops::Mul<U,Output=V>+Copy,
|
|
|
|
V:core::iter::Sum,
|
2024-09-06 10:36:24 -07:00
|
|
|
U:Copy,
|
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-06 11:44:43 -07:00
|
|
|
Matrix::new(
|
|
|
|
self.array.map(|axis|
|
2024-09-05 17:36:37 -07:00
|
|
|
core::array::from_fn(|_|
|
|
|
|
// axis dot product with transposed rhs array
|
2024-09-05 17:44:44 -07:00
|
|
|
axis.iter().zip(
|
2024-09-06 10:36:24 -07:00
|
|
|
array_of_iterators.iter_mut()
|
|
|
|
).map(|(&lhs_value,rhs_iter)|
|
|
|
|
lhs_value*rhs_iter.next().unwrap()
|
2024-09-05 17:36:37 -07:00
|
|
|
).sum()
|
|
|
|
)
|
|
|
|
)
|
2024-09-06 11:44:43 -07:00
|
|
|
)
|
2024-09-05 17:36:37 -07:00
|
|
|
}
|
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{
|
2024-09-06 11:44:43 -07:00
|
|
|
Self::new([[value;X];Y])
|
2024-09-05 15:41:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const X:usize,const Y:usize,T:Default> Default for Matrix<X,Y,T>{
|
|
|
|
fn default()->Self{
|
2024-09-06 11:44:43 -07:00
|
|
|
Self::new(
|
|
|
|
core::array::from_fn(|_|core::array::from_fn(|_|Default::default()))
|
|
|
|
)
|
2024-09-05 15:41:39 -07:00
|
|
|
}
|
2024-09-05 13:52:54 -07:00
|
|
|
}
|
2024-09-09 17:01:52 -07:00
|
|
|
impl<const X:usize,const Y:usize,const Z:usize,T,U,V> core::ops::Mul<Matrix<Z,X,U>> for Matrix<X,Y,T>
|
|
|
|
where
|
|
|
|
T:core::ops::Mul<U,Output=V>+Copy,
|
|
|
|
V:core::iter::Sum,
|
|
|
|
U:Copy,
|
|
|
|
{
|
|
|
|
type Output=Matrix<Z,Y,V>;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self,rhs:Matrix<Z,X,U>)->Self::Output{
|
|
|
|
self.dot(rhs)
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 11:38:22 -07:00
|
|
|
#[cfg(feature="fixed_wide")]
|
|
|
|
$crate::impl_matrix_wide_dot_8x8!();
|
2024-09-05 13:36:38 -07:00
|
|
|
}
|
|
|
|
}
|
2024-08-30 12:06:33 -07:00
|
|
|
|
2024-09-06 13:23:55 -07:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export(local_inner_macros)]
|
|
|
|
macro_rules! impl_matrix_extend {
|
|
|
|
( $x: expr, $y: expr ) => {
|
|
|
|
impl<T> Matrix<$x,$y,T>{
|
|
|
|
#[inline]
|
|
|
|
pub fn extend_row(self,value:Vector<$x,T>)->Matrix<$x,{$y+1},T>{
|
|
|
|
let mut iter=self.array.into_iter().chain(core::iter::once(value.array));
|
|
|
|
Matrix::new(
|
|
|
|
core::array::from_fn(|_|iter.next().unwrap()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
pub fn extend_column(self,value:Vector<$y,T>)->Matrix<{$x+1},$y,T>{
|
|
|
|
let mut iter_rows=value.array.into_iter();
|
|
|
|
Matrix::new(
|
|
|
|
self.array.map(|axis|{
|
|
|
|
let mut elements_iter=axis.into_iter().chain(core::iter::once(iter_rows.next().unwrap()));
|
|
|
|
core::array::from_fn(|_|elements_iter.next().unwrap())
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
2024-09-06 11:25:46 -07:00
|
|
|
($size_inner: expr)
|
2024-09-05 13:36:38 -07:00
|
|
|
) => {
|
|
|
|
impl<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{
|
2024-09-06 11:25:46 -07:00
|
|
|
type Target=$struct_outer<Vector<$size_inner,T>>;
|
2024-09-05 13:36:38 -07:00
|
|
|
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),+);
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 11:25:46 -07:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export(local_inner_macros)]
|
|
|
|
macro_rules! impl_matrix_3x3 {
|
|
|
|
()=>{
|
|
|
|
#[cfg(feature="fixed_wide")]
|
|
|
|
$crate::impl_matrix_wide_3x3!();
|
|
|
|
}
|
|
|
|
}
|