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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! # Root Finding Methods
//!
//! This module provides a collection of root finding algorithms for solving nonlinear equations.
//! It defines traits for representing root finding problems and root finding methods, and provides implementations of several common algorithms.
//!
//! ## Traits
//!
//! - `RootFindingProblem<const I: usize, const O: usize, T>`: Defines the interface for a root finding problem.
//!   It requires implementing the `function` method to evaluate the function at a given point, and the `initial_guess` method to provide an initial guess for the root.
//!   Optionally, the `derivative` and `hessian` methods can be implemented to provide the derivative and Hessian of the function, respectively.
//!
//!   - `I`: Input dimension
//!   - `O`: Output dimension
//!   - `T`: State type (e.g. `f64`, `(f64, f64)`, or etc.)
//!
//! - `RootFinder<const I: usize, const O: usize, T>`: Defines the interface for a root finding method.
//!   It requires implementing the `find` method, which takes a `RootFindingProblem` and returns the root of the function.
//!   The `max_iter` and `tol` methods provide the maximum number of iterations and the tolerance for the root finding algorithm.
//!
//! ## Root Finding Methods
//!
//! - `BisectionMethod`: Implements the bisection method for finding roots of continuous functions.
//!   It requires an initial interval that brackets the root.
//!
//!  - Type Parameters: `I=1, O=1, T=(f64, f64)`
//!
//! - `NewtonMethod`: Implements Newton's method for finding roots of differentiable functions.
//!   It requires an initial guess for the root and the derivative of the function.
//!
//!   - Type Parameters: `I=1, O=1, T=f64`
//!
//! - `SecantMethod`: Implements the secant method for finding roots of differentiable functions.
//!   It requires two initial guesses for the root.
//!
//!   - Type Parameters: `I=1, O=1, T=f64`
//!
//! - `FalsePositionMethod`: Implements the false position method for finding roots of continuous functions.
//!   It requires an initial interval that brackets the root.
//!
//!   - Type Parameters: `I=1, O=1, T=(f64, f64)`
//!
//! - `BroydenMethod`: Implements Broyden's method for finding roots of systems of nonlinear equations.
//!   It requires an two initial guesses for the first step. (not an interval, just two points)
//!
//!   - Type Parameters: `I>=1, O>=1, T=([f64; I], [f64; I])`
//!
//! ## Convenient type aliases
//!
//! - `Pt<const N: usize>`: Represents a point in N-dimensional space. (`[f64; N]`)
//! - `Intv<const N: usize>`: Represents an interval in I-dimensional space. (`([f64; N], [f64; N])`)
//! - `Jaco<const R: usize, const C: usize>`: Represents the Jacobian matrix of a function. (`[[f64; C]; R]`)
//! - `Hess<const R: usize, const C: usize>`: Represents the Hessian matrix of a function. (`[[[f64; C]; C]; R]`)
//!
//! ## High-level macros
//!
//! Peroxide also provides high-level macros for root finding.
//! Assume `f: fn(f64) -> f64`.
//!
//! - `bisection!(f, (a,b), max_iter, tol)`
//! - `newton!(f, x0, max_iter, tol)`: (**Caution**: newton macro requires `#[ad_function]` attribute)
//! - `secant!(f, (x0, x1), max_iter, tol)`
//! - `false_position!(f, (a,b), max_iter, tol)`
//!
//! ```rust
//! #[macro_use]
//! extern crate peroxide;
//! use peroxide::fuga::*;
//! use anyhow::Result;
//! 
//! fn main() -> Result<()> {
//!     let root_bisect = bisection!(f, (0.0, 2.0), 100, 1e-6)?;
//!     let root_newton = newton!(f, 0.0, 100, 1e-6)?;
//!     let root_false_pos = false_position!(f, (0.0, 2.0), 100, 1e-6)?;
//!     let root_secant = secant!(f, (0.0, 2.0), 100, 1e-6)?;
//! 
//!     println!("root_bisect: {}", root_bisect);
//!     println!("root_newton: {}", root_newton);
//!     println!("root_false_pos: {}", root_false_pos);
//!     println!("root_secant: {}", root_secant);
//! 
//!     assert!(f(root_bisect).abs() < 1e-6);
//!     assert!(f(root_newton).abs() < 1e-6);
//!     assert!(f(root_false_pos).abs() < 1e-6);
//!     assert!(f(root_secant).abs() < 1e-6);
//! 
//!     Ok(())
//! }
//! 
//! #[ad_function]
//! fn f(x: f64) -> f64 {
//!     (x - 1f64).powi(3)
//! }
//! ```
//!
//! ## Examples
//!
//! ### Finding the root of a cubic function
//!
//! ```rust
//! use peroxide::fuga::*;
//! use anyhow::Result;
//!
//! fn main() -> 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)]])
//!     }
//! }
//! ```
//!
//! This example demonstrates how to find the root of a cubic function `(x - 1)^3` using various root finding methods.
//! The `Cubic` struct implements the `RootFindingProblem` trait for both `(f64, f64)` and `f64` initial guess types, allowing the use of different root finding methods.
//!
//! ### Finding the root of the cosine function (error handling example)
//!
//! ```rust
//! use peroxide::fuga::*;
//! use anyhow::Result;
//!
//! fn main() -> 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()]])
//!     }
//! }
//! ```
//!
//! This example shows how to find the root of the cosine function using Newton's method.
//! The `Cosine` struct implements the `RootFindingProblem` trait for the `f64` initial guess type.
//! The initial guess is set to `0.0`, which is a point where the derivative of the cosine function is 0.
//! This leads to the `NewtonMethod` returning a `RootError::ZeroDerivative` error, which is handled in the example.
use anyhow::{Result, bail};

