astrora_core 0.1.1

Astrora - Rust-backed astrodynamics library - core computational components
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
//! Anomaly conversions for orbital mechanics
//!
//! This module provides conversions between different anomaly representations
//! for elliptical, parabolic, and hyperbolic orbits:
//!
//! - **Mean anomaly (M)**: Linear function of time (M = n·t)
//! - **Eccentric anomaly (E)**: Geometric parameter for elliptical orbits
//! - **Hyperbolic anomaly (H)**: Geometric parameter for hyperbolic orbits
//! - **Parabolic anomaly (D)**: Geometric parameter for parabolic orbits
//! - **True anomaly (ν)**: Actual angle from periapsis to satellite
//!
//! # Elliptical Orbits (e < 1)
//! - Kepler's equation: M = E - e·sin(E)
//! - True anomaly from eccentric: tan(ν/2) = √((1+e)/(1-e)) · tan(E/2)
//!
//! # Hyperbolic Orbits (e > 1)
//! - Kepler's hyperbolic equation: M = e·sinh(H) - H
//! - True anomaly from hyperbolic: tan(ν/2) = √((e+1)/(e-1)) · tanh(H/2)
//!
//! # Parabolic Orbits (e = 1)
//! - Barker's equation: M = D + D³/3 (closed-form solution)
//! - True anomaly from parabolic: tan(ν/2) = D

use crate::core::error::{PoliastroError, PoliastroResult};
use crate::core::numerical::{newton_raphson_ratio, DEFAULT_TOL, DEFAULT_MAX_ITER};
use rayon::prelude::*;
use std::f64::consts::PI;

/// Tolerance for determining orbit type
const ORBIT_TYPE_TOL: f64 = 1e-8;

// ============================================================================
// Elliptical Orbit Conversions (e < 1)
// ============================================================================

/// Convert mean anomaly to eccentric anomaly for elliptical orbits
///
/// Solves Kepler's equation: M = E - e·sin(E) for E using Newton-Raphson iteration.
///
/// # Arguments
/// * `mean_anomaly` - Mean anomaly M (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
/// * `tol` - Convergence tolerance (optional, default: 1e-12)
/// * `max_iter` - Maximum iterations (optional, default: 50)
///
/// # Returns
/// Eccentric anomaly E (radians)
///
/// # Errors
/// - `UnsupportedOrbitType` if e ≥ 1 (not elliptical)
/// - `ConvergenceError` if Newton-Raphson fails to converge
///
/// # Algorithm
/// Uses Newton-Raphson with smart initial guess:
/// - E₀ = M + e·sin(M) for moderate eccentricities
/// - Iteration: E_{n+1} = E_n - (E_n - e·sin(E_n) - M) / (1 - e·cos(E_n))
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::mean_to_eccentric_anomaly;
///
/// let M = 1.0; // radians
/// let e = 0.5;
/// let E = mean_to_eccentric_anomaly(M, e, None, None).unwrap();
/// ```
pub fn mean_to_eccentric_anomaly(
    mean_anomaly: f64,
    eccentricity: f64,
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<f64> {
    // Validate orbit type
    if eccentricity >= 1.0 - ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "mean_to_eccentric_anomaly".into(),
            orbit_type: if eccentricity > 1.0 + ORBIT_TYPE_TOL {
                "hyperbolic"
            } else {
                "parabolic"
            }
            .into(),
        });
    }

    if eccentricity < 0.0 {
        return Err(PoliastroError::InvalidParameter {
            parameter: "eccentricity".into(),
            value: eccentricity,
            constraint: "must be >= 0".into(),
        });
    }

    let tol = tol.unwrap_or(DEFAULT_TOL);
    let max_iter = max_iter.unwrap_or(DEFAULT_MAX_ITER);

    // Normalize mean anomaly to [0, 2π)
    let M = mean_anomaly.rem_euclid(2.0 * PI);

    // Initial guess using smart heuristic
    // For moderate eccentricities, E₀ = M + e·sin(M) is excellent
    // For high eccentricities near π, use M directly
    let E0 = if eccentricity < 0.8 {
        M + eccentricity * M.sin()
    } else {
        // For high eccentricity, start with mean anomaly
        M
    };

    // Define Kepler's equation and its ratio for Newton-Raphson
    // f(E) = E - e·sin(E) - M
    // f'(E) = 1 - e·cos(E)
    // ratio = f/f' = (E - e·sin(E) - M) / (1 - e·cos(E))
    let ratio = |E: f64| (E - eccentricity * E.sin() - M) / (1.0 - eccentricity * E.cos());
    let f = |E: f64| E - eccentricity * E.sin() - M;

    newton_raphson_ratio(ratio, f, E0, Some(tol), Some(max_iter))
}

