scirs2-special 0.4.2

Special functions module for SciRS2 (scirs2-special)
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
//! q-Analogs of Classical Special Functions
//!
//! This module implements q-analogs of fundamental special functions. A q-analog of a mathematical
//! formula is a generalization involving a parameter q that recovers the original formula in the
//! limit q → 1. These functions appear extensively in:
//!
//! - Combinatorics and enumerative combinatorics
//! - Quantum groups and quantum algebra
//! - Basic hypergeometric series (Gasper & Rahman)
//! - Statistical mechanics (exactly solvable models)
//! - Number theory (partition functions, modular forms)
//!
//! ## Mathematical Background
//!
//! ### q-Pochhammer Symbol
//!
//! The q-Pochhammer symbol (a; q)_n is the fundamental building block:
//! ```text
//! (a; q)_n = prod_{k=0}^{n-1} (1 - a*q^k)
//!          = (1-a)(1-aq)(1-aq^2)...(1-aq^{n-1})
//! ```
//! With the convention (a; q)_0 = 1.
//!
//! ### q-Numbers and q-Factorial
//!
//! The q-analog of an integer n is:
//! ```text
//! [n]_q = (1 - q^n) / (1 - q)  for q ≠ 1
//!       = n                     for q = 1
//! ```
//!
//! The q-factorial is:
//! ```text
//! [n]_q! = [1]_q * [2]_q * ... * [n]_q = (q; q)_n / (1-q)^n
//! ```
//!
//! ### Gaussian Binomial Coefficient
//!
//! The q-binomial (Gaussian binomial) coefficient is:
//! ```text
//! [n choose k]_q = [n]_q! / ([k]_q! * [n-k]_q!)
//!                = (q; q)_n / ((q; q)_k * (q; q)_{n-k})
//! ```
//!
//! It counts the number of k-dimensional subspaces of an n-dimensional vector space over F_q.
//!
//! ### q-Gamma Function
//!
//! The q-gamma function generalizes the classical gamma function:
//! ```text
//! Γ_q(x) = (q; q)_∞ / (q^x; q)_∞  * (1-q)^{1-x}   for 0 < q < 1
//! ```
//! It satisfies Γ_q(x+1) = [x]_q * Γ_q(x) and lim_{q→1} Γ_q(x) = Γ(x).
//!
//! ### q-Exponential Functions
//!
//! There are two natural q-exponentials:
//! ```text
//! e_q(x) = sum_{n=0}^∞ x^n / [n]_q!     (Jackson's e_q)
//!        = 1 / (x(1-q); q)_∞             (product formula, |x(1-q)| < 1)
//! E_q(x) = sum_{n=0}^∞ q^{n(n-1)/2} x^n / [n]_q!  (Jackson's E_q)
//! ```
//!
//! ### q-Logarithm
//!
//! The q-logarithm is defined as the inverse of e_q:
//! ```text
//! ln_q(x) = (x^{1-q} - 1) / (1 - q)   (Tsallis q-logarithm)
//! ```
//!
//! ## References
//!
//! - Gasper, G. & Rahman, M. (2004). *Basic Hypergeometric Series*, 2nd ed. Cambridge.
//! - Andrews, G.E. (1976). *The Theory of Partitions*. Addison-Wesley.
//! - Koekoek, R., Lesky, P.A., Swarttouw, R.F. (2010). *Hypergeometric Orthogonal
//!   Polynomials and Their q-Analogues*. Springer.
//! - DLMF Chapter 17: q-Hypergeometric and Related Functions.

use crate::error::{SpecialError, SpecialResult};

// ============================================================================
// Constants and thresholds
// ============================================================================

/// Maximum number of terms in series expansions
const MAX_SERIES_TERMS: usize = 500;

/// Convergence tolerance for infinite products / series
const CONVERGENCE_TOL: f64 = 1e-15;

// ============================================================================
// q-Pochhammer Symbol
// ============================================================================

