yufmath 0.1.1

A Rust CAS Lib.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
//! # 表达式数据结构
//!
//! 定义数学表达式的核心数据结构,支持各种数学运算和操作。

use super::{Number, MathConstant, BinaryOperator, UnaryOperator, ExprType, NumericType};
use std::fmt::{self, Display};
use std::collections::HashMap;
use num_traits::{ToPrimitive, Zero, Signed};

/// 数学表达式的核心数据结构
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Expression {
    /// 数值常量
    Number(Number),
    /// 变量
    Variable(String),
    /// 数学常量
    Constant(MathConstant),
    /// 二元运算
    BinaryOp {
        op: BinaryOperator,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    /// 一元运算
    UnaryOp {
        op: UnaryOperator,
        operand: Box<Expression>,
    },
    /// 函数调用
    Function {
        name: String,
        args: Vec<Expression>,
    },
    /// 矩阵表达式
    Matrix(Vec<Vec<Expression>>),
    /// 向量表达式
    Vector(Vec<Expression>),
    /// 集合表达式
    Set(Vec<Expression>),
    /// 区间表达式
    Interval {
        start: Box<Expression>,
        end: Box<Expression>,
        start_inclusive: bool,
        end_inclusive: bool,
    },
}

impl Expression {
    /// 创建数值表达式
    pub fn number(n: Number) -> Self {
        Expression::Number(n)
    }
    
    /// 创建变量表达式
    pub fn variable(name: impl Into<String>) -> Self {
        Expression::Variable(name.into())
    }
    
    /// 创建常量表达式
    pub fn constant(c: MathConstant) -> Self {
        Expression::Constant(c)
    }
    
    /// 创建二元运算表达式
    pub fn binary_op(op: BinaryOperator, left: Expression, right: Expression) -> Self {
        Expression::BinaryOp {
            op,
            left: Box::new(left),
            right: Box::new(right),
        }
    }
    
    /// 创建一元运算表达式
    pub fn unary_op(op: UnaryOperator, operand: Expression) -> Self {
        Expression::UnaryOp {
            op,
            operand: Box::new(operand),
        }
    }
    
    /// 创建函数调用表达式
    pub fn function(name: impl Into<String>, args: Vec<Expression>) -> Self {
        Expression::Function {
            name: name.into(),
            args,
        }
    }
    
    /// 创建矩阵表达式
    pub fn matrix(rows: Vec<Vec<Expression>>) -> Result<Self, String> {
        if rows.is_empty() {
            return Err("矩阵不能为空".to_string());
        }
        
        let cols = rows[0].len();
        if cols == 0 {
            return Err("矩阵行不能为空".to_string());
        }
        
        // 检查所有行的列数是否相同
        for (i, row) in rows.iter().enumerate() {
            if row.len() != cols {
                return Err(format!("矩阵第{}行的列数({})与第一行的列数({})不匹配", i + 1, row.len(), cols));
            }
        }
        
        Ok(Expression::Matrix(rows))
    }
    
    /// 创建向量表达式
    pub fn vector(elements: Vec<Expression>) -> Result<Self, String> {
        if elements.is_empty() {
            return Err("向量不能为空".to_string());
        }
        Ok(Expression::Vector(elements))
    }
    
    /// 创建集合表达式
    pub fn set(elements: Vec<Expression>) -> Self {
        Expression::Set(elements)
    }
    
    /// 创建区间表达式
    pub fn interval(start: Expression, end: Expression, start_inclusive: bool, end_inclusive: bool) -> Self {
        Expression::Interval {
            start: Box::new(start),
            end: Box::new(end),
            start_inclusive,
            end_inclusive,
        }
    }
    
    /// 创建加法表达式
    pub fn add(left: Expression, right: Expression) -> Self {
        Expression::binary_op(BinaryOperator::Add, left, right)
    }
    
    /// 创建减法表达式
    pub fn subtract(left: Expression, right: Expression) -> Self {
        Expression::binary_op(BinaryOperator::Subtract, left, right)
    }
    
    /// 创建乘法表达式
    pub fn multiply(left: Expression, right: Expression) -> Self {
        Expression::binary_op(BinaryOperator::Multiply, left, right)
    }
    
    /// 创建除法表达式
    pub fn divide(left: Expression, right: Expression) -> Self {
        Expression::binary_op(BinaryOperator::Divide, left, right)
    }
    
    /// 创建幂运算表达式
    pub fn power(base: Expression, exponent: Expression) -> Self {
        Expression::binary_op(BinaryOperator::Power, base, exponent)
    }
    
    /// 创建负号表达式
    pub fn negate(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Negate, operand)
    }
    
    /// 创建平方根表达式
    pub fn sqrt(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Sqrt, operand)
    }
    
    /// 创建绝对值表达式
    pub fn abs(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Abs, operand)
    }
    
    /// 创建正弦函数表达式
    pub fn sin(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Sin, operand)
    }
    
    /// 创建余弦函数表达式
    pub fn cos(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Cos, operand)
    }
    
    /// 创建正切函数表达式
    pub fn tan(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Tan, operand)
    }
    
    /// 创建自然对数表达式
    pub fn ln(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Ln, operand)
    }
    
    /// 创建指数函数表达式
    pub fn exp(operand: Expression) -> Self {
        Expression::unary_op(UnaryOperator::Exp, operand)
    }
    
    /// 检查表达式是否为常量
    pub fn is_constant(&self) -> bool {
        match self {
            Expression::Number(_) | Expression::Constant(_) => true,
            Expression::Variable(_) => false,
            Expression::BinaryOp { left, right, .. } => {
                left.is_constant() && right.is_constant()
            }
            Expression::UnaryOp { operand, .. } => operand.is_constant(),
            Expression::Function { args, .. } => args.iter().all(|arg| arg.is_constant()),
            Expression::Matrix(rows) => {
                rows.iter().all(|row| row.iter().all(|expr| expr.is_constant()))
            }
            Expression::Vector(elements) => {
                elements.iter().all(|expr| expr.is_constant())
            }
            Expression::Set(elements) => {
                elements.iter().all(|expr| expr.is_constant())
            }
            Expression::Interval { start, end, .. } => {
                start.is_constant() && end.is_constant()
            }
        }
    }
    
    /// 获取表达式中的所有变量
    pub fn get_variables(&self) -> Vec<String> {
        let mut vars = Vec::new();
        self.collect_variables(&mut vars);
        vars.sort();
        vars.dedup();
        vars
    }
    
    /// 递归收集变量名
    fn collect_variables(&self, vars: &mut Vec<String>) {
        match self {
            Expression::Variable(name) => vars.push(name.clone()),
            Expression::BinaryOp { left, right, .. } => {
                left.collect_variables(vars);
                right.collect_variables(vars);
            }
            Expression::UnaryOp { operand, .. } => {
                operand.collect_variables(vars);
            }
            Expression::Function { args, .. } => {
                for arg in args {
                    arg.collect_variables(vars);
                }
            }
            Expression::Matrix(rows) => {
                for row in rows {
                    for expr in row {
                        expr.collect_variables(vars);
                    }
                }
            }
            Expression::Vector(elements) => {
                for expr in elements {
                    expr.collect_variables(vars);
                }
            }
            Expression::Set(elements) => {
                for expr in elements {
                    expr.collect_variables(vars);
                }
            }
            Expression::Interval { start, end, .. } => {
                start.collect_variables(vars);
                end.collect_variables(vars);
            }
            _ => {} // 数值和常量不包含变量
        }
    }
    
    /// 推断表达式的类型
    pub fn infer_type(&self) -> ExprType {
        match self {
            Expression::Number(n) => ExprType::Numeric(n.get_numeric_type()),
            Expression::Variable(_) => ExprType::Symbolic,
            Expression::Constant(c) => match c {
                MathConstant::I => ExprType::Numeric(NumericType::Complex),
                MathConstant::PositiveInfinity | MathConstant::NegativeInfinity => ExprType::Numeric(NumericType::Real),
                MathConstant::Undefined => ExprType::Unknown,
                _ => ExprType::Numeric(NumericType::Real),
            },
            Expression::BinaryOp { op, left, right } => {
                let left_type = left.infer_type();
                let right_type = right.infer_type();
                self.infer_binary_op_type(op, &left_type, &right_type)
            }
            Expression::UnaryOp { op, operand } => {
                let operand_type = operand.infer_type();
                self.infer_unary_op_type(op, &operand_type)
            }
            Expression::Function { name, args } => {
                let arg_types: Vec<ExprType> = args.iter().map(|arg| arg.infer_type()).collect();
                self.infer_function_type(name, &arg_types)
            }
            Expression::Matrix(rows) => {
                if rows.is_empty() {
                    return ExprType::Unknown;
                }
                let rows_count = rows.len();
                let cols_count = rows[0].len();
                
                // 推断矩阵元素的公共类型
                let mut element_type = ExprType::Unknown;
                for row in rows {
                    for elem in row {
                        let elem_type = elem.infer_type();
                        element_type = self.common_type(&element_type, &elem_type);
                    }
                }
                
                ExprType::Matrix(rows_count, cols_count, Box::new(element_type))
            }
            Expression::Vector(elements) => {
                if elements.is_empty() {
                    return ExprType::Unknown;
                }
                
                let mut element_type = ExprType::Unknown;
                for elem in elements {
                    let elem_type = elem.infer_type();
                    element_type = self.common_type(&element_type, &elem_type);
                }
                
                ExprType::Vector(elements.len(), Box::new(element_type))
            }
            Expression::Set(elements) => {
                let mut element_type = ExprType::Unknown;
                for elem in elements {
                    let elem_type = elem.infer_type();
                    element_type = self.common_type(&element_type, &elem_type);
                }
                
                ExprType::Set(Box::new(element_type))
            }
            Expression::Interval { start, end, .. } => {
                let start_type = start.infer_type();
                let end_type = end.infer_type();
                let common_type = self.common_type(&start_type, &end_type);
                
                ExprType::Interval(Box::new(common_type))
            }
        }
    }
    
    /// 推断二元运算的结果类型
    fn infer_binary_op_type(&self, op: &BinaryOperator, left_type: &ExprType, right_type: &ExprType) -> ExprType {
        match op {
            BinaryOperator::Add | BinaryOperator::Subtract | BinaryOperator::Multiply | BinaryOperator::Divide => {
                match (left_type, right_type) {
                    (ExprType::Numeric(l), ExprType::Numeric(r)) => {
                        ExprType::Numeric(l.common_type(r))
                    }
                    _ => ExprType::Symbolic
                }
            }
            BinaryOperator::Power => {
                match (left_type, right_type) {
                    (ExprType::Numeric(NumericType::Integer), ExprType::Numeric(NumericType::Integer)) => {
                        ExprType::Numeric(NumericType::Rational)
                    }
                    (ExprType::Numeric(l), ExprType::Numeric(r)) => {
                        ExprType::Numeric(l.common_type(r))
                    }
                    _ => ExprType::Symbolic
                }
            }
            BinaryOperator::Modulo => {
                match (left_type, right_type) {
                    (ExprType::Numeric(NumericType::Integer), ExprType::Numeric(NumericType::Integer)) => {
                        ExprType::Numeric(NumericType::Integer)
                    }
                    _ => ExprType::Symbolic
                }
            }
            BinaryOperator::Equal | BinaryOperator::NotEqual | BinaryOperator::Less | 
            BinaryOperator::LessEqual | BinaryOperator::Greater | BinaryOperator::GreaterEqual => {
                ExprType::Numeric(NumericType::Integer) // 布尔值用整数表示
            }
            BinaryOperator::And | BinaryOperator::Or => {
                ExprType::Numeric(NumericType::Integer) // 布尔值用整数表示
            }
            BinaryOperator::Union | BinaryOperator::Intersection | BinaryOperator::SetDifference => {
                match (left_type, right_type) {
                    (ExprType::Set(l), ExprType::Set(r)) => {
                        let common_elem_type = self.common_type(l, r);
                        ExprType::Set(Box::new(common_elem_type))
                    }
                    _ => ExprType::Unknown
                }
            }
            BinaryOperator::MatrixMultiply => {
                match (left_type, right_type) {
                    (ExprType::Matrix(m, n, l_elem), ExprType::Matrix(p, q, r_elem)) => {
                        if n == p {
                            let common_elem_type = self.common_type(l_elem, r_elem);
                            ExprType::Matrix(*m, *q, Box::new(common_elem_type))
                        } else {
                            ExprType::Unknown // 矩阵维度不匹配
                        }
                    }
                    _ => ExprType::Unknown
                }
            }
            BinaryOperator::DotProduct => {
                match (left_type, right_type) {
                    (ExprType::Vector(m, l_elem), ExprType::Vector(n, r_elem)) => {
                        if m == n {
                            self.common_type(l_elem, r_elem)
                        } else {
                            ExprType::Unknown // 向量维度不匹配
                        }
                    }
                    _ => ExprType::Unknown
                }
            }
            BinaryOperator::CrossProduct => {
                match (left_type, right_type) {
                    (ExprType::Vector(3, l_elem), ExprType::Vector(3, r_elem)) => {
                        let common_elem_type = self.common_type(l_elem, r_elem);
                        ExprType::Vector(3, Box::new(common_elem_type))
                    }
                    _ => ExprType::Unknown // 叉积只适用于3维向量
                }
            }
        }
    }
    
    /// 推断一元运算的结果类型
    fn infer_unary_op_type(&self, op: &UnaryOperator, operand_type: &ExprType) -> ExprType {
        match op {
            UnaryOperator::Negate | UnaryOperator::Plus => operand_type.clone(),
            UnaryOperator::Abs => {
                match operand_type {
                    ExprType::Numeric(NumericType::Complex) => ExprType::Numeric(NumericType::Real),
                    _ => operand_type.clone()
                }
            }
            UnaryOperator::Sqrt => {
                match operand_type {
                    ExprType::Numeric(NumericType::Integer) => ExprType::Numeric(NumericType::Real),
                    ExprType::Numeric(NumericType::Rational) => ExprType::Numeric(NumericType::Real),
                    _ => operand_type.clone()
                }
            }
            UnaryOperator::Sin | UnaryOperator::Cos | UnaryOperator::Tan |
            UnaryOperator::Asin | UnaryOperator::Acos | UnaryOperator::Atan |
            UnaryOperator::Sinh | UnaryOperator::Cosh | UnaryOperator::Tanh |
            UnaryOperator::Asinh | UnaryOperator::Acosh | UnaryOperator::Atanh => {
                match operand_type {
                    ExprType::Numeric(NumericType::Complex) => ExprType::Numeric(NumericType::Complex),
                    ExprType::Numeric(_) => ExprType::Numeric(NumericType::Real),
                    _ => ExprType::Symbolic
                }
            }
            UnaryOperator::Ln | UnaryOperator::Log10 | UnaryOperator::Log2 => {
                match operand_type {
                    ExprType::Numeric(NumericType::Complex) => ExprType::Numeric(NumericType::Complex),
                    ExprType::Numeric(_) => ExprType::Numeric(NumericType::Real),
                    _ => ExprType::Symbolic
                }
            }
            UnaryOperator::Exp => {
                match operand_type {
                    ExprType::Numeric(NumericType::Complex) => ExprType::Numeric(NumericType::Complex),
                    ExprType::Numeric(_) => ExprType::Numeric(NumericType::Real),
                    _ => ExprType::Symbolic
                }
            }
            UnaryOperator::Factorial => {
                match operand_type {
                    ExprType::Numeric(NumericType::Integer) => ExprType::Numeric(NumericType::Integer),
                    _ => ExprType::Symbolic
                }
            }
            UnaryOperator::Gamma => ExprType::Numeric(NumericType::Real),
            UnaryOperator::Not => ExprType::Numeric(NumericType::Integer), // 布尔值用整数表示
            UnaryOperator::Real | UnaryOperator::Imaginary => ExprType::Numeric(NumericType::Real),
            UnaryOperator::Conjugate => operand_type.clone(),
            UnaryOperator::Argument => ExprType::Numeric(NumericType::Real),
            UnaryOperator::Transpose => {
                match operand_type {
                    ExprType::Matrix(m, n, elem_type) => ExprType::Matrix(*n, *m, elem_type.clone()),
                    ExprType::Vector(n, elem_type) => ExprType::Matrix(*n, 1, elem_type.clone()),
                    _ => ExprType::Unknown
                }
            }
            UnaryOperator::Determinant => {
                match operand_type {
                    ExprType::Matrix(m, n, elem_type) if m == n => elem_type.as_ref().clone(),
                    _ => ExprType::Unknown
                }
            }
            UnaryOperator::Inverse => {
                match operand_type {
                    ExprType::Matrix(m, n, elem_type) if m == n => {
                        ExprType::Matrix(*m, *n, elem_type.clone())
                    }
                    _ => ExprType::Unknown
                }
            }
            UnaryOperator::Trace => {
                match operand_type {
                    ExprType::Matrix(m, n, elem_type) if m == n => elem_type.as_ref().clone(),
                    _ => ExprType::Unknown
                }
            }
        }
    }
    
    /// 推断函数调用的结果类型
    fn infer_function_type(&self, name: &str, arg_types: &[ExprType]) -> ExprType {
        match name {
            "sin" | "cos" | "tan" | "asin" | "acos" | "atan" |
            "sinh" | "cosh" | "tanh" | "asinh" | "acosh" | "atanh" |
            "ln" | "log" | "exp" | "sqrt" | "abs" => {
                if arg_types.len() == 1 {
                    match &arg_types[0] {
                        ExprType::Numeric(NumericType::Complex) => ExprType::Numeric(NumericType::Complex),
                        ExprType::Numeric(_) => ExprType::Numeric(NumericType::Real),
                        _ => ExprType::Symbolic
                    }
                } else {
                    ExprType::Unknown
                }
            }
            "max" | "min" => {
                if arg_types.len() >= 2 {
                    let mut result_type = arg_types[0].clone();
                    for arg_type in &arg_types[1..] {
                        result_type = self.common_type(&result_type, arg_type);
                    }
                    result_type
                } else {
                    ExprType::Unknown
                }
            }
            _ => ExprType::Unknown // 未知函数
        }
    }
    
    /// 获取两个类型的公共类型
    fn common_type(&self, type1: &ExprType, type2: &ExprType) -> ExprType {
        match (type1, type2) {
            (ExprType::Numeric(n1), ExprType::Numeric(n2)) => {
                ExprType::Numeric(n1.common_type(n2))
            }
            (ExprType::Unknown, t) | (t, ExprType::Unknown) => t.clone(),
            (ExprType::Symbolic, _) | (_, ExprType::Symbolic) => ExprType::Symbolic,
            (t1, t2) if t1 == t2 => t1.clone(),
            _ => ExprType::Unknown
        }
    }
    
    /// 验证表达式的类型一致性
    pub fn validate(&self) -> Result<(), String> {
        match self {
            Expression::BinaryOp { op, left, right } => {
                left.validate()?;
                right.validate()?;
                
                let left_type = left.infer_type();
                let right_type = right.infer_type();
                
                match op {
                    BinaryOperator::MatrixMultiply => {
                        match (&left_type, &right_type) {
                            (ExprType::Matrix(_, n, _), ExprType::Matrix(p, _, _)) => {
                                if n != p {
                                    return Err(format!("矩阵乘法维度不匹配:{}×? 和 {}×?", n, p));
                                }
                            }
                            _ => return Err("矩阵乘法只能应用于矩阵".to_string())
                        }
                    }
                    BinaryOperator::DotProduct => {
                        match (&left_type, &right_type) {
                            (ExprType::Vector(m, _), ExprType::Vector(n, _)) => {
                                if m != n {
                                    return Err(format!("向量点积维度不匹配:{}{}", m, n));
                                }
                            }
                            _ => return Err("点积只能应用于向量".to_string())
                        }
                    }
                    BinaryOperator::CrossProduct => {
                        match (&left_type, &right_type) {
                            (ExprType::Vector(3, _), ExprType::Vector(3, _)) => {}
                            _ => return Err("叉积只能应用于3维向量".to_string())
                        }
                    }
                    _ => {} // 其他运算符暂不做特殊验证
                }
                
                Ok(())
            }
            Expression::UnaryOp { op, operand } => {
                operand.validate()?;
                
                let operand_type = operand.infer_type();
                
                match op {
                    UnaryOperator::Determinant | UnaryOperator::Inverse | UnaryOperator::Trace => {
                        match &operand_type {
                            ExprType::Matrix(m, n, _) => {
                                if m != n {
                                    return Err(format!("{}只能应用于方阵", op.name()));
                                }
                            }
                            _ => return Err(format!("{}只能应用于矩阵", op.name()))
                        }
                    }
                    UnaryOperator::Factorial => {
                        match &operand_type {
                            ExprType::Numeric(NumericType::Integer) | ExprType::Symbolic => {}
                            _ => return Err("阶乘只能应用于整数".to_string())
                        }
                    }
                    _ => {} // 其他运算符暂不做特殊验证
                }
                
                Ok(())
            }
            Expression::Function { args, .. } => {
                for arg in args {
                    arg.validate()?;
                }
                Ok(())
            }
            Expression::Matrix(rows) => {
                if rows.is_empty() {
                    return Err("矩阵不能为空".to_string());
                }
                
                let cols = rows[0].len();
                if cols == 0 {
                    return Err("矩阵行不能为空".to_string());
                }
                
                for (i, row) in rows.iter().enumerate() {
                    if row.len() != cols {
                        return Err(format!("矩阵第{}行的列数({})与第一行的列数({})不匹配", i + 1, row.len(), cols));
                    }
                    
                    for elem in row {
                        elem.validate()?;
                    }
                }
                
                Ok(())
            }
            Expression::Vector(elements) => {
                if elements.is_empty() {
                    return Err("向量不能为空".to_string());
                }
                
                for elem in elements {
                    elem.validate()?;
                }
                
                Ok(())
            }
            Expression::Set(elements) => {
                for elem in elements {
                    elem.validate()?;
                }
                Ok(())
            }
            Expression::Interval { start, end, .. } => {
                start.validate()?;
                end.validate()?;
                Ok(())
            }
            _ => Ok(()) // 基本表达式不需要特殊验证
        }
    }
    
    /// 获取表达式的复杂度(用于性能优化)
    pub fn complexity(&self) -> usize {
        match self {
            Expression::Number(_) | Expression::Variable(_) | Expression::Constant(_) => 1,
            Expression::BinaryOp { left, right, .. } => 1 + left.complexity() + right.complexity(),
            Expression::UnaryOp { operand, .. } => 1 + operand.complexity(),
            Expression::Function { args, .. } => {
                1 + args.iter().map(|arg| arg.complexity()).sum::<usize>()
            }
            Expression::Matrix(rows) => {
                1 + rows.iter().map(|row| {
                    row.iter().map(|elem| elem.complexity()).sum::<usize>()
                }).sum::<usize>()
            }
            Expression::Vector(elements) => {
                1 + elements.iter().map(|elem| elem.complexity()).sum::<usize>()
            }
            Expression::Set(elements) => {
                1 + elements.iter().map(|elem| elem.complexity()).sum::<usize>()
            }
            Expression::Interval { start, end, .. } => {
                1 + start.complexity() + end.complexity()
            }
        }
    }
    
    /// 使用变量值替换表达式中的变量
    pub fn substitute(&self, variables: &HashMap<String, Expression>) -> Expression {
        match self {
            Expression::Variable(name) => {
                variables.get(name).cloned().unwrap_or_else(|| self.clone())
            }
            Expression::BinaryOp { op, left, right } => {
                Expression::BinaryOp {
                    op: op.clone(),
                    left: Box::new(left.substitute(variables)),
                    right: Box::new(right.substitute(variables)),
                }
            }
            Expression::UnaryOp { op, operand } => {
                Expression::UnaryOp {
                    op: op.clone(),
                    operand: Box::new(operand.substitute(variables)),
                }
            }
            Expression::Function { name, args } => {
                Expression::Function {
                    name: name.clone(),
                    args: args.iter().map(|arg| arg.substitute(variables)).collect(),
                }
            }
            Expression::Matrix(rows) => {
                Expression::Matrix(
                    rows.iter()
                        .map(|row| row.iter().map(|elem| elem.substitute(variables)).collect())
                        .collect()
                )
            }
            Expression::Vector(elements) => {
                Expression::Vector(
                    elements.iter().map(|elem| elem.substitute(variables)).collect()
                )
            }
            Expression::Set(elements) => {
                Expression::Set(
                    elements.iter().map(|elem| elem.substitute(variables)).collect()
                )
            }
            Expression::Interval { start, end, start_inclusive, end_inclusive } => {
                Expression::Interval {
                    start: Box::new(start.substitute(variables)),
                    end: Box::new(end.substitute(variables)),
                    start_inclusive: *start_inclusive,
                    end_inclusive: *end_inclusive,
                }
            }
            // 对于数值和常量,直接返回克隆
            _ => self.clone(),
        }
    }
    
    /// 使用数值替换表达式中的变量
    pub fn substitute_numbers(&self, variables: &HashMap<String, Number>) -> Expression {
        let expr_vars: HashMap<String, Expression> = variables
            .iter()
            .map(|(k, v)| (k.clone(), Expression::Number(v.clone())))
            .collect();
        self.substitute(&expr_vars)
    }
    
    /// 求值表达式,返回数值结果
    pub fn evaluate(&self, variables: &HashMap<String, Number>) -> Result<Number, String> {
        // 首先进行变量替换
        let substituted = self.substitute_numbers(variables);
        
        // 然后对替换后的表达式进行求值
        substituted.evaluate_exact()
    }
    
    /// 精确求值表达式(不包含变量)
    pub fn evaluate_exact(&self) -> Result<Number, String> {
        match self {
            Expression::Number(n) => Ok(n.clone()),
            
            Expression::Variable(name) => {
                Err(format!("未定义的变量: {}", name))
            }
            
            Expression::Constant(c) => {
                Ok(self.evaluate_constant(c))
            }
            
            Expression::BinaryOp { op, left, right } => {
                let left_val = left.evaluate_exact()?;
                let right_val = right.evaluate_exact()?;
                self.evaluate_binary_op(op, &left_val, &right_val)
            }
            
            Expression::UnaryOp { op, operand } => {
                let operand_val = operand.evaluate_exact()?;
                self.evaluate_unary_op(op, &operand_val)
            }
            
            Expression::Function { name, args } => {
                // 对于函数调用,我们需要特殊处理,因为有些函数需要检查原始表达式结构
                self.evaluate_function_with_expressions(name, args)
            }
            
            Expression::Matrix(rows) => {
                // 矩阵求值:对每个元素求值
                let evaluated_rows: Result<Vec<Vec<Number>>, String> = rows
                    .iter()
                    .map(|row| {
                        row.iter()
                            .map(|elem| elem.evaluate_exact())
                            .collect()
                    })
                    .collect();
                
                match evaluated_rows {
                    Ok(values) => {
                        // 将矩阵包装为符号表达式,因为 Number 不直接支持矩阵
                        let matrix_expr = Expression::Matrix(
                            values.into_iter()
                                .map(|row| row.into_iter().map(Expression::Number).collect())
                                .collect()
                        );
                        Ok(Number::Symbolic(Box::new(matrix_expr)))
                    }
                    Err(e) => Err(e),
                }
            }
            
            Expression::Vector(elements) => {
                // 向量求值:对每个元素求值
                let evaluated_elements: Result<Vec<Number>, String> = elements
                    .iter()
                    .map(|elem| elem.evaluate_exact())
                    .collect();
                
                match evaluated_elements {
                    Ok(values) => {
                        // 将向量包装为符号表达式
                        let vector_expr = Expression::Vector(
                            values.into_iter().map(Expression::Number).collect()
                        );
                        Ok(Number::Symbolic(Box::new(vector_expr)))
                    }
                    Err(e) => Err(e),
                }
            }
            
            Expression::Set(elements) => {
                // 集合求值:对每个元素求值
                let evaluated_elements: Result<Vec<Number>, String> = elements
                    .iter()
                    .map(|elem| elem.evaluate_exact())
                    .collect();
                
                match evaluated_elements {
                    Ok(values) => {
                        // 将集合包装为符号表达式
                        let set_expr = Expression::Set(
                            values.into_iter().map(Expression::Number).collect()
                        );
                        Ok(Number::Symbolic(Box::new(set_expr)))
                    }
                    Err(e) => Err(e),
                }
            }
            
            Expression::Interval { start, end, .. } => {
                let start_val = start.evaluate_exact()?;
                let end_val = end.evaluate_exact()?;
                
                // 将区间包装为符号表达式
                let interval_expr = Expression::Interval {
                    start: Box::new(Expression::Number(start_val)),
                    end: Box::new(Expression::Number(end_val)),
                    start_inclusive: true, // 简化处理
                    end_inclusive: true,
                };
                Ok(Number::Symbolic(Box::new(interval_expr)))
            }
        }
    }
    
    /// 求值数学常量
    fn evaluate_constant(&self, constant: &MathConstant) -> Number {
        match constant {
            MathConstant::Pi => {
                // 返回符号表示,保持精确性
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::Pi)))
            }
            MathConstant::E => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::E)))
            }
            MathConstant::I => {
                // 虚数单位可以精确表示
                Number::i()
            }
            MathConstant::EulerGamma => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::EulerGamma)))
            }
            MathConstant::GoldenRatio => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::GoldenRatio)))
            }
            MathConstant::Catalan => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::Catalan)))
            }
            MathConstant::PositiveInfinity => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::PositiveInfinity)))
            }
            MathConstant::NegativeInfinity => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::NegativeInfinity)))
            }
            MathConstant::Undefined => {
                Number::Symbolic(Box::new(Expression::Constant(MathConstant::Undefined)))
            }
        }
    }
    
    /// 求值二元运算
    fn evaluate_binary_op(&self, op: &BinaryOperator, left: &Number, right: &Number) -> Result<Number, String> {
        match op {
            BinaryOperator::Add => Ok(left.clone() + right.clone()),
            BinaryOperator::Subtract => Ok(left.clone() - right.clone()),
            BinaryOperator::Multiply => Ok(left.clone() * right.clone()),
            BinaryOperator::Divide => {
                if right.is_zero() {
                    Err("除零错误".to_string())
                } else {
                    Ok(left.clone() / right.clone())
                }
            }
            BinaryOperator::Power => {
                self.evaluate_power(left, right)
            }
            BinaryOperator::Modulo => {
                self.evaluate_modulo(left, right)
            }
            BinaryOperator::Equal => {
                Ok(if left == right { Number::one() } else { Number::zero() })
            }
            BinaryOperator::NotEqual => {
                Ok(if left != right { Number::one() } else { Number::zero() })
            }
            BinaryOperator::Less => {
                self.evaluate_comparison(left, right, |a, b| a < b)
            }
            BinaryOperator::LessEqual => {
                self.evaluate_comparison(left, right, |a, b| a <= b)
            }
            BinaryOperator::Greater => {
                self.evaluate_comparison(left, right, |a, b| a > b)
            }
            BinaryOperator::GreaterEqual => {
                self.evaluate_comparison(left, right, |a, b| a >= b)
            }
            BinaryOperator::And => {
                Ok(if !left.is_zero() && !right.is_zero() { Number::one() } else { Number::zero() })
            }
            BinaryOperator::Or => {
                Ok(if !left.is_zero() || !right.is_zero() { Number::one() } else { Number::zero() })
            }
            // 对于复杂运算,返回符号表示
            _ => {
                Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
                    op: op.clone(),
                    left: Box::new(Expression::Number(left.clone())),
                    right: Box::new(Expression::Number(right.clone())),
                })))
            }
        }
    }
    
    /// 求值一元运算
    fn evaluate_unary_op(&self, op: &UnaryOperator, operand: &Number) -> Result<Number, String> {
        match op {
            UnaryOperator::Negate => Ok(-operand.clone()),
            UnaryOperator::Plus => Ok(operand.clone()),
            UnaryOperator::Abs => operand.abs().map_err(|e| format!("{}", e)),
            UnaryOperator::Sqrt => {
                self.evaluate_sqrt(operand)
            }
            UnaryOperator::Factorial => {
                self.evaluate_factorial(operand)
            }
            // 对于三角函数和其他复杂函数,返回符号表示以保持精确性
            UnaryOperator::Sin | UnaryOperator::Cos | UnaryOperator::Tan |
            UnaryOperator::Asin | UnaryOperator::Acos | UnaryOperator::Atan |
            UnaryOperator::Sinh | UnaryOperator::Cosh | UnaryOperator::Tanh |
            UnaryOperator::Asinh | UnaryOperator::Acosh | UnaryOperator::Atanh |
            UnaryOperator::Ln | UnaryOperator::Log10 | UnaryOperator::Log2 |
            UnaryOperator::Exp | UnaryOperator::Gamma => {
                Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                    op: op.clone(),
                    operand: Box::new(Expression::Number(operand.clone())),
                })))
            }
            UnaryOperator::Real => {
                match operand {
                    Number::Complex { real, .. } => Ok(*real.clone()),
                    _ => Ok(operand.clone()),
                }
            }
            UnaryOperator::Imaginary => {
                match operand {
                    Number::Complex { imaginary, .. } => Ok(*imaginary.clone()),
                    _ => Ok(Number::zero()),
                }
            }
            UnaryOperator::Conjugate => {
                match operand {
                    Number::Complex { real, imaginary } => {
                        Ok(Number::Complex {
                            real: real.clone(),
                            imaginary: Box::new(imaginary.clone().neg()),
                        })
                    }
                    _ => Ok(operand.clone()),
                }
            }
            // 其他运算符返回符号表示
            _ => {
                Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                    op: op.clone(),
                    operand: Box::new(Expression::Number(operand.clone())),
                })))
            }
        }
    }
    
    /// 求值幂运算
    fn evaluate_power(&self, base: &Number, exponent: &Number) -> Result<Number, String> {
        // 处理一些特殊情况
        if exponent.is_zero() {
            return Ok(Number::one());
        }
        if exponent.is_one() {
            return Ok(base.clone());
        }
        if base.is_zero() {
            if exponent.is_positive() {
                return Ok(Number::zero());
            } else {
                return Err("0的负数次幂未定义".to_string());
            }
        }
        if base.is_one() {
            return Ok(Number::one());
        }
        
        // 对于整数底数和小的正整数指数,可以精确计算
        if let (Some(base_int), Some(exp_int)) = (base.to_integer(), exponent.to_integer()) {
            if exp_int >= num_bigint::BigInt::from(0) && exp_int <= num_bigint::BigInt::from(100) {
                let exp_u32 = exp_int.to_u32();
                if let Some(exp_val) = exp_u32 {
                    return Ok(Number::Integer(base_int.pow(exp_val)));
                }
            }
        }
        
        // 其他情况返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
            op: BinaryOperator::Power,
            left: Box::new(Expression::Number(base.clone())),
            right: Box::new(Expression::Number(exponent.clone())),
        })))
    }
    
    /// 求值取模运算
    fn evaluate_modulo(&self, left: &Number, right: &Number) -> Result<Number, String> {
        if right.is_zero() {
            return Err("模零错误".to_string());
        }
        
        match (left.to_integer(), right.to_integer()) {
            (Some(a), Some(b)) => {
                Ok(Number::Integer(a % b))
            }
            _ => {
                // 非整数的取模运算返回符号表示
                Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
                    op: BinaryOperator::Modulo,
                    left: Box::new(Expression::Number(left.clone())),
                    right: Box::new(Expression::Number(right.clone())),
                })))
            }
        }
    }
    
    /// 求值比较运算
    fn evaluate_comparison<F>(&self, left: &Number, right: &Number, compare: F) -> Result<Number, String>
    where
        F: Fn(f64, f64) -> bool,
    {
        // 对于数值比较,我们使用近似值
        let left_approx = left.approximate();
        let right_approx = right.approximate();
        
        if left_approx.is_nan() || right_approx.is_nan() {
            // 如果无法比较,返回符号表示
            return Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
                op: BinaryOperator::Less, // 这里应该根据实际的比较运算符来设置
                left: Box::new(Expression::Number(left.clone())),
                right: Box::new(Expression::Number(right.clone())),
            })));
        }
        
        Ok(if compare(left_approx, right_approx) { Number::one() } else { Number::zero() })
    }
    
    /// 求值平方根
    fn evaluate_sqrt(&self, operand: &Number) -> Result<Number, String> {
        match operand {
            Number::Integer(i) => {
                // 检查是否为完全平方数
                let sqrt_approx = (i.to_f64().unwrap_or(0.0)).sqrt();
                let sqrt_int = sqrt_approx.round() as i64;
                if (sqrt_int * sqrt_int) as f64 == sqrt_approx * sqrt_approx {
                    Ok(Number::Integer(num_bigint::BigInt::from(sqrt_int)))
                } else {
                    // 不是完全平方数,返回符号表示
                    Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                        op: UnaryOperator::Sqrt,
                        operand: Box::new(Expression::Number(operand.clone())),
                    })))
                }
            }
            Number::Rational(r) => {
                // 有理数的平方根通常是无理数,返回符号表示
                Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                    op: UnaryOperator::Sqrt,
                    operand: Box::new(Expression::Number(operand.clone())),
                })))
            }
            _ => {
                // 其他情况返回符号表示
                Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                    op: UnaryOperator::Sqrt,
                    operand: Box::new(Expression::Number(operand.clone())),
                })))
            }
        }
    }
    
    /// 求值阶乘
    fn evaluate_factorial(&self, operand: &Number) -> Result<Number, String> {
        if let Some(n) = operand.to_integer() {
            if n < num_bigint::BigInt::from(0) {
                return Err("负数的阶乘未定义".to_string());
            }
            
            if n > num_bigint::BigInt::from(1000) {
                // 对于大数,返回符号表示以避免计算过慢
                return Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                    op: UnaryOperator::Factorial,
                    operand: Box::new(Expression::Number(operand.clone())),
                })));
            }
            
            let mut result = num_bigint::BigInt::from(1);
            let mut i = num_bigint::BigInt::from(1);
            while i <= n {
                result *= &i;
                i += 1;
            }
            
            Ok(Number::Integer(result))
        } else {
            Err("阶乘只能应用于非负整数".to_string())
        }
    }
    
    /// 求值函数调用(使用表达式参数)
    fn evaluate_function_with_expressions(&self, name: &str, args: &[Expression]) -> Result<Number, String> {
        match name {
            // 三角函数
            "sin" => {
                if args.len() != 1 {
                    return Err("sin函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function_with_expr("sin", &args[0])
            }
            "cos" => {
                if args.len() != 1 {
                    return Err("cos函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function_with_expr("cos", &args[0])
            }
            "tan" => {
                if args.len() != 1 {
                    return Err("tan函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function_with_expr("tan", &args[0])
            }
            
            // 指数和对数函数
            "exp" => {
                if args.len() != 1 {
                    return Err("exp函数需要一个参数".to_string());
                }
                self.evaluate_exponential_function_with_expr(&args[0])
            }
            "ln" | "log" => {
                if args.len() != 1 {
                    return Err("ln函数需要一个参数".to_string());
                }
                let arg_val = args[0].evaluate_exact()?;
                self.evaluate_logarithm_function(&arg_val)
            }
            
            // 其他函数,先求值参数再调用原有方法
            _ => {
                let arg_values: Result<Vec<Number>, String> = args
                    .iter()
                    .map(|arg| arg.evaluate_exact())
                    .collect();
                let arg_values = arg_values?;
                self.evaluate_function(name, &arg_values)
            }
        }
    }
    
    /// 求值三角函数(使用表达式参数)
    fn evaluate_trigonometric_function_with_expr(&self, name: &str, arg: &Expression) -> Result<Number, String> {
        use super::MathConstant;
        
        // 检查特殊的表达式模式
        match arg {
            // 直接的数学常量
            Expression::Constant(MathConstant::Pi) => {
                match name {
                    "sin" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    "cos" => return Ok(Number::Integer(num_bigint::BigInt::from(-1))),
                    "tan" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            
            // π 的分数
            Expression::BinaryOp { op: BinaryOperator::Divide, left, right } => {
                if let (Expression::Constant(MathConstant::Pi), Expression::Number(Number::Integer(n))) = 
                    (left.as_ref(), right.as_ref()) {
                    if n == &num_bigint::BigInt::from(2) {
                        // π/2 的三角函数值
                        match name {
                            "sin" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                            "cos" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                            "tan" => return Ok(Number::Constant(MathConstant::Undefined)),
                            _ => {}
                        }
                    } else if n == &num_bigint::BigInt::from(4) {
                        // π/4 的三角函数值
                        match name {
                            "sin" | "cos" => {
                                // sin(π/4) = cos(π/4) = √2/2
                                return Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                })));
                            }
                            "tan" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                            _ => {}
                        }
                    }
                }
            }
            
            _ => {}
        }
        
        // 对于其他情况,先求值参数再调用原有方法
        let arg_val = arg.evaluate_exact()?;
        self.evaluate_trigonometric_function(name, &arg_val)
    }
    
    /// 求值指数函数(使用表达式参数)
    fn evaluate_exponential_function_with_expr(&self, arg: &Expression) -> Result<Number, String> {
        use super::MathConstant;
        
        // 检查特殊的表达式模式
        match arg {
            // exp(i*π) = -1 (欧拉公式)
            Expression::BinaryOp { op: BinaryOperator::Multiply, left, right } => {
                // 检查是否为 i*π 或 π*i 的形式
                let is_i_pi = match (left.as_ref(), right.as_ref()) {
                    (Expression::Constant(MathConstant::I), Expression::Constant(MathConstant::Pi)) |
                    (Expression::Constant(MathConstant::Pi), Expression::Constant(MathConstant::I)) => true,
                    _ => false,
                };
                
                if is_i_pi {
                    return Ok(Number::Integer(num_bigint::BigInt::from(-1)));
                }
            }
            
            _ => {}
        }
        
        // 对于其他情况,先求值参数再调用原有方法
        let arg_val = arg.evaluate_exact()?;
        self.evaluate_exponential_function(&arg_val)
    }

    /// 求值函数调用
    fn evaluate_function(&self, name: &str, args: &[Number]) -> Result<Number, String> {
        match name {
            // 三角函数
            "sin" => {
                if args.len() != 1 {
                    return Err("sin函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function("sin", &args[0])
            }
            "cos" => {
                if args.len() != 1 {
                    return Err("cos函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function("cos", &args[0])
            }
            "tan" => {
                if args.len() != 1 {
                    return Err("tan函数需要一个参数".to_string());
                }
                self.evaluate_trigonometric_function("tan", &args[0])
            }
            
            // 反三角函数
            "asin" | "arcsin" => {
                if args.len() != 1 {
                    return Err("asin函数需要一个参数".to_string());
                }
                self.evaluate_inverse_trigonometric_function("asin", &args[0])
            }
            "acos" | "arccos" => {
                if args.len() != 1 {
                    return Err("acos函数需要一个参数".to_string());
                }
                self.evaluate_inverse_trigonometric_function("acos", &args[0])
            }
            "atan" | "arctan" => {
                if args.len() != 1 {
                    return Err("atan函数需要一个参数".to_string());
                }
                self.evaluate_inverse_trigonometric_function("atan", &args[0])
            }
            
            // 指数和对数函数
            "exp" => {
                if args.len() != 1 {
                    return Err("exp函数需要一个参数".to_string());
                }
                self.evaluate_exponential_function(&args[0])
            }
            "ln" | "log" => {
                if args.len() != 1 {
                    return Err("ln函数需要一个参数".to_string());
                }
                self.evaluate_logarithm_function(&args[0])
            }
            "log10" => {
                if args.len() != 1 {
                    return Err("log10函数需要一个参数".to_string());
                }
                self.evaluate_log10_function(&args[0])
            }
            "log2" => {
                if args.len() != 1 {
                    return Err("log2函数需要一个参数".to_string());
                }
                self.evaluate_log2_function(&args[0])
            }
            
            // 幂函数和根函数
            "sqrt" => {
                if args.len() != 1 {
                    return Err("sqrt函数需要一个参数".to_string());
                }
                self.evaluate_sqrt_function(&args[0])
            }
            "pow" => {
                if args.len() != 2 {
                    return Err("pow函数需要两个参数".to_string());
                }
                self.evaluate_power_function(&args[0], &args[1])
            }
            
            // 双曲函数
            "sinh" => {
                if args.len() != 1 {
                    return Err("sinh函数需要一个参数".to_string());
                }
                self.evaluate_hyperbolic_function("sinh", &args[0])
            }
            "cosh" => {
                if args.len() != 1 {
                    return Err("cosh函数需要一个参数".to_string());
                }
                self.evaluate_hyperbolic_function("cosh", &args[0])
            }
            "tanh" => {
                if args.len() != 1 {
                    return Err("tanh函数需要一个参数".to_string());
                }
                self.evaluate_hyperbolic_function("tanh", &args[0])
            }
            
            // 统计函数
            "max" => {
                if args.is_empty() {
                    return Err("max函数需要至少一个参数".to_string());
                }
                let mut max_val = &args[0];
                for arg in &args[1..] {
                    if arg.approximate() > max_val.approximate() {
                        max_val = arg;
                    }
                }
                Ok(max_val.clone())
            }
            "min" => {
                if args.is_empty() {
                    return Err("min函数需要至少一个参数".to_string());
                }
                let mut min_val = &args[0];
                for arg in &args[1..] {
                    if arg.approximate() < min_val.approximate() {
                        min_val = arg;
                    }
                }
                Ok(min_val.clone())
            }
            "abs" => {
                if args.len() != 1 {
                    return Err("abs函数需要一个参数".to_string());
                }
                args[0].abs().map_err(|e| format!("{}", e))
            }
            
            // 对于其他函数,返回符号表示
            _ => {
                Ok(Number::Symbolic(Box::new(Expression::Function {
                    name: name.to_string(),
                    args: args.iter().map(|arg| Expression::Number(arg.clone())).collect(),
                })))
            }
        }
    }
    
    /// 尝试将表达式简化为数值(如果可能)
    pub fn try_to_number(&self) -> Option<Number> {
        match self.evaluate_exact() {
            Ok(num) => Some(num),
            Err(_) => None,
        }
    }
    
    /// 求值三角函数
    fn evaluate_trigonometric_function(&self, name: &str, arg: &Number) -> Result<Number, String> {
        use super::MathConstant;
        
        // 检查特殊值
        match arg {
            Number::Integer(n) if n.is_zero() => {
                match name {
                    "sin" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    "cos" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                    "tan" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            _ => {}
        }
        
        // 检查是否为数学常量
        if let Number::Constant(c) = arg {
            match (name, c) {
                ("sin", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                ("cos", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(-1))),
                ("tan", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                _ => {}
            }
        }
        
        // 检查是否为符号表达式中的数学常量
        if let Number::Symbolic(expr) = arg {
            if let Expression::Constant(c) = expr.as_ref() {
                match (name, c) {
                    ("sin", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    ("cos", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(-1))),
                    ("tan", MathConstant::Pi) => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            
            // 检查是否为 π 的分数
            if let Expression::BinaryOp { op: BinaryOperator::Divide, left, right } = expr.as_ref() {
                if let (Expression::Constant(MathConstant::Pi), Expression::Number(Number::Integer(n))) = 
                    (left.as_ref(), right.as_ref()) {
                    if n == &num_bigint::BigInt::from(2) {
                        // π/2 的三角函数值
                        match name {
                            "sin" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                            "cos" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                            "tan" => return Ok(Number::Constant(MathConstant::Undefined)),
                            _ => {}
                        }
                    } else if n == &num_bigint::BigInt::from(4) {
                        // π/4 的三角函数值
                        match name {
                            "sin" | "cos" => {
                                // sin(π/4) = cos(π/4) = √2/2
                                return Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                })));
                            }
                            "tan" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                            _ => {}
                        }
                    }
                }
            }
        }
        
        // 检查是否为数学常量的倍数
        if let Some(result) = self.evaluate_trig_constant_multiple(name, arg) {
            return Ok(result);
        }
        
        // 对于一般情况,返回符号表示
        let unary_op = match name {
            "sin" => UnaryOperator::Sin,
            "cos" => UnaryOperator::Cos,
            "tan" => UnaryOperator::Tan,
            _ => return Err(format!("未知的三角函数: {}", name)),
        };
        
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: unary_op,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值反三角函数
    fn evaluate_inverse_trigonometric_function(&self, name: &str, arg: &Number) -> Result<Number, String> {
        // 检查特殊值
        match arg {
            Number::Integer(n) if n.is_zero() => {
                match name {
                    "asin" | "atan" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                match name {
                    "acos" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    "asin" => return Ok(Number::Constant(MathConstant::Pi)),
                    _ => {}
                }
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(-1) => {
                match name {
                    "acos" => return Ok(Number::Constant(MathConstant::Pi)),
                    "asin" => return Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                        op: UnaryOperator::Negate,
                        operand: Box::new(Expression::BinaryOp {
                            op: BinaryOperator::Divide,
                            left: Box::new(Expression::Constant(MathConstant::Pi)),
                            right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                        }),
                    }))),
                    _ => {}
                }
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        let unary_op = match name {
            "asin" => UnaryOperator::Asin,
            "acos" => UnaryOperator::Acos,
            "atan" => UnaryOperator::Atan,
            _ => return Err(format!("未知的反三角函数: {}", name)),
        };
        
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: unary_op,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值指数函数
    fn evaluate_exponential_function(&self, arg: &Number) -> Result<Number, String> {
        use super::MathConstant;
        
        // 检查特殊值
        match arg {
            Number::Integer(n) if n.is_zero() => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Constant(MathConstant::E));
            }
            // 检查符号表达式
            Number::Symbolic(expr) => {
                if let Expression::BinaryOp { op: BinaryOperator::Multiply, left, right } = expr.as_ref() {
                    // 检查是否为 i*π 或 π*i 的形式
                    let is_i_pi = match (left.as_ref(), right.as_ref()) {
                        (Expression::Constant(MathConstant::I), Expression::Constant(MathConstant::Pi)) |
                        (Expression::Constant(MathConstant::Pi), Expression::Constant(MathConstant::I)) => true,
                        // 检查复数形式的虚数单位
                        (Expression::Number(Number::Complex { real, imaginary }), Expression::Constant(MathConstant::Pi)) 
                            if real.is_zero() && imaginary.is_one() => true,
                        (Expression::Constant(MathConstant::Pi), Expression::Number(Number::Complex { real, imaginary })) 
                            if real.is_zero() && imaginary.is_one() => true,
                        // 检查符号表示的常量
                        (Expression::Number(Number::Symbolic(inner_expr)), Expression::Constant(MathConstant::Pi)) => {
                            matches!(inner_expr.as_ref(), Expression::Constant(MathConstant::I))
                        }
                        (Expression::Constant(MathConstant::Pi), Expression::Number(Number::Symbolic(inner_expr))) => {
                            matches!(inner_expr.as_ref(), Expression::Constant(MathConstant::I))
                        }
                        // 检查符号表示的 π
                        (Expression::Constant(MathConstant::I), Expression::Number(Number::Symbolic(inner_expr))) => {
                            matches!(inner_expr.as_ref(), Expression::Constant(MathConstant::Pi))
                        }
                        (Expression::Number(Number::Symbolic(inner_expr)), Expression::Constant(MathConstant::I)) => {
                            matches!(inner_expr.as_ref(), Expression::Constant(MathConstant::Pi))
                        }
                        _ => false,
                    };
                    
                    if is_i_pi {
                        return Ok(Number::Integer(num_bigint::BigInt::from(-1)));
                    }
                }
            }
            // 检查复数形式的 i*π
            Number::Complex { real, imaginary } => {
                if real.is_zero() && imaginary.is_one() {
                    // 这是虚数单位 i,但我们需要检查是否乘以了 π
                    // 这种情况在函数调用时处理
                }
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: UnaryOperator::Exp,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值对数函数
    fn evaluate_logarithm_function(&self, arg: &Number) -> Result<Number, String> {
        use super::MathConstant;
        
        // 检查特殊值
        match arg {
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(0)));
            }
            Number::Constant(MathConstant::E) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            Number::Integer(n) if n <= &num_bigint::BigInt::from(0) => {
                return Err("对数函数的参数必须为正数".to_string());
            }
            // 检查符号表达式中的常量
            Number::Symbolic(expr) => {
                if let Expression::Constant(MathConstant::E) = expr.as_ref() {
                    return Ok(Number::Integer(num_bigint::BigInt::from(1)));
                }
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: UnaryOperator::Ln,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值常用对数函数
    fn evaluate_log10_function(&self, arg: &Number) -> Result<Number, String> {
        // 检查特殊值
        match arg {
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(0)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(10) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            Number::Integer(n) if n <= &num_bigint::BigInt::from(0) => {
                return Err("对数函数的参数必须为正数".to_string());
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: UnaryOperator::Log10,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值二进制对数函数
    fn evaluate_log2_function(&self, arg: &Number) -> Result<Number, String> {
        // 检查特殊值
        match arg {
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(0)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(2) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            Number::Integer(n) if n <= &num_bigint::BigInt::from(0) => {
                return Err("对数函数的参数必须为正数".to_string());
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: UnaryOperator::Log2,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值平方根函数
    fn evaluate_sqrt_function(&self, arg: &Number) -> Result<Number, String> {
        // 检查特殊值
        match arg {
            Number::Integer(n) if n.is_zero() => {
                return Ok(Number::Integer(num_bigint::BigInt::from(0)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(4) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(2)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(9) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(3)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(16) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(4)));
            }
            Number::Integer(n) if n == &num_bigint::BigInt::from(25) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(5)));
            }
            Number::Integer(n) if n < &num_bigint::BigInt::from(0) => {
                // 负数的平方根是复数
                let abs_n = n.abs();
                return Ok(Number::Complex {
                    real: Box::new(Number::Integer(num_bigint::BigInt::from(0))),
                    imaginary: Box::new(Number::Symbolic(Box::new(Expression::UnaryOp {
                        op: UnaryOperator::Sqrt,
                        operand: Box::new(Expression::Number(Number::Integer(abs_n))),
                    }))),
                });
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: UnaryOperator::Sqrt,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值幂函数
    fn evaluate_power_function(&self, base: &Number, exponent: &Number) -> Result<Number, String> {
        // 检查特殊情况
        match (base, exponent) {
            // 任何数的0次幂都是1
            (_, Number::Integer(n)) if n.is_zero() => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            // 任何数的1次幂都是它本身
            (_, Number::Integer(n)) if n == &num_bigint::BigInt::from(1) => {
                return Ok(base.clone());
            }
            // 0的正数次幂是0
            (Number::Integer(n), _) if n.is_zero() && exponent.is_positive() => {
                return Ok(Number::Integer(num_bigint::BigInt::from(0)));
            }
            // 1的任何次幂都是1
            (Number::Integer(n), _) if n == &num_bigint::BigInt::from(1) => {
                return Ok(Number::Integer(num_bigint::BigInt::from(1)));
            }
            // 整数的整数次幂
            (Number::Integer(base_int), Number::Integer(exp_int)) => {
                if exp_int >= &num_bigint::BigInt::from(0) {
                    if let Some(exp_u32) = exp_int.to_u32() {
                        return Ok(Number::Integer(base_int.pow(exp_u32)));
                    }
                }
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        Ok(Number::Symbolic(Box::new(Expression::BinaryOp {
            op: BinaryOperator::Power,
            left: Box::new(Expression::Number(base.clone())),
            right: Box::new(Expression::Number(exponent.clone())),
        })))
    }
    
    /// 求值双曲函数
    fn evaluate_hyperbolic_function(&self, name: &str, arg: &Number) -> Result<Number, String> {
        // 检查特殊值
        match arg {
            Number::Integer(n) if n.is_zero() => {
                match name {
                    "sinh" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    "cosh" => return Ok(Number::Integer(num_bigint::BigInt::from(1))),
                    "tanh" => return Ok(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            _ => {}
        }
        
        // 对于一般情况,返回符号表示
        let unary_op = match name {
            "sinh" => UnaryOperator::Sinh,
            "cosh" => UnaryOperator::Cosh,
            "tanh" => UnaryOperator::Tanh,
            _ => return Err(format!("未知的双曲函数: {}", name)),
        };
        
        Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
            op: unary_op,
            operand: Box::new(Expression::Number(arg.clone())),
        })))
    }
    
    /// 求值三角函数的常量倍数
    fn evaluate_trig_constant_multiple(&self, name: &str, arg: &Number) -> Option<Number> {
        use super::MathConstant;
        
        // 检查是否为 π 的倍数
        if let Number::Constant(MathConstant::Pi) = arg {
            match name {
                "sin" => return Some(Number::Integer(num_bigint::BigInt::from(0))),
                "cos" => return Some(Number::Integer(num_bigint::BigInt::from(-1))),
                "tan" => return Some(Number::Integer(num_bigint::BigInt::from(0))),
                _ => {}
            }
        }
        
        // 检查符号表达式中的常量
        if let Number::Symbolic(expr) = arg {
            // 检查是否为常量
            if let Expression::Constant(MathConstant::Pi) = expr.as_ref() {
                match name {
                    "sin" => return Some(Number::Integer(num_bigint::BigInt::from(0))),
                    "cos" => return Some(Number::Integer(num_bigint::BigInt::from(-1))),
                    "tan" => return Some(Number::Integer(num_bigint::BigInt::from(0))),
                    _ => {}
                }
            }
            
            // 检查是否为 π/2 的倍数
            if let Expression::BinaryOp { op: BinaryOperator::Divide, left, right } = expr.as_ref() {
                if let (Expression::Constant(MathConstant::Pi), Expression::Number(Number::Integer(n))) = 
                    (left.as_ref(), right.as_ref()) {
                    if n == &num_bigint::BigInt::from(2) {
                        match name {
                            "sin" => return Some(Number::Integer(num_bigint::BigInt::from(1))),
                            "cos" => return Some(Number::Integer(num_bigint::BigInt::from(0))),
                            // tan(π/2) 是未定义的
                            "tan" => return Some(Number::Constant(MathConstant::Undefined)),
                            _ => {}
                        }
                    } else if n == &num_bigint::BigInt::from(4) {
                        // π/4 的三角函数值
                        match name {
                            "sin" | "cos" => {
                                // sin(π/4) = cos(π/4) = √2/2
                                return Some(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                })));
                            }
                            "tan" => return Some(Number::Integer(num_bigint::BigInt::from(1))),
                            _ => {}
                        }
                    } else if n == &num_bigint::BigInt::from(6) {
                        // π/6 的三角函数值
                        match name {
                            "sin" => return Some(Number::Rational(num_rational::BigRational::new(
                                num_bigint::BigInt::from(1), 
                                num_bigint::BigInt::from(2)
                            ))),
                            "cos" => {
                                // cos(π/6) = √3/2
                                return Some(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(3)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                })));
                            }
                            "tan" => {
                                // tan(π/6) = √3/3
                                return Some(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(3)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(3)))),
                                })));
                            }
                            _ => {}
                        }
                    } else if n == &num_bigint::BigInt::from(3) {
                        // π/3 的三角函数值
                        match name {
                            "sin" => {
                                // sin(π/3) = √3/2
                                return Some(Number::Symbolic(Box::new(Expression::BinaryOp {
                                    op: BinaryOperator::Divide,
                                    left: Box::new(Expression::UnaryOp {
                                        op: UnaryOperator::Sqrt,
                                        operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(3)))),
                                    }),
                                    right: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(2)))),
                                })));
                            }
                            "cos" => return Some(Number::Rational(num_rational::BigRational::new(
                                num_bigint::BigInt::from(1), 
                                num_bigint::BigInt::from(2)
                            ))),
                            "tan" => {
                                // tan(π/3) = √3
                                return Some(Number::Symbolic(Box::new(Expression::UnaryOp {
                                    op: UnaryOperator::Sqrt,
                                    operand: Box::new(Expression::Number(Number::Integer(num_bigint::BigInt::from(3)))),
                                })));
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        
        None
    }
    
    /// 检查表达式是否可以求值为数值
    pub fn is_evaluable(&self) -> bool {
        self.evaluate_exact().is_ok()
    }
}

impl Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expression::Number(n) => write!(f, "{}", n),
            Expression::Variable(name) => write!(f, "{}", name),
            Expression::Constant(c) => write!(f, "{}", c.symbol()),
            Expression::BinaryOp { op, left, right } => {
                // 根据运算符优先级决定是否需要括号
                let needs_left_parens = match left.as_ref() {
                    Expression::BinaryOp { op: left_op, .. } => {
                        left_op.precedence() < op.precedence() ||
                        (left_op.precedence() == op.precedence() && op.is_right_associative())
                    }
                    _ => false,
                };
                
                let needs_right_parens = match right.as_ref() {
                    Expression::BinaryOp { op: right_op, .. } => {
                        right_op.precedence() < op.precedence() ||
                        (right_op.precedence() == op.precedence() && !op.is_right_associative())
                    }
                    _ => false,
                };
                
                if needs_left_parens {
                    write!(f, "({})", left)?;
                } else {
                    write!(f, "{}", left)?;
                }
                
                write!(f, " {} ", op.symbol())?;
                
                if needs_right_parens {
                    write!(f, "({})", right)?;
                } else {
                    write!(f, "{}", right)?;
                }
                
                Ok(())
            }
            Expression::UnaryOp { op, operand } => {
                match op {
                    UnaryOperator::Negate => write!(f, "-{}", operand),
                    UnaryOperator::Plus => write!(f, "+{}", operand),
                    UnaryOperator::Factorial => write!(f, "{}!", operand),
                    UnaryOperator::Transpose => write!(f, "{}^T", operand),
                    UnaryOperator::Conjugate => write!(f, "{}*", operand),
                    _ => {
                        // 函数形式的一元运算符
                        write!(f, "{}({})", op.symbol(), operand)
                    }
                }
            }
            Expression::Function { name, args } => {
                write!(f, "{}(", name)?;
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", arg)?;
                }
                write!(f, ")")
            }
            Expression::Matrix(rows) => {
                write!(f, "[")?;
                for (i, row) in rows.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "[")?;
                    for (j, elem) in row.iter().enumerate() {
                        if j > 0 {
                            write!(f, ", ")?;
                        }
                        write!(f, "{}", elem)?;
                    }
                    write!(f, "]")?;
                }
                write!(f, "]")
            }
            Expression::Vector(elements) => {
                write!(f, "[")?;
                for (i, elem) in elements.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", elem)?;
                }
                write!(f, "]")
            }
            Expression::Set(elements) => {
                write!(f, "{{")?;
                for (i, elem) in elements.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", elem)?;
                }
                write!(f, "}}")
            }
            Expression::Interval { start, end, start_inclusive, end_inclusive } => {
                let left_bracket = if *start_inclusive { "[" } else { "(" };
                let right_bracket = if *end_inclusive { "]" } else { ")" };
                write!(f, "{}{}, {}{}", left_bracket, start, end, right_bracket)
            }
        }
    }
}

