rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
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
//! # Symbolic Special Functions
//!
//! This module provides symbolic representations and "smart" constructors for various
//! special functions commonly encountered in mathematics, physics, and engineering.
//! These constructors include built-in simplification logic for common arguments and identities.
//!
//! ## Supported Functions
//!
//! ### Gamma and Related Functions
//! - `gamma(z)` - Gamma function Γ(z)
//! - `beta(a, b)` - Beta function B(a, b)
//! - `digamma(z)` - Digamma function ψ(z) = Γ'(z)/Γ(z)
//! - `polygamma(n, z)` - Polygamma function ψ⁽ⁿ⁾(z)
//! - `ln_gamma(z)` - Log-gamma function ln(Γ(z))
//!
//! ### Error Functions
//! - `erf(z)` - Error function
//! - `erfc(z)` - Complementary error function 1 - erf(z)
//! - `erfi(z)` - Imaginary error function -i·erf(iz)
//!
//! ### Number Theory
//! - `zeta(s)` - Riemann zeta function ζ(s)
//!
//! ### Bessel Functions
//! - `bessel_j(n, x)` - Bessel function of first kind Jₙ(x)
//! - `bessel_y(n, x)` - Bessel function of second kind Yₙ(x)
//! - `bessel_i(n, x)` - Modified Bessel function of first kind Iₙ(x)
//! - `bessel_k(n, x)` - Modified Bessel function of second kind Kₙ(x)
//!
//! ### Orthogonal Polynomials
//! - `legendre_p(n, x)` - Legendre polynomial Pₙ(x)
//! - `laguerre_l(n, x)` - Laguerre polynomial Lₙ(x)
//! - `hermite_h(n, x)` - Hermite polynomial Hₙ(x)
//! - `chebyshev_t(n, x)` - Chebyshev polynomial of first kind Tₙ(x)
//! - `chebyshev_u(n, x)` - Chebyshev polynomial of second kind Uₙ(x)
//!
//! ### Differential Equations
//! - `bessel_differential_equation` - x²y'' + xy' + (x² - n²)y = 0
//! - `legendre_differential_equation` - (1-x²)y'' - 2xy' + n(n+1)y = 0
//! - `laguerre_differential_equation` - xy'' + (1-x)y' + ny = 0
//! - `hermite_differential_equation` - y'' - 2xy' + 2ny = 0
//!
//! ### Rodrigues Formulas
//! - `legendre_rodrigues_formula` - Pₙ(x) = (1/2ⁿn!)·dⁿ/dxⁿ[(x²-1)ⁿ]
//! - `hermite_rodrigues_formula` - Hₙ(x) = (-1)ⁿeˣ²·dⁿ/dxⁿ[e⁻ˣ²]
//!
//! ## Examples
//!
//! ### Gamma Function
//! ```
//! use rssn::symbolic::core::Expr;
//! use rssn::symbolic::special_functions::gamma;
//!
//! // Γ(5) = 4! = 24
//! let g = gamma(&Expr::Constant(5.0));
//!
//! assert_eq!(g, Expr::Constant(24.0));
//!
//! // Γ(0.5) = √π
//! let g_half = gamma(&Expr::Constant(0.5));
//! // Returns Expr::Sqrt(Expr::Pi)
//! ```
//!
//! ### Orthogonal Polynomials
//! ```
//! use rssn::symbolic::core::Expr;
//! use rssn::symbolic::special_functions::hermite_h;
//! use rssn::symbolic::special_functions::legendre_p;
//!
//! // P₀(x) = 1
//! let p0 = legendre_p(&Expr::Constant(0.0), Expr::Variable("x".to_string()));
//!
//! assert_eq!(p0, Expr::Constant(1.0));
//!
//! // H₀(x) = 1, H₁(x) = 2x
//! let h0 = hermite_h(&Expr::Constant(0.0), Expr::Variable("x".to_string()));
//!
//! assert_eq!(h0, Expr::Constant(1.0));
//! ```

use std::sync::Arc;

use crate::symbolic::calculus::differentiate;
use crate::symbolic::calculus::factorial;
use crate::symbolic::core::Expr;
use crate::symbolic::simplify::is_zero;
use crate::symbolic::simplify_dag::simplify;

// ============================================================================
// Gamma and Related Functions
// ============================================================================

