calnexus 0.1.0

A command-line math expression evaluator with 11 computation domains, symbolic calculus, REPL, and batch processing
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
// Copyright (c) 2026 Kirky.X. Licensed under the MIT License.

//! Combinatorics 计算域:排列 P、组合 C、Catalan 数、Stirling 数。
//!
//! 设计依据:
//! - combinatorics-domain spec:5 个 requirements / 13+ scenarios
//! - design.md D5(u128→BigInt 自动升级)、D6(priority=25)
//!
//! 路由策略:AST 含组合函数调用(P/C/catalan/stirling)时路由至本域。
//! 内部用 u128 累积,溢出时自动升级为 BigInt,返回 Scalar(fit i64)或 BigInt。

use crate::core::domain::CalculationDomain;
use crate::core::types::{AstNode, BinaryOp, CalcError, EvalContext, EvalResult, UnaryOp};
use num_bigint::BigInt;
use num_traits::{One, Signed, ToPrimitive, Zero};

/// 组合函数白名单。
const COMBINATORICS_FUNCTIONS: &[&str] = &["P", "C", "catalan", "stirling"];

/// Combinatorics 计算域。
///
/// priority=25,支持 P/C/catalan/stirling。
pub struct CombinatoricsDomain;

impl CalculationDomain for CombinatoricsDomain {
    fn domain_name(&self) -> &str {
        "combinatorics"
    }

    fn priority(&self) -> u8 {
        25
    }

    fn supports(&self, ast: &AstNode) -> bool {
        contains_combinatorics_function(ast)
    }

    fn evaluate(&self, ast: &AstNode, ctx: &EvalContext) -> Result<EvalResult, CalcError> {
        self.eval_node(ast, ctx)
    }
}

impl Default for CombinatoricsDomain {
    fn default() -> Self {
        Self
    }
}

impl CombinatoricsDomain {
    /// 递归求值 AST 节点,返回 EvalResult。
    fn eval_node(&self, ast: &AstNode, ctx: &EvalContext) -> Result<EvalResult, CalcError> {
        match ast {
            AstNode::FunctionCall(name, args) => self.eval_function(name, args, ctx),
            AstNode::Number(n) => {
                if n.fract() != 0.0 {
                    return Err(CalcError::DomainError(format!(
                        "combinatorics domain requires integer, got {}",
                        n
                    )));
                }
                Ok(EvalResult::Scalar(*n))
            }
            AstNode::BigNumber(s) => {
                let b: BigInt = s
                    .parse()
                    .map_err(|_| CalcError::DomainError(format!("invalid big number: {}", s)))?;
                Ok(EvalResult::BigInt(b))
            }
            AstNode::Variable(name) => ctx
                .get_var(name)
                .map(EvalResult::Scalar)
                .ok_or_else(|| CalcError::EvalError(format!("unbound variable: {}", name))),
            AstNode::BinaryOp(op, l, r) => {
                let a = self.eval_int(l, ctx)?;
                let b = self.eval_int(r, ctx)?;
                let result = self.eval_int_binary(*op, a, b)?;
                Ok(bigint_to_result(result))
            }
            AstNode::UnaryOp(op, e) => {
                let v = self.eval_int(e, ctx)?;
                match op {
                    UnaryOp::Neg => Ok(bigint_to_result(-v)),
                    UnaryOp::Abs => Ok(bigint_to_result(v.abs())),
                    UnaryOp::Factorial => Err(CalcError::DomainError(
                        "factorial not supported in combinatorics domain".to_string(),
                    )),
                }
            }
            AstNode::Complex(_, _) | AstNode::Matrix(_) | AstNode::List(_) => {
                Err(CalcError::DomainError(format!(
                    "combinatorics domain does not support this node type: {:?}",
                    ast
                )))
            }
        }
    }

