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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#![warn(missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts, trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces, unused_qualifications)]
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
#[allow(missing_docs)]
pub mod errors {
error_chain! {
errors {
ValueLimit
IterLimit(limit: usize) {
description("Max iteration limit exceeded")
display("Could not converge function after {} iterations", limit)
}
}
}
}
use errors::*;
pub fn fixedpoint<T, U>(func: &Fn(U, &T) -> U, x0: U, args: &T, maxiter: Option<usize>, maxval: Option<U>) -> Result<U>
where U: PartialEq + PartialOrd + Copy + std::fmt::Debug {
let maxiter = maxiter.unwrap_or(100);
let mut itr = maxiter;
let mut x = x0;
let mut val = func(x0, args);
while val != x {
trace!("Iteration: {}", maxiter - itr);
x = val;
val = func(x, args);
trace!("\tx: {:?}; F(x): {:?}", x, val);
itr -= 1;
if itr == 0 {
debug!("Max Iterations reached. Last value: {:?}", val);
bail!(ErrorKind::IterLimit(maxiter));
} else if val > maxval.unwrap_or(val) {
debug!("Maximum value of function reached. Last value: {:?}", val);
bail!(ErrorKind::ValueLimit);
}
};
Ok(val)
}
#[cfg(test)]
mod tests {
use fixedpoint;
fn func_with_fixed_point(num: u32, param: &u32) -> u32 {
150 + (((num as f32 / param.clone() as f32).ceil() as u32)*100)
}
#[test]
fn basic_tests() {
assert_eq!(fixedpoint(&func_with_fixed_point, 0, &150, None, None).unwrap(), 450);
assert_eq!(fixedpoint(&func_with_fixed_point, 0, &150, None, Some(400)).is_err(), true);
assert_eq!(fixedpoint(&func_with_fixed_point, 0, &10, Some(5), None).is_err(), true);
}
#[test]
#[should_panic(expected = "attempt to multiply with overflow")]
fn maxiter() {
fixedpoint(&func_with_fixed_point, 0, &10, Some(10), None).unwrap_or(0);
}
}