/// Computes the finite q-Pochhammer symbol (a; q)_n.
///
/// # Definition
///
/// ```text
/// (a; q)_n = prod_{k=0}^{n-1} (1 - a*q^k)
/// ```
///
/// Special cases:
/// - (a; q)_0 = 1 for all a, q
/// - If q = 0: (a; 0)_1 = 1 - a, (a; 0)_n = 1 - a for n ≥ 1
///
/// # Arguments
///
/// * `a` - Base parameter
/// * `q` - Deformation parameter (typically 0 < q < 1 or |q| < 1)
/// * `n` - Number of factors
///
/// # Returns
///
/// `Ok((a; q)_n)` or an error if computation fails.
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_pochhammer;
///
/// // (1/2; 1/2)_3 = (1 - 1/2)(1 - 1/4)(1 - 1/8) = (1/2)(3/4)(7/8)
/// let val = q_pochhammer(0.5, 0.5, 3).unwrap();
/// let expected = 0.5 * 0.75 * 0.875;
/// assert!((val - expected).abs() < 1e-12);
/// ```
pub fn q_pochhammer(a: f64, q: f64, n: usize) -> SpecialResult<f64> {
    if n == 0 {
        return Ok(1.0);
    }

    let mut result = 1.0f64;
    let mut q_k = 1.0f64; // q^k, starts at q^0 = 1

    for _ in 0..n {
        let factor = 1.0 - a * q_k;
        result *= factor;
        q_k *= q;
    }

    if !result.is_finite() {
        return Err(SpecialError::OverflowError(
            "q_pochhammer: result overflowed or underflowed".to_string(),
        ));
    }

    Ok(result)
}

/// Computes the infinite q-Pochhammer symbol (a; q)_∞ for |q| < 1.
///
/// # Definition
///
/// ```text
/// (a; q)_∞ = prod_{k=0}^∞ (1 - a*q^k)
/// ```
///
/// This converges absolutely for |q| < 1.
///
/// # Arguments
///
/// * `a` - Base parameter
/// * `q` - Deformation parameter, must satisfy |q| < 1
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_pochhammer_inf;
///
/// // (0; q)_∞ = 1 for any q
/// let val = q_pochhammer_inf(0.0, 0.5).unwrap();
/// assert!((val - 1.0).abs() < 1e-12);
/// ```
pub fn q_pochhammer_inf(a: f64, q: f64) -> SpecialResult<f64> {
    if q.abs() >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "q_pochhammer_inf: |q| must be < 1, got q = {q}"
        )));
    }

    let mut result = 1.0f64;
    let mut q_k = 1.0f64; // q^k

    for _ in 0..MAX_SERIES_TERMS {
        let factor = 1.0 - a * q_k;
        let prev = result;
        result *= factor;
        q_k *= q;

        // Check convergence: factors approach 1 as k → ∞ since |q| < 1
        if (result - prev).abs() < CONVERGENCE_TOL * result.abs() {
            return Ok(result);
        }

        if !result.is_finite() {
            return Err(SpecialError::OverflowError(
                "q_pochhammer_inf: partial product overflowed".to_string(),
            ));
        }
    }

    // If we didn't formally converge but result is finite, return it with a note
    // (the series may have de facto converged but our criterion wasn't met)
    if result.is_finite() {
        Ok(result)
    } else {
        Err(SpecialError::ConvergenceError(
            "q_pochhammer_inf: infinite product failed to converge".to_string(),
        ))
    }
}

// ============================================================================
// q-Number and q-Factorial
// ============================================================================

/// Computes the q-analog of an integer: [n]_q = (1 - q^n) / (1 - q).
///
/// # Definition
///
/// ```text
/// [n]_q = (1 - q^n) / (1 - q)   for q ≠ 1
///       = n                       for q = 1 (limit)
/// ```
///
/// This is the q-integer or q-number.
fn q_number(n: u64, q: f64) -> f64 {
    if (q - 1.0).abs() < 1e-14 {
        // Limit as q → 1
        n as f64
    } else {
        (1.0 - q.powi(n as i32)) / (1.0 - q)
    }
}

/// Computes the q-factorial [n]_q! = [1]_q * [2]_q * ... * [n]_q.
///
/// # Definition
///
/// ```text
/// [n]_q! = prod_{k=1}^{n} [k]_q
///        = prod_{k=1}^{n} (1 - q^k) / (1 - q)
///        = (q; q)_n / (1-q)^n
/// ```
///
/// # Arguments
///
/// * `n` - Non-negative integer
/// * `q` - Deformation parameter
///
/// # Returns
///
/// `Ok([n]_q!)` or an error.
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_factorial;
///
/// // q-factorial at q=1 should equal n!
/// let val = q_factorial(5, 1.0 - 1e-10).unwrap();
/// assert!((val - 120.0).abs() < 1e-4);
/// ```
pub fn q_factorial(n: usize, q: f64) -> SpecialResult<f64> {
    if n == 0 {
        return Ok(1.0);
    }

    let mut result = 1.0f64;

    if (q - 1.0).abs() < 1e-14 {
        // Classical limit: [n]! = n!
        for k in 1..=(n as u64) {
            result *= k as f64;
        }
    } else {
        for k in 1..=(n as u64) {
            let q_k = q_number(k, q);
            result *= q_k;
            if !result.is_finite() {
                return Err(SpecialError::OverflowError(format!(
                    "q_factorial: overflow at k = {k}"
                )));
            }
        }
    }

    Ok(result)
}