    /// 将 AST 求值为 BigInt(精确整数运算)。
    fn eval_int(&self, ast: &AstNode, ctx: &EvalContext) -> Result<BigInt, CalcError> {
        match ast {
            AstNode::Number(n) => {
                if n.fract() != 0.0 {
                    return Err(CalcError::DomainError(format!(
                        "expected integer argument, got {}",
                        n
                    )));
                }
                if *n > i64::MAX as f64 || *n < i64::MIN as f64 {
                    return Err(CalcError::Overflow);
                }
                Ok(BigInt::from(*n as i64))
            }
            AstNode::BigNumber(s) => s
                .parse::<BigInt>()
                .map_err(|_| CalcError::DomainError(format!("invalid big number: {}", s))),
            AstNode::Variable(name) => {
                let v = ctx
                    .get_var(name)
                    .ok_or_else(|| CalcError::EvalError(format!("unbound variable: {}", name)))?;
                if v.fract() != 0.0 {
                    return Err(CalcError::DomainError(format!(
                        "variable {} is not an integer: {}",
                        name, v
                    )));
                }
                Ok(BigInt::from(v as i64))
            }
            AstNode::BinaryOp(op, l, r) => {
                let a = self.eval_int(l, ctx)?;
                let b = self.eval_int(r, ctx)?;
                self.eval_int_binary(*op, a, b)
            }
            AstNode::UnaryOp(op, e) => {
                let v = self.eval_int(e, ctx)?;
                match op {
                    UnaryOp::Neg => Ok(-v),
                    UnaryOp::Abs => Ok(v.abs()),
                    UnaryOp::Factorial => Err(CalcError::DomainError(
                        "factorial not supported in combinatorics domain".to_string(),
                    )),
                }
            }
            AstNode::Complex(_, _)
            | AstNode::Matrix(_)
            | AstNode::List(_)
            | AstNode::FunctionCall(_, _) => Err(CalcError::DomainError(format!(
                "expected integer expression, got: {:?}",
                ast
            ))),
        }
    }

    /// 整数二元运算。
    fn eval_int_binary(&self, op: BinaryOp, a: BigInt, b: BigInt) -> Result<BigInt, CalcError> {
        match op {
            BinaryOp::Add => Ok(a + b),
            BinaryOp::Sub => Ok(a - b),
            BinaryOp::Mul => Ok(a * b),
            BinaryOp::Div => {
                if b.is_zero() {
                    return Err(CalcError::DivisionByZero);
                }
                Ok(a / b)
            }
            BinaryOp::Pow => {
                if b.is_negative() {
                    return Err(CalcError::DomainError(
                        "negative exponent not supported for integers".to_string(),
                    ));
                }
                let exp: u32 = b.to_u32().ok_or(CalcError::Overflow)?;
                Ok(a.pow(exp))
            }
            BinaryOp::Mod => {
                if b.is_zero() {
                    return Err(CalcError::DivisionByZero);
                }
                Ok(a % b)
            }
        }
    }

    /// 求值组合函数调用。
    fn eval_function(
        &self,
        name: &str,
        args: &[AstNode],
        ctx: &EvalContext,
    ) -> Result<EvalResult, CalcError> {
        if !COMBINATORICS_FUNCTIONS.contains(&name) {
            return Err(CalcError::DomainError(format!(
                "unsupported function in combinatorics domain: {}",
                name
            )));
        }
        match name {
            "P" => {
                if args.len() != 2 {
                    return Err(CalcError::DomainError(format!(
                        "P() requires exactly 2 arguments, got {}",
                        args.len()
                    )));
                }
                let n = self.eval_int(&args[0], ctx)?;
                let k = self.eval_int(&args[1], ctx)?;
                if n.is_negative() || k.is_negative() {
                    return Err(CalcError::DomainError(
                        "P() requires non-negative arguments".to_string(),
                    ));
                }
                if k > n {
                    return Ok(EvalResult::Scalar(0.0));
                }
                let result = permutation(&n, &k);
                Ok(bigint_to_result(result))
            }
            "C" => {
                if args.len() != 2 {
                    return Err(CalcError::DomainError(format!(
                        "C() requires exactly 2 arguments, got {}",
                        args.len()
                    )));
                }
                let n = self.eval_int(&args[0], ctx)?;
                let k = self.eval_int(&args[1], ctx)?;
                if n.is_negative() || k.is_negative() {
                    return Err(CalcError::DomainError(
                        "C() requires non-negative arguments".to_string(),
                    ));
                }
                if k > n {
                    return Ok(EvalResult::Scalar(0.0));
                }
                let result = combination(&n, &k);
                Ok(bigint_to_result(result))
            }
            "catalan" => {
                if args.len() != 1 {
                    return Err(CalcError::DomainError(format!(
                        "catalan() requires exactly 1 argument, got {}",
                        args.len()
                    )));
                }
                let n = self.eval_int(&args[0], ctx)?;
                if n.is_negative() {
                    return Err(CalcError::DomainError(
                        "catalan() requires non-negative argument".to_string(),
                    ));
                }
                let result = catalan(&n);
                Ok(bigint_to_result(result))
            }
            "stirling" => {
                if args.len() != 2 {
                    return Err(CalcError::DomainError(format!(
                        "stirling() requires exactly 2 arguments, got {}",
                        args.len()
                    )));
                }
                let n = self.eval_int(&args[0], ctx)?;
                let k = self.eval_int(&args[1], ctx)?;
                if n.is_negative() || k.is_negative() {
                    return Err(CalcError::DomainError(
                        "stirling() requires non-negative arguments".to_string(),
                    ));
                }
                let result = stirling_second(&n, &k);
                Ok(bigint_to_result(result))
            }
            _ => unreachable!(),
        }
    }
}

