russell_lab 1.13.0

Scientific laboratory for linear algebra and numerical mathematics
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use crate::StrError;
use crate::{mat_eigenvalues, InterpChebyshev, TOL_RANGE};
use crate::{Matrix, Vector};

/// Implements root finding algorithms
pub struct RootFinder {
    /// Holds the tolerance to avoid division by zero with the trailing Chebyshev coefficient
    ///
    /// Default = 1e-13
    pub tol_zero_an: f64,

    /// Holds the tolerance to discard roots with imaginary part
    ///
    /// Accepts only roots such that `abs(Im(root)) < tol_abs_imaginary
    ///
    /// Default = 1e-7
    pub tol_abs_imaginary: f64,

    /// Holds the tolerance to discard roots outside the boundaries
    ///
    /// Accepts only roots such that `abs(Re(root)) <= 1 + tol_abs_range`
    ///
    /// Default = [TOL_RANGE] / 10.0
    ///
    /// The root will then be moved back to the lower or upper bound
    pub tol_abs_boundary: f64,

    /// Holds the tolerance to stop Newton's iterations when dx ~ 0
    ///
    /// Default = 1e-13
    pub newton_tol_zero_dx: f64,

    /// Holds the tolerance to stop Newton's iterations when f(x) ~ 0
    ///
    /// Default = 1e-13
    pub newton_tol_zero_fx: f64,

    /// Holds the maximum number of iterations for the Newton refinement/polishing
    ///
    /// Default = 15
    pub newton_max_iterations: usize,

    /// Max number of iterations for Brent's method
    ///
    /// Default = 100
    pub brent_max_iterations: usize,

    /// Tolerance for Brent's method
    ///
    /// Default = 1e-13
    pub brent_tolerance: f64,

    /// Stepsize for one-sided differences
    h_osd: f64,

    /// Stepsize for central differences
    h_cen: f64,
}

impl RootFinder {
    /// Allocates a new instance
    pub fn new() -> Self {
        RootFinder {
            tol_zero_an: 1e-13,
            tol_abs_imaginary: 1.0e-7,
            tol_abs_boundary: TOL_RANGE / 10.0,
            newton_tol_zero_dx: 1e-13,
            newton_tol_zero_fx: 1e-13,
            newton_max_iterations: 15,
            brent_max_iterations: 100,
            brent_tolerance: 1e-13,
            h_osd: f64::powf(f64::EPSILON, 1.0 / 2.0),
            h_cen: f64::powf(f64::EPSILON, 1.0 / 3.0),
        }
    }

