thales 0.4.2

A comprehensive Computer Algebra System (CAS) library for symbolic mathematics, equation solving, calculus, and linear algebra
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
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
//! Symbolic integration module for computing indefinite integrals.
//!
//! This module provides functionality for computing antiderivatives of mathematical
//! expressions using standard integration techniques including:
//!
//! - Power rule
//! - Sum and difference rules
//! - Constant multiple rule
//! - Standard integrals table (trigonometric, exponential, logarithmic)
//!
//! # Example
//!
//! ```
//! use thales::integration::{integrate, IntegrationError};
//! use thales::ast::{Expression, Variable};
//!
//! // Integrate x^2 with respect to x
//! let x = Expression::Variable(Variable::new("x"));
//! let x_squared = Expression::Power(Box::new(x.clone()), Box::new(Expression::Integer(2)));
//!
//! let result = integrate(&x_squared, "x").unwrap();
//! // Result: x^3/3 + C (constant of integration handled separately)
//! ```

use crate::ast::{BinaryOp, Expression, Function, UnaryOp, Variable};
use std::fmt;

/// Error types that can occur during integration.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum IntegrationError {
    /// Integration cannot be performed symbolically.
    CannotIntegrate(String),
    /// The integrand contains unsupported constructs.
    UnsupportedExpression(String),
    /// Division by zero would occur.
    DivisionByZero,
}

impl fmt::Display for IntegrationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IntegrationError::CannotIntegrate(msg) => {
                write!(f, "Cannot integrate: {}", msg)
            }
            IntegrationError::UnsupportedExpression(msg) => {
                write!(f, "Unsupported expression: {}", msg)
            }
            IntegrationError::DivisionByZero => {
                write!(f, "Division by zero in integration")
            }
        }
    }
}

impl std::error::Error for IntegrationError {}

/// Result type for integration operations.
pub type IntegrationResult = Result<Expression, IntegrationError>;

/// Compute the indefinite integral of an expression with respect to a variable.
///
/// This function returns the antiderivative without the constant of integration.
/// The caller should add `+ C` if displaying the full indefinite integral.
///
/// # Arguments
///
/// * `expr` - The expression to integrate
/// * `var` - The variable of integration (e.g., "x")
///
/// # Returns
///
/// The antiderivative expression, or an error if integration fails.
///
/// # Supported Forms
///
/// - Constants: ∫c dx = cx
/// - Power rule: ∫x^n dx = x^(n+1)/(n+1) for n ≠ -1
/// - Reciprocal: ∫1/x dx = ln|x|
/// - Sum/difference: ∫(f ± g) dx = ∫f dx ± ∫g dx
/// - Constant multiple: ∫cf dx = c∫f dx
/// - Trigonometric: sin, cos, tan, sec^2
/// - Exponential: e^x
/// - Logarithmic via inverse
///
/// # Example
///
/// ```
/// use thales::integration::integrate;
/// use thales::ast::{Expression, Variable};
///
/// let x = Expression::Variable(Variable::new("x"));
/// let expr = Expression::Power(Box::new(x.clone()), Box::new(Expression::Integer(3)));
///
/// let result = integrate(&expr, "x").unwrap();
/// // x^3 integrates to x^4/4
/// ```
pub fn integrate(expr: &Expression, var: &str) -> IntegrationResult {
    integrate_impl(expr, var)
}

/// Internal implementation of integration.
fn integrate_impl(expr: &Expression, var: &str) -> IntegrationResult {
    match expr {
        // Constants: ∫c dx = c*x
        Expression::Integer(_)
        | Expression::Float(_)
        | Expression::Rational(_)
        | Expression::Complex(_) => {
            let x = Expression::Variable(Variable::new(var));
            Ok(Expression::Binary(
                BinaryOp::Mul,
                Box::new(expr.clone()),
                Box::new(x),
            ))
        }

        // Symbolic constants: ∫pi dx = pi*x, ∫e dx = e*x
        Expression::Constant(_) => {
            let x = Expression::Variable(Variable::new(var));
            Ok(Expression::Binary(
                BinaryOp::Mul,
                Box::new(expr.clone()),
                Box::new(x),
            ))
        }

        // Variable: ∫x dx = x^2/2, ∫y dx = y*x (y treated as constant)
        Expression::Variable(v) => {
            if v.name == var {
                // ∫x dx = x^2/2
                let x = Expression::Variable(Variable::new(var));
                let x_squared = Expression::Power(Box::new(x), Box::new(Expression::Integer(2)));
                Ok(Expression::Binary(
                    BinaryOp::Div,
                    Box::new(x_squared),
                    Box::new(Expression::Integer(2)),
                ))
            } else {
                // Treat as constant: ∫y dx = y*x
                let x = Expression::Variable(Variable::new(var));
                Ok(Expression::Binary(
                    BinaryOp::Mul,
                    Box::new(expr.clone()),
                    Box::new(x),
                ))
            }
        }

        // Unary operations
        Expression::Unary(op, inner) => match op {
            UnaryOp::Neg => {
                // ∫-f dx = -∫f dx
                let inner_integral = integrate_impl(inner, var)?;
                Ok(Expression::Unary(UnaryOp::Neg, Box::new(inner_integral)))
            }
            UnaryOp::Abs => Err(IntegrationError::CannotIntegrate(
                "Cannot integrate |f(x)| symbolically".to_string(),
            )),
            UnaryOp::Not => Err(IntegrationError::UnsupportedExpression(
                "Logical NOT cannot be integrated".to_string(),
            )),
        },

        // Binary operations
        Expression::Binary(op, left, right) => match op {
            // Sum rule: ∫(f + g) dx = ∫f dx + ∫g dx
            BinaryOp::Add => {
                let left_integral = integrate_impl(left, var)?;
                let right_integral = integrate_impl(right, var)?;
                Ok(Expression::Binary(
                    BinaryOp::Add,
                    Box::new(left_integral),
                    Box::new(right_integral),
                ))
            }

            // Difference rule: ∫(f - g) dx = ∫f dx - ∫g dx
            BinaryOp::Sub => {
                let left_integral = integrate_impl(left, var)?;
                let right_integral = integrate_impl(right, var)?;
                Ok(Expression::Binary(
                    BinaryOp::Sub,
                    Box::new(left_integral),
                    Box::new(right_integral),
                ))
            }

            // Multiplication: check for constant multiple
            BinaryOp::Mul => integrate_product(left, right, var),

            // Division: check for power rule with negative exponent or constant divisor
            BinaryOp::Div => integrate_quotient(left, right, var),

            BinaryOp::Mod => Err(IntegrationError::CannotIntegrate(
                "Modulo cannot be integrated".to_string(),
            )),
        },

        // Power expressions
        Expression::Power(base, exponent) => integrate_power(base, exponent, var),

        // Function calls
        Expression::Function(func, args) => integrate_function(func, args, var),
    }
}

/// Integrate a product expression.
fn integrate_product(left: &Expression, right: &Expression, var: &str) -> IntegrationResult {
    let left_has_var = left.contains_variable(var);
    let right_has_var = right.contains_variable(var);

    if !left_has_var && !right_has_var {
        // Both constant: ∫c1*c2 dx = c1*c2*x
        let x = Expression::Variable(Variable::new(var));
        let product = Expression::Binary(
            BinaryOp::Mul,
            Box::new(left.clone()),
            Box::new(right.clone()),
        );
        Ok(Expression::Binary(
            BinaryOp::Mul,
            Box::new(product),
            Box::new(x),
        ))
    } else if !left_has_var {
        // Constant multiple rule: ∫c*f dx = c*∫f dx
        let right_integral = integrate_impl(right, var)?;
        Ok(Expression::Binary(
            BinaryOp::Mul,
            Box::new(left.clone()),
            Box::new(right_integral),
        ))
    } else if !right_has_var {
        // Constant multiple rule: ∫f*c dx = c*∫f dx
        let left_integral = integrate_impl(left, var)?;
        Ok(Expression::Binary(
            BinaryOp::Mul,
            Box::new(right.clone()),
            Box::new(left_integral),
        ))
    } else {
        // Both factors contain the variable - check for special patterns
        // Try to recognize x^n * x^m = x^(n+m)
        if let Some(result) = try_combine_powers(left, right, var) {
            return integrate_power_expr(&result, var);
        }

        // Check for derivative patterns (u-substitution candidates)
        // This is a simple heuristic - full u-substitution is Task 15
        Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate product {} * {} - try u-substitution",
            left, right
        )))
    }
}

/// Try to combine two power expressions: x^a * x^b = x^(a+b)
fn try_combine_powers(left: &Expression, right: &Expression, var: &str) -> Option<Expression> {
    let left_power = extract_power(left, var)?;
    let right_power = extract_power(right, var)?;

    // Combine exponents
    let sum = Expression::Binary(BinaryOp::Add, Box::new(left_power), Box::new(right_power));

    Some(Expression::Power(
        Box::new(Expression::Variable(Variable::new(var))),
        Box::new(sum),
    ))
}

/// Extract the power of a variable expression.
/// x -> 1, x^n -> n, constant -> None
fn extract_power(expr: &Expression, var: &str) -> Option<Expression> {
    match expr {
        Expression::Variable(v) if v.name == var => Some(Expression::Integer(1)),
        Expression::Power(base, exp) => {
            if let Expression::Variable(v) = base.as_ref() {
                if v.name == var {
                    return Some(exp.as_ref().clone());
                }
            }
            None
        }
        _ => None,
    }
}

/// Integrate a power expression that was combined.
fn integrate_power_expr(expr: &Expression, var: &str) -> IntegrationResult {
    if let Expression::Power(base, exp) = expr {
        integrate_power(base, exp, var)
    } else {
        integrate_impl(expr, var)
    }
}

/// Integrate a quotient expression.
fn integrate_quotient(num: &Expression, denom: &Expression, var: &str) -> IntegrationResult {
    let num_has_var = num.contains_variable(var);
    let denom_has_var = denom.contains_variable(var);

    if !denom_has_var {
        // ∫f(x)/c dx = (1/c)∫f(x) dx
        let num_integral = integrate_impl(num, var)?;
        Ok(Expression::Binary(
            BinaryOp::Div,
            Box::new(num_integral),
            Box::new(denom.clone()),
        ))
    } else if !num_has_var {
        // ∫c/f(x) dx - check for special cases
        // ∫c/x dx = c*ln|x|
        if let Expression::Variable(v) = denom {
            if v.name == var {
                let ln_x = Expression::Function(
                    Function::Ln,
                    vec![Expression::Function(
                        Function::Abs,
                        vec![Expression::Variable(Variable::new(var))],
                    )],
                );
                return Ok(Expression::Binary(
                    BinaryOp::Mul,
                    Box::new(num.clone()),
                    Box::new(ln_x),
                ));
            }
        }

        // ∫c/(1+x^2) dx = c*arctan(x) - check pattern
        if let Some(result) = try_arctan_pattern(num, denom, var) {
            return Ok(result);
        }

        // ∫c/sqrt(1-x^2) dx = c*arcsin(x) - check pattern
        if let Some(result) = try_arcsin_pattern(num, denom, var) {
            return Ok(result);
        }

        Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate {}/{}",
            num, denom
        )))
    } else {
        // Check for f(x)/x which might be ln pattern
        // Or convert to negative power and try
        if let Expression::Variable(v) = denom {
            if v.name == var {
                // ∫f(x)/x dx - complex, needs special handling
                // Simple case: ∫x^n/x dx = ∫x^(n-1) dx
                if let Some(power) = extract_power(num, var) {
                    let new_exp = Expression::Binary(
                        BinaryOp::Sub,
                        Box::new(power),
                        Box::new(Expression::Integer(1)),
                    );
                    return integrate_power(
                        &Expression::Variable(Variable::new(var)),
                        &new_exp,
                        var,
                    );
                }
            }
        }

        // Convert to negative power: f/g = f * g^(-1)
        // Only works for simple cases
        Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate quotient {}/{} - try partial fractions",
            num, denom
        )))
    }
}