use crate::traits::math::{Normed, Norm, LinearOp};
use crate::traits::sugar::{ConvToMat, VecOps};
use crate::util::non_macro::zeros;

// ┌─────────────────────────────────────────────────────────┐
//  High level macro
// └─────────────────────────────────────────────────────────┘
/// High level macro for bisection
///
/// # Arguments
///
/// - `f`: `Fn(f64) -> f64` (allow closure)
/// - `(a, b)`: `(f64, f64)`
/// - `max_iter`: `usize`
/// - `tol`: `f64`
#[macro_export]
macro_rules! bisection {
    ($f:expr, ($a:expr, $b:expr), $max_iter:expr, $tol:expr) => {{
        struct BisectionProblem<F: Fn(f64) -> f64> {
            f: F,
        };

        impl<F: Fn(f64) -> f64> RootFindingProblem<1, 1, (f64, f64)> for BisectionProblem<F> {
            fn initial_guess(&self) -> (f64, f64) {
                ($a, $b)
            }

            fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
                Ok([(self.f)(x[0])])
            }
        }

        let problem = BisectionProblem { f: $f };
        let bisection = BisectionMethod { max_iter: $max_iter, tol: $tol };
        match bisection.find(&problem) {
            Ok(root) => Ok(root[0]),
            Err(e) => Err(e),
        }
    }}
}

/// High level macro for newton (using Automatic differentiation)
///
/// # Requirements
///
/// - This macro requires the function with `ad_function`
///
///   ```rust
///   use peroxide::fuga::*;
///
///   #[ad_function]
///   fn f(x: f64) -> f64 {
///       (x - 1f64).powi(3)
///   }
///   ```
///
/// # Arguments
///
/// - `f`: `fn(f64) -> f64` (not allow closure)
/// - `x`: `f64`
/// - `max_iter`: `usize`
/// - `tol`: `f64`
#[macro_export]
macro_rules! newton {
    ($f:ident, $x:expr, $max_iter:expr, $tol:expr) => {{
        use paste::paste;
        struct NewtonProblem;

        impl RootFindingProblem<1, 1, f64> for NewtonProblem {
            fn initial_guess(&self) -> f64 {
                $x 
            }

            fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
                Ok([$f(x[0])])
            }

            fn derivative(&self, x: [f64; 1]) -> Result<Jaco<1, 1>> {
                paste! {
                    let x_ad = AD1(x[0], 1f64);
                    Ok([[[<$f _ad>](x_ad).dx()]])
                }
            }
        }

        let problem = NewtonProblem;
        let newton = NewtonMethod { max_iter: $max_iter, tol: $tol };
        match newton.find(&problem) {
            Ok(root) => Ok(root[0]),
            Err(e) => Err(e),
        }
    }}
}