/// Symbolic representation and smart constructor for the Gamma function, `Γ(z)`.
///
/// The Gamma function is an extension of the factorial function to complex numbers.
/// This constructor includes simplification rules for integer and half-integer arguments,
/// as well as the recurrence relation `Γ(z+1) = zΓ(z)`.
///
/// # Arguments
/// * `arg` - The argument `z` of the Gamma function.
///
/// # Returns
/// An `Expr` representing `Γ(z)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::gamma;
///
/// // Γ(5) = 4! = 24
/// let g = gamma(&Expr::Constant(5.0));
///
/// assert_eq!(g, Expr::Constant(24.0));
/// ```
#[must_use]
pub fn gamma(arg: &Expr) -> Expr {
    let s_arg = simplify(arg);

    if let Some(n) = s_arg.to_f64() {
        if n > 0.0 && n.fract() == 0.0 {
            return Expr::Constant(factorial(((n - 1.0) as i64).try_into().unwrap_or(0)));
        }

        if (n - 0.5).abs() < 1e-9 {
            return Expr::new_sqrt(Expr::Pi);
        }
    }

    if s_arg == Expr::Constant(1.0) {
        return Expr::Constant(1.0);
    }

    if let Expr::Add(a, b) = &s_arg {
        if **b == Expr::Constant(1.0) {
            return simplify(&Expr::new_mul(a.clone(), gamma(&a.as_ref().clone())));
        }

        if **a == Expr::Constant(1.0) {
            return simplify(&Expr::new_mul(b.clone(), gamma(&b.as_ref().clone())));
        }
    }

    Expr::new_gamma(s_arg)
}

/// Symbolic representation for the log-gamma function, `ln(Γ(z))`.
///
/// The log-gamma function is useful for avoiding overflow when computing
/// the gamma function for large arguments.
///
/// # Arguments
/// * `arg` - The argument `z`.
///
/// # Returns
/// An `Expr` representing `ln(Γ(z))`.
#[must_use]
pub fn ln_gamma(arg: &Expr) -> Expr {
    let g = gamma(arg);

    // If gamma simplified to a constant, compute the log
    if let Expr::Constant(v) = &g {
        if *v > 0.0 {
            return Expr::Constant(v.ln());
        }
    }

    Expr::new_log(g)
}

/// Symbolic representation and smart constructor for the Beta function, `B(x, y)`.
///
/// The Beta function is closely related to the Gamma function by the identity:
/// `B(x, y) = Γ(x)Γ(y) / Γ(x+y)`.
///
/// # Arguments
/// * `a` - The first argument `x`.
/// * `b` - The second argument `y`.
///
/// # Returns
/// An `Expr` representing `B(x, y)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::beta;
///
/// // B(1, 1) = 1
/// let b = beta(Expr::Constant(1.0), Expr::Constant(1.0));
///
/// assert_eq!(b, Expr::Constant(1.0));
/// ```
#[must_use]
pub fn beta(
    a: Expr,
    b: Expr,
) -> Expr {
    let gamma_a = gamma(&a);

    let gamma_b = gamma(&b);

    let gamma_a_plus_b = gamma(&simplify(&Expr::new_add(a, b)));

    simplify(&Expr::new_div(
        Expr::new_mul(gamma_a, gamma_b),
        gamma_a_plus_b,
    ))
}

/// Symbolic representation and smart constructor for the Digamma function, `ψ(z)`.
///
/// The Digamma function is the logarithmic derivative of the Gamma function: `ψ(z) = Γ'(z)/Γ(z)`.
/// This constructor includes simplification rules for `ψ(1)` (related to Euler-Mascheroni constant)
/// and the recurrence relation `ψ(z+1) = ψ(z) + 1/z`.
///
/// # Arguments
/// * `arg` - The argument `z` of the Digamma function.
///
/// # Returns
/// An `Expr` representing `ψ(z)`.
#[must_use]
pub fn digamma(arg: &Expr) -> Expr {
    let s_arg = simplify(arg);

    if let Some(n) = s_arg.to_f64() {
        if (n - 1.0).abs() < 1e-9 {
            return Expr::Variable("-gamma".to_string());
        }
    }

    if let Expr::Add(a, b) = &s_arg {
        if **b == Expr::Constant(1.0) {
            return simplify(&Expr::new_add(
                digamma(&a.as_ref().clone()),
                Expr::new_div(Expr::Constant(1.0), a.clone()),
            ));
        }

        if **a == Expr::Constant(1.0) {
            return simplify(&Expr::new_add(
                digamma(&b.as_ref().clone()),
                Expr::new_div(Expr::Constant(1.0), b.clone()),
            ));
        }
    }

    Expr::new_digamma(s_arg)
}

/// Symbolic representation for the Polygamma function, `ψ⁽ⁿ⁾(z)`.
///
/// The polygamma function is the n-th derivative of the digamma function.
/// `ψ⁽⁰⁾(z) = ψ(z)` (digamma)
/// `ψ⁽¹⁾(z)` is called the trigamma function
/// `ψ⁽²⁾(z)` is called the tetragamma function, etc.
///
/// # Arguments
/// * `n` - The order of the derivative (non-negative integer).
/// * `z` - The argument of the function.
///
/// # Returns
/// An `Expr` representing `ψ⁽ⁿ⁾(z)`.
#[must_use]
pub fn polygamma(
    n: &Expr,
    z: &Expr,
) -> Expr {
    let s_n = simplify(n);

    let s_z = simplify(z);

    // ψ⁽⁰⁾(z) = ψ(z)
    if let Some(order) = s_n.to_f64() {
        if order.abs() < 1e-9 {
            return digamma(&s_z);
        }
    }

    // Use BinaryList for polygamma as it's not in the core constructors
    Expr::BinaryList("polygamma".to_string(), Arc::new(s_n), Arc::new(s_z))
}