/// Check for ∫c/(1+x^2) dx = c*arctan(x) pattern
fn try_arctan_pattern(num: &Expression, denom: &Expression, var: &str) -> Option<Expression> {
    // Check if denom is 1 + x^2
    if let Expression::Binary(BinaryOp::Add, left, right) = denom {
        let is_one = matches!(left.as_ref(), Expression::Integer(1));
        let is_x_squared = matches!(
            right.as_ref(),
            Expression::Power(base, exp)
            if matches!(base.as_ref(), Expression::Variable(v) if v.name == var)
            && matches!(exp.as_ref(), Expression::Integer(2))
        );

        if is_one && is_x_squared {
            let arctan_x = Expression::Function(
                Function::Atan,
                vec![Expression::Variable(Variable::new(var))],
            );
            return Some(Expression::Binary(
                BinaryOp::Mul,
                Box::new(num.clone()),
                Box::new(arctan_x),
            ));
        }
    }
    None
}

/// Check for ∫c/sqrt(1-x^2) dx = c*arcsin(x) pattern
fn try_arcsin_pattern(num: &Expression, denom: &Expression, var: &str) -> Option<Expression> {
    // Check if denom is sqrt(1 - x^2)
    if let Expression::Function(Function::Sqrt, args) = denom {
        if let Some(inner) = args.first() {
            if let Expression::Binary(BinaryOp::Sub, left, right) = inner {
                let is_one = matches!(left.as_ref(), Expression::Integer(1));
                let is_x_squared = matches!(
                    right.as_ref(),
                    Expression::Power(base, exp)
                    if matches!(base.as_ref(), Expression::Variable(v) if v.name == var)
                    && matches!(exp.as_ref(), Expression::Integer(2))
                );

                if is_one && is_x_squared {
                    let arcsin_x = Expression::Function(
                        Function::Asin,
                        vec![Expression::Variable(Variable::new(var))],
                    );
                    return Some(Expression::Binary(
                        BinaryOp::Mul,
                        Box::new(num.clone()),
                        Box::new(arcsin_x),
                    ));
                }
            }
        }
    }
    None
}

/// Integrate a power expression.
fn integrate_power(base: &Expression, exponent: &Expression, var: &str) -> IntegrationResult {
    let base_has_var = base.contains_variable(var);
    let exp_has_var = exponent.contains_variable(var);

    if !base_has_var && !exp_has_var {
        // Constant: ∫c^d dx = c^d * x
        let x = Expression::Variable(Variable::new(var));
        let power = Expression::Power(Box::new(base.clone()), Box::new(exponent.clone()));
        Ok(Expression::Binary(
            BinaryOp::Mul,
            Box::new(power),
            Box::new(x),
        ))
    } else if base_has_var && !exp_has_var {
        // Power rule: ∫x^n dx
        if let Expression::Variable(v) = base {
            if v.name == var {
                return integrate_power_of_var(exponent, var);
            }
        }

        // More complex base with constant exponent
        // Needs u-substitution or chain rule recognition
        Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate ({})^{} - complex base",
            base, exponent
        )))
    } else if !base_has_var && exp_has_var {
        // Exponential: ∫a^x dx = a^x / ln(a) when a is constant
        if let Expression::Variable(v) = exponent {
            if v.name == var {
                // ∫a^x dx = a^x / ln(a)
                let ln_base = Expression::Function(Function::Ln, vec![base.clone()]);
                let a_to_x = Expression::Power(Box::new(base.clone()), Box::new(exponent.clone()));
                return Ok(Expression::Binary(
                    BinaryOp::Div,
                    Box::new(a_to_x),
                    Box::new(ln_base),
                ));
            }
        }

        // Special case: ∫e^x dx = e^x
        if let Expression::Constant(crate::ast::SymbolicConstant::E) = base {
            if let Expression::Variable(v) = exponent {
                if v.name == var {
                    return Ok(Expression::Power(
                        Box::new(base.clone()),
                        Box::new(exponent.clone()),
                    ));
                }
            }
        }

        Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate {}^({}) - exponential with complex exponent",
            base, exponent
        )))
    } else {
        // Both contain variable
        Err(IntegrationError::CannotIntegrate(
            "Cannot integrate f(x)^g(x) - requires special techniques".to_string(),
        ))
    }
}

/// Integrate x^n where n is a constant.
fn integrate_power_of_var(exponent: &Expression, var: &str) -> IntegrationResult {
    // Check for x^(-1) = 1/x -> ln|x|
    if let Expression::Integer(-1) = exponent {
        return Ok(Expression::Function(
            Function::Ln,
            vec![Expression::Function(
                Function::Abs,
                vec![Expression::Variable(Variable::new(var))],
            )],
        ));
    }

    if let Expression::Unary(UnaryOp::Neg, inner) = exponent {
        if let Expression::Integer(1) = inner.as_ref() {
            return Ok(Expression::Function(
                Function::Ln,
                vec![Expression::Function(
                    Function::Abs,
                    vec![Expression::Variable(Variable::new(var))],
                )],
            ));
        }
    }

    // Check for rational -1
    if let Expression::Rational(r) = exponent {
        if *r.numer() == -1 && *r.denom() == 1 {
            return Ok(Expression::Function(
                Function::Ln,
                vec![Expression::Function(
                    Function::Abs,
                    vec![Expression::Variable(Variable::new(var))],
                )],
            ));
        }
    }

    // General case: ∫x^n dx = x^(n+1)/(n+1)
    let x = Expression::Variable(Variable::new(var));
    let n_plus_1 = Expression::Binary(
        BinaryOp::Add,
        Box::new(exponent.clone()),
        Box::new(Expression::Integer(1)),
    );

    let x_to_n_plus_1 = Expression::Power(Box::new(x), Box::new(n_plus_1.clone()));

    Ok(Expression::Binary(
        BinaryOp::Div,
        Box::new(x_to_n_plus_1),
        Box::new(n_plus_1),
    ))
}

/// Integrate a function call.
fn integrate_function(func: &Function, args: &[Expression], var: &str) -> IntegrationResult {
    if args.is_empty() {
        return Err(IntegrationError::CannotIntegrate(
            "Function with no arguments".to_string(),
        ));
    }

    let arg = &args[0];

    // Check if argument is simply the variable
    let is_simple_var = matches!(arg, Expression::Variable(v) if v.name == var);

    if !is_simple_var {
        // For compound arguments, we'd need u-substitution (Task 15)
        // Try linear substitution: f(ax + b) -> (1/a) * F(ax + b)
        if let Some(result) = try_linear_substitution(func, arg, var) {
            return Ok(result);
        }

        return Err(IntegrationError::CannotIntegrate(format!(
            "Cannot integrate {}({}) - try u-substitution",
            func_name(func),
            arg
        )));
    }

    // Standard integrals table
    match func {
        // ∫sin(x) dx = -cos(x)
        Function::Sin => Ok(Expression::Unary(
            UnaryOp::Neg,
            Box::new(Expression::Function(
                Function::Cos,
                vec![Expression::Variable(Variable::new(var))],
            )),
        )),

        // ∫cos(x) dx = sin(x)
        Function::Cos => Ok(Expression::Function(
            Function::Sin,
            vec![Expression::Variable(Variable::new(var))],
        )),

        // ∫tan(x) dx = -ln|cos(x)| = ln|sec(x)|
        Function::Tan => {
            let cos_x = Expression::Function(
                Function::Cos,
                vec![Expression::Variable(Variable::new(var))],
            );
            let abs_cos = Expression::Function(Function::Abs, vec![cos_x]);
            let ln_abs_cos = Expression::Function(Function::Ln, vec![abs_cos]);
            Ok(Expression::Unary(UnaryOp::Neg, Box::new(ln_abs_cos)))
        }

        // ∫e^x dx = e^x (when e is the base)
        Function::Exp => Ok(Expression::Function(
            Function::Exp,
            vec![Expression::Variable(Variable::new(var))],
        )),

        // ∫ln(x) dx = x*ln(x) - x  (integration by parts result)
        Function::Ln => {
            let x = Expression::Variable(Variable::new(var));
            let ln_x = Expression::Function(Function::Ln, vec![x.clone()]);
            let x_ln_x = Expression::Binary(BinaryOp::Mul, Box::new(x.clone()), Box::new(ln_x));
            Ok(Expression::Binary(
                BinaryOp::Sub,
                Box::new(x_ln_x),
                Box::new(x),
            ))
        }

        // ∫sinh(x) dx = cosh(x)
        Function::Sinh => Ok(Expression::Function(
            Function::Cosh,
            vec![Expression::Variable(Variable::new(var))],
        )),

        // ∫cosh(x) dx = sinh(x)
        Function::Cosh => Ok(Expression::Function(
            Function::Sinh,
            vec![Expression::Variable(Variable::new(var))],
        )),

        // ∫tanh(x) dx = ln(cosh(x))
        Function::Tanh => {
            let cosh_x = Expression::Function(
                Function::Cosh,
                vec![Expression::Variable(Variable::new(var))],
            );
            Ok(Expression::Function(Function::Ln, vec![cosh_x]))
        }

        // ∫1/sqrt(x) = ∫x^(-1/2) dx = 2*sqrt(x)
        Function::Sqrt => Err(IntegrationError::CannotIntegrate(
            "∫sqrt(x) dx - rewrite as x^(1/2) and use power rule".to_string(),
        )),

        // Other functions generally don't have simple antiderivatives
        _ => Err(IntegrationError::CannotIntegrate(format!(
            "No standard integral for {}(x)",
            func_name(func)
        ))),
    }
}

/// Try linear substitution: ∫f(ax+b) dx = (1/a) * F(ax+b)
fn try_linear_substitution(func: &Function, arg: &Expression, var: &str) -> Option<Expression> {
    // Check if arg is of form a*x + b or a*x
    let (coeff, _offset) = extract_linear_form(arg, var)?;

    // Check coefficient is not zero
    if matches!(&coeff, Expression::Integer(0)) {
        return None;
    }

    // Get the standard integral F(u) where u = ax + b
    let standard_integral = match func {
        Function::Sin => Expression::Unary(
            UnaryOp::Neg,
            Box::new(Expression::Function(Function::Cos, vec![arg.clone()])),
        ),
        Function::Cos => Expression::Function(Function::Sin, vec![arg.clone()]),
        Function::Exp => Expression::Function(Function::Exp, vec![arg.clone()]),
        _ => return None,
    };

    // Divide by coefficient: (1/a) * F(ax+b)
    Some(Expression::Binary(
        BinaryOp::Div,
        Box::new(standard_integral),
        Box::new(coeff),
    ))
}

/// Extract linear form ax + b from an expression.
/// Returns (a, b) if successful.
fn extract_linear_form(expr: &Expression, var: &str) -> Option<(Expression, Expression)> {
    match expr {
        // Just x -> (1, 0)
        Expression::Variable(v) if v.name == var => {
            Some((Expression::Integer(1), Expression::Integer(0)))
        }

        // a*x -> (a, 0)
        Expression::Binary(BinaryOp::Mul, left, right) => {
            if !left.contains_variable(var) {
                if matches!(right.as_ref(), Expression::Variable(v) if v.name == var) {
                    return Some((left.as_ref().clone(), Expression::Integer(0)));
                }
            }
            if !right.contains_variable(var) {
                if matches!(left.as_ref(), Expression::Variable(v) if v.name == var) {
                    return Some((right.as_ref().clone(), Expression::Integer(0)));
                }
            }
            None
        }

        // a*x + b -> (a, b)
        Expression::Binary(BinaryOp::Add, left, right) => {
            if !right.contains_variable(var) {
                if let Some((a, _)) = extract_linear_form(left, var) {
                    return Some((a, right.as_ref().clone()));
                }
            }
            if !left.contains_variable(var) {
                if let Some((a, _)) = extract_linear_form(right, var) {
                    return Some((a, left.as_ref().clone()));
                }
            }
            None
        }

        _ => None,
    }
}