/// 将 BigInt 转换为 EvalResult:fit i64 → Scalar,否则 → BigInt。
fn bigint_to_result(b: BigInt) -> EvalResult {
    if let Some(n) = b.to_i64() {
        EvalResult::Scalar(n as f64)
    } else {
        EvalResult::BigInt(b)
    }
}

/// 排列数 P(n, k) = n!/(n-k)! = n*(n-1)*...*(n-k+1)。
fn permutation(n: &BigInt, k: &BigInt) -> BigInt {
    if k.is_zero() {
        return BigInt::one();
    }
    if k > n {
        return BigInt::zero();
    }
    let mut result = BigInt::one();
    let mut current = n.clone();
    let k_u64 = k.to_u64().unwrap_or(0);
    for _ in 0..k_u64 {
        result *= &current;
        current -= 1;
    }
    result
}

/// 组合数 C(n, k) = n!/(k!(n-k)!) = P(n,k)/k!。
fn combination(n: &BigInt, k: &BigInt) -> BigInt {
    if k.is_zero() || k == n {
        return BigInt::one();
    }
    if k > n {
        return BigInt::zero();
    }
    // C(n,k) = C(n, n-k),取较小的 k 提高效率
    let k_opt = if k < &(n - k) { k.clone() } else { n - k };
    let k_u64 = k_opt.to_u64().unwrap_or(0);
    let mut result = BigInt::one();
    let mut current = n.clone();
    for i in 0..k_u64 {
        result *= &current;
        result /= BigInt::from(i + 1);
        current -= 1;
    }
    result
}

/// Catalan 数 C(n) = C(2n,n)/(n+1)。
fn catalan(n: &BigInt) -> BigInt {
    if n.is_zero() {
        return BigInt::one();
    }
    let two_n = n * 2;
    let c_2n_n = combination(&two_n, n);
    c_2n_n / (n + 1)
}

/// 第二类 Stirling 数 S(n, k):将 n 个元素划分为 k 个非空子集的方式数。
/// 递推:S(n,k) = k*S(n-1,k) + S(n-1,k-1)
/// 边界:S(0,0)=1, S(n,0)=0 (n>0), S(0,k)=0 (k>0), S(n,k)=0 (k>n)
fn stirling_second(n: &BigInt, k: &BigInt) -> BigInt {
    if n.is_zero() && k.is_zero() {
        return BigInt::one();
    }
    if n.is_zero() || k.is_zero() {
        return BigInt::zero();
    }
    if k > n {
        return BigInt::zero();
    }
    let n_u64 = n.to_u64().unwrap_or(0);
    let k_u64 = k.to_u64().unwrap_or(0);
    // DP 表
    let mut dp: Vec<Vec<BigInt>> =
        vec![vec![BigInt::zero(); k_u64 as usize + 1]; n_u64 as usize + 1];
    dp[0][0] = BigInt::one();
    for i in 1..=n_u64 as usize {
        for j in 1..=k_u64 as usize {
            if j > i {
                break;
            }
            dp[i][j] = &dp[i - 1][j - 1] + &dp[i - 1][j] * BigInt::from(j);
        }
    }
    dp[n_u64 as usize][k_u64 as usize].clone()
}

