scirs2-integrate 0.4.2

Numerical integration module for SciRS2 (scirs2-integrate)
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
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
//! Stability analysis tools for dynamical systems
//!
//! This module provides the StabilityAnalyzer and related functionality
//! for assessing the stability of fixed points and periodic orbits.

use crate::analysis::advanced;
use crate::analysis::types::*;
use crate::error::{IntegrateError, IntegrateResult};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Complex64;

/// Stability analyzer for dynamical systems
pub struct StabilityAnalyzer {
    /// System dimension
    pub dimension: usize,
    /// Tolerance for fixed point detection
    pub tolerance: f64,
    /// Integration time for trajectory analysis
    pub integration_time: f64,
    /// Number of test points for basin analysis
    pub basin_grid_size: usize,
}

impl StabilityAnalyzer {
    /// Create a new stability analyzer
    pub fn new(dimension: usize) -> Self {
        Self {
            dimension,
            tolerance: 1e-8,
            integration_time: 100.0,
            basin_grid_size: 50,
        }
    }

    /// Perform comprehensive stability analysis
    pub fn analyze_stability<F>(
        &self,
        system: F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<StabilityResult>
    where
        F: Fn(&Array1<f64>) -> Array1<f64> + Send + Sync + Clone + 'static,
    {
        // Find fixed points
        let fixed_points = self.find_fixed_points(&system, domain)?;

        // Find periodic orbits (simplified)
        let periodic_orbits = self.find_periodic_orbits(&system, domain)?;

        // Compute Lyapunov exponents
        let lyapunov_exponents = self.compute_lyapunov_exponents(&system)?;

        // Analyze basins of attraction
        let basin_analysis = if self.dimension == 2 {
            Some(self.analyze_basins(&system, domain, &fixed_points)?)
        } else {
            None
        };

        Ok(StabilityResult {
            fixed_points,
            periodic_orbits,
            lyapunov_exponents,
            basin_analysis,
        })
    }

    /// Find fixed points in the given domain
    fn find_fixed_points<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<Vec<FixedPoint>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut fixed_points: Vec<FixedPoint> = Vec::new();
        let grid_size = 10; // Number of initial guesses per dimension

        // Generate grid of initial guesses
        let mut initial_guesses = Vec::new();
        self.generate_grid_points(domain, grid_size, &mut initial_guesses);

        for guess in initial_guesses {
            if let Ok(fixed_point) = self.newton_raphson_fixed_point(system, &guess) {
                // Check if this fixed point is already found
                let mut is_duplicate = false;
                for existing_fp in &fixed_points {
                    let distance = (&fixed_point - &existing_fp.location)
                        .iter()
                        .map(|&x| x * x)
                        .sum::<f64>()
                        .sqrt();

                    if distance < self.tolerance * 10.0 {
                        is_duplicate = true;
                        break;
                    }
                }

                if !is_duplicate {
                    // Compute stability
                    let jacobian = self.compute_jacobian_at_point(system, &fixed_point)?;
                    let eigenvalues = self.compute_real_eigenvalues(&jacobian)?;
                    let eigenvectors = self.compute_eigenvectors(&jacobian, &eigenvalues)?;
                    let stability = self.classify_stability(&eigenvalues);

                    fixed_points.push(FixedPoint {
                        location: fixed_point,
                        stability,
                        eigenvalues,
                        eigenvectors,
                    });
                }
            }
        }

        Ok(fixed_points)
    }

    /// Generate grid of points in domain
    fn generate_grid_points(
        &self,
        domain: &[(f64, f64)],
        grid_size: usize,
        points: &mut Vec<Array1<f64>>,
    ) {
        fn generate_recursive(
            domain: &[(f64, f64)],
            grid_size: usize,
            current: &mut Vec<f64>,
            dim: usize,
            points: &mut Vec<Array1<f64>>,
        ) {
            if dim == domain.len() {
                points.push(Array1::from_vec(current.clone()));
                return;
            }

            // Check for division by zero in step calculation
            if grid_size <= 1 {
                return; // Skip invalid grid size
            }
            let step = (domain[dim].1 - domain[dim].0) / (grid_size - 1) as f64;
            for i in 0..grid_size {
                let value = domain[dim].0 + i as f64 * step;
                current.push(value);
                generate_recursive(domain, grid_size, current, dim + 1, points);
                current.pop();
            }
        }

        let mut current = Vec::new();
        generate_recursive(domain, grid_size, &mut current, 0, points);
    }

    /// Find fixed point using Newton-Raphson method
    fn newton_raphson_fixed_point<F>(
        &self,
        system: &F,
        initial_guess: &Array1<f64>,
    ) -> IntegrateResult<Array1<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut x = initial_guess.clone();
        let max_iterations = 100;