// ============================================================================
// Gaussian Binomial Coefficient
// ============================================================================

/// Computes the Gaussian (q-) binomial coefficient [n choose k]_q.
///
/// # Definition
///
/// ```text
/// [n choose k]_q = [n]_q! / ([k]_q! * [n-k]_q!)
///                = (q; q)_n / ((q; q)_k * (q; q)_{n-k})
/// ```
///
/// At q = 1 this reduces to the ordinary binomial coefficient C(n, k).
///
/// # Properties
///
/// - [n choose 0]_q = [n choose n]_q = 1
/// - [n choose k]_q = [n choose n-k]_q  (symmetry)
/// - [n choose k]_q ∈ Z[q]  (polynomial in q with non-negative integer coefficients)
/// - At q = prime power, counts k-dim subspaces of F_q^n
///
/// # Arguments
///
/// * `n` - Top parameter (non-negative integer)
/// * `k` - Bottom parameter (0 ≤ k ≤ n)
/// * `q` - Deformation parameter
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_binomial;
///
/// // Classical binomial C(4,2) = 6
/// let val = q_binomial(4, 2, 1.0 - 1e-10).unwrap();
/// assert!((val - 6.0).abs() < 1e-4);
///
/// // q-binomial [4 choose 2]_q at q = 2 (counts 2-dim subspaces of F_2^4)
/// let val2 = q_binomial(4, 2, 2.0).unwrap();
/// assert!((val2 - 35.0).abs() < 1e-6);
/// ```
pub fn q_binomial(n: usize, k: usize, q: f64) -> SpecialResult<f64> {
    if k > n {
        return Err(SpecialError::DomainError(format!(
            "q_binomial: k = {k} must be ≤ n = {n}"
        )));
    }

    if k == 0 || k == n {
        return Ok(1.0);
    }

    // Use the more numerically stable formula via q-Pochhammer symbols:
    // [n choose k]_q = (q; q)_n / ((q; q)_k * (q; q)_{n-k})
    // Computed as product to avoid cancellation

    // Use the recurrence / product form that is more stable:
    // [n choose k]_q = prod_{j=0}^{k-1} (1 - q^{n-j}) / (1 - q^{j+1})
    let k_eff = k.min(n - k); // symmetry to reduce number of terms

    let mut result = 1.0f64;

    if (q - 1.0).abs() < 1e-14 {
        // Classical binomial coefficient C(n, k_eff)
        for j in 0..k_eff {
            result *= (n - j) as f64 / (j + 1) as f64;
        }
    } else {
        for j in 0..k_eff {
            let num = 1.0 - q.powi((n - j) as i32);
            let den = 1.0 - q.powi((j + 1) as i32);
            if den.abs() < 1e-300 {
                return Err(SpecialError::ComputationError(
                    "q_binomial: denominator factor vanished".to_string(),
                ));
            }
            result *= num / den;
            if !result.is_finite() {
                return Err(SpecialError::OverflowError(format!(
                    "q_binomial: overflow at j = {j}"
                )));
            }
        }
    }

    Ok(result)
}

// ============================================================================
// q-Gamma Function
// ============================================================================

/// Computes the q-gamma function Γ_q(x) for 0 < q < 1.
///
/// # Definition
///
/// For 0 < q < 1:
/// ```text
/// Γ_q(x) = (q; q)_∞ / (q^x; q)_∞  * (1-q)^{1-x}
/// ```
///
/// This satisfies:
/// - Γ_q(x+1) = [x]_q * Γ_q(x)  (functional equation)
/// - Γ_q(1) = 1
/// - lim_{q→1^-} Γ_q(x) = Γ(x)  (classical limit)
///
/// # Arguments
///
/// * `x` - Argument (x > 0)
/// * `q` - Deformation parameter (0 < q < 1)
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_gamma;
///
/// // Γ_q(1) = 1 for any q ∈ (0,1)
/// let val = q_gamma(1.0, 0.5).unwrap();
/// assert!((val - 1.0).abs() < 1e-10);
///
/// // Γ_q(2) = [1]_q = 1 for any q (since [1]_q = 1)
/// let val2 = q_gamma(2.0, 0.5).unwrap();
/// assert!((val2 - 1.0).abs() < 1e-10);
/// ```
pub fn q_gamma(x: f64, q: f64) -> SpecialResult<f64> {
    if q <= 0.0 || q >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "q_gamma: q must satisfy 0 < q < 1, got q = {q}"
        )));
    }

    if x <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "q_gamma: x must be positive, got x = {x}"
        )));
    }

    // Γ_q(x) = (q; q)_∞ / (q^x; q)_∞  * (1-q)^{1-x}

    let qx = q.powf(x);

    let poch_q = q_pochhammer_inf(q, q)?;
    let poch_qx = q_pochhammer_inf(qx, q)?;

    if poch_qx.abs() < 1e-300 {
        return Err(SpecialError::ComputationError(
            "q_gamma: (q^x; q)_∞ is too close to zero".to_string(),
        ));
    }

    let result = poch_q / poch_qx * (1.0 - q).powf(1.0 - x);

    if !result.is_finite() {
        return Err(SpecialError::OverflowError(
            "q_gamma: result is not finite".to_string(),
        ));
    }

    Ok(result)
}