// ============================================================================
// Error Functions
// ============================================================================

/// Symbolic representation and smart constructor for the Error Function, `erf(z)`.
///
/// The error function is a special function of sigmoid shape that arises in probability,
/// statistics, and partial differential equations. This constructor includes simplification
/// rules for `erf(0)` and `erf(-z)`.
///
/// # Arguments
/// * `arg` - The argument `z` of the error function.
///
/// # Returns
/// An `Expr` representing `erf(z)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::erf;
///
/// // erf(0) = 0
/// let e = erf(&Expr::Constant(0.0));
///
/// assert_eq!(e, Expr::Constant(0.0));
/// ```
#[must_use]
pub fn erf(arg: &Expr) -> Expr {
    let s_arg = simplify(arg);

    if is_zero(&s_arg) {
        return Expr::Constant(0.0);
    }

    if let Expr::Neg(inner) = s_arg {
        return Expr::new_neg(erf(&(*inner).clone()));
    }

    Expr::new_erf(s_arg)
}

/// Symbolic representation and smart constructor for the Complementary Error Function, `erfc(z)`.
///
/// Defined as `erfc(z) = 1 - erf(z)`.
///
/// # Arguments
/// * `arg` - The argument `z`.
///
/// # Returns
/// An `Expr` representing `erfc(z)`.
#[must_use]
pub fn erfc(arg: &Expr) -> Expr {
    simplify(&Expr::new_sub(Expr::Constant(1.0), erf(arg)))
}

/// Symbolic representation and smart constructor for the Imaginary Error Function, `erfi(z)`.
///
/// Defined as `erfi(z) = -i * erf(iz)`.
///
/// # Arguments
/// * `arg` - The argument `z`.
///
/// # Returns
/// An `Expr` representing `erfi(z)`.
#[must_use]
pub fn erfi(arg: Expr) -> Expr {
    let i = Expr::new_complex(Expr::Constant(0.0), Expr::Constant(1.0));

    simplify(&Expr::new_mul(
        Expr::new_neg(i.clone()),
        erf(&Expr::new_mul(i, arg)),
    ))
}

// ============================================================================
// Riemann Zeta Function
// ============================================================================

/// Symbolic representation and smart constructor for the Riemann Zeta function, `ζ(s)`.
///
/// The Riemann Zeta function is a central object in number theory, with connections
/// to the distribution of prime numbers. This constructor includes simplification
/// rules for specific integer arguments (e.g., `ζ(0)`, `ζ(1)`, `ζ(2)`) and trivial zeros.
///
/// # Arguments
/// * `arg` - The argument `s` of the Zeta function.
///
/// # Returns
/// An `Expr` representing `ζ(s)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::zeta;
///
/// // ζ(0) = -1/2
/// let z0 = zeta(&Expr::Constant(0.0));
///
/// assert_eq!(z0, Expr::Constant(-0.5));
/// ```
#[must_use]
pub fn zeta(arg: &Expr) -> Expr {
    let s_arg = simplify(arg);

    if let Some(n) = s_arg.to_f64() {
        if n.fract() == 0.0 {
            let n_int = n as i32;

            if n_int == 0 {
                return Expr::Constant(-0.5);
            }

            if n_int == 1 {
                return Expr::Infinity;
            }

            if n_int == 2 {
                return simplify(&Expr::new_div(
                    Expr::new_pow(Expr::Pi, Expr::Constant(2.0)),
                    Expr::Constant(6.0),
                ));
            }

            if n_int == 4 {
                return simplify(&Expr::new_div(
                    Expr::new_pow(Expr::Pi, Expr::Constant(4.0)),
                    Expr::Constant(90.0),
                ));
            }

            if n_int < 0 && n_int % 2 == 0 {
                return Expr::Constant(0.0);
            }
        }
    }

    Expr::new_zeta(s_arg)
}

// ============================================================================
// Bessel Functions
// ============================================================================

