Refactor to move ops impls alongside matrix

This commit is contained in:
Andrew Cassidy 2023-05-17 22:44:02 -07:00
parent f697db6d35
commit 58ff79f649
4 changed files with 58 additions and 64 deletions

View File

@ -2,7 +2,6 @@ extern crate core;
pub mod decompose; pub mod decompose;
pub mod index; pub mod index;
mod macros;
mod matrix; mod matrix;
mod util; mod util;

View File

@ -1 +0,0 @@
pub mod ops;

View File

@ -1,12 +1,13 @@
use crate::impl_matrix_op;
use crate::index::Index2D; use crate::index::Index2D;
use num_traits::{Num, NumOps, One, Zero}; use num_traits::{NumOps, One, Zero};
use std::fmt::Debug; use std::fmt::Debug;
use std::iter::{zip, Flatten, Product, Sum}; use std::iter::{zip, Flatten, Product, Sum};
use std::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Mul, MulAssign, Neg}; use std::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Mul, MulAssign, Neg};
pub mod ops;
/// A 2D array of values which can be operated upon. /// A 2D array of values which can be operated upon.
/// ///
/// Matrices have a fixed size known at compile time /// Matrices have a fixed size known at compile time
@ -529,16 +530,3 @@ where
iter.fold(Self::one(), Self::mul) iter.fold(Self::one(), Self::mul)
} }
} }
impl_matrix_op!(neg);
impl_matrix_op!(!);
impl_matrix_op!(+);
impl_matrix_op!(-);
impl_matrix_op!(*);
impl_matrix_op!(/);
impl_matrix_op!(%);
impl_matrix_op!(&);
impl_matrix_op!(|);
impl_matrix_op!(^);
impl_matrix_op!(<<);
impl_matrix_op!(>>);

View File