/// High level macro for false position
///
/// # Arguments
///
/// - `f`: `Fn(f64) -> f64` (allow closure)
/// - `(a, b)`: `(f64, f64)`
/// - `max_iter`: `usize`
/// - `tol`: `f64`
#[macro_export]
macro_rules! false_position {
    ($f:expr, ($a:expr, $b:expr), $max_iter:expr, $tol:expr) => {{
        struct FalsePositionProblem<F: Fn(f64) -> f64> {
            f: F,
        };

        impl<F: Fn(f64) -> f64> RootFindingProblem<1, 1, (f64, f64)> for FalsePositionProblem<F> {
            fn initial_guess(&self) -> (f64, f64) {
                ($a, $b)
            }

            fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
                Ok([(self.f)(x[0])])
            }
        }

        let problem = FalsePositionProblem { f: $f };
        let false_position = FalsePositionMethod { max_iter: $max_iter, tol: $tol };
        match false_position.find(&problem) {
            Ok(root) => Ok(root[0]),
            Err(e) => Err(e),
        }
    }}
}

/// High level macro for secant
///
/// # Arguments
///
/// - `f`: `Fn(f64) -> f64` (allow closure)
/// - `(a, b)`: `(f64, f64)`
/// - `max_iter`: `usize`
/// - `tol`: `f64`
#[macro_export]
macro_rules! secant {
    ($f:expr, ($a:expr, $b:expr), $max_iter:expr, $tol:expr) => {{
        struct SecantProblem<F: Fn(f64) -> f64> {
            f: F,
        };

        impl<F: Fn(f64) -> f64> RootFindingProblem<1, 1, (f64, f64)> for SecantProblem<F> {
            fn initial_guess(&self) -> (f64, f64) {
                ($a, $b)
            }

            fn function(&self, x: [f64; 1]) -> Result<[f64; 1]> {
                Ok([(self.f)(x[0])])
            }
        }

        let problem = SecantProblem { f: $f };
        let secant = SecantMethod { max_iter: $max_iter, tol: $tol };
        match secant.find(&problem) {
            Ok(root) => Ok(root[0]),
            Err(e) => Err(e),
        }
    }}
}


// ┌─────────────────────────────────────────────────────────┐
//  Type aliases
// └─────────────────────────────────────────────────────────┘
/// Point alias (`[f64; N]`)
pub type Pt<const N: usize> = [f64; N];
/// Interval alias (`([f64; N], [f64; N])`)
pub type Intv<const N: usize> = (Pt<N>, Pt<N>);
/// Jacobian alias (`[[f64; C]; R]`)
pub type Jaco<const R: usize, const C: usize> = [[f64; C]; R];
/// Hessian alias (`[[[f64; C]; C]; R]`)
pub type Hess<const R: usize, const C: usize> = [[[f64; C]; C]; R];

// ┌─────────────────────────────────────────────────────────┐
//  Traits
// └─────────────────────────────────────────────────────────┘
/// Trait to define a root finding problem
///
/// # Type Parameters
///
/// - `I`: Input type (e.g. `f64`, `[f64; N]`, or etc.)
/// - `O`: Output type (e.g. `f64`, `[f64; N]`, or etc.)
/// - `T`: State type (e.g. `f64`, `(f64, f64)`, or etc.)
///
/// # Methods
///
/// - `function`: Function
/// - `initial_guess`: Initial guess
/// - `derivative`: Derivative (optional)
/// - `hessian`: Hessian (optional)
pub trait RootFindingProblem<const I: usize, const O: usize, T> {
    fn function(&self, x: Pt<I>) -> Result<Pt<O>>;
    fn initial_guess(&self) -> T;
    #[allow(unused_variables)]
    fn derivative(&self, x: Pt<I>) -> Result<Jaco<O, I>> {
        unimplemented!()
    }
    #[allow(unused_variables)]
    fn hessian(&self, x: Pt<I>) -> Result<Hess<O, I>> {
        unimplemented!()
    }
}