/// Symbolic representation and smart constructor for the Bessel function of the first kind, `J_n(x)`.
///
/// Bessel functions are solutions to Bessel's differential equation and arise in many
/// problems of wave propagation and potential theory. This constructor includes simplification
/// rules for `J_n(0)` and `J_{-n}(x)`.
///
/// # Arguments
/// * `order` - The order `n` of the Bessel function.
/// * `arg` - The argument `x` of the Bessel function.
///
/// # Returns
/// An `Expr` representing `J_n(x)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::bessel_j;
///
/// // J_0(0) = 1
/// let j0 = bessel_j(&Expr::Constant(0.0), &Expr::Constant(0.0));
///
/// assert_eq!(j0, Expr::Constant(1.0));
/// ```
#[must_use]
pub fn bessel_j(
    order: &Expr,
    arg: &Expr,
) -> Expr {
    let s_order = simplify(order);

    let s_arg = simplify(arg);

    if is_zero(&s_arg) {
        if let Some(n) = s_order.to_f64() {
            if n == 0.0 {
                return Expr::Constant(1.0);
            }

            if n > 0.0 {
                return Expr::Constant(0.0);
            }
        }
    }

    if let Expr::Neg(inner_order) = &s_order {
        if let Some(n) = inner_order.to_f64() {
            if n.fract() == 0.0 {
                let factor = Expr::new_pow(Expr::Constant(-1.0), Expr::Constant(n));

                return simplify(&Expr::new_mul(
                    factor,
                    bessel_j(&inner_order.as_ref().clone(), &s_arg),
                ));
            }
        }
    }

    Expr::new_bessel_j(s_order, s_arg)
}

/// Symbolic representation and smart constructor for the Bessel function of the second kind, `Y_n(x)`.
///
/// Bessel functions of the second kind are also solutions to Bessel's differential equation,
/// linearly independent from `J_n(x)`. They typically have a singularity at the origin.
///
/// # Arguments
/// * `order` - The order `n` of the Bessel function.
/// * `arg` - The argument `x` of the Bessel function.
///
/// # Returns
/// An `Expr` representing `Y_n(x)`.
#[must_use]
pub fn bessel_y(
    order: &Expr,
    arg: &Expr,
) -> Expr {
    let s_order = simplify(order);

    let s_arg = simplify(arg);

    if is_zero(&s_arg) {
        return Expr::NegativeInfinity;
    }

    Expr::new_bessel_y(s_order, s_arg)
}

/// Symbolic representation for the Modified Bessel function of the first kind, `I_n(x)`.
///
/// Modified Bessel functions satisfy the modified Bessel equation and arise in
/// problems with cylindrical symmetry involving non-oscillatory behavior.
///
/// # Arguments
/// * `order` - The order `n` of the Bessel function.
/// * `arg` - The argument `x` of the Bessel function.
///
/// # Returns
/// An `Expr` representing `I_n(x)`.
#[must_use]
pub fn bessel_i(
    order: &Expr,
    arg: &Expr,
) -> Expr {
    let s_order = simplify(order);

    let s_arg = simplify(arg);

    if is_zero(&s_arg) {
        if let Some(n) = s_order.to_f64() {
            if n == 0.0 {
                return Expr::Constant(1.0);
            }

            if n > 0.0 {
                return Expr::Constant(0.0);
            }
        }
    }

    // Use BinaryList for bessel_i as it's not in the core constructors
    Expr::BinaryList("bessel_i".to_string(), Arc::new(s_order), Arc::new(s_arg))
}

/// Symbolic representation for the Modified Bessel function of the second kind, `K_n(x)`.
///
/// This function is also known as the Macdonald function or Basset function.
///
/// # Arguments
/// * `order` - The order `n` of the Bessel function.
/// * `arg` - The argument `x` of the Bessel function.
///
/// # Returns
/// An `Expr` representing `K_n(x)`.
#[must_use]
pub fn bessel_k(
    order: &Expr,
    arg: &Expr,
) -> Expr {
    let s_order = simplify(order);

    let s_arg = simplify(arg);

    if is_zero(&s_arg) {
        return Expr::Infinity;
    }

    // Use BinaryList for bessel_k as it's not in the core constructors
    Expr::BinaryList("bessel_k".to_string(), Arc::new(s_order), Arc::new(s_arg))
}

// ============================================================================
// Orthogonal Polynomials
// ============================================================================