    /// Find all roots in the interval using Chebyshev interpolation
    ///
    /// # Input
    ///
    /// * `interp` -- The Chebyshev-Gauss-Lobatto interpolant with the data vector U
    ///    already computed. The interpolant must have the same degree N as this struct.
    ///
    /// # Output
    ///
    /// Returns a sorted list (from xa to xb) with the roots.
    ///
    /// # Warnings
    ///
    /// 1. It is essential that the interpolant best approximates the data/function;
    ///    otherwise, not all roots can be found.
    ///
    /// # Method
    ///
    /// The roots are the eigenvalues of the companion matrix as explained in the references.
    ///
    /// # References
    ///
    /// 1. Boyd JP (2002) Computing zeros on a real interval through Chebyshev expansion
    ///    and polynomial rootfinding, SIAM Journal of Numerical Analysis, 40(5):1666-1682
    /// 2. Boyd JP (2013) Finding the zeros of a univariate equation: proxy rootfinders,
    ///    Chebyshev interpolation, and the companion matrix, SIAM Journal of Numerical
    ///    Analysis, 55(2):375-396.
    /// 3. Boyd JP (2014) Solving Transcendental Equations: The Chebyshev Polynomial Proxy
    ///    and Other Numerical Rootfinders, Perturbation Series, and Oracles, SIAM, pp460
    ///
    /// # Example
    ///
    /// ```
    /// use russell_lab::*;
    ///
    /// fn main() -> Result<(), StrError> {
    ///     // function
    ///     let f = |x, _: &mut NoArgs| Ok(x * x - 1.0);
    ///     let (xa, xb) = (-2.0, 2.0);
    ///     let args = &mut 0;
    ///
    ///     // interpolant
    ///     let nn = 2;
    ///     let mut interp = InterpChebyshev::new(nn, xa, xb)?;
    ///     interp.set_function(nn, args, f)?;
    ///
    ///     // find all roots in the interval
    ///     let mut solver = RootFinder::new();
    ///     let roots = solver.chebyshev(&interp)?;
    ///     array_approx_eq(&roots, &[-1.0, 1.0], 1e-15);
    ///     Ok(())
    /// }
    /// ```
    pub fn chebyshev(&self, interp: &InterpChebyshev) -> Result<Vec<f64>, StrError> {
        // check
        if !interp.is_ready() {
            return Err("the interpolant must initialized first");
        }

        // handle constant function
        let nn = interp.get_degree();
        if nn == 0 {
            return Ok(Vec::new());
        }

        // expansion coefficients
        let a = interp.get_coefficients();
        let an = a[nn];
        if f64::abs(an) < self.tol_zero_an {
            return Err("the trailing Chebyshev coefficient vanishes; try a smaller degree N");
        }

        // handle linear function
        let (xa, xb, dx) = interp.get_range();
        if nn == 1 {
            let z = -a[0] / a[1];
            if f64::abs(z) <= 1.0 + self.tol_abs_boundary {
                let root = (xb + xa + dx * z) / 2.0;
                return Ok(vec![root]);
            } else {
                return Ok(Vec::new());
            }
        }

        // companion matrix
        let mut aa = Matrix::new(nn, nn);
        aa.set(0, 1, 1.0);
        for r in 1..(nn - 1) {
            aa.set(r, r + 1, 0.5); // upper diagonal
            aa.set(r, r - 1, 0.5); // lower diagonal
        }
        for k in 0..nn {
            aa.set(nn - 1, k, -0.5 * a[k] / an);
        }
        aa.add(nn - 1, nn - 2, 0.5);

        // eigenvalues
        let mut l_real = Vector::new(nn);
        let mut l_imag = Vector::new(nn);
        mat_eigenvalues(&mut l_real, &mut l_imag, &mut aa).unwrap();

        // roots = real eigenvalues within the interval
        let mut roots = Vec::new();
        for i in 0..nn {
            if f64::abs(l_imag[i]) < self.tol_abs_imaginary {
                if f64::abs(l_real[i]) <= 1.0 + self.tol_abs_boundary {
                    let x = (xb + xa + dx * l_real[i]) / 2.0;
                    roots.push(f64::max(xa, f64::min(xb, x)));
                }
            }
        }

        // sort roots
        if roots.len() > 0 {
            roots.sort_by(|a, b| a.partial_cmp(b).unwrap());
        }
        Ok(roots)
    }