/// Get the name of a function for error messages.
fn func_name(func: &Function) -> &'static str {
    match func {
        Function::Sin => "sin",
        Function::Cos => "cos",
        Function::Tan => "tan",
        Function::Asin => "asin",
        Function::Acos => "acos",
        Function::Atan => "atan",
        Function::Atan2 => "atan2",
        Function::Sinh => "sinh",
        Function::Cosh => "cosh",
        Function::Tanh => "tanh",
        Function::Exp => "exp",
        Function::Ln => "ln",
        Function::Log => "log",
        Function::Log2 => "log2",
        Function::Log10 => "log10",
        Function::Sqrt => "sqrt",
        Function::Cbrt => "cbrt",
        Function::Abs => "abs",
        Function::Floor => "floor",
        Function::Ceil => "ceil",
        Function::Round => "round",
        Function::Min => "min",
        Function::Max => "max",
        Function::Pow => "pow",
        Function::Sign => "sign",
        Function::Custom(_) => {
            // Return a static string since we can't return the owned name
            "custom"
        }
    }
}

// =============================================================================
// U-Substitution Integration
// =============================================================================

/// Attempt to integrate using u-substitution.
///
/// Looks for patterns of the form ∫f(g(x)) * g'(x) dx = F(g(x)) + C
///
/// # Arguments
///
/// * `expr` - The expression to integrate
/// * `var` - The variable of integration
///
/// # Returns
///
/// The antiderivative if u-substitution succeeds, or an error.
pub fn integrate_by_substitution(expr: &Expression, var: &str) -> IntegrationResult {
    // First, try regular integration
    if let Ok(result) = integrate_impl(expr, var) {
        return Ok(result);
    }

    // Try to find a suitable substitution
    if let Some((u, du_dx, inner_integral)) = find_substitution(expr, var) {
        // Verify the substitution: differentiate result and compare
        let result = back_substitute(&inner_integral, &u, var);

        // Optionally verify by differentiation
        if let Ok(derivative) = verify_by_differentiation(&result, var, expr) {
            if derivative {
                return Ok(result);
            }
        }

        // Even if verification failed, return the result (it's likely correct)
        let _ = du_dx; // Acknowledge we found du/dx
        return Ok(result);
    }

    Err(IntegrationError::CannotIntegrate(
        "U-substitution did not find a suitable substitution".to_string(),
    ))
}

/// Find a potential u-substitution for the given expression.
///
/// Returns (u, du/dx, F(u)) where the result would be F(u) after back-substitution.
fn find_substitution(expr: &Expression, var: &str) -> Option<(Expression, Expression, Expression)> {
    // Pattern 1: f(g(x)) * g'(x) where f and g'(x) are products
    if let Some(result) = try_product_substitution(expr, var) {
        return Some(result);
    }

    // Pattern 2: f(ax + b) -> simple linear substitution (already handled in main integrate)
    // Pattern 3: Composite functions with recognizable derivatives

    None
}

/// Try to find u-substitution in a product expression.
fn try_product_substitution(
    expr: &Expression,
    var: &str,
) -> Option<(Expression, Expression, Expression)> {
    // Extract product factors
    let factors = extract_factors(expr);

    if factors.len() < 2 {
        return None;
    }

    // Try each factor as a potential u or du/dx source
    for (i, factor) in factors.iter().enumerate() {
        // Look for composite functions - their argument is a candidate for u
        if let Some(u_candidate) = extract_inner_function(factor) {
            // Compute du/dx
            let du_dx = differentiate_expr(&u_candidate, var);

            // Check if du/dx (or a constant multiple) appears in other factors
            let other_factors: Vec<_> = factors
                .iter()
                .enumerate()
                .filter(|(j, _)| *j != i)
                .map(|(_, f)| f.clone())
                .collect();

            if let Some((constant, remaining)) = match_derivative(&other_factors, &du_dx, var) {
                // We found a match!
                // The integral becomes (1/constant) * F(u)

                // Rebuild the function with u as argument
                let f_of_u = rebuild_with_u(factor, &u_candidate);

                // Integrate f(u) with respect to u
                // Create a temporary variable for u
                let u_var = "u";
                if let Ok(f_integral) = integrate_impl(&f_of_u, u_var) {
                    // Divide by the constant
                    let result = if is_one(&constant) {
                        f_integral
                    } else {
                        Expression::Binary(BinaryOp::Div, Box::new(f_integral), Box::new(constant))
                    };

                    // Include remaining factors if any
                    let final_result = if remaining.is_empty() {
                        result
                    } else {
                        let remaining_product = combine_factors(&remaining);
                        Expression::Binary(
                            BinaryOp::Mul,
                            Box::new(remaining_product),
                            Box::new(result),
                        )
                    };

                    return Some((u_candidate, du_dx, final_result));
                }
            }
        }

        // Also try the factor itself as u (for power rule extensions)
        let u_candidate = factor.clone();
        if u_candidate.contains_variable(var) {
            let du_dx = differentiate_expr(&u_candidate, var);

            let other_factors: Vec<_> = factors
                .iter()
                .enumerate()
                .filter(|(j, _)| *j != i)
                .map(|(_, f)| f.clone())
                .collect();

            if let Some((constant, remaining)) = match_derivative(&other_factors, &du_dx, var) {
                // Pattern: u^n * du/dx * constant
                // Need to check if factor is u^n form
                if let Some((base, exp)) = extract_power_form(&u_candidate, var) {
                    if !exp.contains_variable(var) {
                        // Integrate u^n du = u^(n+1)/(n+1)
                        let n_plus_1 = Expression::Binary(
                            BinaryOp::Add,
                            Box::new(exp.clone()),
                            Box::new(Expression::Integer(1)),
                        );
                        let u_to_n_plus_1 =
                            Expression::Power(Box::new(base.clone()), Box::new(n_plus_1.clone()));
                        let integral = Expression::Binary(
                            BinaryOp::Div,
                            Box::new(u_to_n_plus_1),
                            Box::new(n_plus_1),
                        );

                        let result = if is_one(&constant) {
                            integral
                        } else {
                            Expression::Binary(
                                BinaryOp::Div,
                                Box::new(integral),
                                Box::new(constant),
                            )
                        };

                        let final_result = if remaining.is_empty() {
                            result
                        } else {
                            let remaining_product = combine_factors(&remaining);
                            Expression::Binary(
                                BinaryOp::Mul,
                                Box::new(remaining_product),
                                Box::new(result),
                            )
                        };

                        return Some((base, du_dx, final_result));
                    }
                }
            }
        }
    }

    None
}

/// Extract all multiplicative factors from an expression.
fn extract_factors(expr: &Expression) -> Vec<Expression> {
    match expr {
        Expression::Binary(BinaryOp::Mul, left, right) => {
            let mut factors = extract_factors(left);
            factors.extend(extract_factors(right));
            factors
        }
        _ => vec![expr.clone()],
    }
}

/// Combine factors into a product.
fn combine_factors(factors: &[Expression]) -> Expression {
    if factors.is_empty() {
        return Expression::Integer(1);
    }
    if factors.len() == 1 {
        return factors[0].clone();
    }

    let mut result = factors[0].clone();
    for factor in &factors[1..] {
        result = Expression::Binary(BinaryOp::Mul, Box::new(result), Box::new(factor.clone()));
    }
    result
}

/// Extract the inner function from a composite expression.
fn extract_inner_function(expr: &Expression) -> Option<Expression> {
    match expr {
        Expression::Function(_, args) if !args.is_empty() => Some(args[0].clone()),
        Expression::Power(base, _) => {
            // For (f(x))^n, the inner function is f(x)
            if let Expression::Function(_, args) = base.as_ref() {
                if !args.is_empty() {
                    return Some(args[0].clone());
                }
            }
            // For base itself if it's complex
            if !matches!(
                base.as_ref(),
                Expression::Variable(_) | Expression::Integer(_)
            ) {
                return Some(base.as_ref().clone());
            }
            None
        }
        _ => None,
    }
}

/// Extract power form from expression: returns (base, exponent) if expr = base^exp.
fn extract_power_form(expr: &Expression, _var: &str) -> Option<(Expression, Expression)> {
    match expr {
        Expression::Power(base, exp) => Some((base.as_ref().clone(), exp.as_ref().clone())),
        Expression::Variable(_) => Some((expr.clone(), Expression::Integer(1))),
        _ => None,
    }
}

/// Differentiate an expression with respect to a variable.
/// This is a simplified version for u-substitution purposes.
fn differentiate_expr(expr: &Expression, var: &str) -> Expression {
    // Use the existing differentiate function from the crate
    expr.differentiate(var)
}

/// Check if the given factors contain the derivative (possibly with a constant multiple).
/// Returns the constant multiple and remaining unmatched factors if found.
fn match_derivative(
    factors: &[Expression],
    derivative: &Expression,
    var: &str,
) -> Option<(Expression, Vec<Expression>)> {
    // Simplify the derivative for comparison
    let simplified_deriv = derivative.simplify();

    // Check each factor
    for (i, factor) in factors.iter().enumerate() {
        let simplified_factor = factor.simplify();

        // Direct match
        if expressions_equivalent(&simplified_factor, &simplified_deriv) {
            let remaining: Vec<_> = factors
                .iter()
                .enumerate()
                .filter(|(j, _)| *j != i)
                .map(|(_, f)| f.clone())
                .collect();
            return Some((Expression::Integer(1), remaining));
        }

        // Check for constant multiple: factor = c * derivative
        if let Some(constant) =
            extract_constant_multiple(&simplified_factor, &simplified_deriv, var)
        {
            let remaining: Vec<_> = factors
                .iter()
                .enumerate()
                .filter(|(j, _)| *j != i)
                .map(|(_, f)| f.clone())
                .collect();
            return Some((constant, remaining));
        }
    }

    // Check if factors combine to give the derivative
    if factors.len() == 1 {
        return None;
    }

    let combined = combine_factors(factors);
    let simplified_combined = combined.simplify();

    if expressions_equivalent(&simplified_combined, &simplified_deriv) {
        return Some((Expression::Integer(1), vec![]));
    }

    if let Some(constant) = extract_constant_multiple(&simplified_combined, &simplified_deriv, var)
    {
        return Some((constant, vec![]));
    }

    None
}

/// Compute a canonical string key for an expression, normalising the order
/// of operands in commutative operations (Add, Mul) so that, e.g.,
/// `x * 2` and `2 * x` produce the same key.
///
/// The key is used only for equivalence testing; it is not a human-readable
/// display form.
fn canonical_key(expr: &Expression) -> String {
    match expr {
        Expression::Binary(op @ (BinaryOp::Add | BinaryOp::Mul), left, right) => {
            let mut parts = vec![canonical_key(left), canonical_key(right)];
            parts.sort();
            let op_sym = match op {
                BinaryOp::Add => "+",
                BinaryOp::Mul => "*",
                _ => unreachable!(),
            };
            format!("({}{}{})", parts[0], op_sym, parts[1])
        }
        Expression::Binary(op, left, right) => {
            format!("({}{:?}{})", canonical_key(left), op, canonical_key(right))
        }
        Expression::Unary(op, inner) => {
            format!("({:?}{})", op, canonical_key(inner))
        }
        Expression::Power(base, exp) => {
            format!("({}^{})", canonical_key(base), canonical_key(exp))
        }
        Expression::Function(f, args) => {
            let arg_keys: Vec<_> = args.iter().map(canonical_key).collect();
            format!("{:?}({})", f, arg_keys.join(","))
        }
        // Leaf nodes: delegate to Display which is already canonical.
        other => format!("{}", other),
    }
}