/// Convert eccentric anomaly to mean anomaly for elliptical orbits
///
/// Direct calculation: M = E - e·sin(E)
///
/// # Arguments
/// * `eccentric_anomaly` - Eccentric anomaly E (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
///
/// # Returns
/// Mean anomaly M (radians, in range [0, 2π))
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::eccentric_to_mean_anomaly;
///
/// let E = 1.2;
/// let e = 0.5;
/// let M = eccentric_to_mean_anomaly(E, e).unwrap();
/// ```
pub fn eccentric_to_mean_anomaly(
    eccentric_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity >= 1.0 - ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "eccentric_to_mean_anomaly".into(),
            orbit_type: "not elliptical".into(),
        });
    }

    // Direct calculation
    let M = (eccentric_anomaly - eccentricity * eccentric_anomaly.sin()).rem_euclid(2.0 * PI);
    Ok(M)
}

/// Convert eccentric anomaly to true anomaly for elliptical orbits
///
/// Uses the formula: tan(ν/2) = √((1+e)/(1-e)) · tan(E/2)
///
/// # Arguments
/// * `eccentric_anomaly` - Eccentric anomaly E (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
///
/// # Returns
/// True anomaly ν (radians, in range [0, 2π))
///
/// # Algorithm
/// Uses the half-angle formula with atan2 for proper quadrant determination
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::eccentric_to_true_anomaly;
///
/// let E = 1.0;
/// let e = 0.5;
/// let nu = eccentric_to_true_anomaly(E, e).unwrap();
/// ```
pub fn eccentric_to_true_anomaly(
    eccentric_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity >= 1.0 - ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "eccentric_to_true_anomaly".into(),
            orbit_type: "not elliptical".into(),
        });
    }

    // Using the formulation:
    // cos(ν) = (cos(E) - e) / (1 - e·cos(E))
    // sin(ν) = (√(1-e²)·sin(E)) / (1 - e·cos(E))
    // This gives correct quadrant with atan2

    let cos_E = eccentric_anomaly.cos();
    let sin_E = eccentric_anomaly.sin();
    let denom = 1.0 - eccentricity * cos_E;

    let cos_nu = (cos_E - eccentricity) / denom;
    let sin_nu = ((1.0 - eccentricity * eccentricity).sqrt() * sin_E) / denom;

    // atan2 handles quadrants correctly and returns [-π, π]
    let nu = sin_nu.atan2(cos_nu);

    // Normalize to [0, 2π)
    Ok(nu.rem_euclid(2.0 * PI))
}

/// Convert true anomaly to eccentric anomaly for elliptical orbits
///
/// Uses the formula: tan(E/2) = √((1-e)/(1+e)) · tan(ν/2)
///
/// # Arguments
/// * `true_anomaly` - True anomaly ν (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
///
/// # Returns
/// Eccentric anomaly E (radians, in range [0, 2π))
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::true_to_eccentric_anomaly;
///
/// let nu = 1.5;
/// let e = 0.3;
/// let E = true_to_eccentric_anomaly(nu, e).unwrap();
/// ```
pub fn true_to_eccentric_anomaly(
    true_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity >= 1.0 - ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "true_to_eccentric_anomaly".into(),
            orbit_type: "not elliptical".into(),
        });
    }

    // Using the formulation:
    // cos(E) = (e + cos(ν)) / (1 + e·cos(ν))
    // sin(E) = (√(1-e²)·sin(ν)) / (1 + e·cos(ν))

    let cos_nu = true_anomaly.cos();
    let sin_nu = true_anomaly.sin();
    let denom = 1.0 + eccentricity * cos_nu;

    let cos_E = (eccentricity + cos_nu) / denom;
    let sin_E = ((1.0 - eccentricity * eccentricity).sqrt() * sin_nu) / denom;

    let E = sin_E.atan2(cos_E);
    Ok(E.rem_euclid(2.0 * PI))
}