/// Symbolic representation and smart constructor for the Legendre Polynomials, `P_n(x)`.
///
/// Legendre polynomials are solutions to Legendre's differential equation and form a complete
/// orthogonal set over the interval `[-1, 1]`. They are widely used in physics and engineering.
/// This constructor includes simplification rules for `P_0(x)`, `P_1(x)`, and the recurrence relation.
///
/// # Arguments
/// * `degree` - The degree `n` of the Legendre polynomial.
/// * `arg` - The argument `x` of the polynomial.
///
/// # Returns
/// An `Expr` representing `P_n(x)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::legendre_p;
///
/// // P_0(x) = 1
/// let p0 = legendre_p(&Expr::Constant(0.0), Expr::Variable("x".to_string()));
///
/// assert_eq!(p0, Expr::Constant(1.0));
/// ```
#[must_use]
pub fn legendre_p(
    degree: &Expr,
    arg: Expr,
) -> Expr {
    let s_degree = simplify(degree);

    if let Some(n) = s_degree.to_f64() {
        let n_int = n as i32;

        if n >= 0.0 && n.fract() == 0.0 {
            if n_int == 0 {
                return Expr::Constant(1.0);
            }

            if n_int == 1 {
                return arg;
            }

            // Use recurrence for small n only to avoid stack overflow
            if n_int <= 10 {
                let p_n = legendre_p(&Expr::Constant(n - 1.0), arg.clone());

                let p_n_minus_1 = legendre_p(&Expr::Constant(n - 2.0), arg.clone());

                let term1 = Expr::new_mul(
                    Expr::Constant(2.0f64.mul_add(n, -1.0)),
                    Expr::new_mul(arg, p_n),
                );

                let term2 = Expr::new_mul(Expr::Constant(n - 1.0), p_n_minus_1);

                return simplify(&Expr::new_div(
                    Expr::new_sub(term1, term2),
                    Expr::Constant(n),
                ));
            }
        }
    }

    Expr::new_legendre_p(s_degree, arg)
}

/// Symbolic representation and smart constructor for the Laguerre Polynomials, `L_n(x)`.
///
/// Laguerre polynomials are solutions to Laguerre's differential equation and are important
/// in quantum mechanics (e.g., hydrogen atom wave functions) and other areas.
/// This constructor includes simplification rules for `L_0(x)`, `L_1(x)`, and the recurrence relation.
///
/// # Arguments
/// * `degree` - The degree `n` of the Laguerre polynomial.
/// * `arg` - The argument `x` of the polynomial.
///
/// # Returns
/// An `Expr` representing `L_n(x)`.
#[must_use]
pub fn laguerre_l(
    degree: &Expr,
    arg: Expr,
) -> Expr {
    let s_degree = simplify(degree);

    if let Some(n) = s_degree.to_f64() {
        let n_int = n as i32;

        if n >= 0.0 && n.fract() == 0.0 {
            if n_int == 0 {
                return Expr::Constant(1.0);
            }

            if n_int == 1 {
                return simplify(&Expr::new_sub(Expr::Constant(1.0), arg));
            }

            // Use recurrence for small n only
            if n_int <= 10 {
                let l_n = laguerre_l(&Expr::Constant(n - 1.0), arg.clone());

                let l_n_minus_1 = laguerre_l(&Expr::Constant(n - 2.0), arg.clone());

                let term1_factor =
                    simplify(&Expr::new_sub(Expr::Constant(2.0f64.mul_add(n, -1.0)), arg));

                let term1 = Expr::new_mul(term1_factor, l_n);

                let term2 = Expr::new_mul(Expr::Constant(n - 1.0), l_n_minus_1);

                return simplify(&Expr::new_div(
                    Expr::new_sub(term1, term2),
                    Expr::Constant(n),
                ));
            }
        }
    }

    Expr::new_laguerre_l(s_degree, arg)
}

/// Symbolic representation for the Generalized Laguerre Polynomials, `L_n^α(x)`.
///
/// Generalized (associated) Laguerre polynomials appear in the radial part of
/// the hydrogen atom wave function.
///
/// # Arguments
/// * `n` - The degree.
/// * `alpha` - The generalization parameter.
/// * `x` - The argument.
///
/// # Returns
/// An `Expr` representing `L_n^α(x)`.
#[must_use]
pub fn generalized_laguerre(
    n: &Expr,
    alpha: &Expr,
    x: &Expr,
) -> Expr {
    let s_n = simplify(n);

    let s_alpha = simplify(alpha);

    let s_x = simplify(x);

    // L_n^0(x) = L_n(x)
    if let Some(a) = s_alpha.to_f64() {
        if a.abs() < 1e-9 {
            return laguerre_l(&s_n, s_x);
        }
    }

    // L_0^α(x) = 1
    if let Some(n_val) = s_n.to_f64() {
        if n_val.abs() < 1e-9 {
            return Expr::Constant(1.0);
        }
    }

    // Use NaryList for generalized_laguerre
    Expr::NaryList("generalized_laguerre".to_string(), vec![s_n, s_alpha, s_x])
}