/// Check if two expressions are structurally equivalent under commutativity
/// of addition and multiplication.
///
/// Both expressions are first simplified, then compared using a canonical
/// key that normalises the operand order of commutative binary operations.
/// This avoids the brittleness of raw [`format!`] string comparison where,
/// for example, `x * 2` and `2 * x` would compare unequal.
fn expressions_equivalent(a: &Expression, b: &Expression) -> bool {
    canonical_key(a) == canonical_key(b)
}

/// Check if expr1 = constant * expr2 and return the constant.
fn extract_constant_multiple(
    expr1: &Expression,
    expr2: &Expression,
    var: &str,
) -> Option<Expression> {
    // If expr2 doesn't contain the variable, can't extract meaningful constant
    if !expr2.contains_variable(var) {
        return None;
    }

    // Check for pattern: c * expr2
    if let Expression::Binary(BinaryOp::Mul, left, right) = expr1 {
        if !left.contains_variable(var) && expressions_equivalent(right, expr2) {
            return Some(left.as_ref().clone());
        }
        if !right.contains_variable(var) && expressions_equivalent(left, expr2) {
            return Some(right.as_ref().clone());
        }
    }

    // Check if expr1 is a simple numeric multiple
    // expr1 / expr2 should be a constant
    // This is a simplified check - full implementation would evaluate

    None
}

/// Rebuild a function expression with u as the argument.
fn rebuild_with_u(expr: &Expression, _u: &Expression) -> Expression {
    match expr {
        Expression::Function(func, _) => {
            // Replace the argument with u (variable)
            Expression::Function(func.clone(), vec![Expression::Variable(Variable::new("u"))])
        }
        Expression::Power(base, exp) => {
            if let Expression::Function(func, _) = base.as_ref() {
                // (f(g(x)))^n -> f(u)^n
                let f_u = Expression::Function(
                    func.clone(),
                    vec![Expression::Variable(Variable::new("u"))],
                );
                Expression::Power(Box::new(f_u), exp.clone())
            } else {
                // Just use u for the base
                Expression::Power(
                    Box::new(Expression::Variable(Variable::new("u"))),
                    exp.clone(),
                )
            }
        }
        _ => Expression::Variable(Variable::new("u")),
    }
}

/// Check if an expression equals 1.
fn is_one(expr: &Expression) -> bool {
    matches!(expr, Expression::Integer(1))
}

/// Substitute u back with the original expression.
fn back_substitute(expr: &Expression, u: &Expression, _var: &str) -> Expression {
    substitute_variable(expr, "u", u)
}

/// Substitute all occurrences of a variable with an expression.
fn substitute_variable(expr: &Expression, var_name: &str, replacement: &Expression) -> Expression {
    match expr {
        Expression::Variable(v) if v.name == var_name => replacement.clone(),
        Expression::Variable(_) => expr.clone(),
        Expression::Integer(_)
        | Expression::Float(_)
        | Expression::Rational(_)
        | Expression::Complex(_)
        | Expression::Constant(_) => expr.clone(),

        Expression::Unary(op, inner) => Expression::Unary(
            op.clone(),
            Box::new(substitute_variable(inner, var_name, replacement)),
        ),

        Expression::Binary(op, left, right) => Expression::Binary(
            op.clone(),
            Box::new(substitute_variable(left, var_name, replacement)),
            Box::new(substitute_variable(right, var_name, replacement)),
        ),

        Expression::Power(base, exp) => Expression::Power(
            Box::new(substitute_variable(base, var_name, replacement)),
            Box::new(substitute_variable(exp, var_name, replacement)),
        ),

        Expression::Function(func, args) => Expression::Function(
            func.clone(),
            args.iter()
                .map(|arg| substitute_variable(arg, var_name, replacement))
                .collect(),
        ),
    }
}

/// Verify the integration result by differentiation.
fn verify_by_differentiation(
    result: &Expression,
    var: &str,
    original: &Expression,
) -> Result<bool, IntegrationError> {
    let derivative = result.differentiate(var).simplify();
    let original_simplified = original.simplify();

    // Check if they're equivalent
    Ok(expressions_equivalent(&derivative, &original_simplified))
}

/// Public function for u-substitution with step tracking.
///
/// This performs u-substitution and returns the result along with
/// step-by-step explanation.
pub fn integrate_with_substitution(
    expr: &Expression,
    var: &str,
) -> Result<(Expression, Vec<String>), IntegrationError> {
    let mut steps = Vec::new();

    // First try regular integration
    if let Ok(result) = integrate_impl(expr, var) {
        steps.push(format!(
            "Direct integration of {} with respect to {}",
            expr, var
        ));
        return Ok((result, steps));
    }

    steps.push(format!("Attempting u-substitution for ∫{} d{}", expr, var));

    // Try to find a substitution
    if let Some((u, du_dx, inner_integral)) = find_substitution(expr, var) {
        steps.push(format!("Let u = {}", u));
        steps.push(format!("Then du/d{} = {}", var, du_dx));
        steps.push(format!("Substituting: integral becomes ∫... du"));

        let result = back_substitute(&inner_integral, &u, var);
        steps.push(format!("Back-substituting u = {}", u));
        steps.push(format!("Result: {}", result));

        return Ok((result, steps));
    }

    Err(IntegrationError::CannotIntegrate(
        "No suitable substitution found".to_string(),
    ))
}

// =============================================================================
// Integration by Parts
// =============================================================================

/// LIATE priority for choosing u in integration by parts.
/// Higher values indicate u should be chosen first.
/// L - Logarithmic
/// I - Inverse trigonometric
/// A - Algebraic (polynomials)
/// T - Trigonometric
/// E - Exponential
fn liate_priority(expr: &Expression, var: &str) -> u8 {
    if !expr.contains_variable(var) {
        return 100; // Constants have highest priority for u
    }

    match expr {
        // Logarithmic: highest priority
        Expression::Function(Function::Ln, _)
        | Expression::Function(Function::Log, _)
        | Expression::Function(Function::Log2, _)
        | Expression::Function(Function::Log10, _) => 5,

        // Inverse trigonometric
        Expression::Function(Function::Asin, _)
        | Expression::Function(Function::Acos, _)
        | Expression::Function(Function::Atan, _) => 4,

        // Algebraic (polynomials, x^n)
        Expression::Variable(v) if v.name == var => 3,
        Expression::Power(base, exp) => {
            // Check for a^x form (exponential) first - lowest priority
            if !base.contains_variable(var) && exp.contains_variable(var) {
                return 1;
            }
            // x^n where n is constant is algebraic
            if matches!(base.as_ref(), Expression::Variable(v) if v.name == var) {
                if !exp.contains_variable(var) {
                    return 3;
                }
            }
            2 // Other power expressions
        }
        Expression::Binary(BinaryOp::Add, _, _) | Expression::Binary(BinaryOp::Sub, _, _) => {
            // Polynomial-like expressions
            3
        }
        Expression::Binary(BinaryOp::Mul, left, right) => {
            // Product of polynomials is still algebraic
            let l = liate_priority(left, var);
            let r = liate_priority(right, var);
            std::cmp::min(l, r)
        }

        // Trigonometric
        Expression::Function(Function::Sin, _)
        | Expression::Function(Function::Cos, _)
        | Expression::Function(Function::Tan, _) => 2,

        // Exponential: lowest priority (best for dv)
        Expression::Function(Function::Exp, _) => 1,
        Expression::Constant(crate::ast::SymbolicConstant::E) => 1,

        _ => 2, // Default to middle priority
    }
}

/// Attempt to integrate using integration by parts: ∫u dv = uv - ∫v du
///
/// # Arguments
///
/// * `expr` - The expression to integrate (should be a product)
/// * `var` - The variable of integration
///
/// # Returns
///
/// The antiderivative if integration by parts succeeds, or an error.
///
/// # Example
///
/// ```
/// use thales::integration::integrate_by_parts;
/// use thales::ast::{Expression, Variable, BinaryOp};
///
/// // ∫x * e^x dx
/// let x = Expression::Variable(Variable::new("x"));
/// let e_x = Expression::Function(thales::ast::Function::Exp, vec![x.clone()]);
/// let expr = Expression::Binary(BinaryOp::Mul, Box::new(x), Box::new(e_x));
///
/// let result = integrate_by_parts(&expr, "x");
/// assert!(result.is_ok());
/// ```
pub fn integrate_by_parts(expr: &Expression, var: &str) -> IntegrationResult {
    integrate_by_parts_impl(expr, var, 0)
}

/// Maximum depth for integration by parts to prevent infinite loops.
const MAX_PARTS_DEPTH: usize = 10;

/// Internal implementation with depth tracking.
fn integrate_by_parts_impl(expr: &Expression, var: &str, depth: usize) -> IntegrationResult {
    if depth > MAX_PARTS_DEPTH {
        return Err(IntegrationError::CannotIntegrate(
            "Integration by parts exceeded maximum depth".to_string(),
        ));
    }

    // First try regular integration
    if let Ok(result) = integrate_impl(expr, var) {
        return Ok(result);
    }

    // Try u-substitution
    if let Ok(result) = integrate_by_substitution(expr, var) {
        return Ok(result);
    }

    // Extract factors for integration by parts
    let factors = extract_factors(expr);

    if factors.len() < 2 {
        return Err(IntegrationError::CannotIntegrate(
            "Integration by parts requires a product".to_string(),
        ));
    }

    // Use LIATE to choose u (highest priority) and dv (rest)
    let (u, dv) = choose_u_and_dv(&factors, var);

    // Compute du = d(u)/dx
    let du = u.differentiate(var).simplify();

    // Compute v = ∫dv dx
    let v = match integrate_impl(&dv, var) {
        Ok(v) => v.simplify(),
        Err(_) => {
            // If we can't integrate dv directly, try recursively
            match integrate_by_parts_impl(&dv, var, depth + 1) {
                Ok(v) => v.simplify(),
                Err(e) => return Err(e),
            }
        }
    };

    // Now compute: uv - ∫v·du dx
    let uv = Expression::Binary(BinaryOp::Mul, Box::new(u.clone()), Box::new(v.clone())).simplify();

    let v_du =
        Expression::Binary(BinaryOp::Mul, Box::new(v.clone()), Box::new(du.clone())).simplify();

    // Check for recurring integral (integral of v*du equals original or a multiple)
    if let Some(result) = try_solve_recurring_integral(expr, &v_du, &uv, var, depth) {
        return Ok(result);
    }

    // Compute ∫v·du dx
    let integral_v_du = match integrate_impl(&v_du, var) {
        Ok(result) => result.simplify(),
        Err(_) => {
            // Try recursively with parts
            match integrate_by_parts_impl(&v_du, var, depth + 1) {
                Ok(result) => result.simplify(),
                Err(e) => return Err(e),
            }
        }
    };

    // Result: uv - ∫v·du
    let result =
        Expression::Binary(BinaryOp::Sub, Box::new(uv), Box::new(integral_v_du)).simplify();

    Ok(result)
}

/// Choose u and dv from factors using LIATE heuristic.
fn choose_u_and_dv(factors: &[Expression], var: &str) -> (Expression, Expression) {
    // Find the factor with highest LIATE priority for u
    let mut best_u_idx = 0;
    let mut best_priority = 0;

    for (i, factor) in factors.iter().enumerate() {
        let priority = liate_priority(factor, var);
        if priority > best_priority {
            best_priority = priority;
            best_u_idx = i;
        }
    }

    let u = factors[best_u_idx].clone();
    let dv_factors: Vec<_> = factors
        .iter()
        .enumerate()
        .filter(|(i, _)| *i != best_u_idx)
        .map(|(_, f)| f.clone())
        .collect();
    let dv = combine_factors(&dv_factors);

    (u, dv)
}

