impl det + adjugate with trait bounds

This commit is contained in:
Quaternions 2024-09-09 18:02:05 -07:00
parent 57c3f2dd9e
commit eb7eb30814
2 changed files with 28 additions and 2 deletions

View File

@ -191,6 +191,32 @@ macro_rules! impl_matrix_named_fields {
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_matrix_3x3 { macro_rules! impl_matrix_3x3 {
()=>{ ()=>{
impl<T,T2,T3> Matrix<3,3,T>
where
//cross
T:core::ops::Mul<T,Output=T2>+Copy,
T2:core::ops::Sub,
//dot
T:core::ops::Mul<<T2 as core::ops::Sub>::Output,Output=T3>,
T3:core::iter::Sum,
{
pub fn det(self)->T3{
self.x_axis.dot(self.y_axis.cross(self.z_axis))
}
}
impl<T,T2> Matrix<3,3,T>
where
T:core::ops::Mul<T,Output=T2>+Copy,
T2:core::ops::Sub,
{
pub fn adjugate(self)->Matrix<3,3,<T2 as core::ops::Sub>::Output>{
Matrix::new([
[self.y_axis.y*self.z_axis.z-self.y_axis.z*self.z_axis.y,self.x_axis.z*self.z_axis.y-self.x_axis.y*self.z_axis.z,self.x_axis.y*self.y_axis.z-self.x_axis.z*self.y_axis.y],
[self.y_axis.z*self.z_axis.x-self.y_axis.x*self.z_axis.z,self.x_axis.x*self.z_axis.z-self.x_axis.z*self.z_axis.x,self.x_axis.z*self.y_axis.x-self.x_axis.x*self.y_axis.z],
[self.y_axis.x*self.z_axis.y-self.y_axis.y*self.z_axis.x,self.x_axis.y*self.z_axis.x-self.x_axis.x*self.z_axis.y,self.x_axis.x*self.y_axis.y-self.x_axis.y*self.y_axis.x],
])
}
}
#[cfg(feature="fixed_wide")] #[cfg(feature="fixed_wide")]
$crate::impl_matrix_wide_3x3!(); $crate::impl_matrix_wide_3x3!();
} }

View File

@ -71,7 +71,7 @@ fn wide_matrix_det(){
]); ]);
// In[2]:= Det[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}] // In[2]:= Det[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}]
// Out[2]= 7 // Out[2]= 7
assert_eq!(m.wide_det_3x3_1(),fixed_wide::fixed::Fixed::<3,96>::from(7)); assert_eq!(m.det(),fixed_wide::fixed::Fixed::<3,96>::from(7));
} }
#[test] #[test]
@ -84,7 +84,7 @@ fn wide_matrix_adjugate(){
// In[6]:= Adjugate[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}] // In[6]:= Adjugate[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}]
// Out[6]= {{-11, 6, -1}, {6, -9, 5}, {2, 4, -3}} // Out[6]= {{-11, 6, -1}, {6, -9, 5}, {2, 4, -3}}
assert_eq!( assert_eq!(
m.wide_adjugate_3x3_1().array, m.adjugate().array,
Matrix3::new([ Matrix3::new([
[Planar64Wide1::from(-11),Planar64Wide1::from(6),Planar64Wide1::from(-1)], [Planar64Wide1::from(-11),Planar64Wide1::from(6),Planar64Wide1::from(-1)],
[Planar64Wide1::from(6),Planar64Wide1::from(-9),Planar64Wide1::from(5)], [Planar64Wide1::from(6),Planar64Wide1::from(-9),Planar64Wide1::from(5)],