// ============================================================================
// q-Beta Function
// ============================================================================

/// Computes the q-beta function B_q(a, b).
///
/// # Definition
///
/// The q-beta function is defined via the q-gamma function:
/// ```text
/// B_q(a, b) = Γ_q(a) * Γ_q(b) / Γ_q(a + b)
/// ```
///
/// For 0 < q < 1, a, b > 0.
///
/// # Properties
///
/// - B_q(a, b) = B_q(b, a)  (symmetry)
/// - lim_{q→1} B_q(a, b) = B(a, b) = Γ(a)Γ(b)/Γ(a+b)
///
/// # Arguments
///
/// * `a` - First parameter (a > 0)
/// * `b` - Second parameter (b > 0)
/// * `q` - Deformation parameter (0 < q < 1)
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_beta;
///
/// // B_q(1, 1) = Γ_q(1)^2 / Γ_q(2) = 1/1 = 1
/// let val = q_beta(1.0, 1.0, 0.5).unwrap();
/// assert!((val - 1.0).abs() < 1e-8);
/// ```
pub fn q_beta(a: f64, b: f64, q: f64) -> SpecialResult<f64> {
    if q <= 0.0 || q >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "q_beta: q must satisfy 0 < q < 1, got q = {q}"
        )));
    }

    if a <= 0.0 || b <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "q_beta: a and b must be positive, got a = {a}, b = {b}"
        )));
    }

    let gamma_a = q_gamma(a, q)?;
    let gamma_b = q_gamma(b, q)?;
    let gamma_ab = q_gamma(a + b, q)?;

    if gamma_ab.abs() < 1e-300 {
        return Err(SpecialError::ComputationError(
            "q_beta: Γ_q(a+b) is too close to zero".to_string(),
        ));
    }

    let result = gamma_a * gamma_b / gamma_ab;

    if !result.is_finite() {
        return Err(SpecialError::OverflowError(
            "q_beta: result is not finite".to_string(),
        ));
    }

    Ok(result)
}

// ============================================================================
// q-Exponential Function
// ============================================================================

/// Computes Jackson's q-exponential e_q(x).
///
/// # Definition
///
/// Jackson's q-exponential is defined by the series:
/// ```text
/// e_q(x) = sum_{n=0}^∞ x^n / [n]_q!
/// ```
///
/// It satisfies D_q(e_q(x)) = e_q(x) where D_q is the q-derivative.
///
/// Product formula (for |x(1-q)| < 1):
/// ```text
/// e_q(x) = 1 / ((1-q) * x; q)_∞
/// ```
///
/// Note: There are two standard q-exponentials:
/// - `e_q(x)` (this function): product formula 1/((x(1-q)); q)_∞
/// - `E_q(x)`: includes q^{n(n-1)/2} factors (see `q_exponential_big`)
///
/// # Arguments
///
/// * `x` - Argument
/// * `q` - Deformation parameter (typically |q| < 1)
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_exponential;
///
/// // Near q=1, e_q(x) ≈ exp(x)
/// let val = q_exponential(1.0, 0.999).unwrap();
/// assert!((val - std::f64::consts::E).abs() < 0.01);
/// ```
pub fn q_exponential(x: f64, q: f64) -> SpecialResult<f64> {
    if q.abs() >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "q_exponential: |q| must be < 1, got q = {q}"
        )));
    }

    // Use series: e_q(x) = sum_{n=0}^∞ x^n / [n]_q!
    // This converges for all x when |q| < 1 (the radius of convergence is
    // 1/(1-q) for the series formulation)

    let mut sum = 0.0f64;
    let mut x_pow_n = 1.0f64; // x^n
    let mut q_fact = 1.0f64; // [n]_q!
    let mut q_pow_n = 1.0f64; // q^n (for computing [n]_q)

    for n in 0..MAX_SERIES_TERMS {
        if n > 0 {
            x_pow_n *= x;
            // [n]_q = (1 - q^n) / (1 - q)
            let q_n = if (q - 1.0).abs() < 1e-14 {
                n as f64
            } else {
                (1.0 - q_pow_n) / (1.0 - q)
            };
            q_fact *= q_n;

            if q_fact.abs() < 1e-300 {
                // Series is dominted by zero denominator, stop
                break;
            }
        }

        let term = x_pow_n / q_fact;
        sum += term;

        if term.abs() < CONVERGENCE_TOL * sum.abs() && n > 5 {
            return Ok(sum);
        }

        q_pow_n *= q;

        if !sum.is_finite() {
            return Err(SpecialError::OverflowError(
                "q_exponential: series diverged".to_string(),
            ));
        }
    }

    if sum.is_finite() {
        Ok(sum)
    } else {
        Err(SpecialError::ConvergenceError(
            "q_exponential: series did not converge".to_string(),
        ))
    }
}