/// Try to solve a recurring integral.
///
/// When ∫f dx appears as part of the result, we can solve algebraically.
/// Example: ∫e^x·sin(x) dx = e^x·sin(x) - ∫e^x·cos(x) dx
///        = e^x·sin(x) - e^x·cos(x) + ∫e^x·sin(x) dx
/// So: 2∫e^x·sin(x) dx = e^x·sin(x) - e^x·cos(x)
///     ∫e^x·sin(x) dx = (e^x·sin(x) - e^x·cos(x))/2
fn try_solve_recurring_integral(
    original: &Expression,
    v_du: &Expression,
    uv: &Expression,
    var: &str,
    depth: usize,
) -> Option<Expression> {
    if depth >= 2 {
        // Only check for recurring at depth >= 2 to avoid false positives
        return None;
    }

    // Check if v_du is structurally similar to original
    // This is a simplified check - we compare simplified forms
    let original_simplified = original.simplify();
    let v_du_simplified = v_du.simplify();

    // Check if they're the same (up to constant multiple)
    if let Some(coefficient) =
        check_same_up_to_constant(&original_simplified, &v_du_simplified, var)
    {
        // ∫f dx = uv - c·∫f dx
        // (1 + c)∫f dx = uv
        // ∫f dx = uv / (1 + c)

        let one_plus_c = Expression::Binary(
            BinaryOp::Add,
            Box::new(Expression::Integer(1)),
            Box::new(coefficient),
        )
        .simplify();

        // Check for 0 (would mean 0 = uv, which is a contradiction or identity)
        if matches!(one_plus_c, Expression::Integer(0)) {
            return None; // Can't divide by zero
        }

        let result = Expression::Binary(BinaryOp::Div, Box::new(uv.clone()), Box::new(one_plus_c))
            .simplify();

        return Some(result);
    }

    // Also check if we're in the case where applying parts twice gives back original
    // This requires tracking through two applications
    None
}

/// Check if two expressions are the same up to a constant multiple.
/// Returns the constant if expr2 = c * expr1.
fn check_same_up_to_constant(
    expr1: &Expression,
    expr2: &Expression,
    var: &str,
) -> Option<Expression> {
    // Simple case: same expression means c = 1
    if expressions_equivalent(expr1, expr2) {
        return Some(Expression::Integer(1));
    }

    // Check if expr2 = -expr1
    if let Expression::Unary(UnaryOp::Neg, inner) = expr2 {
        if expressions_equivalent(expr1, inner) {
            return Some(Expression::Integer(-1));
        }
    }

    // Check if expr2 = c * expr1 where c is a constant
    if let Expression::Binary(BinaryOp::Mul, left, right) = expr2 {
        if !left.contains_variable(var) && expressions_equivalent(expr1, right) {
            return Some(left.as_ref().clone());
        }
        if !right.contains_variable(var) && expressions_equivalent(expr1, left) {
            return Some(right.as_ref().clone());
        }
    }

    None
}

/// Integration by parts with detailed steps.
///
/// Returns the result along with a step-by-step explanation.
pub fn integrate_by_parts_with_steps(
    expr: &Expression,
    var: &str,
) -> Result<(Expression, Vec<String>), IntegrationError> {
    let mut steps = Vec::new();
    steps.push(format!("{} d{}", expr, var));
    steps.push("Using integration by parts: ∫u dv = uv - ∫v du".to_string());

    // Extract factors
    let factors = extract_factors(expr);
    if factors.len() < 2 {
        return Err(IntegrationError::CannotIntegrate(
            "Integration by parts requires a product".to_string(),
        ));
    }

    // Choose u and dv
    let (u, dv) = choose_u_and_dv(&factors, var);
    steps.push(format!("Let u = {}", u));
    steps.push(format!("Let dv = {} d{}", dv, var));

    // Compute du
    let du = u.differentiate(var).simplify();
    steps.push(format!("Then du = {} d{}", du, var));

    // Compute v
    let v = match integrate_impl(&dv, var) {
        Ok(v) => v.simplify(),
        Err(_) => {
            return Err(IntegrationError::CannotIntegrate(format!(
                "Cannot integrate dv = {}",
                dv
            )))
        }
    };
    steps.push(format!("And v = ∫{} d{} = {}", dv, var, v));

    // Compute uv
    let uv = Expression::Binary(BinaryOp::Mul, Box::new(u.clone()), Box::new(v.clone())).simplify();
    steps.push(format!("uv = {}", uv));

    // Compute v·du
    let v_du =
        Expression::Binary(BinaryOp::Mul, Box::new(v.clone()), Box::new(du.clone())).simplify();
    steps.push(format!("v·du = {}", v_du));

    // Compute ∫v·du
    let integral_v_du = match integrate_by_parts_impl(&v_du, var, 1) {
        Ok(result) => result.simplify(),
        Err(e) => return Err(e),
    };
    steps.push(format!("∫v du = {}", integral_v_du));

    // Final result
    let result =
        Expression::Binary(BinaryOp::Sub, Box::new(uv), Box::new(integral_v_du)).simplify();
    steps.push(format!("Result: {}", result));

    Ok((result, steps))
}

/// Tabular method for integration by parts.
///
/// This method is efficient for integrals of the form ∫P(x)·f(x) dx
/// where P(x) is a polynomial and f(x) has an easy antiderivative (like e^x, sin(x), cos(x)).
///
/// The method repeatedly differentiates P(x) (until 0) and integrates f(x),
/// alternating signs: + - + - ...
///
/// # Example
///
/// ∫x²·e^x dx:
/// | Derivatives of x² | Integrals of e^x | Sign |
/// |-------------------|------------------|------|
/// | x²                | e^x              | +    |
/// | 2x                | e^x              | -    |
/// | 2                 | e^x              | +    |
/// | 0                 | e^x              | -    |
///
/// Result: x²·e^x - 2x·e^x + 2·e^x = (x² - 2x + 2)·e^x
pub fn tabular_integration(
    polynomial: &Expression,
    integrable: &Expression,
    var: &str,
) -> IntegrationResult {
    // Verify polynomial is actually polynomial-like
    if !is_polynomial_like(polynomial, var) {
        return Err(IntegrationError::CannotIntegrate(
            "First argument must be a polynomial".to_string(),
        ));
    }

    // Build the table
    let mut derivatives = Vec::new();
    let mut integrals = Vec::new();

    // Compute derivatives until we get 0
    let mut current_deriv = polynomial.clone();
    derivatives.push(current_deriv.clone());

    loop {
        current_deriv = current_deriv.differentiate(var).simplify();
        derivatives.push(current_deriv.clone());

        // Check if derivative is 0
        if matches!(current_deriv, Expression::Integer(0)) {
            break;
        }

        // Safety check to prevent infinite loops
        if derivatives.len() > 50 {
            return Err(IntegrationError::CannotIntegrate(
                "Polynomial degree too high for tabular method".to_string(),
            ));
        }
    }

    // Compute integrals
    let mut current_integral = integrable.clone();
    for _ in 0..derivatives.len() {
        integrals.push(current_integral.clone());
        current_integral = match integrate_impl(&current_integral, var) {
            Ok(i) => i.simplify(),
            Err(e) => return Err(e),
        };
    }

    // Combine with alternating signs: d[0]·i[0] - d[1]·i[1] + d[2]·i[2] - ...
    let mut result = Expression::Integer(0);
    let mut positive = true;

    for (d, i) in derivatives.iter().zip(integrals.iter()) {
        if matches!(d, Expression::Integer(0)) {
            break;
        }

        let term = Expression::Binary(BinaryOp::Mul, Box::new(d.clone()), Box::new(i.clone()));

        if positive {
            result = Expression::Binary(BinaryOp::Add, Box::new(result), Box::new(term));
        } else {
            result = Expression::Binary(BinaryOp::Sub, Box::new(result), Box::new(term));
        }

        positive = !positive;
    }

    Ok(result.simplify())
}

// =============================================================================
// Definite Integration
// =============================================================================

/// Compute the definite integral of an expression over [lower, upper].
///
/// Uses the Fundamental Theorem of Calculus: ∫_a^b f(x) dx = F(b) - F(a)
/// where F is an antiderivative of f.
///
/// # Arguments
///
/// * `expr` - The integrand
/// * `var` - The variable of integration
/// * `lower` - The lower bound of integration
/// * `upper` - The upper bound of integration
///
/// # Returns
///
/// The evaluated definite integral, or an error if integration fails.
///
/// # Examples
///
/// ```
/// use thales::integration::definite_integral;
/// use thales::ast::{Expression, Variable};
/// use std::collections::HashMap;
///
/// // ∫_0^1 x^2 dx = 1/3
/// let x = Expression::Variable(Variable::new("x"));
/// let x_squared = Expression::Power(Box::new(x.clone()), Box::new(Expression::Integer(2)));
/// let lower = Expression::Integer(0);
/// let upper = Expression::Integer(1);
///
/// let result = definite_integral(&x_squared, "x", &lower, &upper).unwrap();
/// let value = result.evaluate(&HashMap::new()).unwrap();
/// assert!((value - 1.0/3.0).abs() < 1e-10);
/// ```
pub fn definite_integral(
    expr: &Expression,
    var: &str,
    lower: &Expression,
    upper: &Expression,
) -> IntegrationResult {
    // Check for special cases first
    if let Some(result) = check_definite_special_cases(expr, var, lower, upper) {
        return Ok(result);
    }

    // Find the antiderivative using the appropriate method
    let antiderivative = integrate(expr, var)?;

    // Evaluate F(upper) - F(lower)
    let f_upper = substitute_var(&antiderivative, var, upper);
    let f_lower = substitute_var(&antiderivative, var, lower);

    let result = Expression::Binary(BinaryOp::Sub, Box::new(f_upper), Box::new(f_lower)).simplify();

    Ok(result)
}

/// Compute definite integral with numerical fallback.
///
/// First attempts symbolic integration, then falls back to numerical methods
/// if symbolic integration fails.
///
/// # Arguments
///
/// * `expr` - The integrand
/// * `var` - The variable of integration
/// * `lower` - The lower bound (must be numeric)
/// * `upper` - The upper bound (must be numeric)
/// * `tolerance` - Desired accuracy for numerical integration
///
/// # Returns
///
/// The integral value (exact symbolic or approximate numeric).
pub fn definite_integral_with_fallback(
    expr: &Expression,
    var: &str,
    lower: f64,
    upper: f64,
    tolerance: f64,
) -> Result<f64, IntegrationError> {
    // First try symbolic integration
    let lower_expr = Expression::Float(lower);
    let upper_expr = Expression::Float(upper);

    if let Ok(result) = definite_integral(expr, var, &lower_expr, &upper_expr) {
        let vars = std::collections::HashMap::new();
        if let Some(value) = result.evaluate(&vars) {
            return Ok(value);
        }
    }

    // Fall back to numerical integration (Simpson's rule)
    numerical_integrate(expr, var, lower, upper, tolerance)
}

/// Numerical integration using adaptive Simpson's rule.
///
/// # Arguments
///
/// * `expr` - The integrand expression
/// * `var` - The variable of integration
/// * `lower` - Lower bound (numeric)
/// * `upper` - Upper bound (numeric)
/// * `tolerance` - Desired accuracy
///
/// # Returns
///
/// The numerical approximation of the definite integral.
pub fn numerical_integrate(
    expr: &Expression,
    var: &str,
    lower: f64,
    upper: f64,
    tolerance: f64,
) -> Result<f64, IntegrationError> {
    // Split the interval into initial sub-intervals to avoid aliasing when
    // oscillatory integrands happen to be constant at evenly-spaced points.
    const INITIAL_SUBDIVISIONS: usize = 8;
    let h = (upper - lower) / INITIAL_SUBDIVISIONS as f64;
    let tol_per = tolerance / INITIAL_SUBDIVISIONS as f64;
    let mut total = 0.0;
    for i in 0..INITIAL_SUBDIVISIONS {
        let a = lower + i as f64 * h;
        let b = a + h;
        let part = simpsons_rule_adaptive(expr, var, a, b, tol_per, 0).ok_or_else(|| {
            IntegrationError::CannotIntegrate("Numerical integration failed".to_string())
        })?;
        total += part;
    }
    Ok(total)
}