/// Symbolic representation and smart constructor for the Hermite Polynomials, `H_n(x)`.
///
/// Hermite polynomials are solutions to Hermite's differential equation and are crucial
/// in quantum mechanics (e.g., harmonic oscillator) and probability theory.
/// This constructor includes simplification rules for `H_0(x)`, `H_1(x)`, and the recurrence relation.
///
/// # Arguments
/// * `degree` - The degree `n` of the Hermite polynomial.
/// * `arg` - The argument `x` of the polynomial.
///
/// # Returns
/// An `Expr` representing `H_n(x)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::hermite_h;
///
/// // H_0(x) = 1
/// let h0 = hermite_h(&Expr::Constant(0.0), Expr::Variable("x".to_string()));
///
/// assert_eq!(h0, Expr::Constant(1.0));
/// ```
#[must_use]
pub fn hermite_h(
    degree: &Expr,
    arg: Expr,
) -> Expr {
    let s_degree = simplify(degree);

    if let Some(n) = s_degree.to_f64() {
        let n_int = n as i32;

        if n >= 0.0 && n.fract() == 0.0 {
            if n_int == 0 {
                return Expr::Constant(1.0);
            }

            if n_int == 1 {
                return simplify(&Expr::new_mul(Expr::Constant(2.0), arg));
            }

            // Use recurrence for small n only
            if n_int <= 10 {
                let h_n = hermite_h(&Expr::Constant(n - 1.0), arg.clone());

                let h_n_minus_1 = hermite_h(&Expr::Constant(n - 2.0), arg.clone());

                let term1 = Expr::new_mul(Expr::Constant(2.0), Expr::new_mul(arg, h_n));

                let term2 = Expr::new_mul(Expr::Constant(2.0 * (n - 1.0)), h_n_minus_1);

                return simplify(&Expr::new_sub(term1, term2));
            }
        }
    }

    Expr::new_hermite_h(s_degree, arg)
}

/// Symbolic representation for the Chebyshev Polynomial of the first kind, `T_n(x)`.
///
/// Chebyshev polynomials are defined by `T_n(cos(θ)) = cos(nθ)` and are important
/// in approximation theory and numerical analysis.
///
/// # Arguments
/// * `n` - The degree.
/// * `x` - The argument.
///
/// # Returns
/// An `Expr` representing `T_n(x)`.
///
/// # Examples
/// ```
/// use rssn::symbolic::core::Expr;
/// use rssn::symbolic::special_functions::chebyshev_t;
///
/// // T_0(x) = 1, T_1(x) = x
/// let t0 = chebyshev_t(&Expr::Constant(0.0), &Expr::Variable("x".to_string()));
///
/// assert_eq!(t0, Expr::Constant(1.0));
/// ```
#[must_use]
pub fn chebyshev_t(
    n: &Expr,
    x: &Expr,
) -> Expr {
    let s_n = simplify(n);

    let s_x = simplify(x);

    if let Some(n_val) = s_n.to_f64() {
        let n_int = n_val as i32;

        if n_val >= 0.0 && n_val.fract() == 0.0 {
            if n_int == 0 {
                return Expr::Constant(1.0);
            }

            if n_int == 1 {
                return s_x;
            }

            // Recurrence: T_n(x) = 2x*T_{n-1}(x) - T_{n-2}(x)
            if n_int <= 10 {
                let t_n1 = chebyshev_t(&Expr::Constant(n_val - 1.0), &s_x);

                let t_n2 = chebyshev_t(&Expr::Constant(n_val - 2.0), &s_x);

                return simplify(&Expr::new_sub(
                    Expr::new_mul(Expr::Constant(2.0), Expr::new_mul(s_x, t_n1)),
                    t_n2,
                ));
            }
        }
    }

    // Use BinaryList for chebyshev_t
    Expr::BinaryList("chebyshev_t".to_string(), Arc::new(s_n), Arc::new(s_x))
}

/// Symbolic representation for the Chebyshev Polynomial of the second kind, `U_n(x)`.
///
/// Chebyshev polynomials of the second kind satisfy `U_n(cos(θ))·sin(θ) = sin((n+1)θ)`.
///
/// # Arguments
/// * `n` - The degree.
/// * `x` - The argument.
///
/// # Returns
/// An `Expr` representing `U_n(x)`.
#[must_use]
pub fn chebyshev_u(
    n: &Expr,
    x: &Expr,
) -> Expr {
    let s_n = simplify(n);

    let s_x = simplify(x);

    if let Some(n_val) = s_n.to_f64() {
        let n_int = n_val as i32;

        if n_val >= 0.0 && n_val.fract() == 0.0 {
            if n_int == 0 {
                return Expr::Constant(1.0);
            }

            if n_int == 1 {
                return simplify(&Expr::new_mul(Expr::Constant(2.0), s_x));
            }

            // Recurrence: U_n(x) = 2x*U_{n-1}(x) - U_{n-2}(x)
            if n_int <= 10 {
                let u_n1 = chebyshev_u(&Expr::Constant(n_val - 1.0), &s_x);

                let u_n2 = chebyshev_u(&Expr::Constant(n_val - 2.0), &s_x);

                return simplify(&Expr::new_sub(
                    Expr::new_mul(Expr::Constant(2.0), Expr::new_mul(s_x, u_n1)),
                    u_n2,
                ));
            }
        }
    }

    // Use BinaryList for chebyshev_u
    Expr::BinaryList("chebyshev_u".to_string(), Arc::new(s_n), Arc::new(s_x))
}