/// Trait to define a root finder
///
/// # Type Parameters
///
/// - `I`: Input type (e.g. `f64`, `[f64; N]`, or etc.)
/// - `O`: Output type (e.g. `f64`, `[f64; N]`, or etc.)
/// - `T`: State type (e.g. `f64`, `(f64, f64)`, or etc.)
///
/// # Methods
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: Absolute tolerance
/// - `find`: Find root
///
/// # Available root finders
///
/// - `BisectionMethod`: `I=1, O=1, T=(f64, f64)`
/// - `FalsePositionMethod`: `I=1, O=1, T=(f64, f64)`
/// - `NewtonMethod`: `I=1, O=1, T=f64`
/// - `SecantMethod`: `I=1, O=1, T=(f64, f64)`
pub trait RootFinder<const I: usize, const O: usize, T> {
    fn max_iter(&self) -> usize;
    fn tol(&self) -> f64;
    fn find<P: RootFindingProblem<I, O, T>>(&self, problem: &P) -> Result<Pt<I>>;
}

#[derive(Debug, Copy, Clone)]
pub enum RootError<const I: usize> {
    NotConverge(Pt<I>),
    NoRoot,
    ZeroDerivative(Pt<I>),
    ZeroSecant(Pt<I>, Pt<I>),
}

impl<const I: usize> std::fmt::Display for RootError<I> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RootError::NoRoot => write!(f, "There is no root in the interval"),
            RootError::NotConverge(a) => write!(f, "Not yet converge. Our guess is {:?}", a),
            RootError::ZeroDerivative(a) => write!(f, "Zero derivative in {:?}", a),
            RootError::ZeroSecant(a,b) => write!(f, "Zero secant in ({:?}, {:?})", a, b),
        }
    }
}

/// Macro for single function
///
/// # Description
///
/// For I=1, O=1, it is bother to write below code.
///
/// ```ignore
/// let fx = problem.function([x])?[0];
/// ```
///
/// This macro solve this problem as follows.
///
/// ```ignore
/// let fx = single_function!(problem, x);
/// ```
#[macro_export]
macro_rules! single_function {
    ($problem:expr, $x:expr) => {{
        $problem.function([$x])?[0]
    }}
}

/// Macro for single derivative
///
/// # Description
///
/// For I=1, O=1, it is bother to write below code.
///
/// ```ignore
/// let fx = problem.derivative([x])?[0][0];
/// ```
///
/// This macro solve this problem as follows.
///
/// ```ignore
/// let fx = single_derivative!(problem, x);
/// ```
#[macro_export]
macro_rules! single_derivative {
    ($problem:expr, $x:expr) => {{
        $problem.derivative([$x])?[0][0]
    }}
}

// ┌─────────────────────────────────────────────────────────┐
//  Bisection method
// └─────────────────────────────────────────────────────────┘
/// Bisection method
///
/// # Type for `RootFinder`
///
/// - `I`: 1
/// - `O`: 1
/// - `T`: `(f64, f64)`
///
/// # Arguments
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: tol
///
/// # Caution
///
/// - The function should be continuous
/// - The function should have a root in the initial interval
pub struct BisectionMethod {
    pub max_iter: usize,
    pub tol: f64,
}

impl RootFinder<1, 1, (f64, f64)> for BisectionMethod {
    fn max_iter(&self) -> usize {
        self.max_iter
    }

    fn tol(&self) -> f64 {
        self.tol
    }

    fn find<P: RootFindingProblem<1, 1, (f64, f64)>>(
        &self,
        problem: &P,
    ) -> Result<[f64; 1]> {
        let state = problem.initial_guess();
        let (mut a, mut b) = state;
        let mut fa = single_function!(problem, a);
        let mut fb = single_function!(problem, b);

        if fa.abs() < self.tol {
            return Ok([a]);
        } else if fb.abs() < self.tol {
            return Ok([b]);
        } else if fa * fb > 0.0 {
            bail!(RootError::<1>::NoRoot);
        }

        for _ in 0..self.max_iter {
            let c = (a + b) / 2.0;
            let fc = single_function!(problem, c);

            if fc.abs() < self.tol {
                return Ok([c]);
            } else if fa * fc < 0.0 {
                b = c;
                fb = fc;
            } else if fb * fc < 0.0 {
                a = c;
                fa = fc;
            } else {
                bail!(RootError::<1>::NoRoot);
            }
        }
        let c = (a + b) / 2.0;
        bail!(RootError::NotConverge([c]));
    }
}

