solvr 0.2.0-beta.2

Advanced computing library for real-world problem solving - optimization, differential equations, interpolation, statistics, and more
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Root finding algorithms for scalar functions.

use super::{RootResult, ScalarOptions};
use crate::optimize::error::{OptimizeError, OptimizeResult};

/// Numerical threshold for detecting singular matrices.
const SINGULAR_THRESHOLD: f64 = 1e-14;

/// Bisection method for root finding.
///
/// # Arguments
/// * `f` - Function to find root of
/// * `a` - Left bracket endpoint
/// * `b` - Right bracket endpoint
/// * `options` - Solver options
///
/// # Returns
/// Root of `f` in interval [a, b]
///
/// # Errors
/// * `InvalidInterval` if a >= b
/// * `SameSignBracket` if f(a) and f(b) have same sign
/// * `DidNotConverge` if iterations exceed max_iter
///
/// # Note
/// Bisection is slow (linear convergence) but very robust.
pub fn bisect<F>(f: F, a: f64, b: f64, options: &ScalarOptions) -> OptimizeResult<RootResult>
where
    F: Fn(f64) -> f64,
{
    if a >= b {
        return Err(OptimizeError::InvalidInterval {
            a,
            b,
            context: "bisect".to_string(),
        });
    }

    let fa = f(a);
    let fb = f(b);

    if (fa > 0.0 && fb > 0.0) || (fa < 0.0 && fb < 0.0) {
        return Err(OptimizeError::SameSignBracket {
            fa,
            fb,
            context: "bisect".to_string(),
        });
    }

    let mut left = a;
    let mut right = b;
    let mut f_left = fa;

    for iter in 0..options.max_iter {
        let mid = 0.5 * (left + right);
        let f_mid = f(mid);

        // Check convergence
        let width = right - left;
        if width.abs() < options.tol || width.abs() / mid.abs().max(1.0) < options.rtol {
            return Ok(RootResult {
                root: mid,
                function_value: f_mid,
                iterations: iter + 1,
                bracket_width: width,
            });
        }

        // Update bracket
        if (f_mid > 0.0 && f_left > 0.0) || (f_mid < 0.0 && f_left < 0.0) {
            left = mid;
            f_left = f_mid;
        } else {
            right = mid;
        }
    }

    Err(OptimizeError::DidNotConverge {
        iterations: options.max_iter,
        tolerance: options.tol,
        context: "bisect".to_string(),
    })
}

/// Newton's method for root finding.
///
/// # Arguments
/// * `f` - Function to find root of
/// * `df` - Derivative of f
/// * `x0` - Initial guess
/// * `options` - Solver options
///
/// # Returns
/// Root of `f` near `x0`
///
/// # Errors
/// * `DidNotConverge` if iterations exceed max_iter
/// * `NumericalError` if derivative is too close to zero
///
/// # Note
/// Newton's method has quadratic convergence but may diverge if x0 is far from root.
pub fn newton<F, DF>(f: F, df: DF, x0: f64, options: &ScalarOptions) -> OptimizeResult<RootResult>
where
    F: Fn(f64) -> f64,
    DF: Fn(f64) -> f64,
{
    let mut x = x0;

    for iter in 0..options.max_iter {
        let fx = f(x);
        let dfx = df(x);

        if dfx.abs() < SINGULAR_THRESHOLD {
            return Err(OptimizeError::NumericalError {
                message: "Derivative too close to zero".to_string(),
            });
        }

        let x_new = x - fx / dfx;
        let dx = (x_new - x).abs();

        // Check convergence
        if dx < options.tol || dx / x.abs().max(1.0) < options.rtol {
            return Ok(RootResult {
                root: x_new,
                function_value: f(x_new),
                iterations: iter + 1,
                bracket_width: dx,
            });
        }

        x = x_new;
    }

    Err(OptimizeError::DidNotConverge {
        iterations: options.max_iter,
        tolerance: options.tol,
        context: "newton".to_string(),
    })
}