// ============================================================================
// Differential Equations
// ============================================================================

/// Represents Bessel's differential equation: `x²y'' + xy' + (x² - n²)y = 0`.
///
/// This second-order linear ordinary differential equation is fundamental in many areas
/// of physics and engineering, particularly in problems involving cylindrical symmetry.
/// Its solutions are known as Bessel functions.
///
/// # Arguments
/// * `y` - The unknown function `y(x)`.
/// * `x` - The independent variable `x`.
/// * `n` - The order `n` of the Bessel equation.
///
/// # Returns
/// An `Expr::Eq` representing Bessel's differential equation.
#[must_use]
pub fn bessel_differential_equation(
    y: &Expr,
    x: &Expr,
    n: &Expr,
) -> Expr {
    let y_prime = differentiate(y, "x");

    let y_double_prime = differentiate(&y_prime, "x");

    let term1 = Expr::new_mul(
        Expr::new_pow(x.clone(), Expr::Constant(2.0)),
        y_double_prime,
    );

    let term2 = Expr::new_mul(x.clone(), y_prime);

    let term3 = Expr::new_mul(
        Expr::new_sub(
            Expr::new_pow(x.clone(), Expr::Constant(2.0)),
            Expr::new_pow(n.clone(), Expr::Constant(2.0)),
        ),
        y.clone(),
    );

    Expr::Eq(
        Arc::new(Expr::new_add(term1, Expr::new_add(term2, term3))),
        Arc::new(Expr::Constant(0.0)),
    )
}

/// Represents Legendre's differential equation: `(1-x²)y'' - 2xy' + n(n+1)y = 0`.
///
/// This second-order linear ordinary differential equation arises in the solution of
/// Laplace's equation in spherical coordinates. Its solutions are Legendre polynomials.
///
/// # Arguments
/// * `y` - The unknown function `y(x)`.
/// * `x` - The independent variable `x`.
/// * `n` - The degree `n` of the Legendre equation.
///
/// # Returns
/// An `Expr::Eq` representing Legendre's differential equation.
#[must_use]
pub fn legendre_differential_equation(
    y: &Expr,
    x: &Expr,
    n: &Expr,
) -> Expr {
    let y_prime = differentiate(y, "x");

    let y_double_prime = differentiate(&y_prime, "x");

    let term1 = Expr::new_mul(
        Expr::new_sub(
            Expr::Constant(1.0),
            Expr::new_pow(x.clone(), Expr::Constant(2.0)),
        ),
        y_double_prime,
    );

    let term2 = Expr::new_mul(Expr::Constant(-2.0), Expr::new_mul(x.clone(), y_prime));

    let term3 = Expr::new_mul(
        Expr::new_mul(n.clone(), Expr::new_add(n.clone(), Expr::Constant(1.0))),
        y.clone(),
    );

    Expr::Eq(
        Arc::new(Expr::new_add(term1, Expr::new_add(term2, term3))),
        Arc::new(Expr::Constant(0.0)),
    )
}

/// Represents Rodrigues' Formula for Legendre Polynomials: `P_n(x) = (1/(2^n n!)) * d^n/dx^n [(x^2 - 1)^n]`.
///
/// This formula provides a compact way to define Legendre polynomials and is useful
/// for deriving their properties.
///
/// # Arguments
/// * `n` - The degree `n` of the Legendre polynomial.
/// * `x` - The independent variable `x`.
///
/// # Returns
/// An `Expr::Eq` representing Rodrigues' formula.
#[must_use]
pub fn legendre_rodrigues_formula(
    n: &Expr,
    x: &Expr,
) -> Expr {
    let n_f64 = if let Expr::Constant(val) = n {
        *val
    } else {
        return Expr::new_legendre_p(n.clone(), x.clone());
    };

    let n_factorial = Expr::Constant(if n_f64 >= 0.0 {
        factorial((n_f64 as i64).try_into().unwrap_or(0))
    } else {
        f64::NAN
    });

    Expr::Eq(
        Arc::new(legendre_p(&n.clone(), x.clone())),
        Arc::new(Expr::new_mul(
            Expr::new_div(
                Expr::Constant(1.0),
                Expr::new_mul(Expr::new_pow(Expr::Constant(2.0), n.clone()), n_factorial),
            ),
            Expr::DerivativeN(
                Arc::new(Expr::new_pow(
                    Expr::new_sub(
                        Expr::new_pow(x.clone(), Expr::Constant(2.0)),
                        Expr::Constant(1.0),
                    ),
                    n.clone(),
                )),
                "x".to_string(),
                Arc::new(n.clone()),
            ),
        )),
    )
}

