use num::{Float, FromPrimitive};
pub fn zero<T>(_param: &[T]) -> T
where
T: Float + FromPrimitive,
{
T::from_f64(0.0).unwrap()
}
pub fn zero_derivative<T>(param: &[T]) -> Vec<T>
where
T: Float + FromPrimitive,
{
vec![T::from_f64(0.0).unwrap(); param.len()]
}
pub fn zero_derivative_const<const N: usize, T>(_param: &[T; N]) -> [T; N]
where
T: Float + FromPrimitive,
{
[T::from_f64(0.0).unwrap(); N]
}
pub fn zero_hessian<T>(param: &[T]) -> Vec<Vec<T>>
where
T: Float + FromPrimitive,
{
vec![vec![T::from_f64(0.0).unwrap(); param.len()]; param.len()]
}
pub fn zero_hessian_const<const N: usize, T>(_param: &[T; N]) -> [[T; N]; N]
where
T: Float + FromPrimitive,
{
[[T::from_f64(0.0).unwrap(); N]; N]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zero() {
assert_eq!(
zero(&[0.0_f64, 0.0_f64]).to_ne_bytes(),
0.0_f64.to_ne_bytes()
);
assert_eq!(
zero(&[0.0_f32, 0.0_f32]).to_ne_bytes(),
0.0_f32.to_ne_bytes()
);
}
#[test]
fn test_zero_derivative() {
zero_derivative(&[0.0_f64, 0.0, 23.0, 28.0])
.iter()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
.count();
zero_derivative(&[0.0_f32, 0.0, 23.0, 28.0])
.iter()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
.count();
zero_derivative_const(&[0.0_f64, 0.0, 23.0, 28.0])
.iter()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
.count();
zero_derivative_const(&[0.0_f32, 0.0, 23.0, 28.0])
.iter()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
.count();
}
#[test]
fn test_zero_hessian() {
zero_hessian(&[0.0_f64, 0.0, 23.0, 28.0])
.iter()
.flatten()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
.count();
zero_hessian(&[0.0_f32, 0.0, 23.0, 28.0])
.iter()
.flatten()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
.count();
zero_hessian_const(&[0.0_f64, 0.0, 23.0, 28.0])
.iter()
.flatten()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
.count();
zero_hessian_const(&[0.0_f32, 0.0, 23.0, 28.0])
.iter()
.flatten()
.map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
.count();
}
}