matrix and vector extend functions

This commit is contained in:
Quaternions 2024-09-06 13:23:55 -07:00
parent 66186c7354
commit 898407a0e9
4 changed files with 53 additions and 0 deletions

View File

@ -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<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())
})
)
}
}
}
}
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_matrix_named_fields_shape { macro_rules! impl_matrix_named_fields_shape {

View File

@ -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<T> 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)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_vector_named_fields { macro_rules! impl_vector_named_fields {

View File

@ -1,3 +1,5 @@
use crate::vector::Vector;
#[derive(Clone,Copy,Hash,Eq,PartialEq)] #[derive(Clone,Copy,Hash,Eq,PartialEq)]
pub struct Matrix<const X:usize,const Y:usize,T>{ pub struct Matrix<const X:usize,const Y:usize,T>{
pub(crate) array:[[T;X];Y], pub(crate) array:[[T;X];Y],
@ -5,5 +7,10 @@ pub struct Matrix<const X:usize,const Y:usize,T>{
crate::impl_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 //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases
crate::impl_matrix_3x3!(); crate::impl_matrix_3x3!();

View File

@ -10,5 +10,9 @@ pub struct Vector<const N:usize,T>{
crate::impl_vector!(); crate::impl_vector!();
// Needs const generics for generic case
crate::impl_vector_extend!(2);
crate::impl_vector_extend!(3);
//cross product //cross product
crate::impl_vector_3!(); crate::impl_vector_3!();