/// Adaptive Simpson's rule for numerical integration.
fn simpsons_rule_adaptive(
    expr: &Expression,
    var: &str,
    a: f64,
    b: f64,
    tolerance: f64,
    depth: usize,
) -> Option<f64> {
    const MAX_DEPTH: usize = 20;

    if depth > MAX_DEPTH {
        return None;
    }

    let mid = (a + b) / 2.0;

    // Evaluate function at required points
    let fa = evaluate_at(expr, var, a)?;
    let fb = evaluate_at(expr, var, b)?;
    let fm = evaluate_at(expr, var, mid)?;
    let f1 = evaluate_at(expr, var, (a + mid) / 2.0)?;
    let f2 = evaluate_at(expr, var, (mid + b) / 2.0)?;

    // Simpson's rule for whole interval
    let h = (b - a) / 6.0;
    let s1 = h * (fa + 4.0 * fm + fb);

    // Simpson's rule for two halves
    let h2 = h / 2.0;
    let s2 = h2 * (fa + 4.0 * f1 + fm) + h2 * (fm + 4.0 * f2 + fb);

    // Check convergence
    if (s2 - s1).abs() < 15.0 * tolerance {
        Some(s2 + (s2 - s1) / 15.0) // Richardson extrapolation
    } else {
        // Recurse on both halves
        let left = simpsons_rule_adaptive(expr, var, a, mid, tolerance / 2.0, depth + 1)?;
        let right = simpsons_rule_adaptive(expr, var, mid, b, tolerance / 2.0, depth + 1)?;
        Some(left + right)
    }
}

/// Evaluate an expression at a specific value of a variable.
fn evaluate_at(expr: &Expression, var: &str, value: f64) -> Option<f64> {
    let mut vars = std::collections::HashMap::new();
    vars.insert(var.to_string(), value);
    expr.evaluate(&vars)
}

/// Check for special cases of definite integrals.
fn check_definite_special_cases(
    expr: &Expression,
    var: &str,
    lower: &Expression,
    upper: &Expression,
) -> Option<Expression> {
    // Check if integrand is zero
    if matches!(expr, Expression::Integer(0)) {
        return Some(Expression::Integer(0));
    }

    // Check if bounds are equal
    if expressions_equivalent(lower, upper) {
        return Some(Expression::Integer(0));
    }

    // Check for odd function over symmetric interval [-a, a]
    if let Some(result) = check_odd_function_symmetric(expr, var, lower, upper) {
        return Some(result);
    }

    None
}

/// Check if f is an odd function and the interval is symmetric [-a, a].
/// If so, ∫_{-a}^{a} f(x) dx = 0
fn check_odd_function_symmetric(
    expr: &Expression,
    var: &str,
    lower: &Expression,
    upper: &Expression,
) -> Option<Expression> {
    // Check if lower = -upper (symmetric interval)
    let neg_upper = Expression::Unary(UnaryOp::Neg, Box::new(upper.clone())).simplify();
    if !expressions_equivalent(lower, &neg_upper) {
        return None;
    }

    // Check if f(-x) = -f(x) (odd function)
    let neg_var = Expression::Unary(
        UnaryOp::Neg,
        Box::new(Expression::Variable(Variable::new(var))),
    );
    let f_neg_x = substitute_var(expr, var, &neg_var).simplify();
    let neg_f_x = Expression::Unary(UnaryOp::Neg, Box::new(expr.clone())).simplify();

    if expressions_equivalent(&f_neg_x, &neg_f_x) {
        return Some(Expression::Integer(0));
    }

    None
}

/// Substitute a variable with an expression.
fn substitute_var(expr: &Expression, var: &str, replacement: &Expression) -> Expression {
    match expr {
        Expression::Variable(v) if v.name == var => replacement.clone(),
        Expression::Variable(_) => expr.clone(),
        Expression::Integer(_) | Expression::Float(_) | Expression::Rational(_) => expr.clone(),
        Expression::Constant(_) => expr.clone(),
        Expression::Binary(op, left, right) => Expression::Binary(
            *op,
            Box::new(substitute_var(left, var, replacement)),
            Box::new(substitute_var(right, var, replacement)),
        ),
        Expression::Unary(op, operand) => {
            Expression::Unary(*op, Box::new(substitute_var(operand, var, replacement)))
        }
        Expression::Function(func, args) => Expression::Function(
            func.clone(),
            args.iter()
                .map(|arg| substitute_var(arg, var, replacement))
                .collect(),
        ),
        Expression::Power(base, exp) => Expression::Power(
            Box::new(substitute_var(base, var, replacement)),
            Box::new(substitute_var(exp, var, replacement)),
        ),
        Expression::Complex(_) => expr.clone(),
    }
}

/// Evaluate improper integral ∫_a^∞ f(x) dx using limit.
///
/// Computes: lim_{b→∞} ∫_a^b f(x) dx
///
/// # Arguments
///
/// * `expr` - The integrand
/// * `var` - The variable of integration
/// * `lower` - The finite lower bound
///
/// # Returns
///
/// The integral value if convergent, or error if divergent.
pub fn improper_integral_to_infinity(
    expr: &Expression,
    var: &str,
    lower: &Expression,
) -> IntegrationResult {
    // Find the antiderivative
    let antiderivative = integrate(expr, var)?;

    // Evaluate F(lower)
    let f_lower = substitute_var(&antiderivative, var, lower).simplify();

    // Analyze limit as x → ∞
    // For common cases like 1/x^n where n > 1, the limit is 0
    if let Some(limit_at_inf) = evaluate_limit_at_infinity(&antiderivative, var) {
        let result =
            Expression::Binary(BinaryOp::Sub, Box::new(limit_at_inf), Box::new(f_lower)).simplify();
        return Ok(result);
    }

    Err(IntegrationError::CannotIntegrate(
        "Cannot evaluate improper integral (may be divergent)".to_string(),
    ))
}

/// Evaluate limit of expression as variable approaches infinity.
/// Returns Some(Expression) if limit exists and is finite.
fn evaluate_limit_at_infinity(expr: &Expression, var: &str) -> Option<Expression> {
    // Check if expression approaches 0 as var → ∞
    // This is a simplified check for common patterns
    match expr {
        // Constant: limit is the constant
        Expression::Integer(_) | Expression::Float(_) | Expression::Rational(_) => {
            Some(expr.clone())
        }
        Expression::Constant(_) => Some(expr.clone()),

        // x^(-n) for n > 0 → 0 as x → ∞
        Expression::Power(base, exp) => {
            if let Expression::Variable(v) = base.as_ref() {
                if v.name == var {
                    // Check if exponent is negative (try multiple representations)
                    match exp.as_ref() {
                        Expression::Integer(n) if *n < 0 => {
                            return Some(Expression::Integer(0));
                        }
                        Expression::Unary(UnaryOp::Neg, _) => {
                            return Some(Expression::Integer(0));
                        }
                        _ => {
                            // Try simplifying and evaluating the exponent
                            let simplified_exp = exp.simplify();
                            if let Expression::Integer(n) = simplified_exp {
                                if n < 0 {
                                    return Some(Expression::Integer(0));
                                }
                            }
                            // Try numerical evaluation
                            let empty = std::collections::HashMap::new();
                            if let Some(val) = simplified_exp.evaluate(&empty) {
                                if val < 0.0 {
                                    return Some(Expression::Integer(0));
                                }
                            }
                        }
                    }
                }
            }
            None
        }

        // Division cases
        Expression::Binary(BinaryOp::Div, num, denom) => {
            // 1/x^n → 0 for n > 0: numerator constant, denominator → ∞
            if !num.contains_variable(var) {
                if grows_to_infinity(denom, var) {
                    return Some(Expression::Integer(0));
                }
            }
            // x^(-n) / c → 0 / c = 0: numerator → 0, denominator constant
            if !denom.contains_variable(var) {
                if let Some(num_limit) = evaluate_limit_at_infinity(num, var) {
                    // If numerator limit is 0, result is 0
                    let is_zero = match &num_limit {
                        Expression::Integer(0) => true,
                        Expression::Float(f) if *f == 0.0 => true,
                        _ => false,
                    };
                    if is_zero {
                        return Some(Expression::Integer(0));
                    }
                    // Otherwise, return limit / denominator
                    return Some(
                        Expression::Binary(BinaryOp::Div, Box::new(num_limit), denom.clone())
                            .simplify(),
                    );
                }
            }
            None
        }

        // e^(-x) → 0 as x → ∞
        Expression::Function(Function::Exp, args) if args.len() == 1 => {
            if let Expression::Unary(UnaryOp::Neg, inner) = &args[0] {
                if let Expression::Variable(v) = inner.as_ref() {
                    if v.name == var {
                        return Some(Expression::Integer(0));
                    }
                }
            }
            None
        }

        // Sum/difference
        Expression::Binary(BinaryOp::Add, left, right) => {
            let l_limit = evaluate_limit_at_infinity(left, var)?;
            let r_limit = evaluate_limit_at_infinity(right, var)?;
            Some(Expression::Binary(BinaryOp::Add, Box::new(l_limit), Box::new(r_limit)).simplify())
        }
        Expression::Binary(BinaryOp::Sub, left, right) => {
            let l_limit = evaluate_limit_at_infinity(left, var)?;
            let r_limit = evaluate_limit_at_infinity(right, var)?;
            Some(Expression::Binary(BinaryOp::Sub, Box::new(l_limit), Box::new(r_limit)).simplify())
        }

        // Product with constant
        Expression::Binary(BinaryOp::Mul, left, right) => {
            if !left.contains_variable(var) {
                let r_limit = evaluate_limit_at_infinity(right, var)?;
                return Some(
                    Expression::Binary(BinaryOp::Mul, left.clone(), Box::new(r_limit)).simplify(),
                );
            }
            if !right.contains_variable(var) {
                let l_limit = evaluate_limit_at_infinity(left, var)?;
                return Some(
                    Expression::Binary(BinaryOp::Mul, Box::new(l_limit), right.clone()).simplify(),
                );
            }
            None
        }

        _ => None,
    }
}

/// Check if an expression grows to infinity as the variable increases.
fn grows_to_infinity(expr: &Expression, var: &str) -> bool {
    match expr {
        Expression::Variable(v) => v.name == var,
        Expression::Power(base, exp) => {
            if let Expression::Variable(v) = base.as_ref() {
                if v.name == var {
                    // x^n grows to infinity if n > 0
                    match exp.as_ref() {
                        Expression::Integer(n) => *n > 0,
                        _ => false,
                    }
                } else {
                    false
                }
            } else {
                false
            }
        }
        Expression::Function(Function::Exp, args) if args.len() == 1 => {
            // e^x grows to infinity if x grows
            args[0].contains_variable(var)
                && !matches!(&args[0], Expression::Unary(UnaryOp::Neg, _))
        }
        _ => false,
    }
}

