From 581d8a2b20ffaf561e3b40bd755cf710fb7b8f6e Mon Sep 17 00:00:00 2001 From: Philipp Rehner Date: Wed, 6 Nov 2024 15:54:05 +0100 Subject: [PATCH] Expose macros for the implementation of `DualNum` publicly --- CHANGELOG.md | 6 +- Cargo.toml | 2 +- src/derivatives.rs | 25 +++-- src/hyperdual_vec.rs | 4 +- src/macros.rs | 216 +++++++++++++++++++++++-------------------- 5 files changed, 139 insertions(+), 114 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b9ef3..3af3397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [0.10.1] - 2204-11-05 +## [0.10.2] - 2024-11-06 +## Changed +- Exposed macros for the implementation of the `DualNum` trait publicly. [#83](https://github.com/itt-ustutt/num-dual/pull/83) + +## [0.10.1] - 2024-11-05 ## Added - Expose the inner type of a generalized (hyper) dual number in the `DualNum` trait. [#82](https://github.com/itt-ustutt/num-dual/pull/82) diff --git a/Cargo.toml b/Cargo.toml index 3b72423..e70442f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "num-dual" -version = "0.10.1" +version = "0.10.2" authors = [ "Gernot Bauer ", "Philipp Rehner ", diff --git a/src/derivatives.rs b/src/derivatives.rs index cc676b9..e297e5f 100644 --- a/src/derivatives.rs +++ b/src/derivatives.rs @@ -1,9 +1,10 @@ +#[macro_export] macro_rules! impl_derivatives { - ($deriv:ident, $nderiv:expr, $struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($deriv:ident, $nderiv:expr, $struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: DualNumFloat$($(, $dim: Dim)*)?> DualNum for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { const NDERIV: usize = T::NDERIV + $nderiv; @@ -303,6 +304,7 @@ macro_rules! impl_derivatives { }; } +#[macro_export] macro_rules! second { (first, $($code:tt)*) => {}; (second, $($code:tt)*) => { @@ -313,6 +315,7 @@ macro_rules! second { }; } +#[macro_export] macro_rules! third { (first, $($code:tt)*) => {}; (second, $($code:tt)*) => {}; @@ -321,6 +324,7 @@ macro_rules! third { }; } +#[macro_export] macro_rules! chain_rule { (first, Self::chain_rule($self:ident, $f0:expr, $f1:expr, $f2:expr, $f3:expr)) => { Self::chain_rule($self, $f0, $f1) @@ -333,20 +337,23 @@ macro_rules! chain_rule { }; } +#[macro_export] macro_rules! impl_first_derivatives { - ($struct:ident, [$($im:ident),*]$(, [$($const:tt),*])?) => { - impl_derivatives!(first, 1, $struct, [$($im),*]$(, [$($const),*])?); + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { + impl_derivatives!(first, 1, $struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); }; } +#[macro_export] macro_rules! impl_second_derivatives { - ($struct:ident, [$($im:ident),*]$(, [$($const:tt),*])?) => { - impl_derivatives!(second, 2, $struct, [$($im),*]$(, [$($const),*])?); + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { + impl_derivatives!(second, 2, $struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); }; } +#[macro_export] macro_rules! impl_third_derivatives { - ($struct:ident, [$($im:ident),*]$(, [$($const:tt),*])?) => { - impl_derivatives!(third, 3, $struct, [$($im),*]$(, [$($const),*])?); + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { + impl_derivatives!(third, 3, $struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); }; } diff --git a/src/hyperdual_vec.rs b/src/hyperdual_vec.rs index 45c3565..c03d280 100644 --- a/src/hyperdual_vec.rs +++ b/src/hyperdual_vec.rs @@ -223,5 +223,5 @@ where } } -impl_second_derivatives!(HyperDualVec, [eps1, eps2, eps1eps2], [M, N]); -impl_dual!(HyperDualVec, [eps1, eps2, eps1eps2], [M, N]); +impl_second_derivatives!(HyperDualVec, [eps1, eps2, eps1eps2], [M, N], [M, N]); +impl_dual!(HyperDualVec, [eps1, eps2, eps1eps2], [M, N], [M, N]); diff --git a/src/macros.rs b/src/macros.rs index ec1b6fe..356264d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,9 +1,10 @@ +#[macro_export] macro_rules! impl_from_f { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F$($(, $dim: Dim)*)?> From for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn from(float: F) -> Self { @@ -13,12 +14,13 @@ macro_rules! impl_from_f { }; } +#[macro_export] macro_rules! impl_zero_one { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float$($(, $dim: Dim)*)?> Zero for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn zero() -> Self { @@ -33,8 +35,8 @@ macro_rules! impl_zero_one { impl, F: Float$($(, $dim: Dim)*)?> One for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn one() -> Self { @@ -49,13 +51,14 @@ macro_rules! impl_zero_one { }; } +#[macro_export] macro_rules! impl_add_sub_rem { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl<'a, 'b, T: DualNum, F: Float$($(, $dim: Dim)*)?> Add<&'a $struct> for &'b $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -67,8 +70,8 @@ macro_rules! impl_add_sub_rem { impl<'a, 'b, T: DualNum, F: Float$($(, $dim: Dim)*)?> Sub<&'a $struct> for &'b $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -79,8 +82,8 @@ macro_rules! impl_add_sub_rem { impl<'a, 'b, T: DualNum, F$($(, $dim: Dim)*)?> Rem<&'a $struct> for &'b $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -91,12 +94,13 @@ macro_rules! impl_add_sub_rem { }; } +#[macro_export] macro_rules! forward_binop { - ($struct:ident, $trt:ident, $operator:tt, $mth:ident$(, [$($dim:tt),*])?) => { + ($struct:ident, $trt:ident, $operator:tt, $mth:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float$($(, $dim: Dim)*)?> $trt<$struct> for &$struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -107,8 +111,8 @@ macro_rules! forward_binop { impl, F: Float$($(, $dim: Dim)*)?> $trt<&$struct> for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -119,8 +123,8 @@ macro_rules! forward_binop { impl, F: Float$($(, $dim: Dim)*)?> $trt for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -131,12 +135,13 @@ macro_rules! forward_binop { }; } +#[macro_export] macro_rules! impl_neg { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float$($(, $dim: Dim)*)?> Neg for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -147,8 +152,8 @@ macro_rules! impl_neg { impl, F: Float$($(, $dim: Dim)*)?> Neg for &$struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = $struct; #[inline] @@ -159,12 +164,13 @@ macro_rules! impl_neg { }; } +#[macro_export] macro_rules! impl_assign_ops { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float$($(, $dim: Dim)*)?> MulAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn mul_assign(&mut self, other: Self) { @@ -174,8 +180,8 @@ macro_rules! impl_assign_ops { impl, F: Float$($(, $dim: Dim)*)?> DivAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn div_assign(&mut self, other: Self) { @@ -185,8 +191,8 @@ macro_rules! impl_assign_ops { impl, F$($(, $dim: Dim)*)?> AddAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn add_assign(&mut self, other: Self) { @@ -197,8 +203,8 @@ macro_rules! impl_assign_ops { impl, F$($(, $dim: Dim)*)?> SubAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn sub_assign(&mut self, other: Self) { @@ -209,8 +215,8 @@ macro_rules! impl_assign_ops { impl, F$($(, $dim: Dim)*)?> RemAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn rem_assign(&mut self, _other: Self) { @@ -220,12 +226,13 @@ macro_rules! impl_assign_ops { }; } +#[macro_export] macro_rules! impl_scalar_op { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: DualNumFloat$($(, $dim: Dim)*)?> Mul for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -237,8 +244,8 @@ macro_rules! impl_scalar_op { impl, F: DualNumFloat$($(, $dim: Dim)*)?> MulAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn mul_assign(&mut self, other: F) { @@ -249,8 +256,8 @@ macro_rules! impl_scalar_op { impl, F: DualNumFloat$($(, $dim: Dim)*)?> Div for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -262,8 +269,8 @@ macro_rules! impl_scalar_op { impl, F: DualNumFloat$($(, $dim: Dim)*)?> DivAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn div_assign(&mut self, other: F) { @@ -274,8 +281,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> Add for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -287,8 +294,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> AddAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn add_assign(&mut self, other: F) { @@ -298,8 +305,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> Sub for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -311,8 +318,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> SubAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn sub_assign(&mut self, other: F) { @@ -322,8 +329,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> Rem for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -334,8 +341,8 @@ macro_rules! impl_scalar_op { impl, F$($(, $dim: Dim)*)?> RemAssign for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn rem_assign(&mut self, _other: F) { @@ -345,12 +352,13 @@ macro_rules! impl_scalar_op { }; } +#[macro_export] macro_rules! impl_inv { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: DualNumFloat$($(, $dim: Dim)*)?> Inv for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type Output = Self; #[inline] @@ -361,12 +369,13 @@ macro_rules! impl_inv { }; } +#[macro_export] macro_rules! impl_iterator { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float$($(, $dim: Dim)*)?> Sum for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn sum(iter: I) -> Self @@ -380,8 +389,8 @@ macro_rules! impl_iterator { impl<'a, T: DualNum, F: Float$($(, $dim: Dim)*)?> Sum<&'a $struct> for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn sum(iter: I) -> Self @@ -393,8 +402,8 @@ macro_rules! impl_iterator { } impl, F: Float$($(, $dim: Dim)*)?> Product for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn product(iter: I) -> Self @@ -407,8 +416,8 @@ macro_rules! impl_iterator { impl<'a, T: DualNum, F: Float$($(, $dim: Dim)*)?> Product<&'a $struct> for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn product(iter: I) -> Self @@ -421,12 +430,13 @@ macro_rules! impl_iterator { }; } +#[macro_export] macro_rules! impl_from_primitive { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float + FromPrimitive$($(, $dim: Dim)*)?> FromPrimitive for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn from_isize(n: isize) -> Option { @@ -501,12 +511,13 @@ macro_rules! impl_from_primitive { }; } +#[macro_export] macro_rules! impl_signed { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: DualNumFloat$($(, $dim: Dim)*)?> Signed for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { #[inline] fn abs(&self) -> Self { @@ -550,12 +561,13 @@ macro_rules! impl_signed { }; } +#[macro_export] macro_rules! impl_float_const { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl, F: Float + FloatConst$($(, $dim: Dim)*)?> FloatConst for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { fn E() -> Self { Self::from(F::E()) @@ -624,12 +636,13 @@ macro_rules! impl_float_const { }; } +#[macro_export] macro_rules! impl_num { - ($struct:ident$(, [$($dim:tt),*])?) => { + ($struct:ident$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { impl + Signed, F: Float$($(, $dim: Dim)*)?> Num for $struct where - $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)* - DefaultAllocator: Allocator<$($dim,)*>)? + $($(DefaultAllocator: Allocator<$dim> + Allocator + Allocator<$dim, $dim>,)*)? + $($(DefaultAllocator: Allocator<$($ddim,)*>)?)? { type FromStrRadixErr = F::FromStrRadixErr; #[inline] @@ -640,24 +653,25 @@ macro_rules! impl_num { }; } +#[macro_export] macro_rules! impl_dual { - ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => { - impl_from_f!($struct, [$($im),*]$(, [$($dim),*])?); - impl_zero_one!($struct$(, [$($dim),*])?); - impl_add_sub_rem!($struct, [$($im),*]$(, [$($dim),*])?); - forward_binop!($struct, Add, +, add$(, [$($dim),*])?); - forward_binop!($struct, Sub, -, sub$(, [$($dim),*])?); - forward_binop!($struct, Mul, *, mul$(, [$($dim),*])?); - forward_binop!($struct, Div, /, div$(, [$($dim),*])?); - forward_binop!($struct, Rem, %, rem$(, [$($dim),*])?); - impl_neg!($struct, [$($im),*]$(, [$($dim),*])?); - impl_assign_ops!($struct, [$($im),*]$(, [$($dim),*])?); - impl_scalar_op!($struct, [$($im),*]$(, [$($dim),*])?); - impl_inv!($struct$(, [$($dim),*])?); - impl_iterator!($struct$(, [$($dim),*])?); - impl_from_primitive!($struct$(, [$($dim),*])?); - impl_signed!($struct$(, [$($dim),*])?); - impl_num!($struct$(, [$($dim),*])?); - impl_float_const!($struct$(, [$($dim),*])?); + ($struct:ident, [$($im:ident),*]$(, [$($dim:tt),*]$(, [$($ddim:tt),*])?)?) => { + impl_from_f!($struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_zero_one!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_add_sub_rem!($struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); + forward_binop!($struct, Add, +, add$(, [$($dim),*]$(, [$($ddim),*])?)?); + forward_binop!($struct, Sub, -, sub$(, [$($dim),*]$(, [$($ddim),*])?)?); + forward_binop!($struct, Mul, *, mul$(, [$($dim),*]$(, [$($ddim),*])?)?); + forward_binop!($struct, Div, /, div$(, [$($dim),*]$(, [$($ddim),*])?)?); + forward_binop!($struct, Rem, %, rem$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_neg!($struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_assign_ops!($struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_scalar_op!($struct, [$($im),*]$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_inv!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_iterator!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_from_primitive!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_signed!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_num!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); + impl_float_const!($struct$(, [$($dim),*]$(, [$($ddim),*])?)?); }; }