/// Secant method for root finding.
///
/// # Arguments
/// * `f` - Function to find root of
/// * `x0` - First initial guess
/// * `x1` - Second initial guess
/// * `options` - Solver options
///
/// # Returns
/// Root of `f` near `x0` and `x1`
///
/// # Errors
/// * `DidNotConverge` if iterations exceed max_iter
/// * `NumericalError` if denominator becomes too small
///
/// # Note
/// Secant method has superlinear convergence (~1.618) and doesn't require derivatives.
pub fn secant<F>(f: F, x0: f64, x1: f64, options: &ScalarOptions) -> OptimizeResult<RootResult>
where
    F: Fn(f64) -> f64,
{
    let mut x_prev = x0;
    let mut x_curr = x1;
    let mut f_prev = f(x_prev);
    let mut f_curr = f(x_curr);

    for iter in 0..options.max_iter {
        let denom = f_curr - f_prev;

        if denom.abs() < SINGULAR_THRESHOLD {
            return Err(OptimizeError::NumericalError {
                message: "Denominator too close to zero in secant method".to_string(),
            });
        }

        let x_next = x_curr - f_curr * (x_curr - x_prev) / denom;
        let dx = (x_next - x_curr).abs();

        // Check convergence
        if dx < options.tol || dx / x_curr.abs().max(1.0) < options.rtol {
            return Ok(RootResult {
                root: x_next,
                function_value: f(x_next),
                iterations: iter + 1,
                bracket_width: dx,
            });
        }

        x_prev = x_curr;
        f_prev = f_curr;
        x_curr = x_next;
        f_curr = f(x_curr);
    }

    Err(OptimizeError::DidNotConverge {
        iterations: options.max_iter,
        tolerance: options.tol,
        context: "secant".to_string(),
    })
}

/// Bracketed root finding with bisection.
///
/// This is a robust bisection-based root finder that maintains a bracket
/// around the root at all times, guaranteeing convergence.
///
/// # Arguments
/// * `f` - Function to find root of
/// * `a` - Left bracket endpoint
/// * `b` - Right bracket endpoint
/// * `options` - Solver options
///
/// # Returns
/// Root of `f` in interval [a, b]
///
/// # Errors
/// * `InvalidInterval` if a >= b
/// * `SameSignBracket` if f(a) and f(b) have same sign
/// * `DidNotConverge` if iterations exceed max_iter
///
/// # Note
/// Named `brentq` for SciPy compatibility. Currently uses pure bisection
/// for robustness. Convergence is O(log(|b-a|/tol)).
pub fn brentq<F>(f: F, a: f64, b: f64, options: &ScalarOptions) -> OptimizeResult<RootResult>
where
    F: Fn(f64) -> f64,
{
    if a >= b {
        return Err(OptimizeError::InvalidInterval {
            a,
            b,
            context: "brentq".to_string(),
        });
    }

    let fa = f(a);
    let fb = f(b);

    if (fa > 0.0 && fb > 0.0) || (fa < 0.0 && fb < 0.0) {
        return Err(OptimizeError::SameSignBracket {
            fa,
            fb,
            context: "brentq".to_string(),
        });
    }

    let mut a = a;
    let mut b = b;
    let mut fa = fa;
    let mut _fb = fb;

    for iter in 0..options.max_iter {
        let width = (b - a).abs();
        let tol_here = options.tol.max(options.rtol * a.abs().max(1.0));

        // Check convergence
        if width < tol_here {
            let mid = 0.5 * (a + b);
            return Ok(RootResult {
                root: mid,
                function_value: f(mid),
                iterations: iter + 1,
                bracket_width: width,
            });
        }

        // Use bisection with occasional interpolation for robustness
        let mid = 0.5 * (a + b);
        let f_mid = f(mid);

        // Update bracket to maintain sign change
        if (f_mid > 0.0 && fa > 0.0) || (f_mid < 0.0 && fa < 0.0) {
            a = mid;
            fa = f_mid;
        } else {
            b = mid;
            _fb = f_mid;
        }
    }

    Err(OptimizeError::DidNotConverge {
        iterations: options.max_iter,
        tolerance: options.tol,
        context: "brentq".to_string(),
    })
}