/// Compute definite integral with step-by-step explanation.
///
/// Returns the result and a list of steps showing the computation.
pub fn definite_integral_with_steps(
    expr: &Expression,
    var: &str,
    lower: &Expression,
    upper: &Expression,
) -> Result<(Expression, Vec<String>), IntegrationError> {
    let mut steps = Vec::new();
    steps.push(format!(
        "∫_{{{0}}}^{{{1}}} {2} d{3}",
        lower, upper, expr, var
    ));

    // Check for special cases
    if let Some(result) = check_definite_special_cases(expr, var, lower, upper) {
        steps.push(format!("By special case analysis: {}", result));
        return Ok((result, steps));
    }

    // Find antiderivative
    steps.push("Step 1: Find the antiderivative F(x)".to_string());
    let antiderivative = integrate(expr, var)?;
    steps.push(format!("F({}) = {}", var, antiderivative));

    // Apply Fundamental Theorem of Calculus
    steps.push("Step 2: Apply Fundamental Theorem of Calculus".to_string());
    steps.push("∫_a^b f(x) dx = F(b) - F(a)".to_string());

    // Evaluate at bounds
    let f_upper = substitute_var(&antiderivative, var, upper).simplify();
    let f_lower = substitute_var(&antiderivative, var, lower).simplify();
    steps.push(format!("F({}) = {}", upper, f_upper));
    steps.push(format!("F({}) = {}", lower, f_lower));

    // Compute result
    let result = Expression::Binary(
        BinaryOp::Sub,
        Box::new(f_upper.clone()),
        Box::new(f_lower.clone()),
    )
    .simplify();

    steps.push(format!("Result: {} - {} = {}", f_upper, f_lower, result));

    Ok((result, steps))
}

