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
// Copyright (c) 2026 Kirky.X. Licensed under the MIT License.

//! Complex 计算域:复数四则运算、模、幅角、共轭、复指数、复对数。
//!
//! 设计依据:
//! - complex-domain spec:10 个 requirements / 24 个 scenarios
//! - design.md D4:基于 `num_complex::Complex64` 实现,priority=30
//!
//! 路由策略:AST 含 `Complex` 节点或 `complex()`/`conj()`/`arg()` 函数调用时路由至本域。
//! `abs()`/`exp()`/`ln()` 仅当参数含 `Complex` 节点时路由至本域。

use crate::core::domain::CalculationDomain;
use crate::core::types::{AstNode, BinaryOp, CalcError, EvalContext, EvalResult, UnaryOp};
use num_complex::Complex64;

/// Complex 计算域。
///
/// priority=30,高于 Scientific(20)与 Arithmetic(10)。
/// 支持复数四则运算、模、幅角、共轭、复指数、复对数。
pub struct ComplexDomain;

impl CalculationDomain for ComplexDomain {
    fn domain_name(&self) -> &str {
        "complex"
    }

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

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

    fn evaluate(&self, ast: &AstNode, ctx: &EvalContext) -> Result<EvalResult, CalcError> {
        // 预绑定 pi/e(若上下文未提供)
        let mut ctx = ctx.clone();
        if ctx.get_var("pi").is_none() {
            ctx = ctx.with_var("pi", std::f64::consts::PI);
        }
        if ctx.get_var("e").is_none() {
            ctx = ctx.with_var("e", std::f64::consts::E);
        }

        let value = self.eval(ast, &ctx)?;
        match value {
            ComplexValue::Scalar(v) => {
                if !v.is_finite() {
                    return Err(CalcError::NaNOrInf);
                }
                Ok(EvalResult::Scalar(v))
            }
            ComplexValue::Complex(c) => {
                if !c.re.is_finite() || !c.im.is_finite() {
                    return Err(CalcError::NaNOrInf);
                }
                Ok(EvalResult::Complex(c.re, c.im))
            }
        }
    }
}

impl ComplexDomain {
    /// 递归求值 AST 节点,返回标量或复数。
    ///
    /// 标量与复数的区分规则(spec Req 9:混合运算):
    /// - `abs()`/`arg()` 返回标量
    /// - `conj()`/`exp()`/`ln()`/`complex()` 返回复数
    /// - `Complex` 节点返回复数,`Number` 节点返回标量
    /// - 二元运算:两个标量运算结果为标量;任一为复数则结果为复数
    fn eval(&self, ast: &AstNode, ctx: &EvalContext) -> Result<ComplexValue, CalcError> {
        match ast {
            AstNode::Number(n) => Ok(ComplexValue::Scalar(*n)),
            AstNode::Complex(re, im) => Ok(ComplexValue::Complex(Complex64::new(*re, *im))),
            AstNode::Variable(name) => {
                if name == "i" {
                    return Ok(ComplexValue::Complex(Complex64::new(0.0, 1.0)));
                }
                ctx.get_var(name)
                    .map(ComplexValue::Scalar)
                    .ok_or_else(|| CalcError::EvalError(format!("unbound variable: {}", name)))
            }
            AstNode::BinaryOp(op, l, r) => {
                let a = self.eval(l, ctx)?;
                let b = self.eval(r, ctx)?;
                self.eval_binary(*op, a, b)
            }
            AstNode::UnaryOp(op, e) => {
                let v = self.eval(e, ctx)?;
                match op {
                    UnaryOp::Neg => match v {
                        ComplexValue::Scalar(s) => Ok(ComplexValue::Scalar(-s)),
                        ComplexValue::Complex(c) => Ok(ComplexValue::Complex(-c)),
                    },
                    UnaryOp::Abs => Ok(ComplexValue::Scalar(v.to_complex().norm())),
                    UnaryOp::Factorial => Err(CalcError::DomainError(
                        "factorial not supported in complex domain".to_string(),
                    )),
                }
            }
            AstNode::FunctionCall(name, args) => self.eval_function(name, args, ctx),
            AstNode::Matrix(_) | AstNode::List(_) | AstNode::BigNumber(_) => {
                Err(CalcError::DomainError(format!(
                    "complex domain does not support this node type: {:?}",
                    ast
                )))
            }
        }
    }