// ┌─────────────────────────────────────────────────────────┐
//  Newton method
// └─────────────────────────────────────────────────────────┘
/// Newton method
///
/// # Type for `RootFinder`
///
/// - `I`: 1
/// - `O`: 1
/// - `T`: `f64`
///
/// # Arguments
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: Absolute tolerance
///
/// # Caution
///
/// - The function should be differentiable
/// - This method highly depends on the initial guess
/// - This method is not guaranteed to converge
pub struct NewtonMethod {
    pub max_iter: usize,
    pub tol: f64,
}

impl RootFinder<1, 1, f64> for NewtonMethod {
    fn max_iter(&self) -> usize {
        self.max_iter
    }
    fn tol(&self) -> f64 {
        self.tol
    }
    fn find<P: RootFindingProblem<1, 1, f64>>(
        &self,
        problem: &P,
    ) -> Result<[f64; 1]> {
        let mut x = problem.initial_guess();

        for _ in 0..self.max_iter {
            let f = single_function!(problem, x);
            let df = single_derivative!(problem, x);

            if f.abs() < self.tol {
                return Ok([x]);
            } else if df == 0.0 {
                bail!(RootError::ZeroDerivative([x]));
            } else {
                x -= f / df;
            }
        }
        bail!(RootError::NotConverge([x]));
    }
}

// ┌─────────────────────────────────────────────────────────┐
//  Secant method
// └─────────────────────────────────────────────────────────┘
/// Secant method
///
/// # Type for `RootFinder`
///
/// - `I`: 1
/// - `O`: 1
/// - `T`: `(f64, f64)`
///
/// # Arguments
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: Absolute tolerance
///
/// # Caution
///
/// - The function should be differentiable
/// - This method is not guaranteed to converge
pub struct SecantMethod {
    pub max_iter: usize,
    pub tol: f64,
}

impl RootFinder<1, 1, (f64, f64)> for SecantMethod {
    fn max_iter(&self) -> usize {
        self.max_iter
    }
    fn tol(&self) -> f64 {
        self.tol
    }
    fn find<P: RootFindingProblem<1, 1, (f64, f64)>>(
        &self,
        problem: &P,
    ) -> Result<[f64; 1]> {
        let state = problem.initial_guess();
        let (mut x0, mut x1) = state;
        let mut f0 = single_function!(problem, x0);

        if f0.abs() < self.tol {
            return Ok([x0]);
        }

        for _ in 0..self.max_iter {
            let f1 = single_function!(problem, x1);

            if f1.abs() < self.tol {
                return Ok([x1]);
            }

            if f0 == f1 {
                bail!(RootError::ZeroSecant([x0], [x1]));
            }

            let f0_old = f0;
            f0 = f1;
            (x0, x1) = (x1, x1 - f1 * (x1 - x0) / (f1 - f0_old))
        }
        bail!(RootError::NotConverge([x1]));
    }
}

// ┌─────────────────────────────────────────────────────────┐
//  False position method
// └─────────────────────────────────────────────────────────┘
/// False position method
///
/// # Type for `RootFinder`
///
/// - `I`: 1
/// - `O`: 1
/// - `T`: `(f64, f64)`
///
/// # Arguments
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: Absolute tolerance
///
/// # Caution
///
/// - The function should be continuous
pub struct FalsePositionMethod {
    pub max_iter: usize,
    pub tol: f64,
}