        for _ in 0..max_iterations {
            let f_x = system(&x);
            let sum_squares = f_x.iter().map(|&v| v * v).sum::<f64>();
            if sum_squares < 0.0 {
                return Err(IntegrateError::ComputationError(
                    "Negative sum of squares in residual norm calculation".to_string(),
                ));
            }
            let residual_norm = sum_squares.sqrt();

            if residual_norm < self.tolerance {
                return Ok(x);
            }

            let jacobian = self.compute_jacobian_at_point(system, &x)?;

            // Solve J * dx = -f(x)
            let mut augmented = Array2::zeros((self.dimension, self.dimension + 1));
            for i in 0..self.dimension {
                for j in 0..self.dimension {
                    augmented[[i, j]] = jacobian[[i, j]];
                }
                augmented[[i, self.dimension]] = -f_x[i];
            }

            let dx = self.gaussian_elimination(&augmented)?;
            x += &dx;
        }

        Err(IntegrateError::ConvergenceError(
            "Newton-Raphson did not converge".to_string(),
        ))
    }

    /// Compute Jacobian at a specific point
    fn compute_jacobian_at_point<F>(
        &self,
        system: &F,
        x: &Array1<f64>,
    ) -> IntegrateResult<Array2<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let h = 1e-8_f64;
        let n = x.len();
        let mut jacobian = Array2::zeros((n, n));

        let f0 = system(x);

        // Check for valid step size
        if h.abs() < 1e-15 {
            return Err(IntegrateError::ComputationError(
                "Step size too small for finite difference calculation".to_string(),
            ));
        }

        for j in 0..n {
            let mut x_plus = x.clone();
            x_plus[j] += h;
            let f_plus = system(&x_plus);

            for i in 0..n {
                jacobian[[i, j]] = (f_plus[i] - f0[i]) / h;
            }
        }

        Ok(jacobian)
    }

    /// Solve linear system using Gaussian elimination
    fn gaussian_elimination(&self, augmented: &Array2<f64>) -> IntegrateResult<Array1<f64>> {
        let n = augmented.nrows();
        let mut a = augmented.clone();

        // Forward elimination
        for k in 0..n {
            // Find pivot
            let mut max_row = k;
            for i in k + 1..n {
                if a[[i, k]].abs() > a[[max_row, k]].abs() {
                    max_row = i;
                }
            }

            // Swap rows
            if max_row != k {
                for j in k..n + 1 {
                    let temp = a[[k, j]];
                    a[[k, j]] = a[[max_row, j]];
                    a[[max_row, j]] = temp;
                }
            }

            // Check for singularity
            if a[[k, k]].abs() < 1e-14 {
                return Err(IntegrateError::ComputationError(
                    "Matrix is singular".to_string(),
                ));
            }

            // Eliminate
            for i in k + 1..n {
                let factor = a[[i, k]] / a[[k, k]];
                for j in k..n + 1 {
                    a[[i, j]] -= factor * a[[k, j]];
                }
            }
        }

        // Back substitution
        let mut x = Array1::zeros(n);
        for i in (0..n).rev() {
            x[i] = a[[i, n]];
            for j in i + 1..n {
                x[i] -= a[[i, j]] * x[j];
            }
            // Check for zero diagonal element
            if a[[i, i]].abs() < 1e-14 {
                return Err(IntegrateError::ComputationError(
                    "Zero diagonal element in back substitution".to_string(),
                ));
            }
            x[i] /= a[[i, i]];
        }

        Ok(x)
    }

    /// Compute real eigenvalues (simplified implementation)
    fn compute_real_eigenvalues(&self, matrix: &Array2<f64>) -> IntegrateResult<Vec<Complex64>> {
        // For now, use a simplified approach for 2x2 matrices
        let n = matrix.nrows();

        if n == 2 {
            let a = matrix[[0, 0]];
            let b = matrix[[0, 1]];
            let c = matrix[[1, 0]];
            let d = matrix[[1, 1]];

            let trace = a + d;
            let det = a * d - b * c;
            let discriminant = trace * trace - 4.0 * det;

            if discriminant >= 0.0 {
                let sqrt_disc = discriminant.sqrt();
                let lambda1 = (trace + sqrt_disc) / 2.0;
                let lambda2 = (trace - sqrt_disc) / 2.0;
                Ok(vec![
                    Complex64::new(lambda1, 0.0),
                    Complex64::new(lambda2, 0.0),
                ])
            } else {
                let real_part = trace / 2.0;
                let imag_part = (-discriminant).sqrt() / 2.0;
                Ok(vec![
                    Complex64::new(real_part, imag_part),
                    Complex64::new(real_part, -imag_part),
                ])
            }
        } else {
            // For higher dimensions, use the QR algorithm
            self.eigenvalues_qr_algorithm(matrix)
        }
    }

    /// Compute eigenvalues using QR algorithm for larger matrices
    fn eigenvalues_qr_algorithm(&self, matrix: &Array2<f64>) -> IntegrateResult<Vec<Complex64>> {
        let n = matrix.nrows();
        let mut a = matrix.clone();
        let max_iterations = 100;
        let tolerance = 1e-10;

        // First, reduce to upper Hessenberg form for better convergence
        a = self.reduce_to_hessenberg(&a)?;

        // Apply QR iterations
        for _ in 0..max_iterations {
            let (q, r) = self.qr_decomposition_real(&a)?;
            a = r.dot(&q);

            // Check convergence by examining sub-diagonal elements
            let mut converged = true;
            for i in 1..n {
                if a[[i, i - 1]].abs() > tolerance {
                    converged = false;
                    break;
                }
            }

            if converged {
                break;
            }
        }

        // Extract eigenvalues from the diagonal
        let mut eigenvalues = Vec::new();
        let mut i = 0;
        while i < n {
            if i == n - 1 || a[[i + 1, i]].abs() < tolerance {
                // Real eigenvalue
                eigenvalues.push(Complex64::new(a[[i, i]], 0.0));
                i += 1;
            } else {
                // Complex conjugate pair
                let trace = a[[i, i]] + a[[i + 1, i + 1]];
                let det = a[[i, i]] * a[[i + 1, i + 1]] - a[[i, i + 1]] * a[[i + 1, i]];
                let discriminant = trace * trace - 4.0 * det;

                if discriminant >= 0.0 {
                    let sqrt_disc = discriminant.sqrt();
                    eigenvalues.push(Complex64::new((trace + sqrt_disc) / 2.0, 0.0));
                    eigenvalues.push(Complex64::new((trace - sqrt_disc) / 2.0, 0.0));
                } else {
                    let real_part = trace / 2.0;
                    let imag_part = (-discriminant).sqrt() / 2.0;
                    eigenvalues.push(Complex64::new(real_part, imag_part));
                    eigenvalues.push(Complex64::new(real_part, -imag_part));
                }
                i += 2;
            }
        }

        Ok(eigenvalues)
    }

    /// Reduce matrix to upper Hessenberg form using Householder reflections
    fn reduce_to_hessenberg(&self, matrix: &Array2<f64>) -> IntegrateResult<Array2<f64>> {
        let n = matrix.nrows();
        let mut h = matrix.clone();

        for k in 0..(n - 2) {
            // Extract the column below the diagonal
            let mut x = Array1::<f64>::zeros(n - k - 1);
            for i in 0..(n - k - 1) {
                x[i] = h[[k + 1 + i, k]];
            }

            if x.iter().map(|&v| v * v).sum::<f64>().sqrt() > 1e-15 {
                // Compute Householder vector
                let alpha = if x[0] >= 0.0 {
                    -x.iter().map(|&v| v * v).sum::<f64>().sqrt()
                } else {
                    x.iter().map(|&v| v * v).sum::<f64>().sqrt()
                };

                let mut v = x.clone();
                v[0] -= alpha;
                let v_norm = v.iter().map(|&vi| vi * vi).sum::<f64>().sqrt();

                if v_norm > 1e-15 {
                    v.mapv_inplace(|vi| vi / v_norm);

                    // Apply Householder reflection: H = I - 2*v*v^T
                    // H * A
                    for j in k..n {
                        let dot_product: f64 =
                            (0..(n - k - 1)).map(|i| v[i] * h[[k + 1 + i, j]]).sum();
                        for i in 0..(n - k - 1) {
                            h[[k + 1 + i, j]] -= 2.0 * v[i] * dot_product;
                        }
                    }

                    // A * H
                    for i in 0..n {
                        let dot_product: f64 =
                            (0..(n - k - 1)).map(|j| h[[i, k + 1 + j]] * v[j]).sum();
                        for j in 0..(n - k - 1) {
                            h[[i, k + 1 + j]] -= 2.0 * v[j] * dot_product;
                        }
                    }
                }
            }
        }

        Ok(h)
    }

    /// QR decomposition for real matrices
    fn qr_decomposition_real(
        &self,
        matrix: &Array2<f64>,
    ) -> IntegrateResult<(Array2<f64>, Array2<f64>)> {
        let (m, n) = matrix.dim();
        let mut q = Array2::<f64>::eye(m);
        let mut r = matrix.clone();

        for k in 0..std::cmp::min(m - 1, n) {
            // Extract column k from row k onwards
            let mut x = Array1::<f64>::zeros(m - k);
            for i in 0..(m - k) {
                x[i] = r[[k + i, k]];
            }

            // Compute Householder vector
            let norm_x = x.iter().map(|&v| v * v).sum::<f64>().sqrt();
            if norm_x > 1e-15 {
                let alpha = if x[0] >= 0.0 { -norm_x } else { norm_x };

                let mut v = x.clone();
                v[0] -= alpha;
                let v_norm = v.iter().map(|&vi| vi * vi).sum::<f64>().sqrt();

                if v_norm > 1e-15 {
                    v.mapv_inplace(|vi| vi / v_norm);

                    // Apply Householder reflection to R
                    for j in k..n {
                        let dot_product: f64 = (0..(m - k)).map(|i| v[i] * r[[k + i, j]]).sum();
                        for i in 0..(m - k) {
                            r[[k + i, j]] -= 2.0 * v[i] * dot_product;
                        }
                    }

                    // Apply Householder reflection to Q
                    for i in 0..m {
                        let dot_product: f64 = (0..(m - k)).map(|j| q[[i, k + j]] * v[j]).sum();
                        for j in 0..(m - k) {
                            q[[i, k + j]] -= 2.0 * v[j] * dot_product;
                        }
                    }
                }
            }
        }

        Ok((q, r))
    }

    /// Compute eigenvectors (simplified)
    fn compute_eigenvectors(
        &self,
        _matrix: &Array2<f64>,
        eigenvalues: &[Complex64],
    ) -> IntegrateResult<Array2<Complex64>> {
        let n = eigenvalues.len();
        let eigenvectors = Array2::<Complex64>::zeros((n, n));

        // Simplified: return identity matrix
        // In practice, would solve (A - λI)v = 0 for each eigenvalue
        Ok(eigenvectors)
    }

    /// Classify stability based on eigenvalues
    fn classify_stability(&self, eigenvalues: &[Complex64]) -> StabilityType {
        let mut has_positive_real = false;
        let mut has_negative_real = false;
        let mut has_zero_real = false;

        for eigenvalue in eigenvalues {
            if eigenvalue.re > 1e-10 {
                has_positive_real = true;
            } else if eigenvalue.re < -1e-10 {
                has_negative_real = true;
            } else {
                has_zero_real = true;
            }
        }

        if has_zero_real {
            StabilityType::Degenerate
        } else if has_positive_real && has_negative_real {
            StabilityType::Saddle
        } else if has_positive_real {
            StabilityType::Unstable
        } else if has_negative_real {
            StabilityType::Stable
        } else {
            StabilityType::Center
        }
    }

    /// Find periodic orbits using multiple detection methods
    fn find_periodic_orbits<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<Vec<PeriodicOrbit>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut periodic_orbits = Vec::new();

        // Method 1: Shooting method for periodic orbits
        if let Ok(shooting_orbits) = self.shooting_method_periodic_orbits(system, domain) {
            periodic_orbits.extend(shooting_orbits);
        }

        // Method 2: Return map analysis
        if let Ok(return_map_orbits) = self.return_map_periodic_orbits(system, domain) {
            periodic_orbits.extend(return_map_orbits);
        }

        // Method 3: Fourier analysis of trajectories
        if let Ok(fourier_orbits) = self.fourier_analysis_periodic_orbits(system, domain) {
            periodic_orbits.extend(fourier_orbits);
        }

        // Remove duplicates based on spatial proximity
        let filtered_orbits = self.remove_duplicate_periodic_orbits(periodic_orbits);

        Ok(filtered_orbits)
    }

    /// Use shooting method to find periodic orbits
    fn shooting_method_periodic_orbits<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<Vec<PeriodicOrbit>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut periodic_orbits = Vec::new();

        if self.dimension != 2 {
            return Ok(periodic_orbits); // Shooting method implementation for 2D systems only
        }

        // Generate initial guesses for periodic orbits
        let n_guesses = 20;
        let mut initial_points = Vec::new();
        self.generate_grid_points(domain, n_guesses, &mut initial_points);

        // Try different periods
        let periods = vec![
            std::f64::consts::PI,       // π
            2.0 * std::f64::consts::PI, // 2Ï€
            std::f64::consts::PI / 2.0, // π/2
            4.0 * std::f64::consts::PI, // 4Ï€
        ];

        for initial_point in &initial_points {
            for &period in &periods {
                if let Ok(orbit) = self.shooting_method_single_orbit(system, initial_point, period)
                {
                    periodic_orbits.push(orbit);
                }
            }
        }

        Ok(periodic_orbits)
    }

    /// Single orbit detection using shooting method
    fn shooting_method_single_orbit<F>(
        &self,
        system: &F,
        initial_guess: &Array1<f64>,
        period: f64,
    ) -> IntegrateResult<PeriodicOrbit>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let max_iterations = 50;
        let tolerance = 1e-8;
        let dt = period / 100.0; // Integration step size

        let mut current_guess = initial_guess.clone();

        // Newton iteration for shooting method
        for _iter in 0..max_iterations {
            // Integrate forward for one period
            let final_state =
                self.integrate_trajectory_period(system, &current_guess, period, dt)?;

            // Compute the shooting function: F(x0) = x(T) - x0
            let shooting_residual = &final_state - &current_guess;
            let residual_norm = shooting_residual.iter().map(|&x| x * x).sum::<f64>().sqrt();

            if residual_norm < tolerance {
                // Found a periodic orbit
                let floquet_multipliers =
                    self.compute_floquet_multipliers(system, &current_guess, period)?;
                let stability = self.classify_periodic_orbit_stability(&floquet_multipliers);

                return Ok(PeriodicOrbit {
                    representative_point: current_guess,
                    period,
                    stability,
                    floquet_multipliers,
                });
            }

            // Compute Jacobian of the flow map
            let flow_jacobian = self.compute_flow_jacobian(system, &current_guess, period, dt)?;

            // Newton step: solve (∂F/∂x0) * Δx0 = -F(x0)
            let identity = Array2::<f64>::eye(self.dimension);
            let shooting_jacobian = &flow_jacobian - &identity;

            // Solve the linear system
            let newton_step =
                self.solve_linear_system_for_shooting(&shooting_jacobian, &(-&shooting_residual))?;
            current_guess += &newton_step;
        }

        Err(IntegrateError::ConvergenceError(
            "Shooting method did not converge to periodic orbit".to_string(),
        ))
    }

    /// Integrate trajectory for a specified period
    fn integrate_trajectory_period<F>(
        &self,
        system: &F,
        initial_state: &Array1<f64>,
        period: f64,
        dt: f64,
    ) -> IntegrateResult<Array1<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let n_steps = (period / dt) as usize;
        let mut state = initial_state.clone();

        // Fourth-order Runge-Kutta integration
        for _ in 0..n_steps {
            let k1 = system(&state);
            let k2 = system(&(&state + &(&k1 * (dt / 2.0))));
            let k3 = system(&(&state + &(&k2 * (dt / 2.0))));
            let k4 = system(&(&state + &(&k3 * dt)));

            state += &((&k1 + &k2 * 2.0 + &k3 * 2.0 + &k4) * (dt / 6.0));
        }

        Ok(state)
    }

    /// Compute flow map Jacobian using finite differences
    fn compute_flow_jacobian<F>(
        &self,
        system: &F,
        initial_state: &Array1<f64>,
        period: f64,
        dt: f64,
    ) -> IntegrateResult<Array2<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let h = 1e-8;
        let n = initial_state.len();
        let mut jacobian = Array2::<f64>::zeros((n, n));

        // Base trajectory
        let base_final = self.integrate_trajectory_period(system, initial_state, period, dt)?;

        // Perturb each component and compute finite differences
        for j in 0..n {
            let mut perturbed_initial = initial_state.clone();
            perturbed_initial[j] += h;

            let perturbed_final =
                self.integrate_trajectory_period(system, &perturbed_initial, period, dt)?;

            for i in 0..n {
                jacobian[[i, j]] = (perturbed_final[i] - base_final[i]) / h;
            }
        }

        Ok(jacobian)
    }

    /// Compute Floquet multipliers for periodic orbit stability analysis
    fn compute_floquet_multipliers<F>(
        &self,
        system: &F,
        representative_point: &Array1<f64>,
        period: f64,
    ) -> IntegrateResult<Vec<Complex64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let dt = period / 100.0;
        let flow_jacobian = self.compute_flow_jacobian(system, representative_point, period, dt)?;

        // Compute eigenvalues of the flow map Jacobian (Floquet multipliers)
        let multipliers = self.compute_real_eigenvalues(&flow_jacobian)?;

        Ok(multipliers)
    }

    /// Classify periodic orbit stability based on Floquet multipliers
    fn classify_periodic_orbit_stability(
        &self,
        floquet_multipliers: &[Complex64],
    ) -> StabilityType {
        // For periodic orbits, stability is determined by Floquet multipliers
        // Stable if all multipliers have |λ| < 1
        // Unstable if any multiplier has |λ| > 1

        let max_magnitude = floquet_multipliers
            .iter()
            .map(|m| m.norm())
            .fold(0.0, f64::max);

        if max_magnitude < 1.0 - 1e-10 {
            StabilityType::Stable
        } else if max_magnitude > 1.0 + 1e-10 {
            StabilityType::Unstable
        } else {
            // One or more multipliers on unit circle
            let on_unit_circle = floquet_multipliers
                .iter()
                .any(|m| (m.norm() - 1.0).abs() < 1e-10);

            if on_unit_circle {
                StabilityType::Center
            } else {
                StabilityType::Degenerate
            }
        }
    }

    /// Return map analysis for periodic orbit detection
    fn return_map_periodic_orbits<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<Vec<PeriodicOrbit>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut periodic_orbits = Vec::new();

        if self.dimension != 2 {
            return Ok(periodic_orbits); // Return map analysis for 2D systems only
        }

        // Define a Poincaré section (e.g., x = 0)
        let section_plane = Array1::from_vec(vec![1.0, 0.0]); // Normal to x-axis
        let section_point = Array1::zeros(2); // Origin

        // Generate several trajectories and find their intersections with the Poincaré section
        let n_trajectories = 10;
        let mut initial_points = Vec::new();
        self.generate_grid_points(domain, n_trajectories, &mut initial_points);

        for initial_point in &initial_points {
            if let Ok(return_points) = self.compute_poincare_return_map(
                system,
                initial_point,
                &section_plane,
                &section_point,
            ) {
                // Analyze return points for periodicity
                if let Ok(orbit) = self.analyze_return_map_for_periodicity(&return_points) {
                    periodic_orbits.push(orbit);
                }
            }
        }

        Ok(periodic_orbits)
    }

    /// Compute Poincaré return map
    fn compute_poincare_return_map<F>(
        &self,
        system: &F,
        initial_point: &Array1<f64>,
        section_normal: &Array1<f64>,
        section_point: &Array1<f64>,
    ) -> IntegrateResult<Vec<Array1<f64>>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut return_points = Vec::new();
        let dt = 0.01;
        let max_time = 50.0;
        let n_steps = (max_time / dt) as usize;

        let mut state = initial_point.clone();
        let mut prev_distance = self.distance_to_section(&state, section_normal, section_point);

        for _ in 0..n_steps {
            // Integrate one step
            let derivative = system(&state);
            state += &(derivative * dt);

            // Check for section crossing
            let curr_distance = self.distance_to_section(&state, section_normal, section_point);

            if prev_distance * curr_distance < 0.0 {
                // Crossed the section, refine the crossing point
                if let Ok(crossing_point) =
                    self.refine_section_crossing(system, &state, dt, section_normal, section_point)
                {
                    return_points.push(crossing_point);

                    if return_points.len() > 20 {
                        break; // Collect enough return points
                    }
                }
            }

            prev_distance = curr_distance;
        }

        Ok(return_points)
    }

    /// Distance from point to Poincaré section
    fn distance_to_section(
        &self,
        point: &Array1<f64>,
        section_normal: &Array1<f64>,
        section_point: &Array1<f64>,
    ) -> f64 {
        let relative_pos = point - section_point;
        relative_pos.dot(section_normal)
    }

    /// Refine section crossing using bisection
    fn refine_section_crossing<F>(
        &self,
        system: &F,
        current_state: &Array1<f64>,
        dt: f64,
        section_normal: &Array1<f64>,
        section_point: &Array1<f64>,
    ) -> IntegrateResult<Array1<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        // Simple bisection refinement
        let derivative = system(current_state);
        let prev_state = current_state - &(derivative * dt);

        let mut left = prev_state;
        let mut right = current_state.clone();

        for _ in 0..10 {
            let mid = (&left + &right) * 0.5;
            let mid_distance = self.distance_to_section(&mid, section_normal, section_point);

            if mid_distance.abs() < 1e-10 {
                return Ok(mid);
            }

            let left_distance = self.distance_to_section(&left, section_normal, section_point);

            if left_distance * mid_distance < 0.0 {
                right = mid;
            } else {
                left = mid;
            }
        }

        Ok((&left + &right) * 0.5)
    }

    /// Analyze return map for periodicity
    fn analyze_return_map_for_periodicity(
        &self,
        return_points: &[Array1<f64>],
    ) -> IntegrateResult<PeriodicOrbit> {
        if return_points.len() < 3 {
            return Err(IntegrateError::ComputationError(
                "Insufficient return points for periodicity analysis".to_string(),
            ));
        }

        let tolerance = 1e-6;

        // Look for approximate returns
        for period in 1..std::cmp::min(return_points.len() / 2, 10) {
            let mut is_periodic = true;
            let mut max_error: f64 = 0.0;

            for i in 0..(return_points.len() - period) {
                let error = (&return_points[i] - &return_points[i + period])
                    .iter()
                    .map(|&x| x * x)
                    .sum::<f64>()
                    .sqrt();

                max_error = max_error.max(error);

                if error > tolerance {
                    is_periodic = false;
                    break;
                }
            }

            if is_periodic {
                // Estimate the period in time (rough approximation)
                let estimated_period = period as f64 * 2.0 * std::f64::consts::PI;

                return Ok(PeriodicOrbit {
                    representative_point: return_points[0].clone(),
                    period: estimated_period,
                    stability: StabilityType::Stable, // Would need proper analysis
                    floquet_multipliers: vec![],      // Would need computation
                });
            }
        }

        Err(IntegrateError::ComputationError(
            "No periodic behavior detected in return map".to_string(),
        ))
    }

    /// Fourier analysis for periodic orbit detection
    fn fourier_analysis_periodic_orbits<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
    ) -> IntegrateResult<Vec<PeriodicOrbit>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let mut periodic_orbits = Vec::new();

        // Generate initial points
        let n_trajectories = 5;
        let mut initial_points = Vec::new();
        self.generate_grid_points(domain, n_trajectories, &mut initial_points);

        for initial_point in &initial_points {
            if let Ok(orbit) = self.fourier_analysis_single_trajectory(system, initial_point) {
                periodic_orbits.push(orbit);
            }
        }

        Ok(periodic_orbits)
    }

    /// Fourier analysis of a single trajectory
    fn fourier_analysis_single_trajectory<F>(
        &self,
        system: &F,
        initial_point: &Array1<f64>,
    ) -> IntegrateResult<PeriodicOrbit>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        let dt = 0.01;
        let total_time = 20.0;
        let n_steps = (total_time / dt) as usize;

        // Integrate trajectory
        let mut trajectory = Vec::new();
        let mut state = initial_point.clone();

        for _ in 0..n_steps {
            trajectory.push(state.clone());
            let derivative = system(&state);
            state += &(derivative * dt);
        }

        // Simple frequency analysis (detect dominant frequency)
        if let Ok(dominant_period) = self.detect_dominant_period(&trajectory, dt) {
            if dominant_period > 0.0 && dominant_period < total_time {
                return Ok(PeriodicOrbit {
                    representative_point: initial_point.clone(),
                    period: dominant_period,
                    stability: StabilityType::Stable, // Would need proper analysis
                    floquet_multipliers: vec![],      // Would need computation
                });
            }
        }

        Err(IntegrateError::ComputationError(
            "No periodic behavior detected via Fourier analysis".to_string(),
        ))
    }

    /// Detect dominant period using autocorrelation
    fn detect_dominant_period(&self, trajectory: &[Array1<f64>], dt: f64) -> IntegrateResult<f64> {
        if trajectory.len() < 100 {
            return Err(IntegrateError::ComputationError(
                "Trajectory too short for period detection".to_string(),
            ));
        }

        // Use first component for period detection
        let signal: Vec<f64> = trajectory.iter().map(|state| state[0]).collect();

        // Autocorrelation approach
        let max_lag = std::cmp::min(signal.len() / 4, 500);
        let mut autocorr = vec![0.0; max_lag];

        let mean = signal.iter().sum::<f64>() / signal.len() as f64;
        let variance =
            signal.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / signal.len() as f64;

        if variance < 1e-12 {
            return Err(IntegrateError::ComputationError(
                "Signal has zero variance".to_string(),
            ));
        }

        for lag in 1..max_lag {
            let mut correlation = 0.0;
            let count = signal.len() - lag;

            for i in 0..count {
                correlation += (signal[i] - mean) * (signal[i + lag] - mean);
            }

            autocorr[lag] = correlation / (count as f64 * variance);
        }

        // Find the first significant peak after lag = 0
        let mut max_corr = 0.0;
        let mut period_lag = 0;

        for lag in 10..max_lag {
            if autocorr[lag] > max_corr && autocorr[lag] > 0.5 {
                // Check if this is a local maximum
                if lag > 0
                    && lag < max_lag - 1
                    && autocorr[lag] > autocorr[lag - 1]
                    && autocorr[lag] > autocorr[lag + 1]
                {
                    max_corr = autocorr[lag];
                    period_lag = lag;
                }
            }
        }

        if period_lag > 0 {
            Ok(period_lag as f64 * dt)
        } else {
            Err(IntegrateError::ComputationError(
                "No dominant period detected".to_string(),
            ))
        }
    }

    /// Remove duplicate periodic orbits based on spatial proximity
    fn remove_duplicate_periodic_orbits(&self, orbits: Vec<PeriodicOrbit>) -> Vec<PeriodicOrbit> {
        let mut filtered: Vec<PeriodicOrbit> = Vec::new();
        let tolerance = 1e-4;

        for orbit in orbits {
            let mut is_duplicate = false;

            for existing in &filtered {
                let distance = (&orbit.representative_point - &existing.representative_point)
                    .iter()
                    .map(|&x| x * x)
                    .sum::<f64>()
                    .sqrt();

                let period_diff = (orbit.period - existing.period).abs();

                if distance < tolerance && period_diff < tolerance {
                    is_duplicate = true;
                    break;
                }
            }

            if !is_duplicate {
                filtered.push(orbit);
            }
        }

        filtered
    }

    /// Solve linear system for shooting method
    fn solve_linear_system_for_shooting(
        &self,
        matrix: &Array2<f64>,
        rhs: &Array1<f64>,
    ) -> IntegrateResult<Array1<f64>> {
        let n = matrix.nrows();
        if n != matrix.ncols() || n != rhs.len() {
            return Err(IntegrateError::ComputationError(
                "Inconsistent matrix dimensions in shooting method".to_string(),
            ));
        }

        let mut augmented = Array2::<f64>::zeros((n, n + 1));
        for i in 0..n {
            for j in 0..n {
                augmented[[i, j]] = matrix[[i, j]];
            }
            augmented[[i, n]] = rhs[i];
        }

        // Gaussian elimination
        for k in 0..n {
            // Find pivot
            let mut max_row = k;
            for i in (k + 1)..n {
                if augmented[[i, k]].abs() > augmented[[max_row, k]].abs() {
                    max_row = i;
                }
            }

            // Swap rows
            if max_row != k {
                for j in k..=n {
                    let temp = augmented[[k, j]];
                    augmented[[k, j]] = augmented[[max_row, j]];
                    augmented[[max_row, j]] = temp;
                }
            }

            // Check for singularity
            if augmented[[k, k]].abs() < 1e-14 {
                return Err(IntegrateError::ComputationError(
                    "Singular matrix in shooting method".to_string(),
                ));
            }

            // Eliminate
            for i in (k + 1)..n {
                let factor = augmented[[i, k]] / augmented[[k, k]];
                for j in k..=n {
                    augmented[[i, j]] -= factor * augmented[[k, j]];
                }
            }
        }

        // Back substitution
        let mut solution = Array1::<f64>::zeros(n);
        for i in (0..n).rev() {
            solution[i] = augmented[[i, n]];
            for j in (i + 1)..n {
                solution[i] -= augmented[[i, j]] * solution[j];
            }
            solution[i] /= augmented[[i, i]];
        }

        Ok(solution)
    }

    /// Compute Lyapunov exponents
    fn compute_lyapunov_exponents<F>(&self, system: &F) -> IntegrateResult<Option<Array1<f64>>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64> + Send + Sync + Clone,
    {
        // For systems with dimension 1-10, compute Lyapunov exponents
        if self.dimension == 0 || self.dimension > 10 {
            return Ok(None);
        }

        // Create initial state in the center of the domain
        // (we'd ideally use an attractor, but this is a reasonable default)
        let initial_state = Array1::zeros(self.dimension);

        // Use adaptive time step based on system dimension
        let dt = match self.dimension {
            1 => 0.01,
            2 => 0.005,
            3 => 0.002,
            4..=6 => 0.001,
            _ => 0.0005,
        };

        // Calculate number of exponents to compute (typically all for small systems)
        let n_exponents = if self.dimension <= 4 {
            self.dimension
        } else {
            // For higher dimensions, compute only the largest few exponents
            std::cmp::min(4, self.dimension)
        };

        let calculator = advanced::LyapunovCalculator::new(n_exponents, dt);

        // Use integration time that scales with system complexity
        let integration_time = self.integration_time * (self.dimension as f64).sqrt();

        // Clone the system function to satisfy trait bounds
        let system_clone = system.clone();
        let system_wrapper = move |state: &Array1<f64>| system_clone(state);

        match calculator.calculate_lyapunov_exponents(
            system_wrapper,
            &initial_state,
            integration_time,
        ) {
            Ok(exponents) => {
                // Filter out numerical artifacts (very small exponents close to machine precision)
                let filtered_exponents = exponents.mapv(|x| if x.abs() < 1e-12 { 0.0 } else { x });
                Ok(Some(filtered_exponents))
            }
            Err(e) => {
                // If Lyapunov computation fails, it's not critical - return None
                eprintln!("Warning: Lyapunov exponent computation failed: {e:?}");
                Ok(None)
            }
        }
    }

    /// Analyze basins of attraction for 2D systems
    fn analyze_basins<F>(
        &self,
        system: &F,
        domain: &[(f64, f64)],
        fixed_points: &[FixedPoint],
    ) -> IntegrateResult<BasinAnalysis>
    where
        F: Fn(&Array1<f64>) -> Array1<f64> + Send + Sync,
    {
        if self.dimension != 2 || domain.len() != 2 {
            return Err(IntegrateError::ValueError(
                "Basin analysis only implemented for 2D systems".to_string(),
            ));
        }

        let grid_size = self.basin_grid_size;
        let mut grid_points = Array2::zeros((grid_size * grid_size, 2));
        let mut attractor_indices = Array2::<i32>::zeros((grid_size, grid_size));

        let dx = (domain[0].1 - domain[0].0) / (grid_size - 1) as f64;
        let dy = (domain[1].1 - domain[1].0) / (grid_size - 1) as f64;

        // Generate grid and integrate each point
        for i in 0..grid_size {
            for j in 0..grid_size {
                let x = domain[0].0 + i as f64 * dx;
                let y = domain[1].0 + j as f64 * dy;

                grid_points[[i * grid_size + j, 0]] = x;
                grid_points[[i * grid_size + j, 1]] = y;

                // Integrate trajectory and find which attractor it converges to
                let initial_state = Array1::from_vec(vec![x, y]);
                let final_state = self.integrate_trajectory(system, &initial_state)?;

                // Find closest fixed point
                let mut closest_attractor = -1;
                let mut min_distance = f64::INFINITY;

                for (idx, fp) in fixed_points.iter().enumerate() {
                    if fp.stability == StabilityType::Stable {
                        let distance = (&final_state - &fp.location)
                            .iter()
                            .map(|&x| x * x)
                            .sum::<f64>()
                            .sqrt();

                        if distance < min_distance && distance < 0.1 {
                            min_distance = distance;
                            closest_attractor = idx as i32;
                        }
                    }
                }

                attractor_indices[[i, j]] = closest_attractor;
            }
        }

        // Extract stable attractors
        let attractors = fixed_points
            .iter()
            .filter(|fp| fp.stability == StabilityType::Stable)
            .map(|fp| fp.location.clone())
            .collect();

        Ok(BasinAnalysis {
            grid_points,
            attractor_indices,
            attractors,
        })
    }

    /// Integrate trajectory to find final state
    fn integrate_trajectory<F>(
        &self,
        system: &F,
        initial_state: &Array1<f64>,
    ) -> IntegrateResult<Array1<f64>>
    where
        F: Fn(&Array1<f64>) -> Array1<f64>,
    {
        // Simple Euler integration
        let dt = 0.01;
        let n_steps = (self.integration_time / dt) as usize;
        let mut state = initial_state.clone();

        for _ in 0..n_steps {
            let derivative = system(&state);
            state += &(derivative * dt);
        }

        Ok(state)
    }
}