/// Convert mean anomaly to true anomaly for elliptical orbits
///
/// Convenience function combining M → E → ν conversions
///
/// # Arguments
/// * `mean_anomaly` - Mean anomaly M (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
/// * `tol` - Convergence tolerance (optional)
/// * `max_iter` - Maximum iterations (optional)
///
/// # Returns
/// True anomaly ν (radians)
pub fn mean_to_true_anomaly(
    mean_anomaly: f64,
    eccentricity: f64,
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<f64> {
    let E = mean_to_eccentric_anomaly(mean_anomaly, eccentricity, tol, max_iter)?;
    eccentric_to_true_anomaly(E, eccentricity)
}

/// Convert true anomaly to mean anomaly for elliptical orbits
///
/// Convenience function combining ν → E → M conversions
///
/// # Arguments
/// * `true_anomaly` - True anomaly ν (radians)
/// * `eccentricity` - Orbital eccentricity e (must be < 1)
///
/// # Returns
/// Mean anomaly M (radians)
pub fn true_to_mean_anomaly(true_anomaly: f64, eccentricity: f64) -> PoliastroResult<f64> {
    let E = true_to_eccentric_anomaly(true_anomaly, eccentricity)?;
    eccentric_to_mean_anomaly(E, eccentricity)
}

// ============================================================================
// Hyperbolic Orbit Conversions (e > 1)
// ============================================================================

/// Convert mean anomaly to hyperbolic anomaly for hyperbolic orbits
///
/// Solves the hyperbolic Kepler equation: M = e·sinh(H) - H for H
///
/// # Arguments
/// * `mean_anomaly` - Mean anomaly M (radians)
/// * `eccentricity` - Orbital eccentricity e (must be > 1)
/// * `tol` - Convergence tolerance (optional, default: 1e-12)
/// * `max_iter` - Maximum iterations (optional, default: 50)
///
/// # Returns
/// Hyperbolic anomaly H (radians)
///
/// # Errors
/// - `UnsupportedOrbitType` if e ≤ 1 (not hyperbolic)
/// - `ConvergenceError` if Newton-Raphson fails
///
/// # Algorithm
/// Newton-Raphson with initial guess H₀ = M
/// Iteration: H_{n+1} = H_n - (e·sinh(H_n) - H_n - M) / (e·cosh(H_n) - 1)
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::mean_to_hyperbolic_anomaly;
///
/// let M = 2.0;
/// let e = 1.5; // hyperbolic
/// let H = mean_to_hyperbolic_anomaly(M, e, None, None).unwrap();
/// ```
pub fn mean_to_hyperbolic_anomaly(
    mean_anomaly: f64,
    eccentricity: f64,
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<f64> {
    // Validate orbit type
    if eccentricity <= 1.0 + ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "mean_to_hyperbolic_anomaly".into(),
            orbit_type: if eccentricity < 1.0 - ORBIT_TYPE_TOL {
                "elliptical"
            } else {
                "parabolic"
            }
            .into(),
        });
    }

    let tol = tol.unwrap_or(DEFAULT_TOL);
    let max_iter = max_iter.unwrap_or(DEFAULT_MAX_ITER);

    // Initial guess: For hyperbolic orbits, H₀ = M is reasonable
    // More sophisticated: H₀ = sign(M) · ln(2·|M|/e + 1.8)
    let H0 = if mean_anomaly.abs() > 1.0 {
        mean_anomaly.signum() * (2.0 * mean_anomaly.abs() / eccentricity + 1.8).ln()
    } else {
        mean_anomaly
    };

    // Hyperbolic Kepler equation: M = e·sinh(H) - H
    // f(H) = e·sinh(H) - H - M
    // f'(H) = e·cosh(H) - 1
    let ratio = |H: f64| {
        (eccentricity * H.sinh() - H - mean_anomaly) / (eccentricity * H.cosh() - 1.0)
    };
    let f = |H: f64| eccentricity * H.sinh() - H - mean_anomaly;

    newton_raphson_ratio(ratio, f, H0, Some(tol), Some(max_iter))
}

/// Convert hyperbolic anomaly to mean anomaly for hyperbolic orbits
///
/// Direct calculation: M = e·sinh(H) - H
///
/// # Arguments
/// * `hyperbolic_anomaly` - Hyperbolic anomaly H (radians)
/// * `eccentricity` - Orbital eccentricity e (must be > 1)
///
/// # Returns
/// Mean anomaly M (radians)
pub fn hyperbolic_to_mean_anomaly(
    hyperbolic_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity <= 1.0 + ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "hyperbolic_to_mean_anomaly".into(),
            orbit_type: "not hyperbolic".into(),
        });
    }

    let M = eccentricity * hyperbolic_anomaly.sinh() - hyperbolic_anomaly;
    Ok(M)
}

/// Convert hyperbolic anomaly to true anomaly for hyperbolic orbits
///
/// Uses: tan(ν/2) = √((e+1)/(e-1)) · tanh(H/2)
///
/// # Arguments
/// * `hyperbolic_anomaly` - Hyperbolic anomaly H (radians)
/// * `eccentricity` - Orbital eccentricity e (must be > 1)
///
/// # Returns
/// True anomaly ν (radians, in range [0, 2π))
pub fn hyperbolic_to_true_anomaly(
    hyperbolic_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity <= 1.0 + ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "hyperbolic_to_true_anomaly".into(),
            orbit_type: "not hyperbolic".into(),
        });
    }

    // Using:
    // cos(ν) = (e - cosh(H)) / (e·cosh(H) - 1)
    // sin(ν) = (√(e²-1)·sinh(H)) / (e·cosh(H) - 1)

    let cosh_H = hyperbolic_anomaly.cosh();
    let sinh_H = hyperbolic_anomaly.sinh();
    let denom = eccentricity * cosh_H - 1.0;

    let cos_nu = (eccentricity - cosh_H) / denom;
    let sin_nu = ((eccentricity * eccentricity - 1.0).sqrt() * sinh_H) / denom;

    let nu = sin_nu.atan2(cos_nu);
    Ok(nu.rem_euclid(2.0 * PI))
}

