use crate::basis_function::BasisFunction;
use nalgebra::{DVector, Scalar};
impl<ScalarType, Func> BasisFunction<ScalarType, ScalarType> for Func
where
ScalarType: Scalar,
Func: Fn(&DVector<ScalarType>, ScalarType) -> DVector<ScalarType> + Send + Sync,
{
fn eval(&self, x: &DVector<ScalarType>, params: &[ScalarType]) -> DVector<ScalarType> {
if params.len() != Self::ARGUMENT_COUNT {
panic!(
"Basisfunction expected {} arguments but the provided parameter slice has {} elements.",
Self::ARGUMENT_COUNT,
params.len()
);
}
(self)(x, params[0].clone())
}
const ARGUMENT_COUNT: usize = 1;
}
macro_rules! count_args {
() => {0usize};
($_head:tt, $($tail:tt),*) => {1usize + count_args!($($tail,)*)}; ($_head:tt, $($tail:tt,)*) => {1usize + count_args!($($tail,)*)};
}
macro_rules! basefunction_impl_helper ({$ScalarType:ident, $(($n:tt, $T:ident)),+} => {
impl<$ScalarType, Func> BasisFunction<$ScalarType,($($T,)+)> for Func
where Func: Fn(&DVector<$ScalarType>,$($T,)+) -> DVector<$ScalarType> + Send + Sync,
$ScalarType : Scalar
{
fn eval(&self, x : &DVector<$ScalarType>,params : &[$ScalarType]) -> DVector<$ScalarType> {
if params.len() != Self::ARGUMENT_COUNT {
panic!("Basisfunction expected {} arguments but the provided parameter slice has {} elements.",Self::ARGUMENT_COUNT,params.len());
}
(&self)(
x,
$(params[$n].clone(),)+)
}
const ARGUMENT_COUNT :usize = count_args!($($T,)+);
}
});
basefunction_impl_helper!(T, (0, T), (1, T));
basefunction_impl_helper!(T, (0, T), (1, T), (2, T));
basefunction_impl_helper!(T, (0, T), (1, T), (2, T), (3, T));
basefunction_impl_helper!(T, (0, T), (1, T), (2, T), (3, T), (4, T));
basefunction_impl_helper!(T, (0, T), (1, T), (2, T), (3, T), (4, T), (5, T));
basefunction_impl_helper!(T, (0, T), (1, T), (2, T), (3, T), (4, T), (5, T), (6, T));
basefunction_impl_helper!(
T,
(0, T),
(1, T),
(2, T),
(3, T),
(4, T),
(5, T),
(6, T),
(7, T)
);
basefunction_impl_helper!(
T,
(0, T),
(1, T),
(2, T),
(3, T),
(4, T),
(5, T),
(6, T),
(7, T),
(8, T)
);
basefunction_impl_helper!(
T,
(0, T),
(1, T),
(2, T),
(3, T),
(4, T),
(5, T),
(6, T),
(7, T),
(8, T),
(9, T)
);