1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::consts;
use crate::function::gamma;
pub fn harmonic(t: u64) -> f64 {
match t {
0 => 1.0,
_ => consts::EULER_MASCHERONI + gamma::digamma(t as f64 + 1.0),
}
}
pub fn gen_harmonic(n: u64, m: f64) -> f64 {
match n {
0 => 1.0,
_ => (0..n).fold(0.0, |acc, x| acc + (x as f64 + 1.0).powf(-m)),
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg(test)]
mod test {
use std::f64;
#[test]
fn test_harmonic() {
assert_eq!(super::harmonic(0), 1.0);
assert_almost_eq!(super::harmonic(1), 1.0, 1e-14);
assert_almost_eq!(super::harmonic(2), 1.5, 1e-14);
assert_almost_eq!(super::harmonic(4), 2.083333333333333333333, 1e-14);
assert_almost_eq!(super::harmonic(8), 2.717857142857142857143, 1e-14);
assert_almost_eq!(super::harmonic(16), 3.380728993228993228993, 1e-14);
}
#[test]
fn test_gen_harmonic() {
assert_eq!(super::gen_harmonic(0, 0.0), 1.0);
assert_eq!(super::gen_harmonic(0, f64::INFINITY), 1.0);
assert_eq!(super::gen_harmonic(0, f64::NEG_INFINITY), 1.0);
assert_eq!(super::gen_harmonic(1, 0.0), 1.0);
assert_eq!(super::gen_harmonic(1, f64::INFINITY), 1.0);
assert_eq!(super::gen_harmonic(1, f64::NEG_INFINITY), 1.0);
assert_eq!(super::gen_harmonic(2, 1.0), 1.5);
assert_eq!(super::gen_harmonic(2, 3.0), 1.125);
assert_eq!(super::gen_harmonic(2, f64::INFINITY), 1.0);
assert_eq!(super::gen_harmonic(2, f64::NEG_INFINITY), f64::INFINITY);
assert_almost_eq!(super::gen_harmonic(4, 1.0), 2.083333333333333333333, 1e-14);
assert_eq!(super::gen_harmonic(4, 3.0), 1.177662037037037037037);
assert_eq!(super::gen_harmonic(4, f64::INFINITY), 1.0);
assert_eq!(super::gen_harmonic(4, f64::NEG_INFINITY), f64::INFINITY);
}
}