-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathroot.rs
163 lines (142 loc) · 3.78 KB
/
root.rs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use anyhow::Result;
use peroxide::fuga::*;
#[test]
fn test_cubic_root() -> Result<()> {
let problem = Cubic;
let bisect = BisectionMethod {
max_iter: 100,
tol: 1e-6,
};
let newton = NewtonMethod {
max_iter: 100,
tol: 1e-6,
};
let false_pos = FalsePositionMethod {
max_iter: 100,
tol: 1e-6,
};
let root_bisect = bisect.find(&problem)?;
let root_newton = newton.find(&problem)?;
let root_false_pos = false_pos.find(&problem)?;
let result_bisect = problem.eval(root_bisect)?[0];
let result_newton = problem.eval(root_newton)?[0];
let result_false_pos = problem.eval(root_false_pos)?[0];
assert!(result_bisect.abs() < 1e-6);
assert!(result_newton.abs() < 1e-6);
assert!(result_false_pos.abs() < 1e-6);
Ok(())
}
struct Cubic;
impl Cubic {
fn eval(&self, x: [f64; 1]) -> Result<[f64; 1]> {
Ok([(x[0] - 1f64).powi(3)])
}
}
impl RootFindingProblem<1, 1, (f64, f64)> for Cubic {
fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
self.eval(x)
}
fn initial_guess(&self) -> (f64, f64) {
(0.0, 2.0)
}
}
impl RootFindingProblem<1, 1, f64> for Cubic {
fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
self.eval(x)
}
fn initial_guess(&self) -> f64 {
0.0
}
fn derivative(&self, x: [f64; 1]) -> Result<Jaco<1, 1>> {
Ok([[3.0 * (x[0] - 1f64).powi(2)]])
}
}
#[test]
fn test_sine_root() -> Result<()> {
let problem = Sine;
let bisect = BisectionMethod {
max_iter: 100,
tol: 1e-6,
};
let newton = NewtonMethod {
max_iter: 100,
tol: 1e-6,
};
let false_pos = FalsePositionMethod {
max_iter: 100,
tol: 1e-6,
};
let root_bisect = bisect.find(&problem)?;
let root_newton = newton.find(&problem)?;
let root_false_pos = false_pos.find(&problem)?;
let result_bisect = problem.eval(root_bisect)?[0];
let result_newton = problem.eval(root_newton)?[0];
let result_false_pos = problem.eval(root_false_pos)?[0];
assert!(result_bisect.abs() < 1e-6);
assert!(result_newton.abs() < 1e-6);
assert!(result_false_pos.abs() < 1e-6);
Ok(())
}
struct Sine;
impl Sine {
fn eval(&self, x: [f64; 1]) -> Result<[f64; 1]> {
Ok([x[0].sin()])
}
}
impl RootFindingProblem<1, 1, (f64, f64)> for Sine {
fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
self.eval(x)
}
fn initial_guess(&self) -> (f64, f64) {
(0.0, 2.0)
}
}
impl RootFindingProblem<1, 1, f64> for Sine {
fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
self.eval(x)
}
fn initial_guess(&self) -> f64 {
1.0
}
fn derivative(&self, x: [f64; 1]) -> Result<Jaco<1, 1>> {
Ok([[x[0].cos()]])
}
}
#[test]
fn test_cosine_root() -> Result<()> {
let problem = Cosine;
let newton = NewtonMethod {
max_iter: 100,
tol: 1e-6,
};
let root_newton = match newton.find(&problem) {
Ok(x) => x,
Err(e) => {
println!("{:?}", e);
match e.downcast::<RootError<1>>() {
Ok(RootError::ZeroDerivative(x)) => x,
Ok(e) => panic!("ok but {:?}", e),
Err(e) => panic!("err {:?}", e),
}
}
};
assert_eq!(root_newton[0], 0.0);
Ok(())
}
struct Cosine;
impl Cosine {
fn eval(&self, x: [f64; 1]) -> Result<[f64; 1]> {
Ok([x[0].cos()])
}
}
impl RootFindingProblem<1, 1, f64> for Cosine {
fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
self.eval(x)
}
fn initial_guess(&self) -> f64 {
0.0 // should fail in newton (derivative is 0)
}
fn derivative(&self, x: [f64; 1]) -> Result<Jaco<1, 1>> {
Ok([[-x[0].sin()]])
}
}