/// 递归检查 AST 是否含组合函数调用。
fn contains_combinatorics_function(ast: &AstNode) -> bool {
    match ast {
        AstNode::FunctionCall(name, _) if COMBINATORICS_FUNCTIONS.contains(&name.as_str()) => true,
        AstNode::FunctionCall(_, args) => args.iter().any(contains_combinatorics_function),
        AstNode::BinaryOp(_, l, r) => {
            contains_combinatorics_function(l) || contains_combinatorics_function(r)
        }
        AstNode::UnaryOp(_, e) => contains_combinatorics_function(e),
        AstNode::Matrix(rows) => rows.iter().flatten().any(contains_combinatorics_function),
        AstNode::List(elements) => elements.iter().any(contains_combinatorics_function),
        AstNode::Number(_)
        | AstNode::Variable(_)
        | AstNode::Complex(_, _)
        | AstNode::BigNumber(_) => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::parser::parse;

    fn eval(input: &str) -> Result<EvalResult, CalcError> {
        let ast = parse(input).unwrap();
        let domain = CombinatoricsDomain;
        let ctx = EvalContext::new();
        domain.evaluate(&ast, &ctx)
    }

    fn eval_scalar(input: &str) -> Result<f64, CalcError> {
        eval(input).map(|r| r.as_scalar().expect("expected scalar result"))
    }

    // ===== UT-CMB-001: P(n,k) =====

    #[test]
    fn test_permutation_basic() {
        assert_eq!(eval_scalar("P(5,2)").unwrap(), 20.0);
    }

    // ===== UT-CMB-002: C(n,k) =====

    #[test]
    fn test_combination_basic() {
        assert_eq!(eval_scalar("C(10,3)").unwrap(), 120.0);
    }

    // ===== UT-CMB-003: Catalan =====

    #[test]
    fn test_catalan_basic() {
        assert_eq!(eval_scalar("catalan(5)").unwrap(), 42.0);
    }

    // ===== UT-CMB-004: Stirling =====

    #[test]
    fn test_stirling_basic() {
        assert_eq!(eval_scalar("stirling(5,2)").unwrap(), 15.0);
    }

    // ===== UT-CMB-005: C(n,0) =====

    #[test]
    fn test_combination_zero_k() {
        assert_eq!(eval_scalar("C(5,0)").unwrap(), 1.0);
    }

    // ===== UT-CMB-006: C(n,n) =====

    #[test]
    fn test_combination_n_n() {
        assert_eq!(eval_scalar("C(5,5)").unwrap(), 1.0);
    }

    // ===== UT-CMB-007: k>n =====

    #[test]
    fn test_combination_k_greater_than_n() {
        assert_eq!(eval_scalar("C(3,5)").unwrap(), 0.0);
    }

    // ===== UT-CMB-008: n<0 =====

    #[test]
    fn test_combination_negative_n() {
        let result = eval("C(-1,2)");
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== UT-CMB-009: k<0 =====

    #[test]
    fn test_combination_negative_k() {
        let result = eval("C(5,-1)");
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== UT-CMB-010: 大数 C =====

    #[test]
    fn test_combination_large() {
        // C(100,50) ≈ 10^29,超 u64 但在 u128 内
        let result = eval("C(100,50)").unwrap();
        match result {
            EvalResult::BigInt(b) => {
                let expected: BigInt = "100891344545564193334812497256".parse().unwrap();
                assert_eq!(b, expected);
            }
            EvalResult::Scalar(v) => panic!("expected BigInt for C(100,50), got Scalar({})", v),
            _ => panic!("unexpected result type"),
        }
    }

    // ===== UT-CMB-011: 大数 P =====

    #[test]
    fn test_permutation_large() {
        // P(20,10) = 20*19*...*11
        let result = eval_scalar("P(20,10)").unwrap();
        // P(20,10) = 670442572800
        assert_eq!(result, 670442572800.0);
    }

    // ===== UT-CMB-012: Catalan 边界 =====

    #[test]
    fn test_catalan_zero() {
        assert_eq!(eval_scalar("catalan(0)").unwrap(), 1.0);
    }

    // ===== UT-CMB-013: Catalan 大数 =====

    #[test]
    fn test_catalan_large() {
        // catalan(30) = 3814986502092304
        let result = eval("catalan(30)").unwrap();
        match result {
            EvalResult::BigInt(b) => {
                let expected: BigInt = "3814986502092304".parse().unwrap();
                assert_eq!(b, expected);
            }
            EvalResult::Scalar(v) => {
                // 可能 fit i64
                assert_eq!(v, 3814986502092304.0);
            }
            _ => panic!("unexpected result type"),
        }
    }

    // ===== UT-CMB-014: Stirling 边界 =====

    #[test]
    fn test_stirling_zero_zero() {
        assert_eq!(eval_scalar("stirling(0,0)").unwrap(), 1.0);
    }

    // ===== UT-CMB-015: 溢出处理 =====

    #[test]
    fn test_permutation_overflow_to_bigint() {
        // P(1000,500) 会溢出 u128,升级为 BigInt
        let result = eval("P(1000,500)");
        assert!(result.is_ok());
        match result.unwrap() {
            EvalResult::BigInt(_) => {} // 预期升级
            EvalResult::Scalar(_) => {} // 也可能 fit(不太可能但接受)
            _ => panic!("unexpected result type"),
        }
    }

    // ===== 补充边界测试 =====

    #[test]
    fn test_permutation_k_zero() {
        assert_eq!(eval_scalar("P(5,0)").unwrap(), 1.0);
    }

    #[test]
    fn test_permutation_n_equals_k() {
        // P(5,5) = 120 = 5!
        assert_eq!(eval_scalar("P(5,5)").unwrap(), 120.0);
    }

    #[test]
    fn test_permutation_k_greater_than_n() {
        assert_eq!(eval_scalar("P(3,5)").unwrap(), 0.0);
    }

    #[test]
    fn test_permutation_negative() {
        assert!(matches!(eval("P(-1,2)"), Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_combination_symmetry() {
        // C(10,3) == C(10,7)
        assert_eq!(
            eval_scalar("C(10,3)").unwrap(),
            eval_scalar("C(10,7)").unwrap()
        );
    }

    #[test]
    fn test_catalan_sequence() {
        // Catalan 数列:1, 1, 2, 5, 14, 42, 132, 429...
        assert_eq!(eval_scalar("catalan(1)").unwrap(), 1.0);
        assert_eq!(eval_scalar("catalan(2)").unwrap(), 2.0);
        assert_eq!(eval_scalar("catalan(3)").unwrap(), 5.0);
        assert_eq!(eval_scalar("catalan(4)").unwrap(), 14.0);
        assert_eq!(eval_scalar("catalan(6)").unwrap(), 132.0);
    }

    #[test]
    fn test_catalan_negative() {
        assert!(matches!(
            eval("catalan(-1)"),
            Err(CalcError::DomainError(_))
        ));
    }

    #[test]
    fn test_stirling_known_values() {
        // S(3,2) = 3
        assert_eq!(eval_scalar("stirling(3,2)").unwrap(), 3.0);
        // S(4,2) = 7
        assert_eq!(eval_scalar("stirling(4,2)").unwrap(), 7.0);
        // S(4,3) = 6
        assert_eq!(eval_scalar("stirling(4,3)").unwrap(), 6.0);
    }

    #[test]
    fn test_stirling_k_greater_than_n() {
        assert_eq!(eval_scalar("stirling(2,5)").unwrap(), 0.0);
    }

    #[test]
    fn test_stirling_n_zero_k_positive() {
        assert_eq!(eval_scalar("stirling(0,5)").unwrap(), 0.0);
    }

    #[test]
    fn test_stirling_n_positive_k_zero() {
        assert_eq!(eval_scalar("stirling(5,0)").unwrap(), 0.0);
    }

    #[test]
    fn test_stirling_negative() {
        assert!(matches!(
            eval("stirling(-1,2)"),
            Err(CalcError::DomainError(_))
        ));
    }

    // ===== 域元信息测试 =====

    #[test]
    fn test_domain_info() {
        let domain = CombinatoricsDomain;
        assert_eq!(domain.domain_name(), "combinatorics");
        assert_eq!(domain.priority(), 25);
    }

    #[test]
    fn test_default_impl() {
        let domain = CombinatoricsDomain;
        assert_eq!(domain.domain_name(), "combinatorics");
    }

    #[test]
    fn test_supports_P() {
        let ast = parse("P(5,2)").unwrap();
        assert!(CombinatoricsDomain.supports(&ast));
    }

    #[test]
    fn test_supports_nested() {
        let ast = parse("C(10,3) + 1").unwrap();
        assert!(CombinatoricsDomain.supports(&ast));
    }

    #[test]
    fn test_supports_unary() {
        let ast = AstNode::UnaryOp(UnaryOp::Neg, Box::new(parse("catalan(5)").unwrap()));
        assert!(CombinatoricsDomain.supports(&ast));
    }

    #[test]
    fn test_supports_matrix() {
        let ast = AstNode::Matrix(vec![vec![parse("P(5,2)").unwrap()]]);
        assert!(CombinatoricsDomain.supports(&ast));
    }

    #[test]
    fn test_supports_list() {
        let ast = AstNode::List(vec![parse("stirling(5,2)").unwrap()]);
        assert!(CombinatoricsDomain.supports(&ast));
    }

    #[test]
    fn test_not_supports_arithmetic() {
        let ast = parse("1+2").unwrap();
        assert!(!CombinatoricsDomain.supports(&ast));
    }

    // ===== 错误路径测试 =====

    #[test]
    fn test_unsupported_function() {
        let ast = AstNode::FunctionCall("sin".to_string(), vec![AstNode::Number(1.0)]);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_P_wrong_args() {
        let ast = AstNode::FunctionCall("P".to_string(), vec![AstNode::Number(1.0)]);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_catalan_wrong_args() {
        let ast = AstNode::FunctionCall(
            "catalan".to_string(),
            vec![AstNode::Number(1.0), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_node_float_rejected() {
        let ast = AstNode::Number(3.14);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_node_complex_rejected() {
        let ast = AstNode::Complex(1.0, 2.0);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_node_list_rejected() {
        let ast = AstNode::List(vec![AstNode::Number(1.0)]);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_unbound_variable() {
        let ast = AstNode::Variable("x".to_string());
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::EvalError(_))));
    }

    #[test]
    fn test_div_by_zero() {
        let ast = AstNode::BinaryOp(
            BinaryOp::Div,
            Box::new(AstNode::Number(10.0)),
            Box::new(AstNode::Number(0.0)),
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DivisionByZero)));
    }

    #[test]
    fn test_negative_exponent() {
        let ast = AstNode::BinaryOp(
            BinaryOp::Pow,
            Box::new(AstNode::Number(2.0)),
            Box::new(AstNode::Number(-1.0)),
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_unary_abs() {
        let ast = AstNode::UnaryOp(UnaryOp::Abs, Box::new(AstNode::Number(-5.0)));
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), 5.0);
    }

    #[test]
    fn test_big_number_invalid() {
        let ast = AstNode::BigNumber("not_a_number".to_string());
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== 底层算法测试 =====

    #[test]
    fn test_permutation_known() {
        assert_eq!(
            permutation(&BigInt::from(5), &BigInt::from(2)),
            BigInt::from(20)
        );
        assert_eq!(
            permutation(&BigInt::from(5), &BigInt::from(0)),
            BigInt::from(1)
        );
        assert_eq!(
            permutation(&BigInt::from(3), &BigInt::from(5)),
            BigInt::from(0)
        );
    }

    #[test]
    fn test_combination_known() {
        assert_eq!(
            combination(&BigInt::from(10), &BigInt::from(3)),
            BigInt::from(120)
        );
        assert_eq!(
            combination(&BigInt::from(5), &BigInt::from(0)),
            BigInt::from(1)
        );
        assert_eq!(
            combination(&BigInt::from(5), &BigInt::from(5)),
            BigInt::from(1)
        );
        assert_eq!(
            combination(&BigInt::from(3), &BigInt::from(5)),
            BigInt::from(0)
        );
    }

    #[test]
    fn test_catalan_known() {
        assert_eq!(catalan(&BigInt::from(0)), BigInt::from(1));
        assert_eq!(catalan(&BigInt::from(1)), BigInt::from(1));
        assert_eq!(catalan(&BigInt::from(5)), BigInt::from(42));
    }

    #[test]
    fn test_stirling_known() {
        assert_eq!(
            stirling_second(&BigInt::from(0), &BigInt::from(0)),
            BigInt::from(1)
        );
        assert_eq!(
            stirling_second(&BigInt::from(5), &BigInt::from(2)),
            BigInt::from(15)
        );
        assert_eq!(
            stirling_second(&BigInt::from(3), &BigInt::from(2)),
            BigInt::from(3)
        );
    }

    #[test]
    fn test_bigint_to_result() {
        assert_eq!(bigint_to_result(BigInt::from(42)), EvalResult::Scalar(42.0));
        let large = BigInt::from(2).pow(100);
        assert!(matches!(bigint_to_result(large), EvalResult::BigInt(_)));
    }

    // ===== 覆盖率补充测试 =====

    #[test]
    fn test_eval_node_integer_number() {
        // eval_node Number success (integer)
        let ast = AstNode::Number(42.0);
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), 42.0);
    }

    #[test]
    fn test_eval_node_bignumber() {
        // eval_node BigNumber success
        let ast = AstNode::BigNumber("12345678901234567890".to_string());
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert!(matches!(result, EvalResult::BigInt(_)));
    }

    #[test]
    fn test_eval_node_binaryop_success() {
        // eval_node BinaryOp success: 2+3 = 5
        let ast = AstNode::BinaryOp(
            BinaryOp::Add,
            Box::new(AstNode::Number(2.0)),
            Box::new(AstNode::Number(3.0)),
        );
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), 5.0);
    }

    #[test]
    fn test_eval_node_unary_neg() {
        // eval_node UnaryOp::Neg
        let ast = AstNode::UnaryOp(UnaryOp::Neg, Box::new(AstNode::Number(5.0)));
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), -5.0);
    }

    #[test]
    fn test_eval_node_unary_factorial_rejected() {
        // eval_node UnaryOp::Factorial error
        let ast = AstNode::UnaryOp(UnaryOp::Factorial, Box::new(AstNode::Number(5.0)));
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_non_integer() {
        // eval_int non-integer error via function arg
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![AstNode::Number(3.14), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_overflow() {
        // eval_int overflow: number > i64::MAX
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![AstNode::Number(1.0e20), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::Overflow)));
    }

    #[test]
    fn test_eval_int_bignumber_arg() {
        // eval_int BigNumber success via function arg
        let ast = AstNode::FunctionCall(
            "C".to_string(),
            vec![AstNode::BigNumber("100".to_string()), AstNode::Number(50.0)],
        );
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert!(matches!(result, EvalResult::BigInt(_)));
    }

    #[test]
    fn test_eval_int_variable_success() {
        // eval_int Variable success
        let ctx = EvalContext::new().with_var("x", 5.0);
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![AstNode::Variable("x".to_string()), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &ctx).unwrap();
        assert_eq!(result.as_scalar().unwrap(), 20.0);
    }

    #[test]
    fn test_eval_int_variable_non_integer() {
        // eval_int Variable non-integer error
        let ctx = EvalContext::new().with_var("x", 3.14);
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![AstNode::Variable("x".to_string()), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &ctx);
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_binaryop() {
        // eval_int BinaryOp: P(3+2, 2) = P(5,2) = 20
        assert_eq!(eval_scalar("P(3+2, 2)").unwrap(), 20.0);
    }

    #[test]
    fn test_eval_int_unary_abs() {
        // eval_int UnaryOp::Abs: P(abs(-5), 2) = P(5,2) = 20
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![
                AstNode::UnaryOp(UnaryOp::Abs, Box::new(AstNode::Number(-5.0))),
                AstNode::Number(2.0),
            ],
        );
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), 20.0);
    }

    #[test]
    fn test_eval_int_unary_factorial_rejected() {
        // eval_int UnaryOp::Factorial error
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![
                AstNode::UnaryOp(UnaryOp::Factorial, Box::new(AstNode::Number(5.0))),
                AstNode::Number(2.0),
            ],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_complex_rejected() {
        // eval_int Complex rejection
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![AstNode::Complex(1.0, 2.0), AstNode::Number(2.0)],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_binary_sub_mul() {
        // eval_int_binary Sub and Mul: P(6-1, 2*1) = P(5,2) = 20
        assert_eq!(eval_scalar("P(6-1, 2*1)").unwrap(), 20.0);
    }

    #[test]
    fn test_eval_int_binary_div_success() {
        // eval_int_binary Div success: P(10/2, 2) = P(5,2) = 20
        assert_eq!(eval_scalar("P(10/2, 2)").unwrap(), 20.0);
    }

    #[test]
    fn test_eval_int_binary_mod_success() {
        // eval_int_binary Mod success via direct AST: P(Mod(7,2), 2) = P(1,2) = 0
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![
                AstNode::BinaryOp(
                    BinaryOp::Mod,
                    Box::new(AstNode::Number(7.0)),
                    Box::new(AstNode::Number(2.0)),
                ),
                AstNode::Number(2.0),
            ],
        );
        let result = CombinatoricsDomain
            .evaluate(&ast, &EvalContext::new())
            .unwrap();
        assert_eq!(result.as_scalar().unwrap(), 0.0);
    }

    #[test]
    fn test_eval_int_binary_mod_by_zero() {
        // eval_int_binary Mod by zero
        let ast = AstNode::BinaryOp(
            BinaryOp::Mod,
            Box::new(AstNode::Number(10.0)),
            Box::new(AstNode::Number(0.0)),
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DivisionByZero)));
    }

    #[test]
    fn test_C_wrong_args() {
        let ast = AstNode::FunctionCall("C".to_string(), vec![AstNode::Number(1.0)]);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_stirling_wrong_args() {
        let ast = AstNode::FunctionCall("stirling".to_string(), vec![AstNode::Number(1.0)]);
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_binary_pow_success() {
        // eval_int_binary Pow success: P(2^3, 2) = P(8,2) = 56
        assert_eq!(eval_scalar("P(2^3, 2)").unwrap(), 56.0);
    }

    #[test]
    fn test_eval_int_binary_pow_negative() {
        // eval_int_binary Pow negative exponent error
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![
                AstNode::BinaryOp(
                    BinaryOp::Pow,
                    Box::new(AstNode::Number(2.0)),
                    Box::new(AstNode::Number(-1.0)),
                ),
                AstNode::Number(2.0),
            ],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_eval_int_binary_pow_overflow() {
        // eval_int_binary Pow overflow (exponent too large for u32)
        let ast = AstNode::FunctionCall(
            "P".to_string(),
            vec![
                AstNode::BinaryOp(
                    BinaryOp::Pow,
                    Box::new(AstNode::Number(2.0)),
                    Box::new(AstNode::BigNumber("99999999999999999999999999".to_string())),
                ),
                AstNode::Number(2.0),
            ],
        );
        let result = CombinatoricsDomain.evaluate(&ast, &EvalContext::new());
        assert!(matches!(result, Err(CalcError::Overflow)));
    }

    // ===== proptest 属性测试 =====

    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig { cases: 128, ..ProptestConfig::default() })]

        /// 属性:C(n,k) == C(n,n-k)
        #[test]
        fn prop_combination_symmetry(n in 0u64..50, k in 0u64..50) {
            if k <= n {
                let c1 = combination(&BigInt::from(n), &BigInt::from(k));
                let c2 = combination(&BigInt::from(n), &BigInt::from(n - k));
                prop_assert_eq!(c1, c2);
            }
        }

        /// 属性:C(n,0) == 1
        #[test]
        fn prop_combination_zero_k(n in 0u64..100) {
            prop_assert_eq!(combination(&BigInt::from(n), &BigInt::from(0)), BigInt::from(1));
        }

        /// 属性:P(n,n) == n!
        #[test]
        fn prop_permutation_n_n_is_factorial(n in 0u64..20) {
            let p = permutation(&BigInt::from(n), &BigInt::from(n));
            let mut factorial = BigInt::from(1);
            for i in 1..=n {
                factorial *= BigInt::from(i);
            }
            prop_assert_eq!(p, factorial);
        }

        /// 属性:C(n,k) <= P(n,k)
        #[test]
        fn prop_combination_le_permutation(n in 0u64..50, k in 0u64..50) {
            if k <= n {
                let c = combination(&BigInt::from(n), &BigInt::from(k));
                let p = permutation(&BigInt::from(n), &BigInt::from(k));
                prop_assert!(c <= p);
            }
        }
    }
}