// 手动实现 Eq trait
impl Eq for Expression {}

// 手动实现 Hash trait
impl std::hash::Hash for Expression {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Expression::Number(n) => {
                0u8.hash(state);
                n.hash(state);
            }
            Expression::Variable(name) => {
                1u8.hash(state);
                name.hash(state);
            }
            Expression::Constant(c) => {
                2u8.hash(state);
                c.hash(state);
            }
            Expression::BinaryOp { op, left, right } => {
                3u8.hash(state);
                op.hash(state);
                left.hash(state);
                right.hash(state);
            }
            Expression::UnaryOp { op, operand } => {
                4u8.hash(state);
                op.hash(state);
                operand.hash(state);
            }
            Expression::Function { name, args } => {
                5u8.hash(state);
                name.hash(state);
                args.hash(state);
            }
            Expression::Matrix(rows) => {
                6u8.hash(state);
                rows.hash(state);
            }
            Expression::Vector(elements) => {
                7u8.hash(state);
                elements.hash(state);
            }
            Expression::Set(elements) => {
                8u8.hash(state);
                elements.hash(state);
            }
            Expression::Interval { start, end, start_inclusive, end_inclusive } => {
                9u8.hash(state);
                start.hash(state);
                end.hash(state);
                start_inclusive.hash(state);
                end_inclusive.hash(state);
            }
        }
    }
}

// 包含测试模块
#[cfg(test)]
#[path = "expression_tests.rs"]
mod expression_tests;

#[cfg(test)]
#[path = "function_tests.rs"]
mod function_tests;