    /// 求值二元运算。
    ///
    /// 两个标量做标量运算;任一为复数则提升为复数运算。
    fn eval_binary(
        &self,
        op: BinaryOp,
        a: ComplexValue,
        b: ComplexValue,
    ) -> Result<ComplexValue, CalcError> {
        // 两个标量 → 标量运算
        if let (ComplexValue::Scalar(av), ComplexValue::Scalar(bv)) = (&a, &b) {
            let result = match op {
                BinaryOp::Add => av + bv,
                BinaryOp::Sub => av - bv,
                BinaryOp::Mul => av * bv,
                BinaryOp::Div => {
                    if *bv == 0.0 {
                        return Err(CalcError::DivisionByZero);
                    }
                    av / bv
                }
                BinaryOp::Pow => av.powf(*bv),
                BinaryOp::Mod => {
                    return Err(CalcError::DomainError(
                        "mod not supported in complex domain".to_string(),
                    ))
                }
            };
            return Ok(ComplexValue::Scalar(result));
        }

        // 任一为复数 → 复数运算
        let ac = a.to_complex();
        let bc = b.to_complex();
        let result = match op {
            BinaryOp::Add => ac + bc,
            BinaryOp::Sub => ac - bc,
            BinaryOp::Mul => ac * bc,
            BinaryOp::Div => {
                if bc.norm() == 0.0 {
                    return Err(CalcError::DivisionByZero);
                }
                ac / bc
            }
            BinaryOp::Pow => ac.powc(bc),
            BinaryOp::Mod => {
                return Err(CalcError::DomainError(
                    "mod not supported for complex numbers".to_string(),
                ))
            }
        };
        Ok(ComplexValue::Complex(result))
    }

    /// 求值函数调用。
    fn eval_function(
        &self,
        name: &str,
        args: &[AstNode],
        ctx: &EvalContext,
    ) -> Result<ComplexValue, CalcError> {
        match name {
            "complex" => {
                if args.len() != 2 {
                    return Err(CalcError::DomainError(format!(
                        "complex() requires exactly 2 arguments, got {}",
                        args.len()
                    )));
                }
                let re = self.eval(&args[0], ctx)?.to_complex().re;
                let im = self.eval(&args[1], ctx)?.to_complex().re;
                Ok(ComplexValue::Complex(Complex64::new(re, im)))
            }
            "conj" => {
                let c = self.expect_one_arg(name, args, ctx)?;
                Ok(ComplexValue::Complex(c.conj()))
            }
            "arg" => {
                let c = self.expect_one_arg(name, args, ctx)?;
                Ok(ComplexValue::Scalar(c.arg()))
            }
            "abs" => {
                let c = self.expect_one_arg(name, args, ctx)?;
                Ok(ComplexValue::Scalar(c.norm()))
            }
            "exp" => {
                let c = self.expect_one_arg(name, args, ctx)?;
                Ok(ComplexValue::Complex(c.exp()))
            }
            "ln" => {
                let c = self.expect_one_arg(name, args, ctx)?;
                Ok(ComplexValue::Complex(c.ln()))
            }
            _ => Err(CalcError::DomainError(format!(
                "unsupported function in complex domain: {}",
                name
            ))),
        }
    }

    /// 辅助:要求单参数函数并返回复数值。
    fn expect_one_arg(
        &self,
        name: &str,
        args: &[AstNode],
        ctx: &EvalContext,
    ) -> Result<Complex64, CalcError> {
        if args.len() != 1 {
            return Err(CalcError::DomainError(format!(
                "{}() requires exactly 1 argument, got {}",
                name,
                args.len()
            )));
        }
        Ok(self.eval(&args[0], ctx)?.to_complex())
    }
}

