diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index e5ec16e..34b7fd8 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -76,6 +76,32 @@ macro_rules! impl_matrix { } } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_extend { + ( $x: expr, $y: expr ) => { + impl 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()) + }) + ) + } + } + } +} + #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_matrix_named_fields_shape { diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 9d976e9..3f016ee 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -159,6 +159,22 @@ macro_rules! impl_vector_assign_operator { } } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_extend { + ( $size: expr ) => { + impl Vector<$size,T>{ + #[inline] + pub fn extend(self,value:T)->Vector<{$size+1},T>{ + let mut iter=self.array.into_iter().chain(core::iter::once(value)); + Vector::new( + core::array::from_fn(|_|iter.next().unwrap()), + ) + } + } + } +} + #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_vector_named_fields { diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 02070ec..5a6b914 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -1,3 +1,5 @@ +use crate::vector::Vector; + #[derive(Clone,Copy,Hash,Eq,PartialEq)] pub struct Matrix{ pub(crate) array:[[T;X];Y], @@ -5,5 +7,10 @@ pub struct Matrix{ crate::impl_matrix!(); +crate::impl_matrix_extend!(2,2); +crate::impl_matrix_extend!(2,3); +crate::impl_matrix_extend!(3,2); +crate::impl_matrix_extend!(3,3); + //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases crate::impl_matrix_3x3!(); diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index 0cc1ec8..3b7a15d 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -10,5 +10,9 @@ pub struct Vector{ crate::impl_vector!(); +// Needs const generics for generic case +crate::impl_vector_extend!(2); +crate::impl_vector_extend!(3); + //cross product crate::impl_vector_3!();