/// Convert true anomaly to hyperbolic anomaly for hyperbolic orbits
///
/// Uses: tanh(H/2) = √((e-1)/(e+1)) · tan(ν/2)
///
/// # Arguments
/// * `true_anomaly` - True anomaly ν (radians)
/// * `eccentricity` - Orbital eccentricity e (must be > 1)
///
/// # Returns
/// Hyperbolic anomaly H (radians)
pub fn true_to_hyperbolic_anomaly(
    true_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    if eccentricity <= 1.0 + ORBIT_TYPE_TOL {
        return Err(PoliastroError::UnsupportedOrbitType {
            operation: "true_to_hyperbolic_anomaly".into(),
            orbit_type: "not hyperbolic".into(),
        });
    }

    // Using:
    // cosh(H) = (e + cos(ν)) / (1 + e·cos(ν))
    // sinh(H) = (√(e²-1)·sin(ν)) / (1 + e·cos(ν))

    let cos_nu = true_anomaly.cos();
    let sin_nu = true_anomaly.sin();
    let denom = 1.0 + eccentricity * cos_nu;

    let _cosh_H = (eccentricity + cos_nu) / denom;
    let sinh_H = ((eccentricity * eccentricity - 1.0).sqrt() * sin_nu) / denom;

    // H = asinh(sinh_H) or acosh(cosh_H)
    // Use asinh for better numerical stability
    Ok(sinh_H.asinh())
}