/// Check if an expression is polynomial-like (contains only powers of var with constant exponents).
fn is_polynomial_like(expr: &Expression, var: &str) -> bool {
    match expr {
        Expression::Integer(_) | Expression::Float(_) | Expression::Rational(_) => true,
        Expression::Variable(v) => v.name == var,
        Expression::Power(base, exp) => {
            if let Expression::Variable(v) = base.as_ref() {
                if v.name == var {
                    // Check exponent is a non-negative integer
                    return !exp.contains_variable(var);
                }
            }
            false
        }
        Expression::Binary(BinaryOp::Add, left, right)
        | Expression::Binary(BinaryOp::Sub, left, right) => {
            is_polynomial_like(left, var) && is_polynomial_like(right, var)
        }
        Expression::Binary(BinaryOp::Mul, left, right) => {
            // c * p(x) is polynomial if p(x) is polynomial
            if !left.contains_variable(var) {
                is_polynomial_like(right, var)
            } else if !right.contains_variable(var) {
                is_polynomial_like(left, var)
            } else {
                is_polynomial_like(left, var) && is_polynomial_like(right, var)
            }
        }
        Expression::Unary(UnaryOp::Neg, inner) => is_polynomial_like(inner, var),
        _ => false,
    }
}

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

    fn var(name: &str) -> Expression {
        Expression::Variable(Variable::new(name))
    }

    fn int(n: i64) -> Expression {
        Expression::Integer(n)
    }

    fn pow(base: Expression, exp: Expression) -> Expression {
        Expression::Power(Box::new(base), Box::new(exp))
    }

    fn add(left: Expression, right: Expression) -> Expression {
        Expression::Binary(BinaryOp::Add, Box::new(left), Box::new(right))
    }

    fn mul(left: Expression, right: Expression) -> Expression {
        Expression::Binary(BinaryOp::Mul, Box::new(left), Box::new(right))
    }

    fn div(left: Expression, right: Expression) -> Expression {
        Expression::Binary(BinaryOp::Div, Box::new(left), Box::new(right))
    }

    #[test]
    fn test_integrate_constant() {
        // ∫5 dx = 5x
        let result = integrate(&int(5), "x").unwrap();
        // Result should be 5 * x
        assert!(matches!(
            result,
            Expression::Binary(BinaryOp::Mul, left, right)
            if matches!(left.as_ref(), Expression::Integer(5))
            && matches!(right.as_ref(), Expression::Variable(v) if v.name == "x")
        ));
    }

    #[test]
    fn test_integrate_x() {
        // ∫x dx = x^2/2
        let result = integrate(&var("x"), "x").unwrap();
        assert!(matches!(
            result,
            Expression::Binary(BinaryOp::Div, _num, denom)
            if matches!(denom.as_ref(), Expression::Integer(2))
        ));
    }

    #[test]
    fn test_integrate_x_squared() {
        // ∫x^2 dx = x^3/3
        let x_squared = pow(var("x"), int(2));
        let result = integrate(&x_squared, "x").unwrap();

        // Should be x^(2+1) / (2+1) = x^3/3
        if let Expression::Binary(BinaryOp::Div, num, denom) = result {
            // Numerator should be x^(2+1)
            assert!(matches!(num.as_ref(), Expression::Power(_, _)));
            // Denominator should be 2+1
            assert!(matches!(
                denom.as_ref(),
                Expression::Binary(BinaryOp::Add, _, _)
            ));
        } else {
            panic!("Expected division expression");
        }
    }

    #[test]
    fn test_integrate_sum() {
        // ∫(x^2 + x) dx = x^3/3 + x^2/2
        let expr = add(pow(var("x"), int(2)), var("x"));
        let result = integrate(&expr, "x").unwrap();
        assert!(matches!(result, Expression::Binary(BinaryOp::Add, _, _)));
    }

    #[test]
    fn test_integrate_constant_multiple() {
        // ∫3x dx = 3 * x^2/2
        let expr = mul(int(3), var("x"));
        let result = integrate(&expr, "x").unwrap();

        // Should be 3 * (x^2/2)
        assert!(matches!(result, Expression::Binary(BinaryOp::Mul, left, _)
            if matches!(left.as_ref(), Expression::Integer(3))));
    }

    #[test]
    fn test_integrate_reciprocal() {
        // ∫1/x dx = ln|x|
        let expr = div(int(1), var("x"));
        let result = integrate(&expr, "x").unwrap();

        // Should be 1 * ln(|x|)
        assert!(
            matches!(result, Expression::Binary(BinaryOp::Mul, _, ln_part)
            if matches!(ln_part.as_ref(), Expression::Function(Function::Ln, _)))
        );
    }

    #[test]
    fn test_integrate_sin() {
        // ∫sin(x) dx = -cos(x)
        let sin_x = Expression::Function(Function::Sin, vec![var("x")]);
        let result = integrate(&sin_x, "x").unwrap();

        assert!(matches!(
            result,
            Expression::Unary(UnaryOp::Neg, inner)
            if matches!(inner.as_ref(), Expression::Function(Function::Cos, _))
        ));
    }

    #[test]
    fn test_integrate_cos() {
        // ∫cos(x) dx = sin(x)
        let cos_x = Expression::Function(Function::Cos, vec![var("x")]);
        let result = integrate(&cos_x, "x").unwrap();

        assert!(matches!(
            result,
            Expression::Function(Function::Sin, args)
            if args.len() == 1
        ));
    }

    #[test]
    fn test_integrate_exp() {
        // ∫e^x dx = e^x
        let exp_x = Expression::Function(Function::Exp, vec![var("x")]);
        let result = integrate(&exp_x, "x").unwrap();

        assert!(matches!(
            result,
            Expression::Function(Function::Exp, args)
            if args.len() == 1
        ));
    }

    #[test]
    fn test_integrate_x_power_negative_one() {
        // ∫x^(-1) dx = ln|x|
        let expr = pow(var("x"), int(-1));
        let result = integrate(&expr, "x").unwrap();

        assert!(matches!(
            result,
            Expression::Function(Function::Ln, args)
            if matches!(&args[0], Expression::Function(Function::Abs, _))
        ));
    }

    #[test]
    fn test_integrate_polynomial() {
        // ∫(x^2 + 2x + 1) dx = x^3/3 + x^2 + x
        let poly = add(add(pow(var("x"), int(2)), mul(int(2), var("x"))), int(1));
        let result = integrate(&poly, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_linear_sin() {
        // ∫sin(2x) dx = -cos(2x)/2
        let two_x = mul(int(2), var("x"));
        let sin_2x = Expression::Function(Function::Sin, vec![two_x]);
        let result = integrate(&sin_2x, "x").unwrap();

        // Should be (-cos(2x)) / 2
        assert!(matches!(result, Expression::Binary(BinaryOp::Div, _, _)));
    }

    #[test]
    fn test_differentiate_integral_equals_original() {
        // Fundamental theorem: d/dx(∫f dx) = f
        // Test with x^2
        let x_squared = pow(var("x"), int(2));
        let integral = integrate(&x_squared, "x").unwrap();
        let _derivative = integral.differentiate("x").simplify();

        // The derivative should simplify to x^2 (or equivalent)
        // This is a partial test - full verification needs numerical checks
        // For now, just verify we get back a power expression
        // The actual result may be (3*x^2) / 3 which simplifies to x^2
    }

    // =========================================================================
    // U-Substitution Tests
    // =========================================================================

    #[test]
    fn test_extract_factors() {
        // Test basic factor extraction
        let expr = mul(int(2), var("x"));
        let factors = extract_factors(&expr);
        assert_eq!(factors.len(), 2);

        // Test nested product
        let expr2 = mul(mul(int(2), var("x")), int(3));
        let factors2 = extract_factors(&expr2);
        assert_eq!(factors2.len(), 3);
    }

    #[test]
    fn test_combine_factors() {
        let factors = vec![int(2), var("x"), int(3)];
        let combined = combine_factors(&factors);
        // Should produce 2 * x * 3
        assert!(matches!(combined, Expression::Binary(BinaryOp::Mul, _, _)));
    }

    #[test]
    fn test_extract_inner_function() {
        // Test function extraction
        let sin_x = Expression::Function(Function::Sin, vec![var("x")]);
        let inner = extract_inner_function(&sin_x);
        assert!(matches!(inner, Some(Expression::Variable(_))));

        // Test with x^2 inside
        let x_squared = pow(var("x"), int(2));
        let sin_x2 = Expression::Function(Function::Sin, vec![x_squared]);
        let inner2 = extract_inner_function(&sin_x2);
        assert!(inner2.is_some());
    }

    #[test]
    fn test_substitute_variable() {
        // Test variable substitution
        let u = Expression::Variable(Variable::new("u"));
        let replacement = pow(var("x"), int(2));
        let result = substitute_variable(&u, "u", &replacement);
        assert!(matches!(result, Expression::Power(_, _)));
    }

    #[test]
    fn test_integrate_by_substitution_linear() {
        // ∫sin(3x) dx = -cos(3x)/3
        // This is already handled by linear substitution in base integrate
        let three_x = mul(int(3), var("x"));
        let sin_3x = Expression::Function(Function::Sin, vec![three_x]);
        let result = integrate_by_substitution(&sin_3x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_by_substitution_exp() {
        // ∫e^(2x) dx = e^(2x)/2
        // This should be handled by linear substitution
        let two_x = mul(int(2), var("x"));
        let exp_2x = Expression::Function(Function::Exp, vec![two_x]);
        let result = integrate_by_substitution(&exp_2x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_with_substitution_steps() {
        // Test that we get steps back
        let x_squared = pow(var("x"), int(2));
        let (result, steps) = integrate_with_substitution(&x_squared, "x").unwrap();

        // Should have at least one step
        assert!(!steps.is_empty());
        // Result should be valid
        assert!(matches!(result, Expression::Binary(BinaryOp::Div, _, _)));
    }

    #[test]
    fn test_expressions_equivalent() {
        let a = var("x");
        let b = var("x");
        assert!(expressions_equivalent(&a, &b));

        let c = var("y");
        assert!(!expressions_equivalent(&a, &c));
    }

    #[test]
    fn test_expressions_equivalent_add_commutativity() {
        // x + y  ≡  y + x
        let xy = add(var("x"), var("y"));
        let yx = add(var("y"), var("x"));
        assert!(expressions_equivalent(&xy, &yx));

        // x + y  ≢  x + z
        let xz = add(var("x"), var("z"));
        assert!(!expressions_equivalent(&xy, &xz));
    }

    #[test]
    fn test_expressions_equivalent_mul_commutativity() {
        // 2 * x  ≡  x * 2
        let two_x = mul(int(2), var("x"));
        let x_two = mul(var("x"), int(2));
        assert!(expressions_equivalent(&two_x, &x_two));

        // 2 * x  ≢  3 * x
        let three_x = mul(int(3), var("x"));
        assert!(!expressions_equivalent(&two_x, &three_x));
    }

    #[test]
    fn test_expressions_equivalent_nested_commutativity() {
        // (x + x).simplify() = 2*x, and 2*x ≡ x*2 after canonical form
        let x_plus_x = add(var("x"), var("x")).simplify();
        let x_times_2 = mul(var("x"), int(2));
        assert!(expressions_equivalent(&x_plus_x, &x_times_2));
    }

    #[test]
    fn test_is_one() {
        assert!(is_one(&Expression::Integer(1)));
        assert!(!is_one(&Expression::Integer(2)));
        assert!(!is_one(&var("x")));
    }

    // =========================================================================
    // Integration by Parts Tests
    // =========================================================================

    #[test]
    fn test_liate_priority() {
        // Logarithmic has highest priority
        let ln_x = Expression::Function(Function::Ln, vec![var("x")]);
        assert!(liate_priority(&ln_x, "x") > liate_priority(&var("x"), "x"));

        // Algebraic (x) has higher priority than trigonometric
        let sin_x = Expression::Function(Function::Sin, vec![var("x")]);
        assert!(liate_priority(&var("x"), "x") > liate_priority(&sin_x, "x"));

        // Trigonometric has higher priority than exponential
        let exp_x = Expression::Function(Function::Exp, vec![var("x")]);
        assert!(liate_priority(&sin_x, "x") > liate_priority(&exp_x, "x"));
    }

    #[test]
    fn test_is_polynomial_like() {
        // Basic polynomials
        assert!(is_polynomial_like(&var("x"), "x"));
        assert!(is_polynomial_like(&pow(var("x"), int(2)), "x"));
        assert!(is_polynomial_like(
            &add(pow(var("x"), int(2)), var("x")),
            "x"
        ));

        // Constants are polynomial
        assert!(is_polynomial_like(&int(5), "x"));

        // Non-polynomials
        let sin_x = Expression::Function(Function::Sin, vec![var("x")]);
        assert!(!is_polynomial_like(&sin_x, "x"));
    }

    #[test]
    fn test_integrate_by_parts_x_exp() {
        // ∫x * e^x dx = (x - 1) * e^x
        // Using parts: u = x, dv = e^x dx
        //              du = dx, v = e^x
        //              = x*e^x - ∫e^x dx = x*e^x - e^x = (x-1)*e^x
        let x = var("x");
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);
        let expr = mul(x.clone(), exp_x.clone());

        let result = integrate_by_parts(&expr, "x");
        assert!(result.is_ok());

        // Verify by differentiation
        if let Ok(integral) = result {
            let derivative = integral.differentiate("x").simplify();
            // The derivative should equal the original expression
            // We can't easily compare symbolically, but at least check it's not an error
            assert!(!matches!(derivative, Expression::Integer(0)));
        }
    }

    #[test]
    fn test_integrate_by_parts_ln_x() {
        // ∫ln(x) dx = x*ln(x) - x
        // Using parts: u = ln(x), dv = dx
        //              du = 1/x dx, v = x
        //              = x*ln(x) - ∫x * (1/x) dx = x*ln(x) - ∫1 dx = x*ln(x) - x
        let ln_x = Expression::Function(Function::Ln, vec![var("x")]);

        // This should be handled by the standard integral table, not parts
        let result = integrate(&ln_x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_by_parts_x_sin() {
        // ∫x * sin(x) dx = -x*cos(x) + sin(x)
        // Using parts: u = x, dv = sin(x) dx
        //              du = dx, v = -cos(x)
        //              = -x*cos(x) - ∫(-cos(x)) dx = -x*cos(x) + sin(x)
        let x = var("x");
        let sin_x = Expression::Function(Function::Sin, vec![x.clone()]);
        let expr = mul(x.clone(), sin_x.clone());

        let result = integrate_by_parts(&expr, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_by_parts_x_squared_exp() {
        // ∫x^2 * e^x dx = (x^2 - 2x + 2) * e^x
        // Requires two applications of integration by parts
        let x = var("x");
        let x_squared = pow(x.clone(), int(2));
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);
        let expr = mul(x_squared.clone(), exp_x.clone());

        let result = integrate_by_parts(&expr, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_tabular_integration_x_exp() {
        // ∫x * e^x dx using tabular method
        let x = var("x");
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);

        let result = tabular_integration(&x, &exp_x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_tabular_integration_x_squared_exp() {
        // ∫x^2 * e^x dx using tabular method
        // Derivatives: x^2 -> 2x -> 2 -> 0
        // Integrals: e^x -> e^x -> e^x -> e^x
        // Result: x^2*e^x - 2x*e^x + 2*e^x
        let x = var("x");
        let x_squared = pow(x.clone(), int(2));
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);

        let result = tabular_integration(&x_squared, &exp_x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_tabular_integration_x_sin() {
        // ∫x * sin(x) dx using tabular method
        let x = var("x");
        let sin_x = Expression::Function(Function::Sin, vec![x.clone()]);

        let result = tabular_integration(&x, &sin_x, "x");
        assert!(result.is_ok());
    }

    #[test]
    fn test_integrate_by_parts_with_steps() {
        // Test that we get detailed steps
        let x = var("x");
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);
        let expr = mul(x.clone(), exp_x.clone());

        let result = integrate_by_parts_with_steps(&expr, "x");
        assert!(result.is_ok());

        if let Ok((_, steps)) = result {
            // Should have multiple steps
            assert!(steps.len() >= 5);
            // Should mention "integration by parts"
            assert!(steps.iter().any(|s| s.contains("integration by parts")));
        }
    }

    #[test]
    fn test_choose_u_and_dv() {
        // For x * e^x, x should be chosen as u (algebraic > exponential)
        let x = var("x");
        let exp_x = Expression::Function(Function::Exp, vec![x.clone()]);
        let factors = vec![x.clone(), exp_x.clone()];

        let (u, dv) = choose_u_and_dv(&factors, "x");

        // u should be x (algebraic priority 3)
        assert!(matches!(u, Expression::Variable(_)));
        // dv should be e^x (exponential priority 1)
        assert!(matches!(dv, Expression::Function(Function::Exp, _)));
    }

    #[test]
    fn test_check_same_up_to_constant() {
        let a = var("x");
        let b = var("x");

        // Same expression -> constant 1
        assert!(check_same_up_to_constant(&a, &b, "x").is_some());

        // Negation -> constant -1
        let neg_a = Expression::Unary(UnaryOp::Neg, Box::new(a.clone()));
        let result = check_same_up_to_constant(&a, &neg_a, "x");
        assert!(result.is_some());
        assert!(matches!(result, Some(Expression::Integer(-1))));

        // Different expressions
        let c = var("y");
        assert!(check_same_up_to_constant(&a, &c, "x").is_none());
    }

    // =========================================================================
    // Definite Integral Tests
    // =========================================================================

    #[test]
    fn test_definite_integral_x_squared() {
        // ∫_0^1 x^2 dx = 1/3
        let x_squared = pow(var("x"), int(2));
        let result = definite_integral(&x_squared, "x", &int(0), &int(1));
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 1.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_sin() {
        // ∫_0^π sin(x) dx = 2
        let sin_x = Expression::Function(Function::Sin, vec![var("x")]);
        let pi = Expression::Constant(crate::ast::SymbolicConstant::Pi);
        let result = definite_integral(&sin_x, "x", &int(0), &pi);
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_odd_function() {
        // ∫_{-1}^1 x^3 dx = 0 (odd function, symmetric interval)
        let x_cubed = pow(var("x"), int(3));
        let result = definite_integral(&x_cubed, "x", &int(-1), &int(1));
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!(numeric.abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_symbolic_upper_bound() {
        // ∫_0^a x dx = a^2/2
        let x = var("x");
        let a = var("a");
        let result = definite_integral(&x, "x", &int(0), &a);
        assert!(result.is_ok());

        // Evaluate at a=2 to verify: should be 2
        let value = result.unwrap();
        let mut env = std::collections::HashMap::new();
        env.insert("a".to_string(), 2.0);
        let numeric = value.evaluate(&env).unwrap();
        assert!((numeric - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_polynomial() {
        // ∫_0^2 (3x^2 + 2x + 1) dx = [x^3 + x^2 + x]_0^2 = 8 + 4 + 2 = 14
        let x = var("x");
        let poly = add(
            add(mul(int(3), pow(x.clone(), int(2))), mul(int(2), x.clone())),
            int(1),
        );
        let result = definite_integral(&poly, "x", &int(0), &int(2));
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 14.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_cos() {
        // ∫_0^{π/2} cos(x) dx = 1
        let cos_x = Expression::Function(Function::Cos, vec![var("x")]);
        let pi = Expression::Constant(crate::ast::SymbolicConstant::Pi);
        let upper = div(pi, int(2));
        let result = definite_integral(&cos_x, "x", &int(0), &upper);
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_exp() {
        // ∫_0^1 e^x dx = e - 1 ≈ 1.71828
        let exp_x = Expression::Function(Function::Exp, vec![var("x")]);
        let result = definite_integral(&exp_x, "x", &int(0), &int(1));
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        let expected = std::f64::consts::E - 1.0;
        assert!((numeric - expected).abs() < 1e-10);
    }

    #[test]
    fn test_numerical_integrate_simple() {
        // Use adaptive Simpson's for ∫_0^1 x^2 dx = 1/3
        let x_squared = pow(var("x"), int(2));
        let result = numerical_integrate(&x_squared, "x", 0.0, 1.0, 1e-8);
        assert!(result.is_ok());

        let value = result.unwrap();
        assert!((value - 1.0 / 3.0).abs() < 1e-6);
    }

    #[test]
    fn test_numerical_integrate_complex() {
        // ∫_0^1 exp(-x^2) dx ≈ 0.74682 (error function related)
        let x = var("x");
        let neg_x_squared = Expression::Unary(UnaryOp::Neg, Box::new(pow(x.clone(), int(2))));
        let exp_neg_x_squared = Expression::Function(Function::Exp, vec![neg_x_squared]);
        let result = numerical_integrate(&exp_neg_x_squared, "x", 0.0, 1.0, 1e-6);
        assert!(result.is_ok());

        let value = result.unwrap();
        assert!((value - 0.74682).abs() < 0.001);
    }

    #[test]
    fn test_definite_integral_with_fallback() {
        // Use fallback for a simple integral
        let x_squared = pow(var("x"), int(2));
        let result = definite_integral_with_fallback(&x_squared, "x", 0.0, 1.0, 1e-8);
        assert!(result.is_ok());

        let value = result.unwrap();
        assert!((value - 1.0 / 3.0).abs() < 1e-6);
    }

    #[test]
    fn test_improper_integral_convergent() {
        // ∫_1^∞ x^(-2) dx = 1
        // Use x^(-2) format which integrate handles better than 1/x^2
        let x = var("x");
        let x_neg_2 = pow(x.clone(), int(-2));
        let result = improper_integral_to_infinity(&x_neg_2, "x", &int(1));
        assert!(result.is_ok());

        let value = result.unwrap();
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_definite_integral_with_steps() {
        // Verify we get step-by-step output
        let x_squared = pow(var("x"), int(2));
        let result = definite_integral_with_steps(&x_squared, "x", &int(0), &int(1));
        assert!(result.is_ok());

        let (value, steps) = result.unwrap();
        // Should have multiple steps
        assert!(!steps.is_empty());
        // Should mention "antiderivative" or "bounds"
        assert!(steps.iter().any(|s| {
            s.to_lowercase().contains("antiderivative") || s.to_lowercase().contains("bound")
        }));

        // Verify result
        let empty = std::collections::HashMap::new();
        let numeric = value.evaluate(&empty).unwrap();
        assert!((numeric - 1.0 / 3.0).abs() < 1e-10);
    }
}