/// Computes the big q-exponential E_q(x) (second Jackson q-exponential).
///
/// # Definition
///
/// ```text
/// E_q(x) = sum_{n=0}^∞ q^{n(n-1)/2} * x^n / [n]_q!
/// ```
///
/// Satisfies E_q(x) * e_q(-x) = 1 and product formula:
/// ```text
/// E_q(x) = (-x(1-q); q)_∞
/// ```
///
/// # Arguments
///
/// * `x` - Argument
/// * `q` - Deformation parameter (|q| < 1)
pub fn q_exponential_big(x: f64, q: f64) -> SpecialResult<f64> {
    if q.abs() >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "q_exponential_big: |q| must be < 1, got q = {q}"
        )));
    }

    // Use product formula: E_q(x) = (-x(1-q); q)_∞
    let a = -x * (1.0 - q);
    q_pochhammer_inf(a, q)
}

// ============================================================================
// q-Logarithm
// ============================================================================

/// Computes the q-logarithm (Tsallis logarithm) ln_q(x).
///
/// # Definition
///
/// The Tsallis q-logarithm is defined as:
/// ```text
/// ln_q(x) = (x^{1-q} - 1) / (1 - q)   for q ≠ 1, x > 0
///          = ln(x)                       for q = 1 (limit)
/// ```
///
/// This is the inverse of the q-exponential in the sense of non-extensive
/// statistical mechanics (Tsallis statistics).
///
/// # Properties
///
/// - ln_q(1) = 0 for all q
/// - ln_q(x * y) = ln_q(x) + ln_q(y) + (1-q) * ln_q(x) * ln_q(y)
///   (q-additivity)
/// - lim_{q→1} ln_q(x) = ln(x)
///
/// # Arguments
///
/// * `x` - Argument (x > 0)
/// * `q` - Deformation parameter
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::q_logarithm;
///
/// // ln_q(1) = 0 for any q
/// let val = q_logarithm(1.0, 0.5).unwrap();
/// assert!(val.abs() < 1e-14);
///
/// // Near q=1: ln_q(e) ≈ 1
/// let val2 = q_logarithm(std::f64::consts::E, 1.0 - 1e-10).unwrap();
/// assert!((val2 - 1.0).abs() < 1e-6);
/// ```
pub fn q_logarithm(x: f64, q: f64) -> SpecialResult<f64> {
    if x <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "q_logarithm: x must be positive, got x = {x}"
        )));
    }

    if (q - 1.0).abs() < 1e-14 {
        // Classical limit
        return Ok(x.ln());
    }

    // ln_q(x) = (x^{1-q} - 1) / (1 - q)
    let exponent = 1.0 - q;
    let x_pow = x.powf(exponent);
    let result = (x_pow - 1.0) / exponent;

    if !result.is_finite() {
        return Err(SpecialError::OverflowError(
            "q_logarithm: result is not finite".to_string(),
        ));
    }

    Ok(result)
}

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

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

    #[test]
    fn test_q_pochhammer_empty_product() {
        // (a; q)_0 = 1 for any a, q
        let val = q_pochhammer(0.5, 0.5, 0).expect("q_pochhammer(0.5, 0.5, 0)");
        assert!((val - 1.0).abs() < 1e-14);
    }

    #[test]
    fn test_q_pochhammer_single_factor() {
        // (a; q)_1 = 1 - a
        let val = q_pochhammer(0.3, 0.5, 1).expect("q_pochhammer single factor");
        assert!((val - 0.7).abs() < 1e-14);
    }

    #[test]
    fn test_q_pochhammer_three_factors() {
        // (1/2; 1/2)_3 = (1 - 1/2)(1 - 1/4)(1 - 1/8)
        let expected = 0.5 * 0.75 * 0.875;
        let val = q_pochhammer(0.5, 0.5, 3).expect("q_pochhammer three factors");
        assert!((val - expected).abs() < 1e-12);
    }

    #[test]
    fn test_q_pochhammer_inf_zero_base() {
        // (0; q)_∞ = 1
        let val = q_pochhammer_inf(0.0, 0.5).expect("q_pochhammer_inf zero base");
        assert!((val - 1.0).abs() < 1e-12);
    }

    #[test]
    fn test_q_factorial_zero() {
        let val = q_factorial(0, 0.5).expect("q_factorial(0)");
        assert!((val - 1.0).abs() < 1e-14);
    }

    #[test]
    fn test_q_factorial_classical_limit() {
        // [n]_{q→1}! = n!
        let q = 1.0 - 1e-9;
        for n in 0..=7usize {
            let qf = q_factorial(n, q).expect("q_factorial classical limit");
            let classical: f64 = (1..=(n as u64)).product::<u64>() as f64;
            let expected = if n == 0 { 1.0 } else { classical };
            assert!(
                (qf - expected).abs() < 1e-3 * expected.max(1.0),
                "n={n}: q_factorial={qf}, expected={expected}"
            );
        }
    }

    #[test]
    fn test_q_binomial_boundary() {
        // [n choose 0]_q = [n choose n]_q = 1
        for n in 1..=5usize {
            let v0 = q_binomial(n, 0, 0.5).expect("q_binomial k=0");
            let vn = q_binomial(n, n, 0.5).expect("q_binomial k=n");
            assert!((v0 - 1.0).abs() < 1e-12);
            assert!((vn - 1.0).abs() < 1e-12);
        }
    }

    #[test]
    fn test_q_binomial_classical_limit() {
        // [4 choose 2]_{q→1} = C(4,2) = 6
        let val = q_binomial(4, 2, 1.0 - 1e-9).expect("q_binomial classical");
        assert!((val - 6.0).abs() < 0.01, "val = {val}");
    }

    #[test]
    fn test_q_binomial_q2() {
        // [4 choose 2]_2 should be 35
        // (2;2)_4 / ((2;2)_2 * (2;2)_2)
        // (1-2)(1-4)(1-8)(1-16) / ((1-2)(1-4))^2
        // = ((-1)(-3)(-7)(-15)) / ((-1)(-3))^2
        // = 315 / 9 = 35
        let val = q_binomial(4, 2, 2.0).expect("q_binomial q=2");
        assert!((val - 35.0).abs() < 1e-6, "val = {val}");
    }

    #[test]
    fn test_q_gamma_at_one() {
        // Γ_q(1) = 1
        let val = q_gamma(1.0, 0.5).expect("q_gamma(1)");
        assert!((val - 1.0).abs() < 1e-8, "val = {val}");
    }

    #[test]
    fn test_q_gamma_functional_eq() {
        // Γ_q(x+1) = [x]_q * Γ_q(x)
        let q = 0.7f64;
        let x = 1.5f64;
        let gx1 = q_gamma(x + 1.0, q).expect("q_gamma(x+1)");
        let gx = q_gamma(x, q).expect("q_gamma(x)");
        let q_x = (1.0 - q.powf(x)) / (1.0 - q);
        assert!(
            (gx1 - q_x * gx).abs() < 1e-8,
            "functional eq: lhs={gx1}, rhs={}, diff={}",
            q_x * gx,
            (gx1 - q_x * gx).abs()
        );
    }

    #[test]
    fn test_q_beta_symmetry() {
        // B_q(a, b) = B_q(b, a)
        let q = 0.6f64;
        let a = 1.5f64;
        let b = 2.0f64;
        let v1 = q_beta(a, b, q).expect("q_beta(a,b)");
        let v2 = q_beta(b, a, q).expect("q_beta(b,a)");
        assert!((v1 - v2).abs() < 1e-8, "symmetry: {v1} vs {v2}");
    }

    #[test]
    fn test_q_exponential_near_classical() {
        // e_q(x) → exp(x) as q → 1
        let val = q_exponential(1.0, 0.999).expect("q_exponential near classical");
        assert!(
            (val - std::f64::consts::E).abs() < 0.01,
            "val = {val}, e = {}",
            std::f64::consts::E
        );
    }

    #[test]
    fn test_q_logarithm_at_one() {
        // ln_q(1) = 0 for any q
        for q in &[0.1, 0.5, 0.9, 2.0] {
            let val = q_logarithm(1.0, *q).expect("q_logarithm(1)");
            assert!(val.abs() < 1e-14, "q={q}: val = {val}");
        }
    }

    #[test]
    fn test_q_logarithm_classical_limit() {
        // ln_q(e) → 1 as q → 1
        let val = q_logarithm(std::f64::consts::E, 1.0 - 1e-9).expect("q_log classical");
        assert!((val - 1.0).abs() < 1e-5, "val = {val}");
    }
}