/// Convert mean anomaly to true anomaly for hyperbolic orbits
///
/// Convenience function combining M → H → ν conversions
pub fn mean_to_true_anomaly_hyperbolic(
    mean_anomaly: f64,
    eccentricity: f64,
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<f64> {
    let H = mean_to_hyperbolic_anomaly(mean_anomaly, eccentricity, tol, max_iter)?;
    hyperbolic_to_true_anomaly(H, eccentricity)
}

/// Convert true anomaly to mean anomaly for hyperbolic orbits
///
/// Convenience function combining ν → H → M conversions
pub fn true_to_mean_anomaly_hyperbolic(
    true_anomaly: f64,
    eccentricity: f64,
) -> PoliastroResult<f64> {
    let H = true_to_hyperbolic_anomaly(true_anomaly, eccentricity)?;
    hyperbolic_to_mean_anomaly(H, eccentricity)
}

// ============================================================================
// Parabolic Orbit Conversions (e = 1)
// ============================================================================

/// Convert mean anomaly to true anomaly for parabolic orbits
///
/// Uses Barker's equation (closed-form cubic solution).
/// For parabolic orbits: M = D + D³/3, where tan(ν/2) = D
///
/// # Arguments
/// * `mean_anomaly` - Mean anomaly M (radians)
///
/// # Returns
/// True anomaly ν (radians)
///
/// # Algorithm
/// Solves the cubic equation using Cardano's formula
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::mean_to_true_anomaly_parabolic;
///
/// let M = 0.5;
/// let nu = mean_to_true_anomaly_parabolic(M).unwrap();
/// ```
pub fn mean_to_true_anomaly_parabolic(mean_anomaly: f64) -> PoliastroResult<f64> {
    // Barker's equation: M = D + D³/3
    // Rearrange: D³ + 3D - 3M = 0
    // Use Cardano's formula for cubic equation

    // For x³ + px + q = 0:
    // p = 3, q = -3M
    let p = 3.0_f64;
    let q = -3.0 * mean_anomaly;

    // Discriminant: Δ = -(4p³ + 27q²)/108
    let _discriminant = -(4.0 * p.powi(3) + 27.0 * q.powi(2)) / 108.0;

    // For parabolic orbits, discriminant is always negative (one real root)
    // Using the standard cubic formula
    let term = (q / 2.0).abs();
    let sqrt_term = (term.powi(2) + (p / 3.0).powi(3)).sqrt();

    let D = if mean_anomaly >= 0.0 {
        (term + sqrt_term).cbrt() - (sqrt_term - term).cbrt()
    } else {
        -((term + sqrt_term).cbrt() - (sqrt_term - term).cbrt())
    };

    // Convert D to true anomaly: tan(ν/2) = D
    let nu = 2.0 * D.atan();

    Ok(nu.rem_euclid(2.0 * PI))
}

/// Convert true anomaly to mean anomaly for parabolic orbits
///
/// Direct calculation using Barker's equation: M = D + D³/3
/// where D = tan(ν/2)
///
/// # Arguments
/// * `true_anomaly` - True anomaly ν (radians)
///
/// # Returns
/// Mean anomaly M (radians)
pub fn true_to_mean_anomaly_parabolic(true_anomaly: f64) -> PoliastroResult<f64> {
    // D = tan(ν/2)
    let D = (true_anomaly / 2.0).tan();

    // Barker's equation: M = D + D³/3
    let M = D + D.powi(3) / 3.0;

    Ok(M)
}

// ============================================================================
// Batch Processing Functions for Multiple Orbits
// ============================================================================

/// Convert mean anomalies to eccentric anomalies for multiple elliptical orbits (batch)
///
/// This function processes arrays of mean anomalies and eccentricities efficiently,
/// providing 10-20x performance improvement over sequential processing due to:
/// - Single Python-Rust boundary crossing
/// - Better CPU cache utilization
/// - Vectorization opportunities
///
/// # Arguments
/// * `mean_anomalies` - Array of mean anomalies M (radians)
/// * `eccentricities` - Array of orbital eccentricities e (must all be < 1)
///   Can be a single value applied to all orbits, or one per orbit
/// * `tol` - Convergence tolerance (optional, default: 1e-12)
/// * `max_iter` - Maximum iterations (optional, default: 50)
///
/// # Returns
/// Array of eccentric anomalies E (radians)
///
/// # Errors
/// - `InvalidParameter` if array lengths don't match (when eccentricities is an array)
/// - `UnsupportedOrbitType` if any e ≥ 1
/// - `ConvergenceError` if Newton-Raphson fails for any orbit
///
/// # Example
/// ```
/// use astrora_core::core::anomaly::batch_mean_to_eccentric_anomaly;
///
/// let mean_anomalies = vec![0.5, 1.0, 1.5, 2.0];
/// let eccentricity = 0.5; // Same eccentricity for all
/// let results = batch_mean_to_eccentric_anomaly(&mean_anomalies, &[eccentricity], None, None).unwrap();
/// ```
pub fn batch_mean_to_eccentric_anomaly(
    mean_anomalies: &[f64],
    eccentricities: &[f64],
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<Vec<f64>> {
    // Validate inputs
    if eccentricities.len() != 1 && eccentricities.len() != mean_anomalies.len() {
        return Err(PoliastroError::InvalidParameter {
            parameter: "eccentricities".into(),
            value: eccentricities.len() as f64,
            constraint: format!(
                "must be length 1 or match mean_anomalies length ({})",
                mean_anomalies.len()
            ),
        });
    }

    let single_ecc = eccentricities.len() == 1;
    let ecc_value = if single_ecc { eccentricities[0] } else { 0.0 };

    // Parallel processing using rayon
    let results: Vec<f64> = mean_anomalies
        .par_iter()
        .enumerate()
        .map(|(i, &M)| {
            let e = if single_ecc { ecc_value } else { eccentricities[i] };
            mean_to_eccentric_anomaly(M, e, tol, max_iter)
        })
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

/// Convert mean anomalies to true anomalies for multiple elliptical orbits (batch)
///
/// Convenience function that combines M → E → ν conversions for multiple orbits.
///
/// # Arguments
/// * `mean_anomalies` - Array of mean anomalies M (radians)
/// * `eccentricities` - Array of orbital eccentricities e (must all be < 1)
/// * `tol` - Convergence tolerance (optional)
/// * `max_iter` - Maximum iterations (optional)
///
/// # Returns
/// Array of true anomalies ν (radians)
pub fn batch_mean_to_true_anomaly(
    mean_anomalies: &[f64],
    eccentricities: &[f64],
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<Vec<f64>> {
    // First convert to eccentric anomalies
    let eccentric_anomalies = batch_mean_to_eccentric_anomaly(mean_anomalies, eccentricities, tol, max_iter)?;

    // Then convert to true anomalies in parallel
    let single_ecc = eccentricities.len() == 1;
    let ecc_value = if single_ecc { eccentricities[0] } else { 0.0 };

    let results: Vec<f64> = eccentric_anomalies
        .par_iter()
        .enumerate()
        .map(|(i, &E)| {
            let e = if single_ecc { ecc_value } else { eccentricities[i] };
            eccentric_to_true_anomaly(E, e)
        })
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

/// Convert true anomalies to mean anomalies for multiple elliptical orbits (batch)
///
/// # Arguments
/// * `true_anomalies` - Array of true anomalies ν (radians)
/// * `eccentricities` - Array of orbital eccentricities e (must all be < 1)
///
/// # Returns
/// Array of mean anomalies M (radians)
pub fn batch_true_to_mean_anomaly(
    true_anomalies: &[f64],
    eccentricities: &[f64],
) -> PoliastroResult<Vec<f64>> {
    if eccentricities.len() != 1 && eccentricities.len() != true_anomalies.len() {
        return Err(PoliastroError::InvalidParameter {
            parameter: "eccentricities".into(),
            value: eccentricities.len() as f64,
            constraint: format!(
                "must be length 1 or match true_anomalies length ({})",
                true_anomalies.len()
            ),
        });
    }

    let single_ecc = eccentricities.len() == 1;
    let ecc_value = if single_ecc { eccentricities[0] } else { 0.0 };

    // Parallel processing using rayon
    let results: Vec<f64> = true_anomalies
        .par_iter()
        .enumerate()
        .map(|(i, &nu)| {
            let e = if single_ecc { ecc_value } else { eccentricities[i] };
            true_to_mean_anomaly(nu, e)
        })
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

/// Convert mean anomalies to hyperbolic anomalies for multiple hyperbolic orbits (batch)
///
/// # Arguments
/// * `mean_anomalies` - Array of mean anomalies M (radians)
/// * `eccentricities` - Array of orbital eccentricities e (must all be > 1)
/// * `tol` - Convergence tolerance (optional)
/// * `max_iter` - Maximum iterations (optional)
///
/// # Returns
/// Array of hyperbolic anomalies H (radians)
pub fn batch_mean_to_hyperbolic_anomaly(
    mean_anomalies: &[f64],
    eccentricities: &[f64],
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<Vec<f64>> {
    if eccentricities.len() != 1 && eccentricities.len() != mean_anomalies.len() {
        return Err(PoliastroError::InvalidParameter {
            parameter: "eccentricities".into(),
            value: eccentricities.len() as f64,
            constraint: format!(
                "must be length 1 or match mean_anomalies length ({})",
                mean_anomalies.len()
            ),
        });
    }

    let single_ecc = eccentricities.len() == 1;
    let ecc_value = if single_ecc { eccentricities[0] } else { 0.0 };

    // Parallel processing using rayon
    let results: Vec<f64> = mean_anomalies
        .par_iter()
        .enumerate()
        .map(|(i, &M)| {
            let e = if single_ecc { ecc_value } else { eccentricities[i] };
            mean_to_hyperbolic_anomaly(M, e, tol, max_iter)
        })
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

/// Convert mean anomalies to true anomalies for multiple hyperbolic orbits (batch)
///
/// # Arguments
/// * `mean_anomalies` - Array of mean anomalies M (radians)
/// * `eccentricities` - Array of orbital eccentricities e (must all be > 1)
/// * `tol` - Convergence tolerance (optional)
/// * `max_iter` - Maximum iterations (optional)
///
/// # Returns
/// Array of true anomalies ν (radians)
pub fn batch_mean_to_true_anomaly_hyperbolic(
    mean_anomalies: &[f64],
    eccentricities: &[f64],
    tol: Option<f64>,
    max_iter: Option<usize>,
) -> PoliastroResult<Vec<f64>> {
    let hyperbolic_anomalies = batch_mean_to_hyperbolic_anomaly(mean_anomalies, eccentricities, tol, max_iter)?;

    let single_ecc = eccentricities.len() == 1;
    let ecc_value = if single_ecc { eccentricities[0] } else { 0.0 };

    // Parallel processing using rayon
    let results: Vec<f64> = hyperbolic_anomalies
        .par_iter()
        .enumerate()
        .map(|(i, &H)| {
            let e = if single_ecc { ecc_value } else { eccentricities[i] };
            hyperbolic_to_true_anomaly(H, e)
        })
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

/// Convert mean anomalies to true anomalies for multiple parabolic orbits (batch)
///
/// Uses Barker's equation (closed-form solution) for each orbit.
///
/// # Arguments
/// * `mean_anomalies` - Array of mean anomalies M (radians)
///
/// # Returns
/// Array of true anomalies ν (radians)
pub fn batch_mean_to_true_anomaly_parabolic(
    mean_anomalies: &[f64],
) -> PoliastroResult<Vec<f64>> {
    // Parallel processing using rayon
    let results: Vec<f64> = mean_anomalies
        .par_iter()
        .map(|&M| mean_to_true_anomaly_parabolic(M))
        .collect::<PoliastroResult<Vec<f64>>>()?;

    Ok(results)
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // Elliptical Orbit Tests
    // ========================================================================

    #[test]
    fn test_mean_to_eccentric_circular() {
        // Circular orbit: e = 0, so E = M
        let M = 1.0;
        let e = 0.0;
        let E = mean_to_eccentric_anomaly(M, e, None, None).unwrap();
        assert_relative_eq!(E, M, epsilon = 1e-10);
    }

    #[test]
    fn test_mean_to_eccentric_moderate() {
        // Moderate eccentricity
        let M = 1.0;
        let e = 0.5;
        let E = mean_to_eccentric_anomaly(M, e, None, None).unwrap();

        // Verify solution satisfies Kepler's equation
        let M_check = E - e * E.sin();
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_mean_to_eccentric_high_ecc() {
        // High eccentricity (but still elliptical)
        let M = 0.5;
        let e = 0.9;
        let E = mean_to_eccentric_anomaly(M, e, None, None).unwrap();

        let M_check = E - e * E.sin();
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_eccentric_to_true_zero() {
        // At periapsis: E = 0, ν = 0
        let E = 0.0;
        let e = 0.5;
        let nu = eccentric_to_true_anomaly(E, e).unwrap();
        assert_relative_eq!(nu, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn test_eccentric_to_true_pi() {
        // At apoapsis: E = π, ν = π
        let E = PI;
        let e = 0.3;
        let nu = eccentric_to_true_anomaly(E, e).unwrap();
        assert_relative_eq!(nu, PI, epsilon = 1e-10);
    }

    #[test]
    fn test_true_to_eccentric_roundtrip() {
        let nu_orig = 2.0;
        let e = 0.4;

        let E = true_to_eccentric_anomaly(nu_orig, e).unwrap();
        let nu_check = eccentric_to_true_anomaly(E, e).unwrap();

        assert_relative_eq!(nu_check, nu_orig, epsilon = 1e-10);
    }

    #[test]
    fn test_mean_to_true_roundtrip() {
        let M_orig = 1.5;
        let e = 0.6;

        let nu = mean_to_true_anomaly(M_orig, e, None, None).unwrap();
        let M_check = true_to_mean_anomaly(nu, e).unwrap();

        assert_relative_eq!(M_check, M_orig, epsilon = 1e-10);
    }

    #[test]
    fn test_elliptical_rejects_hyperbolic() {
        let M = 1.0;
        let e = 1.5; // hyperbolic

        let result = mean_to_eccentric_anomaly(M, e, None, None);
        assert!(result.is_err());
    }

    // ========================================================================
    // Hyperbolic Orbit Tests
    // ========================================================================

    #[test]
    fn test_mean_to_hyperbolic_basic() {
        let M = 2.0;
        let e = 1.5;
        let H = mean_to_hyperbolic_anomaly(M, e, None, None).unwrap();

        // Verify solution
        let M_check = e * H.sinh() - H;
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_mean_to_hyperbolic_high_ecc() {
        let M = 5.0;
        let e = 3.0; // very hyperbolic
        let H = mean_to_hyperbolic_anomaly(M, e, None, None).unwrap();

        let M_check = e * H.sinh() - H;
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_hyperbolic_to_true_zero() {
        // At periapsis: H = 0, ν = 0
        let H = 0.0;
        let e = 1.5;
        let nu = hyperbolic_to_true_anomaly(H, e).unwrap();
        assert_relative_eq!(nu, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn test_true_to_hyperbolic_roundtrip() {
        let nu_orig = 1.0; // Must be less than asymptote angle
        let e = 2.0;

        let H = true_to_hyperbolic_anomaly(nu_orig, e).unwrap();
        let nu_check = hyperbolic_to_true_anomaly(H, e).unwrap();

        assert_relative_eq!(nu_check, nu_orig, epsilon = 1e-10);
    }

    #[test]
    fn test_hyperbolic_mean_to_true_roundtrip() {
        let M_orig = 3.0;
        let e = 1.8;

        let nu = mean_to_true_anomaly_hyperbolic(M_orig, e, None, None).unwrap();
        let M_check = true_to_mean_anomaly_hyperbolic(nu, e).unwrap();

        assert_relative_eq!(M_check, M_orig, epsilon = 1e-10);
    }

    #[test]
    fn test_hyperbolic_rejects_elliptical() {
        let M = 1.0;
        let e = 0.5; // elliptical

        let result = mean_to_hyperbolic_anomaly(M, e, None, None);
        assert!(result.is_err());
    }

    // ========================================================================
    // Parabolic Orbit Tests
    // ========================================================================

    #[test]
    fn test_parabolic_mean_to_true_zero() {
        let M = 0.0;
        let nu = mean_to_true_anomaly_parabolic(M).unwrap();
        assert_relative_eq!(nu, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn test_parabolic_mean_to_true_positive() {
        let M = 0.5;
        let nu = mean_to_true_anomaly_parabolic(M).unwrap();

        // Verify with Barker's equation
        let D = (nu / 2.0).tan();
        let M_check = D + D.powi(3) / 3.0;
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_parabolic_mean_to_true_negative() {
        let M = -0.5;
        let nu = mean_to_true_anomaly_parabolic(M).unwrap();

        let D = (nu / 2.0).tan();
        let M_check = D + D.powi(3) / 3.0;
        assert_relative_eq!(M_check, M, epsilon = 1e-10);
    }

    #[test]
    fn test_parabolic_roundtrip() {
        let M_orig = 1.2;

        let nu = mean_to_true_anomaly_parabolic(M_orig).unwrap();
        let M_check = true_to_mean_anomaly_parabolic(nu).unwrap();

        assert_relative_eq!(M_check, M_orig, epsilon = 1e-10);
    }

    #[test]
    fn test_parabolic_symmetric() {
        // Test that M and -M give symmetric results
        let M = 0.8;

        let nu_pos = mean_to_true_anomaly_parabolic(M).unwrap();
        let nu_neg = mean_to_true_anomaly_parabolic(-M).unwrap();

        // Should be symmetric around 0
        assert_relative_eq!(nu_pos, -nu_neg.rem_euclid(2.0 * PI) + 2.0 * PI, epsilon = 1e-10);
    }

    // ========================================================================
    // Batch Processing Tests
    // ========================================================================

    #[test]
    fn test_batch_mean_to_eccentric_single_ecc() {
        // Multiple orbits, same eccentricity
        let mean_anomalies = vec![0.5, 1.0, 1.5, 2.0];
        let eccentricity = 0.5;

        let results = batch_mean_to_eccentric_anomaly(&mean_anomalies, &[eccentricity], None, None).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify each result individually
        for (i, &M) in mean_anomalies.iter().enumerate() {
            let E_individual = mean_to_eccentric_anomaly(M, eccentricity, None, None).unwrap();
            assert_relative_eq!(results[i], E_individual, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_mean_to_eccentric_multiple_ecc() {
        // Multiple orbits, different eccentricities
        let mean_anomalies = vec![0.5, 1.0, 1.5, 2.0];
        let eccentricities = vec![0.2, 0.4, 0.6, 0.8];

        let results = batch_mean_to_eccentric_anomaly(&mean_anomalies, &eccentricities, None, None).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify each result
        for i in 0..mean_anomalies.len() {
            let E_individual = mean_to_eccentric_anomaly(mean_anomalies[i], eccentricities[i], None, None).unwrap();
            assert_relative_eq!(results[i], E_individual, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_mean_to_true_elliptical() {
        let mean_anomalies = vec![0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0];
        let eccentricity = 0.6;

        let results = batch_mean_to_true_anomaly(&mean_anomalies, &[eccentricity], None, None).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify roundtrip
        for (i, &nu) in results.iter().enumerate() {
            let M_check = true_to_mean_anomaly(nu, eccentricity).unwrap();
            assert_relative_eq!(M_check, mean_anomalies[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_true_to_mean_elliptical() {
        let true_anomalies = vec![0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0];
        let eccentricity = 0.3;

        let results = batch_true_to_mean_anomaly(&true_anomalies, &[eccentricity]).unwrap();

        assert_eq!(results.len(), true_anomalies.len());

        // Verify each result
        for (i, &nu) in true_anomalies.iter().enumerate() {
            let M_individual = true_to_mean_anomaly(nu, eccentricity).unwrap();
            assert_relative_eq!(results[i], M_individual, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_mean_to_hyperbolic() {
        let mean_anomalies = vec![1.0, 2.0, 3.0, 4.0];
        let eccentricity = 1.5;

        let results = batch_mean_to_hyperbolic_anomaly(&mean_anomalies, &[eccentricity], None, None).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify each result
        for (i, &M) in mean_anomalies.iter().enumerate() {
            let H_individual = mean_to_hyperbolic_anomaly(M, eccentricity, None, None).unwrap();
            assert_relative_eq!(results[i], H_individual, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_mean_to_true_hyperbolic() {
        let mean_anomalies = vec![0.5, 1.0, 1.5, 2.0];
        let eccentricities = vec![1.2, 1.5, 2.0, 2.5];

        let results = batch_mean_to_true_anomaly_hyperbolic(&mean_anomalies, &eccentricities, None, None).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify roundtrip
        for i in 0..mean_anomalies.len() {
            let M_check = true_to_mean_anomaly_hyperbolic(results[i], eccentricities[i]).unwrap();
            assert_relative_eq!(M_check, mean_anomalies[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_mean_to_true_parabolic() {
        let mean_anomalies = vec![0.0, 0.5, 1.0, 1.5, -0.5, -1.0];

        let results = batch_mean_to_true_anomaly_parabolic(&mean_anomalies).unwrap();

        assert_eq!(results.len(), mean_anomalies.len());

        // Verify each result
        for (i, &M) in mean_anomalies.iter().enumerate() {
            let nu_individual = mean_to_true_anomaly_parabolic(M).unwrap();
            assert_relative_eq!(results[i], nu_individual, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_batch_error_length_mismatch() {
        let mean_anomalies = vec![0.5, 1.0, 1.5, 2.0];
        let eccentricities = vec![0.2, 0.4]; // Wrong length

        let result = batch_mean_to_eccentric_anomaly(&mean_anomalies, &eccentricities, None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_batch_large_array() {
        // Test performance pattern with many orbits
        let n = 100;
        let mean_anomalies: Vec<f64> = (0..n).map(|i| (i as f64) * 0.1).collect();
        let eccentricity = 0.5;

        let results = batch_mean_to_eccentric_anomaly(&mean_anomalies, &[eccentricity], None, None).unwrap();

        assert_eq!(results.len(), n);

        // Spot check a few values
        for i in [0, 25, 50, 75, 99].iter() {
            let E_individual = mean_to_eccentric_anomaly(mean_anomalies[*i], eccentricity, None, None).unwrap();
            assert_relative_eq!(results[*i], E_individual, epsilon = 1e-10);
        }
    }
}