@ -1,58 +1,60 @@
use crate::matrix::Matrix;
use num_traits::Num;
// borrowed from the auto_ops crate // borrowed from the auto_ops crate
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! impl_matrix_op { macro_rules! impl_matrix_op {
(neg) => { (neg) => {
$crate::_impl_op_m_internal_ex!(Neg, neg); _impl_op_m_internal_ex!(Neg, neg);
}; };
(!) => { (!) => {
$crate::_impl_op_m_internal_ex!(Not, not); _impl_op_m_internal_ex!(Not, not);
}; };
(+) => { (+) => {
$crate::_impl_op_mm_internal_ex!(Add, add); _impl_op_mm_internal_ex!(Add, add);
$crate::_impl_opassign_mm_internal_ex!(AddAssign, add_assign); _impl_opassign_mm_internal_ex!(AddAssign, add_assign);
}; };
(-) => { (-) => {
$crate::_impl_op_mm_internal_ex!(Sub, sub); _impl_op_mm_internal_ex!(Sub, sub);
$crate::_impl_opassign_mm_internal_ex!(SubAssign, sub_assign); _impl_opassign_mm_internal_ex!(SubAssign, sub_assign);
}; };
(*) => { (*) => {
$crate::_impl_op_mm_internal_ex!(Mul, mul); _impl_op_mm_internal_ex!(Mul, mul);
$crate::_impl_op_ms_internal_ex!(Mul, mul); _impl_op_ms_internal_ex!(Mul, mul);
$crate::_impl_opassign_mm_internal_ex!(MulAssign, mul_assign); _impl_opassign_mm_internal_ex!(MulAssign, mul_assign);
$crate::_impl_opassign_ms_internal_ex!(MulAssign, mul_assign); _impl_opassign_ms_internal_ex!(MulAssign, mul_assign);
}; };
(/) => { (/) => {
$crate::_impl_op_mm_internal_ex!(Div, div); _impl_op_mm_internal_ex!(Div, div);
$crate::_impl_op_ms_internal_ex!(Div, div); _impl_op_ms_internal_ex!(Div, div);
$crate::_impl_opassign_mm_internal_ex!(DivAssign, div_assign); _impl_opassign_mm_internal_ex!(DivAssign, div_assign);
$crate::_impl_opassign_ms_internal_ex!(DivAssign, div_assign); _impl_opassign_ms_internal_ex!(DivAssign, div_assign);
}; };
(%) => { (%) => {
$crate::_impl_op_mm_internal_ex!(Rem, rem); _impl_op_mm_internal_ex!(Rem, rem);
$crate::_impl_op_ms_internal_ex!(Rem, rem); _impl_op_ms_internal_ex!(Rem, rem);
$crate::_impl_opassign_mm_internal_ex!(RemAssign, rem_assign); _impl_opassign_mm_internal_ex!(RemAssign, rem_assign);
$crate::_impl_opassign_ms_internal_ex!(RemAssign, rem_assign); _impl_opassign_ms_internal_ex!(RemAssign, rem_assign);
}; };
(&) => { (&) => {
$crate::_impl_op_mm_internal_ex!(BitAnd, bitand); _impl_op_mm_internal_ex!(BitAnd, bitand);
$crate::_impl_opassign_mm_internal_ex!(BitAndAssign, bitand_assign); _impl_opassign_mm_internal_ex!(BitAndAssign, bitand_assign);
}; };
(|) => { (|) => {
$crate::_impl_op_mm_internal_ex!(BitOr, bitor); _impl_op_mm_internal_ex!(BitOr, bitor);
$crate::_impl_opassign_mm_internal_ex!(BitOrAssign, bitor_assign); _impl_opassign_mm_internal_ex!(BitOrAssign, bitor_assign);
}; };
(^) => { (^) => {
$crate::_impl_op_mm_internal_ex!(BitXor, bitxor); _impl_op_mm_internal_ex!(BitXor, bitxor);
$crate::_impl_opassign_mm_internal_ex!(BitXorAssign, bitxor_assign); _impl_opassign_mm_internal_ex!(BitXorAssign, bitxor_assign);
}; };
(<<) => { (<<) => {
$crate::_impl_op_ms_internal_ex!(Shl, shl); _impl_op_ms_internal_ex!(Shl, shl);
$crate::_impl_opassign_ms_internal_ex!(ShlAssign, shl_assign); _impl_opassign_ms_internal_ex!(ShlAssign, shl_assign);
}; };
(>>) => { (>>) => {
$crate::_impl_op_ms_internal_ex!(Shr, shr); _impl_op_ms_internal_ex!(Shr, shr);
$crate::_impl_opassign_ms_internal_ex!(ShrAssign, shr_assign); _impl_opassign_ms_internal_ex!(ShrAssign, shr_assign);
}; };
} }
@ -60,8 +62,8 @@ macro_rules! impl_matrix_op {
#[macro_export] #[macro_export]
macro_rules! _impl_op_m_internal_ex { macro_rules! _impl_op_m_internal_ex {
($ops_trait:ident, $ops_fn:ident) => { ($ops_trait:ident, $ops_fn:ident) => {
$crate::_impl_op_m_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<L,M,N>); _impl_op_m_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<L,M,N>);
$crate::_impl_op_m_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, Matrix<L,M,N>); _impl_op_m_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, Matrix<L,M,N>);
} }
} }
@ -69,10 +71,10 @@ macro_rules! _impl_op_m_internal_ex {
#[macro_export] #[macro_export]
macro_rules! _impl_op_mm_internal_ex { macro_rules! _impl_op_mm_internal_ex {
($ops_trait:ident, $ops_fn:ident) => { ($ops_trait:ident, $ops_fn:ident) => {
$crate::_impl_op_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>); _impl_op_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>);
$crate::_impl_op_mm_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>); _impl_op_mm_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>);
$crate::_impl_op_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>); _impl_op_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>);
$crate::_impl_op_mm_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>); _impl_op_mm_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>);
} }
} }
@ -80,30 +82,27 @@ macro_rules! _impl_op_mm_internal_ex {
#[macro_export] #[macro_export]
macro_rules! _impl_opassign_mm_internal_ex { macro_rules! _impl_opassign_mm_internal_ex {
($ops_trait:ident, $ops_fn:ident) => { ($ops_trait:ident, $ops_fn:ident) => {
$crate::_impl_opassign_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>); _impl_opassign_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, Matrix<R,M,N>, Matrix<L,M,N>);
$crate::_impl_opassign_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>); _impl_opassign_mm_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, &Matrix<R,M,N>, Matrix<L,M,N>);
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_op_ms_internal_ex { macro_rules! _impl_op_ms_internal_ex {
($ops_trait:ident, $ops_fn:ident) => { ($ops_trait:ident, $ops_fn:ident) => {
$crate::_impl_op_ms_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, R, Matrix<L,M,N>); _impl_op_ms_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, R, Matrix<L,M,N>);
$crate::_impl_op_ms_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, R, Matrix<L,M,N>); _impl_op_ms_internal!($ops_trait, $ops_fn, &Matrix<L,M,N>, R, Matrix<L,M,N>);
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_opassign_ms_internal_ex { macro_rules! _impl_opassign_ms_internal_ex {
($ops_trait:ident, $ops_fn:ident) => { ($ops_trait:ident, $ops_fn:ident) => {
$crate::_impl_opassign_ms_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, R, Matrix<L,M,N>); _impl_opassign_ms_internal!($ops_trait, $ops_fn, Matrix<L,M,N>, R, Matrix<L,M,N>);
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_op_m_internal { macro_rules! _impl_op_m_internal {
($ops_trait:ident, $ops_fn:ident, $lhs:ty, $out:ty) => { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $out:ty) => {
impl<L, const M: usize, const N: usize> ::std::ops::$ops_trait for $lhs impl<L, const M: usize, const N: usize> ::std::ops::$ops_trait for $lhs
@ -127,7 +126,6 @@ macro_rules! _impl_op_m_internal {
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_op_mm_internal { macro_rules! _impl_op_mm_internal {
($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => {
impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs
@ -152,7 +150,6 @@ macro_rules! _impl_op_mm_internal {
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_opassign_mm_internal { macro_rules! _impl_opassign_mm_internal {
($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => {
impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs
@ -173,7 +170,6 @@ macro_rules! _impl_opassign_mm_internal {
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_op_ms_internal { macro_rules! _impl_op_ms_internal {
($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => {
impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs
@ -198,7 +194,6 @@ macro_rules! _impl_op_ms_internal {
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export]
macro_rules! _impl_opassign_ms_internal { macro_rules! _impl_opassign_ms_internal {
($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty) => {
impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs impl<L, R, const M: usize, const N: usize> ::std::ops::$ops_trait<$rhs> for $lhs
@ -217,3 +212,16 @@ macro_rules! _impl_opassign_ms_internal {
} }
}; };
} }
impl_matrix_op!(neg);
impl_matrix_op!(!);
impl_matrix_op!(+);
impl_matrix_op!(-);
impl_matrix_op!(*);
impl_matrix_op!(/);
impl_matrix_op!(%);
impl_matrix_op!(&);
impl_matrix_op!(|);
impl_matrix_op!(^);
impl_matrix_op!(<<);
impl_matrix_op!(>>);