    /// Refines the roots using Newton's method
    ///
    /// # Examples
    ///
    /// ```
    /// use russell_lab::*;
    ///
    /// fn main() -> Result<(), StrError> {
    ///     // function
    ///     let f = |x, _: &mut NoArgs| Ok(x * x * x * x - 1.0);
    ///     let (xa, xb) = (-2.0, 2.0);
    ///     let args = &mut 0;
    ///
    ///     // interpolant
    ///     let nn = 2;
    ///     let mut interp = InterpChebyshev::new(nn, xa, xb)?;
    ///     interp.set_function(nn, args, f)?;
    ///
    ///     // find all roots in the interval
    ///     let mut solver = RootFinder::new();
    ///     let mut roots = solver.chebyshev(&interp)?;
    ///     array_approx_eq(&roots, &[-0.5, 0.5], 1e-15); // inaccurate
    ///
    ///     // refine/polish the roots
    ///     solver.refine(&mut roots, xa, xb, args, f)?;
    ///     array_approx_eq(&roots, &[-1.0, 1.0], 1e-15); // accurate
    ///     Ok(())
    /// }
    /// ```
    pub fn refine<F, A>(&self, roots: &mut [f64], xa: f64, xb: f64, args: &mut A, mut f: F) -> Result<(), StrError>
    where
        F: FnMut(f64, &mut A) -> Result<f64, StrError>,
    {
        // check
        let nr = roots.len();
        if nr < 1 {
            return Err("at least one root is required");
        }

        // Newton's method with approximate Jacobian
        let h_cen_2 = self.h_cen * 2.0;
        for r in 0..nr {
            let mut x = roots[r];
            let mut converged = false;
            for _ in 0..self.newton_max_iterations {
                // check convergence on f(x)
                let fx = f(x, args)?;
                if f64::abs(fx) < self.newton_tol_zero_fx {
                    converged = true;
                    break;
                }

                // calculate Jacobian
                let dfdx = if x - self.h_cen <= xa {
                    // forward difference
                    (f(x + self.h_osd, args)? - f(x, args)?) / self.h_osd
                } else if x + self.h_cen >= xb {
                    // backward difference
                    (f(x, args)? - f(x - self.h_osd, args)?) / self.h_osd
                } else {
                    // central difference
                    (f(x + self.h_cen, args)? - f(x - self.h_cen, args)?) / h_cen_2
                };

                // skip zero Jacobian
                if f64::abs(dfdx) < self.newton_tol_zero_fx {
                    converged = true;
                    break;
                }

                // update x
                let dx = -f(x, args)? / dfdx;
                if f64::abs(dx) < self.newton_tol_zero_dx {
                    converged = true;
                    break;
                }
                x += dx;
            }
            if !converged {
                return Err("Newton's method did not converge");
            }
            roots[r] = x;
        }
        Ok(())
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::RootFinder;
    use crate::algo::NoArgs;
    use crate::InterpChebyshev;
    use crate::{approx_eq, array_approx_eq, get_test_functions};

    #[allow(unused)]
    use crate::{StrError, Vector};

    #[allow(unused)]
    use plotpy::{Curve, Legend, Plot};

    /*
    fn graph<F, A>(
        name: &str,
        interp: &InterpChebyshev,
        roots: &[f64],
        roots_refined: &[f64],
        args: &mut A,
        mut f: F,
        nstation: usize,
        fig_width: f64,
    ) where
        F: FnMut(f64, &mut A) -> Result<f64, StrError>,
    {
        let (xa, xb, _) = interp.get_range();
        let xx = Vector::linspace(xa, xb, nstation).unwrap();
        let yy_ana = xx.get_mapped(|x| f(x, args).unwrap());
        let yy_int = xx.get_mapped(|x| interp.eval(x).unwrap());
        let mut curve_ana = Curve::new();
        let mut curve_int = Curve::new();
        let mut zeros = Curve::new();
        let mut zeros_refined = Curve::new();
        curve_ana.set_label("analytical");
        curve_int
            .set_label("interpolated")
            .set_line_style("--")
            .set_marker_style(".")
            .set_marker_every(5);
        zeros
            .set_marker_style("o")
            .set_marker_void(true)
            .set_marker_line_color("#00760F")
            .set_line_style("None");
        zeros_refined
            .set_marker_style("s")
            .set_marker_size(10.0)
            .set_marker_void(true)
            .set_marker_line_color("#00760F")
            .set_line_style("None");
        for root in roots {
            zeros.draw(&[*root], &[interp.eval(*root).unwrap()]);
        }
        for root in roots_refined {
            zeros_refined.draw(&[*root], &[f(*root, args).unwrap()]);
        }
        curve_int.draw(xx.as_data(), yy_int.as_data());
        curve_ana.draw(xx.as_data(), yy_ana.as_data());
        let mut plot = Plot::new();
        let mut legend = Legend::new();
        legend.set_num_col(2);
        legend.set_outside(true);
        legend.draw();
        plot.add(&curve_ana)
            .add(&curve_int)
            .add(&zeros)
            .add(&zeros_refined)
            .add(&legend)
            .set_cross(0.0, 0.0, "gray", "-", 1.5)
            .grid_and_labels("x", "f(x)")
            .set_figure_size_points(fig_width, 500.0)
            .save(&format!("/tmp/russell_lab/{}.svg", name))
            .unwrap();
    }
    */

    #[test]
    fn find_captures_errors() {
        let (xa, xb) = (-4.0, 4.0);
        let nn = 2;
        let interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        let solver = RootFinder::new();
        assert_eq!(
            solver.chebyshev(&interp).err(),
            Some("the interpolant must initialized first")
        );
    }

    #[test]
    fn find_captures_trailing_zero_error() {
        let f = |x, _: &mut NoArgs| Ok(x * x - 1.0);
        let (xa, xb) = (-4.0, 4.0);
        let nn = 3;
        let args = &mut 0;
        let mut interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        interp.set_function(nn, args, f).unwrap();
        let solver = RootFinder::new();
        assert_eq!(
            solver.chebyshev(&interp).err(),
            Some("the trailing Chebyshev coefficient vanishes; try a smaller degree N")
        );
    }

    #[test]
    fn find_works_parabola() {
        // function
        let f = |x, _: &mut NoArgs| Ok(x * x - 1.0);
        let (xa, xb) = (-4.0, 4.0);

        // interpolant
        let nn = 2;
        let args = &mut 0;
        let mut interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        interp.set_function(nn, args, f).unwrap();

        // find roots
        let solver = RootFinder::new();
        let roots = solver.chebyshev(&interp).unwrap();
        let mut roots_refined = roots.clone();
        solver.refine(&mut roots_refined, xa, xb, args, f).unwrap();
        println!("n_roots = {}", roots_refined.len());
        println!("roots = {:?}", roots);
        println!("roots_refined = {:?}", roots_refined);

        // figure
        /*
        graph(
            "test_root_finding_chebyshev_parabola",
            &interp,
            &roots,
            &roots_refined,
            args,
            f,
            101,
            600.0,
        );
        */

        // check
        array_approx_eq(&roots_refined, &[-1.0, 1.0], 1e-14);
    }

    #[test]
    fn find_works_parabola_mult2() {
        // solution: x = 0.0 with multiplicity 2

        // function
        let f = |x, _: &mut NoArgs| Ok(x * x);
        let (xa, xb) = (-4.0, 4.0);

        // interpolant
        let nn = 2;
        let args = &mut 0;
        let mut interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        interp.set_function(nn, args, f).unwrap();

        // find roots
        let solver = RootFinder::new();
        let roots = solver.chebyshev(&interp).unwrap();
        let mut roots_refined = roots.clone();
        solver.refine(&mut roots_refined, xa, xb, args, f).unwrap();
        println!("n_roots = {}", roots_refined.len());
        println!("roots = {:?}", roots);
        println!("roots_refined = {:?}", roots_refined);

        // figure
        /*
        graph(
            "test_root_finding_chebyshev_parabola_mult2",
            &interp,
            &roots,
            &roots_refined,
            args,
            f,
            101,
            600.0,
        );
        */

        // check
        array_approx_eq(&roots_refined, &[0.0, 0.0], 1e-14);
    }

    #[test]
    fn find_works_multiplicity2() {
        // function
        let f = |x, _: &mut NoArgs| Ok((x + 4.0) * (x - 1.0) * (x - 1.0));
        let (xa, xb) = (-5.0, 5.0);

        // interpolant
        let nn = 3;
        let args = &mut 0;
        let mut interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        interp.set_function(nn, args, f).unwrap();

        // find roots
        let solver = RootFinder::new();
        let roots = solver.chebyshev(&interp).unwrap();
        let mut roots_refined = roots.clone();
        solver.refine(&mut roots_refined, xa, xb, args, f).unwrap();
        println!("n_roots = {}", roots_refined.len());
        println!("roots = {:?}", roots);
        println!("roots_refined = {:?}", roots_refined);

        // figure
        /*
        graph(
            "test_root_finding_chebyshev_multiplicity2",
            &interp,
            &roots,
            &roots_refined,
            args,
            f,
            101,
            600.0,
        );
        */

        // check
        array_approx_eq(&roots_refined, &[-4.0, 1.0, 1.0], 1e-14);
    }

    #[test]
    fn refine_captures_errors() {
        let f = |_, _: &mut NoArgs| Ok(0.0);
        let args = &mut 0;
        let _ = f(0.0, args);
        let (xa, xb) = (-1.0, 1.0);
        let mut solver = RootFinder::new();
        let mut roots = Vec::new();
        assert_eq!(
            solver.refine(&mut roots, xa, xb, args, f).err(),
            Some("at least one root is required")
        );
        let mut roots = [0.0];
        solver.newton_max_iterations = 0;
        assert_eq!(
            solver.refine(&mut roots, xa, xb, args, f).err(),
            Some("Newton's method did not converge")
        );
    }

    #[test]
    fn refine_works() {
        // function
        let f = |x, _: &mut NoArgs| Ok(x * x * x * x - 1.0);
        let (xa, xb) = (-2.0, 2.0);

        // interpolant
        let nn = 2;
        let args = &mut 0;
        let mut interp = InterpChebyshev::new(nn, xa, xb).unwrap();
        interp.set_function(nn, args, f).unwrap();

        // find roots
        let solver = RootFinder::new();
        let roots = solver.chebyshev(&interp).unwrap();
        let mut roots_refined = roots.clone();
        solver.refine(&mut roots_refined, xa, xb, args, f).unwrap();
        println!("n_roots = {}", roots_refined.len());
        println!("roots = {:?}", roots);
        println!("roots_refined = {:?}", roots_refined);

        // figure
        /*
        graph(
            "test_root_finding_refine",
            &interp,
            &roots,
            &roots_refined,
            args,
            f,
            101,
            600.0,
        );
        */

        // check
        array_approx_eq(&roots_refined, &[-1.0, 1.0], 1e-14);
    }

    #[test]
    fn find_works_with_test_functions() {
        let nn_max = 200;
        let tol = 1e-8;
        let args = &mut 0;
        for test in get_test_functions() {
            if test.id == 0 {
                continue;
            }
            println!("\n===================================================================");
            println!("\n{}", test.name);
            let (xa, xb) = test.range;
            let mut interp = InterpChebyshev::new(nn_max, xa, xb).unwrap();
            interp.adapt_function(tol, args, test.f).unwrap();
            let solver = RootFinder::new();
            let roots = solver.chebyshev(&interp).unwrap();
            let mut roots_refined = roots.clone();
            if roots.len() > 0 {
                solver.refine(&mut roots_refined, xa, xb, args, test.f).unwrap();
            }
            for xr in &roots_refined {
                let fx = (test.f)(*xr, args).unwrap();
                println!("x = {}, f(x) = {:.2e}", xr, fx);
                assert!(fx < 1e-10);
                if let Some(bracket) = test.root1 {
                    if *xr >= bracket.a && *xr <= bracket.b {
                        approx_eq(*xr, bracket.xo, 1e-14);
                    }
                }
                if let Some(bracket) = test.root2 {
                    if *xr >= bracket.a && *xr <= bracket.b {
                        approx_eq(*xr, bracket.xo, 1e-14);
                    }
                }
                if let Some(bracket) = test.root3 {
                    if *xr >= bracket.a && *xr <= bracket.b {
                        approx_eq(*xr, bracket.xo, 1e-14);
                    }
                }
            }
            assert_eq!(roots.len(), test.nroot);
            // figure
            /*
            let (nstation, fig_width) = if test.id == 9 { (1001, 2048.0) } else { (101, 600.0) };
            graph(
                &format!("test_root_finding_chebyshev_{:0>3}", test.id),
                &interp,
                &roots,
                &roots_refined,
                args,
                test.f,
                nstation,
                fig_width,
            );
            */
        }
    }

    #[test]
    fn constant_function_works() {
        // data
        let (xa, xb) = (0.0, 1.0);
        let uu = &[0.5];

        // interpolant
        let nn_max = 10;
        let mut interp = InterpChebyshev::new(nn_max, xa, xb).unwrap();
        interp.set_data(uu).unwrap();

        // find all roots in the interval
        let solver = RootFinder::new();
        let roots = &solver.chebyshev(&interp).unwrap();
        let nroot = roots.len();
        assert_eq!(nroot, 0)
    }

    #[test]
    fn linear_function_no_roots_works() {
        // data
        let (xa, xb) = (0.0, 1.0);
        let uu = &[0.5, 3.0];

        // interpolant
        let nn_max = 10;
        let tol = 1e-8;
        let mut interp = InterpChebyshev::new(nn_max, xa, xb).unwrap();
        interp.adapt_data(tol, uu).unwrap();

        // find all roots in the interval
        let solver = RootFinder::new();
        let roots = &solver.chebyshev(&interp).unwrap();
        let nroot = roots.len();
        assert_eq!(nroot, 0)
    }

    #[test]
    fn linear_function_works() {
        // data
        let (xa, xb) = (0.0, 1.0);
        let dx = xb - xa;
        let uu = &[-7.0, -4.5, 0.5, 3.0];
        let np = uu.len(); // number of points
        let nn = np - 1; // degree
        let mut xx_dat = Vector::new(np);
        let zz = InterpChebyshev::points(nn);
        for i in 0..np {
            xx_dat[i] = (xb + xa + dx * zz[i]) / 2.0;
        }

        // interpolant
        let nn_max = 100;
        let tol = 1e-8;
        let mut interp = InterpChebyshev::new(nn_max, xa, xb).unwrap();
        interp.adapt_data(tol, uu).unwrap();

        // find all roots in the interval
        let solver = RootFinder::new();
        let roots = solver.chebyshev(&interp).unwrap();
        let nroot = roots.len();
        assert_eq!(nroot, 1);
        approx_eq(roots[0], 0.7, 1e-15);
        approx_eq(interp.eval(roots[0]).unwrap(), 0.0, 1e-15);

        // plot
        /*
        let xx = Vector::linspace(xa, xb, 201).unwrap();
        let yy_int = xx.get_mapped(|x| interp.eval(x).unwrap());
        let mut curve_dat = Curve::new();
        let mut curve_int = Curve::new();
        let mut curve_xr = Curve::new();
        curve_dat.set_label("data").set_line_style("None").set_marker_style(".");
        curve_int
            .set_label(&format!("interpolated,N={}", nn))
            .set_marker_every(5);
        curve_xr
            .set_label("root")
            .set_line_style("None")
            .set_marker_style("o")
            .set_marker_void(true);
        curve_dat.draw(xx_dat.as_data(), uu.as_data());
        curve_int.draw(xx.as_data(), yy_int.as_data());
        curve_xr.draw(&roots, &vec![0.0]);
        let mut plot = Plot::new();
        let mut legend = Legend::new();
        legend.set_num_col(4);
        legend.set_outside(true);
        legend.draw();
        plot.add(&curve_int)
            .add(&curve_dat)
            .add(&curve_xr)
            .add(&legend)
            .set_cross(0.0, 0.0, "gray", "-", 1.5)
            .grid_and_labels("x", "f(x)")
            .save("/tmp/russell_lab/test_root_finding_chebyshev_linear_function.svg")
            .unwrap();
        */
    }
}