// ============================================================================
// Basic Hypergeometric Series  2φ1
// ============================================================================

/// Computes the basic hypergeometric series ₂φ₁(a, b; c; q, z).
///
/// # Definition
///
/// The basic hypergeometric series (q-analogue of the Gauss hypergeometric
/// function) is defined by:
///
/// ```text
/// ₂φ₁(a, b; c; q, z) = Σ_{n=0}^∞  (a;q)_n · (b;q)_n
///                                    ─────────────────── · z^n
///                                    (c;q)_n · (q;q)_n
/// ```
///
/// where `(x; q)_n` is the finite q-Pochhammer symbol.
///
/// The series converges for `|z| < 1` when `|q| < 1`, or terminates when
/// `a = q^{-N}` for a non-negative integer N (in which case all terms with
/// n > N vanish because `(q^{-N}; q)_n = 0` for n > N).
///
/// # Classical limit
///
/// As q → 1⁻, the function reduces to the Gauss hypergeometric function:
/// ```text
/// lim_{q→1} ₂φ₁(q^a, q^b; q^c; q, z) = ₂F₁(a, b; c; z)
/// ```
///
/// # Arguments
///
/// * `a` - First numerator parameter
/// * `b` - Second numerator parameter
/// * `c` - Denominator parameter (c ≠ q^{-k} for k = 0, 1, 2, …)
/// * `q` - Base parameter (typically 0 < q < 1)
/// * `z` - Argument (series converges for |z| < 1)
///
/// # Errors
///
/// Returns an error if:
/// - The series fails to converge within `MAX_SERIES_TERMS` iterations.
/// - A denominator factor `(c;q)_n · (q;q)_n` vanishes (pole of the function).
///
/// # Examples
///
/// ```rust
/// use scirs2_special::q_analogs::basic_hypergeometric_2phi1;
///
/// // Terminating series: a = q^{-2} = 1/q^2 → (a;q)_n = 0 for n > 2
/// let q = 0.5_f64;
/// let a = q.powi(-2); // q^{-2}
/// let b = 0.5_f64;
/// let c = 0.25_f64;
/// let z = 0.3_f64;
/// let val = basic_hypergeometric_2phi1(a, b, c, q, z).unwrap();
/// assert!(val.is_finite());
///
/// // Near classical limit
/// // ₂φ₁(q^a, q^b; q^c; q, z) → ₂F₁(a, b; c; z) as q → 1⁻
/// ```
pub fn basic_hypergeometric_2phi1(a: f64, b: f64, c: f64, q: f64, z: f64) -> SpecialResult<f64> {
    // Validate q
    if q.abs() >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "basic_hypergeometric_2phi1: |q| must be < 1, got q = {q}"
        )));
    }

    // Validate z for convergence  (series converges absolutely for |z| < 1)
    if z.abs() >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "basic_hypergeometric_2phi1: |z| must be < 1 for convergence, got z = {z}"
        )));
    }

    // Sum the series term by term.
    // term_n = (a;q)_n * (b;q)_n / ((c;q)_n * (q;q)_n) * z^n
    //
    // We maintain running Pochhammer products to avoid recomputing from scratch.
    // At step n:
    //   poch_a_n  = (a;q)_n
    //   poch_b_n  = (b;q)_n
    //   poch_c_n  = (c;q)_n
    //   poch_q_n  = (q;q)_n

    let mut poch_a = 1.0f64; // (a;q)_0 = 1
    let mut poch_b = 1.0f64; // (b;q)_0 = 1
    let mut poch_c = 1.0f64; // (c;q)_0 = 1
    let mut poch_qq = 1.0f64; // (q;q)_0 = 1
    let mut z_pow = 1.0f64; // z^0 = 1
    let mut q_pow = 1.0f64; // q^0 (for the factor in Pochhammer update)

    let mut sum = 0.0f64;

    for n in 0..MAX_SERIES_TERMS {
        // Denominator at step n (before updating)
        let denom = poch_c * poch_qq;
        if denom.abs() < 1e-300 {
            // Pole: series is not well-defined at this parameter combination
            return Err(SpecialError::ComputationError(format!(
                "basic_hypergeometric_2phi1: denominator vanished at n = {n} \
                 (c may be a non-positive power of q)"
            )));
        }

        let term = poch_a * poch_b / denom * z_pow;
        sum += term;

        // Convergence check (after a few terms)
        if n >= 5 && term.abs() < CONVERGENCE_TOL * sum.abs().max(1e-300) {
            return Ok(sum);
        }
        if !sum.is_finite() {
            return Err(SpecialError::OverflowError(
                "basic_hypergeometric_2phi1: partial sum overflowed".to_string(),
            ));
        }

        // Update Pochhammer products for the next term n+1:
        // (x;q)_{n+1} = (x;q)_n * (1 - x*q^n)
        // q^n is tracked by q_pow (starts at q^0 = 1, updated to q^n after step n)
        let qn = q_pow; // q^n
        poch_a *= 1.0 - a * qn;
        poch_b *= 1.0 - b * qn;
        poch_c *= 1.0 - c * qn;
        poch_qq *= 1.0 - q * qn; // (q;q)_{n+1} uses factor (1 - q^{n+1})

        z_pow *= z;
        q_pow *= q;

        // Early termination: if poch_a (or poch_b) goes to zero, all subsequent
        // terms are zero — the series terminates.
        if poch_a.abs() < 1e-300 || poch_b.abs() < 1e-300 {
            return Ok(sum);
        }
    }

    // If we reach here the series did not formally converge
    if sum.is_finite() {
        Ok(sum)
    } else {
        Err(SpecialError::ConvergenceError(
            "basic_hypergeometric_2phi1: series did not converge".to_string(),
        ))
    }
}