/// Ridder's method for root finding.
///
/// # Arguments
/// * `f` - Function to find root of
/// * `a` - Left bracket endpoint
/// * `b` - Right bracket endpoint
/// * `options` - Solver options
///
/// # Returns
/// Root of `f` in interval [a, b]
///
/// # Errors
/// * `InvalidInterval` if a >= b
/// * `SameSignBracket` if f(a) and f(b) have same sign
/// * `DidNotConverge` if iterations exceed max_iter
///
/// # Note
/// Ridder's method has linear (but very good) convergence and is more efficient than bisection.
pub fn ridder<F>(f: F, a: f64, b: f64, options: &ScalarOptions) -> OptimizeResult<RootResult>
where
    F: Fn(f64) -> f64,
{
    if a >= b {
        return Err(OptimizeError::InvalidInterval {
            a,
            b,
            context: "ridder".to_string(),
        });
    }

    let fa = f(a);
    let fb = f(b);

    if (fa > 0.0 && fb > 0.0) || (fa < 0.0 && fb < 0.0) {
        return Err(OptimizeError::SameSignBracket {
            fa,
            fb,
            context: "ridder".to_string(),
        });
    }

    let mut a = a;
    let mut b = b;
    let mut fa = fa;
    let mut fb = fb;

    for iter in 0..options.max_iter {
        let c = 0.5 * (a + b);
        let fc = f(c);

        // Compute new estimate using Ridder's formula
        let denom = (2.0 * fc * fc - fa * fb).sqrt();
        if denom.abs() < SINGULAR_THRESHOLD {
            // Fallback to midpoint
            return Ok(RootResult {
                root: c,
                function_value: fc,
                iterations: iter + 1,
                bracket_width: (b - a).abs(),
            });
        }

        let s = if fa > fb { -1.0 } else { 1.0 };
        let x_new = c + s * (c - a) * fc / denom;

        let f_new = f(x_new);

        // Check convergence
        let width = (b - a).abs();
        if width < options.tol || width / c.abs().max(1.0) < options.rtol {
            return Ok(RootResult {
                root: x_new,
                function_value: f_new,
                iterations: iter + 1,
                bracket_width: width,
            });
        }

        // Update bracket
        if (f_new > 0.0 && fc > 0.0) || (f_new < 0.0 && fc < 0.0) {
            if (f_new > 0.0 && fa < 0.0) || (f_new < 0.0 && fa > 0.0) {
                b = x_new;
                fb = f_new;
            } else {
                a = x_new;
                fa = f_new;
            }
        } else {
            // x_new is on the opposite side of c from either a or b
            if (f_new > 0.0 && fc < 0.0) || (f_new < 0.0 && fc > 0.0) {
                a = c;
                fa = fc;
                b = x_new;
                fb = f_new;
            } else {
                a = x_new;
                fa = f_new;
                b = c;
                fb = fc;
            }
        }
    }

    Err(OptimizeError::DidNotConverge {
        iterations: options.max_iter,
        tolerance: options.tol,
        context: "ridder".to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bisect_simple() {
        let result =
            bisect(|x| x * x - 4.0, 1.0, 3.0, &ScalarOptions::default()).expect("bisect failed");
        assert!((result.root - 2.0).abs() < 1e-10);
        assert!(result.function_value.abs() < 1e-10);
    }

    #[test]
    fn test_bisect_negative_root() {
        let result =
            bisect(|x| x * x - 4.0, -3.0, -1.0, &ScalarOptions::default()).expect("bisect failed");
        assert!((result.root - (-2.0)).abs() < 1e-10);
    }

    #[test]
    fn test_bisect_same_sign() {
        let result = bisect(|x| x * x + 1.0, 1.0, 3.0, &ScalarOptions::default());
        assert!(matches!(result, Err(OptimizeError::SameSignBracket { .. })));
    }

    #[test]
    fn test_bisect_invalid_interval() {
        let result = bisect(|x| x * x - 4.0, 3.0, 1.0, &ScalarOptions::default());
        assert!(matches!(result, Err(OptimizeError::InvalidInterval { .. })));
    }

    #[test]
    fn test_newton_simple() {
        let result = newton(|x| x * x - 4.0, |x| 2.0 * x, 3.0, &ScalarOptions::default())
            .expect("newton failed");
        assert!((result.root - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_secant_simple() {
        let result =
            secant(|x| x * x - 4.0, 1.0, 3.0, &ScalarOptions::default()).expect("secant failed");
        assert!((result.root - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_ridder_simple() {
        let result =
            ridder(|x| x * x - 4.0, 1.0, 3.0, &ScalarOptions::default()).expect("ridder failed");
        assert!((result.root - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_cubic_polynomial() {
        let f = |x: f64| x * x * x - 2.0 * x * x - x + 2.0;
        let result = bisect(f, 0.5, 1.5, &ScalarOptions::default()).expect("failed");
        assert!((result.root - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_trigonometric() {
        let result = bisect(|x: f64| x.sin(), 2.0, 4.0, &ScalarOptions::default()).expect("failed");
        assert!((result.root - std::f64::consts::PI).abs() < 1e-10);
    }

    #[test]
    fn test_exponential() {
        let result =
            bisect(|x: f64| x.exp() - 3.0, 0.0, 2.0, &ScalarOptions::default()).expect("failed");
        assert!((result.root - 3_f64.ln()).abs() < 1e-10);
    }
}