/// Represents Laguerre's differential equation: `xy'' + (1-x)y' + ny = 0`.
///
/// This second-order linear ordinary differential equation arises in the quantum mechanical
/// treatment of the hydrogen atom. Its solutions are Laguerre polynomials.
///
/// # Arguments
/// * `y` - The unknown function `y(x)`.
/// * `x` - The independent variable `x`.
/// * `n` - The degree `n` of the Laguerre equation.
///
/// # Returns
/// An `Expr::Eq` representing Laguerre's differential equation.
#[must_use]
pub fn laguerre_differential_equation(
    y: &Expr,
    x: &Expr,
    n: &Expr,
) -> Expr {
    let y_prime = differentiate(y, "x");

    let y_double_prime = differentiate(&y_prime, "x");

    let term1 = Expr::new_mul(x.clone(), y_double_prime);

    let term2 = Expr::new_mul(Expr::new_sub(Expr::Constant(1.0), x.clone()), y_prime);

    let term3 = Expr::new_mul(n.clone(), y.clone());

    Expr::Eq(
        Arc::new(Expr::new_add(term1, Expr::new_add(term2, term3))),
        Arc::new(Expr::Constant(0.0)),
    )
}

/// Represents Hermite's differential equation: `y'' - 2xy' + 2ny = 0`.
///
/// This second-order linear ordinary differential equation is important in quantum mechanics
/// (e.g., for the quantum harmonic oscillator) and probability theory. Its solutions are Hermite polynomials.
///
/// # Arguments
/// * `y` - The unknown function `y(x)`.
/// * `x` - The independent variable `x`.
/// * `n` - The degree `n` of the Hermite equation.
///
/// # Returns
/// An `Expr::Eq` representing Hermite's differential equation.
#[must_use]
pub fn hermite_differential_equation(
    y: &Expr,
    x: &Expr,
    n: &Expr,
) -> Expr {
    let y_prime = differentiate(y, "x");

    let y_double_prime = differentiate(&y_prime, "x");

    let term1 = y_double_prime;

    let term2 = Expr::new_mul(Expr::Constant(-2.0), Expr::new_mul(x.clone(), y_prime));

    let term3 = Expr::new_mul(Expr::new_mul(Expr::Constant(2.0), n.clone()), y.clone());

    Expr::Eq(
        Arc::new(Expr::new_add(term1, Expr::new_add(term2, term3))),
        Arc::new(Expr::Constant(0.0)),
    )
}

/// Represents Rodrigues' Formula for Hermite Polynomials: `H_n(x) = (-1)^n e^(x^2) d^n/dx^n [e^(-x^2)]`.
///
/// This formula provides a compact way to define Hermite polynomials and is useful
/// for deriving their properties.
///
/// # Arguments
/// * `n` - The degree `n` of the Hermite polynomial.
/// * `x` - The independent variable `x`.
///
/// # Returns
/// An `Expr::Eq` representing Rodrigues' formula.
#[must_use]
pub fn hermite_rodrigues_formula(
    n: &Expr,
    x: &Expr,
) -> Expr {
    Expr::Eq(
        Arc::new(hermite_h(&n.clone(), x.clone())),
        Arc::new(Expr::new_mul(
            Expr::new_pow(Expr::Constant(-1.0), n.clone()),
            Expr::new_mul(
                Expr::new_exp(Expr::new_pow(x.clone(), Expr::Constant(2.0))),
                Expr::DerivativeN(
                    Arc::new(Expr::new_exp(Expr::new_neg(Expr::new_pow(
                        x.clone(),
                        Expr::Constant(2.0),
                    )))),
                    "x".to_string(),
                    Arc::new(n.clone()),
                ),
            ),
        )),
    )
}

/// Represents Chebyshev's differential equation: `(1-x²)y'' - xy' + n²y = 0`.
///
/// This differential equation has Chebyshev polynomials as its solutions.
///
/// # Arguments
/// * `y` - The unknown function.
/// * `x` - The independent variable.
/// * `n` - The degree.
///
/// # Returns
/// An `Expr::Eq` representing Chebyshev's differential equation.
#[must_use]
pub fn chebyshev_differential_equation(
    y: &Expr,
    x: &Expr,
    n: &Expr,
) -> Expr {
    let y_prime = differentiate(y, "x");

    let y_double_prime = differentiate(&y_prime, "x");

    let term1 = Expr::new_mul(
        Expr::new_sub(
            Expr::Constant(1.0),
            Expr::new_pow(x.clone(), Expr::Constant(2.0)),
        ),
        y_double_prime,
    );

    let term2 = Expr::new_neg(Expr::new_mul(x.clone(), y_prime));

    let term3 = Expr::new_mul(Expr::new_pow(n.clone(), Expr::Constant(2.0)), y.clone());

    Expr::Eq(
        Arc::new(Expr::new_add(term1, Expr::new_add(term2, term3))),
        Arc::new(Expr::Constant(0.0)),
    )
}