// ============================================================================
// Tests for new q-functions
// ============================================================================

#[cfg(test)]
mod advanced_q_tests {
    use super::*;

    #[test]
    fn test_2phi1_unit_term() {
        // ₂φ₁(0, 0; 0; q, z) = 1 (all Pochhammer symbols are 1 for a=b=0)
        // (0;q)_n = (1-0)(1-0q)...(1-0q^{n-1}) = 1 for all n
        // so ₂φ₁(0, 0; 0; q, z) = sum z^n / (0;q)_n * (q;q)_n
        // (0;q)_n = 1, so sum is z^n/(q;q)_n = e_q(z) product — but c=0 causes denom=0 at n=1
        // Use a non-degenerate case:
        // ₂φ₁(0, b; c; q, 0) = 1 (z=0, only n=0 term survives)
        let val = basic_hypergeometric_2phi1(0.0, 0.5, 0.5, 0.3, 0.0).expect("2phi1 z=0");
        assert!((val - 1.0).abs() < 1e-14, "val = {val}");
    }

    #[test]
    fn test_2phi1_q_geometric_series() {
        // When b = 0:  (0;q)_n = 1 for all n,
        // ₂φ₁(a, 0; c; q, z) = sum_{n=0}^∞ (a;q)_n / ((c;q)_n (q;q)_n) z^n
        // When also a = 0: all Pochhammer factors are 1 except (q;q)_n in denom,
        //   ₂φ₁(0, 0; c; q, z) = sum z^n / ((c;q)_n (q;q)_n)
        // Use specific small z to verify convergence and finiteness:
        let q = 0.5f64;
        let val = basic_hypergeometric_2phi1(0.1, 0.2, 0.7, q, 0.3).expect("2phi1 general");
        assert!(val.is_finite(), "val not finite: {val}");
        assert!(val > 0.0, "val should be positive for these params");
    }

