argmin_testfunctions/
zero.rs

1// Copyright 2018-2024 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! # Zero
9//!
10//! Always returns `0.0`. This is only for performance tests.
11
12use num::{Float, FromPrimitive};
13
14/// Zero test function.
15///
16/// Always returns `0.0`. This is only for performance tests.
17pub fn zero<T>(_param: &[T]) -> T
18where
19    T: Float + FromPrimitive,
20{
21    T::from_f64(0.0).unwrap()
22}
23
24/// Derivative of zero test function.
25///
26/// Always returns a vector with the length of param, full of `0.0`. This is only for performance
27/// tests.
28pub 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
35/// Derivative of zero test function (const version).
36///
37/// Always returns an array with the length of param, full of `0.0`. This is only for performance
38/// tests.
39pub 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
46/// Hessian of zero test function.
47///
48/// Always returns a matrix with size `N` by `N`, full of `0.0`. This is only for performance tests.
49pub 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
56/// Hessian of zero test function (const version).
57///
58/// Always returns a matrix with size `N` by `N`, full of `0.0`. This is only for performance tests.
59pub 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}