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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use alga::general::{ComplexField, RealField};
use nalgebra::{allocator::Allocator, DefaultAllocator, DimName, VectorN};
mod polynomial;
pub use polynomial::*;
pub fn bisection<N: RealField, F: FnMut(N) -> N>(
(mut left, mut right): (N, N),
mut f: F,
tol: N,
n_max: usize,
) -> Result<N, String> {
if left >= right {
return Err("Bisection: requirement: right > left".to_owned());
}
let mut n = 1;
let mut f_a = f(left);
if (f_a * f(right)).is_sign_positive() {
return Err("Bisection: requirement: Signs must be different".to_owned());
}
let half = N::from_f64(0.5).unwrap();
let mut half_interval = (left - right) * half;
let mut middle = left + half_interval;
if middle.abs() <= tol {
return Ok(middle);
}
while n <= n_max {
let f_p = f(middle);
if (f_p * f_a).is_sign_positive() {
left = middle;
f_a = f_p;
} else {
right = middle;
}
half_interval = (right - left) * half;
let middle_new = left + half_interval;
if (middle - middle_new).abs() / middle.abs() < tol || middle_new.abs() < tol {
return Ok(middle_new);
}
middle = middle_new;
n += 1;
}
Err("Bisection: Maximum iterations exceeded".to_owned())
}
pub fn steffensen<N: RealField + From<f64> + Copy>(
mut initial: N,
f: fn(N) -> N,
tol: N,
n_max: usize,
) -> Result<N, String> {
let mut n = 0;
while n < n_max {
let guess = f(initial);
let new_guess = f(guess);
let diff =
initial - (guess - initial).powi(2) / (new_guess - N::from(2.0) * guess + initial);
if (diff - initial).abs() <= tol {
return Ok(diff);
}
initial = diff;
n += 1;
}
Err("Steffensen: Maximum number of iterations exceeded".to_owned())
}
pub fn newton<
N: ComplexField,
S: DimName,
F: FnMut(&[N]) -> VectorN<N, S>,
G: FnMut(&[N]) -> VectorN<N, S>,
>(
initial: &[N],
mut f: F,
mut f_deriv: G,
tol: <N as ComplexField>::RealField,
n_max: usize,
) -> Result<VectorN<N, S>, String>
where
DefaultAllocator: Allocator<N, S>,
{
let mut guess = VectorN::from_column_slice(initial);
let mut norm = guess.dot(&guess).sqrt().abs();
let mut n = 0;
if norm <= tol {
return Ok(guess);
}
while n < n_max {
let f_val = f(guess.as_slice());
let f_deriv_val = f_deriv(guess.as_slice());
let adjustment = VectorN::from_iterator(
f_val
.column(0)
.iter()
.zip(f_deriv_val.column(0).iter())
.map(|(f, f_d)| *f / *f_d),
);
let new_guess = &guess - adjustment;
let new_norm = new_guess.dot(&new_guess).sqrt().abs();
if ((norm - new_norm) / norm).abs() <= tol || new_norm <= tol {
return Ok(new_guess);
}
norm = new_norm;
guess = new_guess;
n += 1;
}
Err("Newton: Maximum iterations exceeded".to_owned())
}
pub fn secant<N: ComplexField, S: DimName, F: FnMut(&[N]) -> VectorN<N, S>>(
initial: (&[N], &[N]),
mut f: F,
tol: <N as ComplexField>::RealField,
n_max: usize,
) -> Result<VectorN<N, S>, String>
where
DefaultAllocator: Allocator<N, S>,
{
let mut n = 0;
let mut left = VectorN::from_column_slice(initial.0);
let mut right = VectorN::from_column_slice(initial.1);
let mut left_val = f(initial.0);
let mut right_val = f(initial.1);
let mut norm = right.dot(&right).sqrt().abs();
if norm <= tol {
return Ok(right);
}
while n <= n_max {
let adjustment = VectorN::from_iterator(right_val.iter().enumerate().map(|(i, q)| {
*q * (*right.get(i).unwrap() - *left.get(i).unwrap()) / (*q - *left_val.get(i).unwrap())
}));
let new_guess = &right - adjustment;
let new_norm = new_guess.dot(&new_guess).sqrt().abs();
if ((norm - new_norm) / norm).abs() <= tol || new_norm <= tol {
return Ok(new_guess);
}
norm = new_norm;
left_val = right_val;
left = right;
right = new_guess;
right_val = f(right.as_slice());
n += 1;
}
Err("Secant: Maximum iterations exceeded".to_owned())
}