    #[test]
    fn test_2phi1_terminating() {
        // a = q^{-N} for integer N makes series terminate after N+1 terms
        let q = 0.5f64;
        let n_terms = 3usize;
        let a = q.powi(-(n_terms as i32)); // q^{-3}
        let b = 0.5f64;
        let c = 0.75f64;
        let z = 0.4f64;
        let val = basic_hypergeometric_2phi1(a, b, c, q, z).expect("2phi1 terminating");
        assert!(val.is_finite(), "terminating series should be finite: {val}");
    }

    #[test]
    fn test_2phi1_domain_error_q_too_large() {
        let result = basic_hypergeometric_2phi1(0.5, 0.5, 0.5, 1.5, 0.3);
        assert!(result.is_err());
    }

    #[test]
    fn test_2phi1_domain_error_z_too_large() {
        let result = basic_hypergeometric_2phi1(0.5, 0.5, 0.5, 0.5, 1.5);
        assert!(result.is_err());
    }

    #[test]
    fn test_2phi1_symmetry_in_a_b() {
        // ₂φ₁(a, b; c; q, z) = ₂φ₁(b, a; c; q, z)
        let (a, b, c, q, z) = (0.3, 0.6, 0.8, 0.4, 0.2);
        let v1 = basic_hypergeometric_2phi1(a, b, c, q, z).expect("v1");
        let v2 = basic_hypergeometric_2phi1(b, a, c, q, z).expect("v2");
        assert!((v1 - v2).abs() < 1e-12, "symmetry: {v1} vs {v2}");
    }
}