argmin_testfunctions/
zero.rs1use num::{Float, FromPrimitive};
13
14pub fn zero<T>(_param: &[T]) -> T
18where
19 T: Float + FromPrimitive,
20{
21 T::from_f64(0.0).unwrap()
22}
23
24pub fn zero_derivative<T>(param: &[T]) -> Vec<T>
29where
30 T: Float + FromPrimitive,
31{
32 vec![T::from_f64(0.0).unwrap(); param.len()]
33}
34
35pub fn zero_derivative_const<const N: usize, T>(_param: &[T; N]) -> [T; N]
40where
41 T: Float + FromPrimitive,
42{
43 [T::from_f64(0.0).unwrap(); N]
44}
45
46pub fn zero_hessian<T>(param: &[T]) -> Vec<Vec<T>>
50where
51 T: Float + FromPrimitive,
52{
53 vec![vec![T::from_f64(0.0).unwrap(); param.len()]; param.len()]
54}
55
56pub fn zero_hessian_const<const N: usize, T>(_param: &[T; N]) -> [[T; N]; N]
60where
61 T: Float + FromPrimitive,
62{
63 [[T::from_f64(0.0).unwrap(); N]; N]
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_zero() {
72 assert_eq!(
73 zero(&[0.0_f64, 0.0_f64]).to_ne_bytes(),
74 0.0_f64.to_ne_bytes()
75 );
76 assert_eq!(
77 zero(&[0.0_f32, 0.0_f32]).to_ne_bytes(),
78 0.0_f32.to_ne_bytes()
79 );
80 }
81
82 #[test]
83 fn test_zero_derivative() {
84 zero_derivative(&[0.0_f64, 0.0, 23.0, 28.0])
85 .iter()
86 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
87 .count();
88
89 zero_derivative(&[0.0_f32, 0.0, 23.0, 28.0])
90 .iter()
91 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
92 .count();
93
94 zero_derivative_const(&[0.0_f64, 0.0, 23.0, 28.0])
95 .iter()
96 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
97 .count();
98
99 zero_derivative_const(&[0.0_f32, 0.0, 23.0, 28.0])
100 .iter()
101 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
102 .count();
103 }
104
105 #[test]
106 fn test_zero_hessian() {
107 zero_hessian(&[0.0_f64, 0.0, 23.0, 28.0])
108 .iter()
109 .flatten()
110 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
111 .count();
112
113 zero_hessian(&[0.0_f32, 0.0, 23.0, 28.0])
114 .iter()
115 .flatten()
116 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
117 .count();
118
119 zero_hessian_const(&[0.0_f64, 0.0, 23.0, 28.0])
120 .iter()
121 .flatten()
122 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f64.to_ne_bytes()))
123 .count();
124
125 zero_hessian_const(&[0.0_f32, 0.0, 23.0, 28.0])
126 .iter()
127 .flatten()
128 .map(|x| assert_eq!(x.to_ne_bytes(), 0.0_f32.to_ne_bytes()))
129 .count();
130 }
131}