/// 内部求值结果:标量或复数。
///
/// 用于区分 `abs()`/`arg()` 返回标量与其他运算返回复数(spec Req 9)。
enum ComplexValue {
    Scalar(f64),
    Complex(Complex64),
}

impl ComplexValue {
    /// 提升为复数。
    fn to_complex(&self) -> Complex64 {
        match self {
            ComplexValue::Scalar(v) => Complex64::new(*v, 0.0),
            ComplexValue::Complex(c) => *c,
        }
    }
}

/// 递归检查 AST 是否应路由至 ComplexDomain。
///
/// 路由条件(spec Req 8):
/// - 含 `Complex` 节点
/// - 含 `complex()`/`conj()`/`arg()` 函数调用(复数专用函数)
/// - `abs()`/`exp()`/`ln()` 的参数含 `Complex` 节点(共享函数,仅复数参数时路由)
fn contains_complex(ast: &AstNode) -> bool {
    match ast {
        AstNode::Complex(_, _) => true,
        AstNode::FunctionCall(name, args) => {
            // 复数专用函数:总是路由至 ComplexDomain
            if matches!(name.as_str(), "complex" | "conj" | "arg") {
                return true;
            }
            // 共享函数(abs/exp/ln):仅当参数含 Complex 节点时路由
            if matches!(name.as_str(), "abs" | "exp" | "ln") {
                return args.iter().any(contains_complex);
            }
            // 其他函数:检查参数
            args.iter().any(contains_complex)
        }
        AstNode::BinaryOp(_, l, r) => contains_complex(l) || contains_complex(r),
        AstNode::UnaryOp(_, e) => contains_complex(e),
        AstNode::Matrix(rows) => rows.iter().flatten().any(contains_complex),
        AstNode::List(elements) => elements.iter().any(contains_complex),
        AstNode::Number(_) | AstNode::Variable(_) | AstNode::BigNumber(_) => false,
    }
}

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

    /// 测试浮点近似相等(默认容差 1e-10)。
    fn assert_approx(actual: f64, expected: f64) {
        assert!(
            (actual - expected).abs() < 1e-10,
            "expected {} but got {}",
            expected,
            actual
        );
    }

    /// 测试复数近似相等。
    fn assert_complex_approx(actual: &EvalResult, expected_re: f64, expected_im: f64) {
        match actual {
            EvalResult::Complex(re, im) => {
                assert_approx(*re, expected_re);
                assert_approx(*im, expected_im);
            }
            other => panic!("expected Complex result, got {:?}", other),
        }
    }

    /// 测试标量结果。
    fn assert_scalar(actual: &EvalResult, expected: f64) {
        match actual {
            EvalResult::Scalar(v) => assert_approx(*v, expected),
            other => panic!("expected Scalar({}), got {:?}", expected, other),
        }
    }

    /// 创建预绑定 pi/e 的上下文。
    fn default_ctx() -> EvalContext {
        EvalContext::new()
            .with_var("pi", std::f64::consts::PI)
            .with_var("e", std::f64::consts::E)
    }

    // ===== Requirement 1: 复数字面量解析 =====

    #[test]
    fn test_complex_literal_standard() {
        // 3+4i → Complex(3, 4)(Req 1 Scen 1)
        let ast = parse("3+4i").unwrap();
        assert_eq!(ast, AstNode::Complex(3.0, 4.0));
    }

    #[test]
    fn test_complex_literal_pure_imaginary() {
        // 2i → Complex(0, 2)(Req 1 Scen 2)
        let ast = parse("2i").unwrap();
        assert_eq!(ast, AstNode::Complex(0.0, 2.0));
    }

    #[test]
    fn test_real_number_not_route_to_complex() {
        // 5 → Number(5),不路由到 ComplexDomain(Req 1 Scen 3)
        let ast = parse("5").unwrap();
        assert_eq!(ast, AstNode::Number(5.0));
        let domain = ComplexDomain;
        assert!(!domain.supports(&ast));
    }

    // ===== Requirement 2: 复数四则运算 =====

    #[test]
    fn test_complex_addition() {
        // (1+2i) + (3+4i) → 4+6i(Req 2 Scen 1)
        let ast = parse("(1+2i) + (3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 4.0, 6.0);
    }

    #[test]
    fn test_complex_subtraction() {
        // (1+2i) - (3+4i) → -2-2i(Req 2 Scen 2)
        let ast = parse("(1+2i) - (3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, -2.0, -2.0);
    }

    #[test]
    fn test_complex_multiplication() {
        // (1+2i) * (3+4i) → -5+10i(Req 2 Scen 3)
        let ast = parse("(1+2i) * (3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, -5.0, 10.0);
    }

    #[test]
    fn test_complex_division() {
        // (1+2i) / (3+4i) → 0.44+0.08i(Req 2 Scen 4)
        let ast = parse("(1+2i) / (3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 0.44, 0.08);
    }

    // ===== Requirement 3: 复数模 =====

    #[test]
    fn test_complex_abs_standard() {
        // abs(3+4i) → 5.0(Req 3 Scen 1)
        let ast = parse("abs(3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 5.0);
    }

    #[test]
    fn test_complex_abs_pure_imaginary() {
        // abs(0+3i) → 3.0(Req 3 Scen 2)
        let ast = parse("abs(0+3i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 3.0);
    }

    // ===== Requirement 4: 复数幅角 =====

    #[test]
    fn test_complex_arg_first_quadrant() {
        // arg(1+1i) → pi/4(Req 4 Scen 1)
        let ast = parse("arg(1+1i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, std::f64::consts::FRAC_PI_4);
    }

    #[test]
    fn test_complex_arg_positive_real() {
        // arg(2+0i) → 0.0(Req 4 Scen 2)
        let ast = parse("arg(2+0i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 0.0);
    }

    // ===== Requirement 5: 复数共轭 =====

    #[test]
    fn test_complex_conjugate_standard() {
        // conj(3+4i) → 3-4i(Req 5 Scen 1)
        let ast = parse("conj(3+4i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 3.0, -4.0);
    }

    #[test]
    fn test_complex_conjugate_pure_real() {
        // conj(5+0i) → 5+0i(Req 5 Scen 2)
        let ast = parse("conj(5+0i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 5.0, 0.0);
    }

    // ===== Requirement 6: 复指数 =====

    #[test]
    fn test_complex_exp_euler_identity() {
        // exp(complex(0,1)*pi) → -1+0i(Req 6 Scen 1,欧拉公式恒等式)
        // 等价于数学上的 exp(i*pi) = -1
        let ast = parse("exp(complex(0,1)*pi)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, -1.0, 0.0);
    }

    #[test]
    fn test_complex_exp_general() {
        // exp(1+1i) → 约 2.718+2.718i(Req 6 Scen 2)
        let ast = parse("exp(1+1i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        let expected_re = std::f64::consts::E * 1.0_f64.cos();
        let expected_im = std::f64::consts::E * 1.0_f64.sin();
        assert_complex_approx(&result, expected_re, expected_im);
    }

    // ===== Requirement 7: 复对数 =====

    #[test]
    fn test_complex_ln_one_plus_i() {
        // ln(1+1i) → 约 0.347+0.785i(Req 7 Scen 1)
        let ast = parse("ln(1+1i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        let c = Complex64::new(1.0, 1.0);
        let expected = c.ln();
        assert_complex_approx(&result, expected.re, expected.im);
    }

    #[test]
    fn test_complex_ln_positive_real() {
        // ln(2+0i) → 约 0.693+0i(Req 7 Scen 2)
        let ast = parse("ln(2+0i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, std::f64::consts::LN_2, 0.0);
    }

    // ===== Requirement 8: 域路由 =====

    #[test]
    fn test_route_complex_node() {
        // 3+4i + 1 → 含 Complex 节点,路由到 ComplexDomain(Req 8 Scen 1)
        let ast = parse("3+4i + 1").unwrap();
        let domain = ComplexDomain;
        assert!(domain.supports(&ast));
    }

    #[test]
    fn test_route_complex_function() {
        // conj(2+3i) → 含 conj() 函数,路由到 ComplexDomain(Req 8 Scen 2)
        let ast = parse("conj(2+3i)").unwrap();
        let domain = ComplexDomain;
        assert!(domain.supports(&ast));
    }

    // ===== Requirement 9: 混合运算 =====

    #[test]
    fn test_mixed_abs_complex_plus_real() {
        // abs(3+4i) + 2 → 标量 7.0(Req 9 Scen 1)
        let ast = parse("abs(3+4i) + 2").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 7.0);
    }

    #[test]
    fn test_mixed_complex_plus_real() {
        // (1+2i) + 3 → 4+2i(Req 9 Scen 2)
        let ast = parse("(1+2i) + 3").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 4.0, 2.0);
    }

    // ===== Requirement 10: 错误处理 =====

    #[test]
    fn test_unsupported_matrix_node() {
        // 矩阵节点 → DomainError(Req 10 Scen 1)
        let ast = AstNode::Matrix(vec![vec![AstNode::Number(1.0)]]);
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        let e = result.unwrap_err();
        assert!(
            matches!(e, CalcError::DomainError(_)),
            "expected DomainError, got {:?}",
            e
        );
    }

    #[test]
    fn test_invalid_complex_literal_parse_error() {
        // 3+*4i → 语法错误(Req 10 Scen 2,非法复数字面量)
        let result = parse("3+*4i");
        let e = result.unwrap_err();
        assert!(
            matches!(e, CalcError::ParseError(_)),
            "expected ParseError, got {:?}",
            e
        );
    }

    // ===== 额外覆盖:域优先级与名称 =====

    #[test]
    fn test_complex_domain_priority() {
        let domain = ComplexDomain;
        assert_eq!(domain.priority(), 30);
        assert_eq!(domain.domain_name(), "complex");
    }

    #[test]
    fn test_complex_priority_higher_than_scientific() {
        let complex = ComplexDomain;
        let scientific = crate::ScientificDomain;
        assert!(complex.priority() > scientific.priority());
    }

    // ===== 额外覆盖:除零与不支持的操作 =====

    #[test]
    fn test_complex_division_by_zero() {
        // (1+2i) / 0 → DivisionByZero
        let ast = parse("(1+2i) / 0").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DivisionByZero)));
    }

    #[test]
    fn test_complex_unsupported_function() {
        // unknown_func(1+2i) → DomainError
        let ast = parse("unknown_func(1+2i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_complex_factorial_unsupported() {
        // (1+2i)! → DomainError(阶乘不支持复数)
        let ast = AstNode::UnaryOp(UnaryOp::Factorial, Box::new(AstNode::Complex(1.0, 2.0)));
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_complex_list_unsupported() {
        // List 节点 → DomainError
        let ast = AstNode::List(vec![AstNode::Number(1.0)]);
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_complex_wrong_arg_count() {
        // conj() 无参数 → DomainError
        let ast = AstNode::FunctionCall("conj".to_string(), vec![]);
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    #[test]
    fn test_complex_pow() {
        // (1+1i)^2 → 0+2i
        let ast = parse("(1+1i)^2").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 0.0, 2.0);
    }

    #[test]
    fn test_complex_neg() {
        // -(1+2i) → -1-2i
        let ast = parse("-(1+2i)").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, -1.0, -2.0);
    }

    #[test]
    fn test_complex_abs_unary_op() {
        // abs 作为 UnaryOp.Abs
        let ast = AstNode::UnaryOp(UnaryOp::Abs, Box::new(AstNode::Complex(3.0, 4.0)));
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 5.0);
    }

    #[test]
    fn test_complex_function_call_abs_with_real_arg() {
        // abs(5) → 不路由到 complex(参数无 Complex 节点)
        let ast = parse("abs(5)").unwrap();
        let domain = ComplexDomain;
        assert!(!domain.supports(&ast));
    }

    #[test]
    fn test_complex_i_variable() {
        // i 变量 → Complex(0, 1)
        let ast = AstNode::Variable("i".to_string());
        let domain = ComplexDomain;
        // Variable("i") alone doesn't route to complex, but if evaluated:
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 0.0, 1.0);
    }

    #[test]
    fn test_complex_nan_detection() {
        // 0+0i / 0 → DivisionByZero (not NaN)
        let ast = parse("(0+0i) / 0").unwrap();
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DivisionByZero)));
    }

    // ===== 额外覆盖:pi/e 自动绑定(ctx 无 pi/e 时触发)=====

    #[test]
    fn test_pi_e_auto_binding() {
        // 使用无 pi/e 的上下文,触发 evaluate 中的自动绑定(lines 37, 40)
        let ast = parse("conj(3+4i)").unwrap();
        let domain = ComplexDomain;
        let ctx = EvalContext::new();
        let result = domain.evaluate(&ast, &ctx).unwrap();
        assert_complex_approx(&result, 3.0, -4.0);
    }

    // ===== 额外覆盖:标量结果 NaNOrInf =====

    #[test]
    fn test_scalar_nan_or_inf() {
        // abs(3+4i)^1000 → 标量结果为 infinity → NaNOrInf(line 47)
        let ast = AstNode::BinaryOp(
            BinaryOp::Pow,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(1000.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::NaNOrInf)));
    }

    // ===== 额外覆盖:复数结果 NaNOrInf =====

    #[test]
    fn test_complex_nan_or_inf() {
        // exp(1000+0i) → 复数结果 re 为 infinity → NaNOrInf(line 53)
        let ast = AstNode::FunctionCall("exp".to_string(), vec![AstNode::Complex(1000.0, 0.0)]);
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::NaNOrInf)));
    }

    // ===== 额外覆盖:标量取负(Neg on Scalar)=====

    #[test]
    fn test_neg_on_scalar_in_complex() {
        // (1+2i) + (-3) → Neg 作用于标量 -3(line 90)
        let ast = AstNode::BinaryOp(
            BinaryOp::Add,
            Box::new(AstNode::Complex(1.0, 2.0)),
            Box::new(AstNode::UnaryOp(
                UnaryOp::Neg,
                Box::new(AstNode::Number(3.0)),
            )),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, -2.0, 2.0);
    }

    // ===== 额外覆盖:标量二元运算(Sub, Mul, Div, Pow, Mod)=====

    #[test]
    fn test_scalar_sub_in_complex() {
        // abs(3+4i) - 1 → 5 - 1 = 4(line 119/120 Sub)
        let ast = AstNode::BinaryOp(
            BinaryOp::Sub,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(1.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 4.0);
    }

    #[test]
    fn test_scalar_mul_in_complex() {
        // abs(3+4i) * 2 → 5 * 2 = 10(line 120/121 Mul)
        let ast = AstNode::BinaryOp(
            BinaryOp::Mul,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(2.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 10.0);
    }

    #[test]
    fn test_scalar_div_in_complex() {
        // abs(3+4i) / 2 → 5 / 2 = 2.5(line 125/126 Div normal)
        let ast = AstNode::BinaryOp(
            BinaryOp::Div,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(2.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 2.5);
    }

    #[test]
    fn test_scalar_div_by_zero_in_complex() {
        // abs(3+4i) / 0 → DivisionByZero(line 123/124 Div by zero)
        let ast = AstNode::BinaryOp(
            BinaryOp::Div,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(0.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DivisionByZero)));
    }

    #[test]
    fn test_scalar_pow_in_complex() {
        // abs(3+4i) ^ 2 → 5 ^ 2 = 25(line 127/128 Pow)
        let ast = AstNode::BinaryOp(
            BinaryOp::Pow,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(2.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_scalar(&result, 25.0);
    }

    #[test]
    fn test_scalar_mod_in_complex() {
        // 两个标量的 Mod → DomainError(lines 129-132 Mod not supported)
        let ast = AstNode::BinaryOp(
            BinaryOp::Mod,
            Box::new(AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(3.0, 4.0)],
            )),
            Box::new(AstNode::Number(2.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== 额外覆盖:复数 Mod 不支持 =====

    #[test]
    fn test_complex_mod_unsupported() {
        // Complex % Complex → DomainError(lines 152-155)
        let ast = AstNode::BinaryOp(
            BinaryOp::Mod,
            Box::new(AstNode::Complex(1.0, 2.0)),
            Box::new(AstNode::Complex(3.0, 4.0)),
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== 额外覆盖:complex() 函数调用 =====

    #[test]
    fn test_complex_function_normal() {
        // complex(1, 2) 作为函数调用 → Complex(1, 2)(lines 176-178)
        let ast = AstNode::FunctionCall(
            "complex".to_string(),
            vec![AstNode::Number(1.0), AstNode::Number(2.0)],
        );
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx()).unwrap();
        assert_complex_approx(&result, 1.0, 2.0);
    }

    #[test]
    fn test_complex_function_wrong_arg_count() {
        // complex(1) → DomainError(lines 171-175 参数数量错误)
        let ast = AstNode::FunctionCall("complex".to_string(), vec![AstNode::Number(1.0)]);
        let domain = ComplexDomain;
        let result = domain.evaluate(&ast, &default_ctx());
        assert!(matches!(result, Err(CalcError::DomainError(_))));
    }

    // ===== 额外覆盖:contains_complex 对 UnaryOp/Matrix/List 的路由 =====

    #[test]
    fn test_supports_unary_op_with_complex() {
        // -(3+4i) → UnaryOp 包含 Complex → supports 返回 true(line 265)
        let ast = AstNode::UnaryOp(UnaryOp::Neg, Box::new(AstNode::Complex(3.0, 4.0)));
        let domain = ComplexDomain;
        assert!(domain.supports(&ast));
    }

    #[test]
    fn test_supports_matrix_with_complex() {
        // Matrix 包含 Complex → supports 返回 true(line 266)
        let ast = AstNode::Matrix(vec![vec![AstNode::Complex(1.0, 2.0)]]);
        let domain = ComplexDomain;
        assert!(domain.supports(&ast));
    }

    #[test]
    fn test_supports_list_with_complex() {
        // List 包含 Complex → supports 返回 true(line 267)
        let ast = AstNode::List(vec![AstNode::Complex(1.0, 2.0)]);
        let domain = ComplexDomain;
        assert!(domain.supports(&ast));
    }

    // ===== 覆盖测试辅助函数的 panic 分支(lines 294, 302)=====

    #[test]
    #[should_panic(expected = "expected Complex result")]
    fn test_assert_complex_approx_panics_on_non_complex() {
        // 传入 Scalar 而非 Complex → panic(line 294)
        assert_complex_approx(&EvalResult::Scalar(1.0), 1.0, 0.0);
    }

    #[test]
    #[should_panic(expected = "expected Scalar")]
    fn test_assert_scalar_panics_on_non_scalar() {
        // 传入 Complex 而非 Scalar → panic(line 302)
        assert_scalar(&EvalResult::Complex(1.0, 2.0), 1.0);
    }

    // ===== proptest 属性测试(task 12.7)=====

    use proptest::prelude::*;

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

        /// 属性:共轭的共轭 = 原复数(spec Req 5 数学性质)
        #[test]
        fn prop_double_conjugate_identity(re in -1e6f64..1e6, im in -1e6f64..1e6) {
            let ast = AstNode::FunctionCall(
                "conj".to_string(),
                vec![AstNode::FunctionCall(
                    "conj".to_string(),
                    vec![AstNode::Complex(re, im)],
                )],
            );
            let domain = ComplexDomain;
            let result = domain.evaluate(&ast, &default_ctx()).unwrap();
            match result {
                EvalResult::Complex(r, i) => {
                    prop_assert!((r - re).abs() < 1e-6);
                    prop_assert!((i - im).abs() < 1e-6);
                }
                _ => panic!("expected Complex result"),
            }
        }

        /// 属性:模非负(spec Req 3 数学性质)
        #[test]
        fn prop_abs_non_negative(re in -1e6f64..1e6, im in -1e6f64..1e6) {
            let ast = AstNode::FunctionCall(
                "abs".to_string(),
                vec![AstNode::Complex(re, im)],
            );
            let domain = ComplexDomain;
            let result = domain.evaluate(&ast, &default_ctx()).unwrap();
            match result {
                EvalResult::Scalar(v) => prop_assert!(v >= 0.0),
                _ => panic!("expected Scalar result"),
            }
        }

        /// 属性:欧拉公式 exp(i*theta) 模为 1
        /// exp(complex(0, theta)) 的模 = 1(对任意实数 theta)
        #[test]
        fn prop_euler_formula_unit_modulus(theta in -10.0f64..10.0) {
            let ast = AstNode::FunctionCall(
                "exp".to_string(),
                vec![AstNode::Complex(0.0, theta)],
            );
            let domain = ComplexDomain;
            let result = domain.evaluate(&ast, &default_ctx()).unwrap();
            match result {
                EvalResult::Complex(re, im) => {
                    let modulus = (re * re + im * im).sqrt();
                    prop_assert!((modulus - 1.0).abs() < 1e-9);
                }
                _ => panic!("expected Complex result"),
            }
        }

        /// 属性:复数加法交换律 a+b = b+a
        #[test]
        fn prop_addition_commutative(
            ar in -1e3f64..1e3, ai in -1e3f64..1e3,
            br in -1e3f64..1e3, bi in -1e3f64..1e3
        ) {
            let domain = ComplexDomain;
            let ctx = default_ctx();
            let ast_ab = AstNode::BinaryOp(
                BinaryOp::Add,
                Box::new(AstNode::Complex(ar, ai)),
                Box::new(AstNode::Complex(br, bi)),
            );
            let ast_ba = AstNode::BinaryOp(
                BinaryOp::Add,
                Box::new(AstNode::Complex(br, bi)),
                Box::new(AstNode::Complex(ar, ai)),
            );
            let r_ab = domain.evaluate(&ast_ab, &ctx).unwrap();
            let r_ba = domain.evaluate(&ast_ba, &ctx).unwrap();
            match (&r_ab, &r_ba) {
                (EvalResult::Complex(r1, i1), EvalResult::Complex(r2, i2)) => {
                    prop_assert!((r1 - r2).abs() < 1e-9);
                    prop_assert!((i1 - i2).abs() < 1e-9);
                }
                _ => panic!("expected Complex results"),
            }
        }

        /// 属性:复数与其共轭之和为实数(2*re)
        #[test]
        fn prop_conjugate_sum_is_real(re in -1e6f64..1e6, im in -1e6f64..1e6) {
            let ast = AstNode::BinaryOp(
                BinaryOp::Add,
                Box::new(AstNode::Complex(re, im)),
                Box::new(AstNode::FunctionCall(
                    "conj".to_string(),
                    vec![AstNode::Complex(re, im)],
                )),
            );
            let domain = ComplexDomain;
            let result = domain.evaluate(&ast, &default_ctx()).unwrap();
            match result {
                EvalResult::Complex(r, i) => {
                    prop_assert!((r - 2.0 * re).abs() < 1e-6);
                    prop_assert!(i.abs() < 1e-6);
                }
                _ => panic!("expected Complex result"),
            }
        }
    }
}