impl RootFinder<1, 1, (f64, f64)> for FalsePositionMethod {
    fn max_iter(&self) -> usize {
        self.max_iter
    }
    fn tol(&self) -> f64 {
        self.tol
    }
    fn find<P: RootFindingProblem<1, 1, (f64, f64)>>(
        &self,
        problem: &P,
    ) -> Result<[f64; 1]> {
        let state = problem.initial_guess();
        let (mut a, mut b) = state;
        let mut fa = single_function!(problem, a);
        let mut fb = single_function!(problem, b);

        if fa.abs() < self.tol {
            return Ok([a]);
        } else if fb.abs() < self.tol {
            return Ok([b]);
        } else if fa * fb > 0.0 {
            bail!(RootError::<1>::NoRoot);
        }

        for _ in 0..self.max_iter {
            let c = (a * fb - b * fa) / (fb - fa);
            let fc = single_function!(problem, c);

            if fc.abs() < self.tol {
                return Ok([c]);
            } else if fa * fc < 0.0 {
                b = c;
                fb = fc;
            } else if fb * fc < 0.0 {
                a = c;
                fa = fc;
            } else {
                bail!(RootError::<1>::NoRoot);
            }
        }
        let c = (a * fb - b * fa) / (fb - fa);
        bail!(RootError::NotConverge([c]));
    }
}

// ┌─────────────────────────────────────────────────────────┐
//  Broyden method
// └─────────────────────────────────────────────────────────┘
/// Broyden method
///
/// # Type for `RootFinder`
///
/// - `I`: free
/// - `O`: free
/// - `T`: `Intv<I>` (=`([f64; I], [f64; I])`)
///
/// # Arguments
///
/// - `max_iter`: Maximum number of iterations
/// - `tol`: Absolute tolerance
///
/// # Caution
///
/// - The function should be differentiable
///
/// # Example
///
/// ```rust
/// use peroxide::fuga::*;
/// use peroxide::numerical::root::{Pt, Intv};
/// 
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let problem = CircleTangentLine;
///     let broyden = BroydenMethod { max_iter: 100, tol: 1e-6, rtol: 1e-6 };
///
///     let root = broyden.find(&problem)?;
///     let result = problem.function(root)?;
///
///     let norm = result.to_vec().norm(Norm::L2);
///     assert!(norm < 1e-6);
///
///     Ok(())
/// }
/// 
/// struct CircleTangentLine;
/// 
/// impl RootFindingProblem<2, 2, Intv<2>> for CircleTangentLine {
///     fn function(&self, x: Pt<2>) -> anyhow::Result<Pt<2>> {
///         Ok([
///             x[0] * x[0] + x[1] * x[1] - 1.0,
///             x[0] + x[1] - 2f64.sqrt()
///         ])
///     }
/// 
///     fn initial_guess(&self) -> Intv<2> {
///         ([0.0, 0.1], [-0.1, 0.2])
///     }
/// }
/// ```

pub struct BroydenMethod {
    pub max_iter: usize,
    pub tol: f64,
    pub rtol: f64,
}

#[allow(unused_variables, non_snake_case)]
impl<const I: usize, const O: usize> RootFinder<I, O, Intv<I>> for BroydenMethod {
    fn max_iter(&self) -> usize {
        self.max_iter
    }
    fn tol(&self) -> f64 {
        self.tol
    }
    fn find<P: RootFindingProblem<I, O, Intv<I>>>(
        &self,
        problem: &P,
    ) -> Result<Pt<I>> {
        // Init state
        let state = problem.initial_guess();
        let (mut x0, mut x1) = state;
        let mut fx0 = problem.function(x0)?.to_vec();

        // Initialize negative inverse jacobian as identity
        // H = -J^{-1}
        let mut H = zeros(I, O);
        for i in 0..O.min(I) {
            H[(i, i)] = 1.0;
        }

        for _ in 0..self.max_iter {
            let fx1 = problem.function(x1)?.to_vec();
            if fx1.norm(Norm::L2) < self.tol {
                return Ok(x1);
            }
            let dx = x1.iter().zip(x0.iter()).map(|(x1, x0)| x1 - x0).collect::<Vec<_>>();
            if dx.norm(Norm::L2) < self.rtol {
                return Ok(x1);
            }
            let df = fx1.iter().zip(fx0.iter()).map(|(fx1, fx0)| fx1 - fx0).collect::<Vec<_>>();

            let denom = dx.add_v(&H.apply(&df));
            let right = &dx.to_row() * &H;
            let num = right.apply(&df)[0];

            let left = denom.div_s(num);

            H = H - left.to_col() * right;

            x0 = x1;
            fx0 = fx1.clone();
            let dx_new = H.apply(&fx1);
            for i in 0..I {
                x1[i] += dx_new[i];
            }
        }
        bail!(RootError::NotConverge(x1));
    }
}