alkahest-cas 3.3.0

High-performance computer algebra kernel: symbolic expressions, polynomials, Gröbner bases, JIT, and Arb ball arithmetic.
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
//! Transcendental Risch symbolic integration.
//!
//! This module implements the **complete decision procedure** for symbolic
//! integration of transcendental elementary functions, deciding whether an
//! antiderivative is expressible in elementary terms and computing it when it is.
//!
//! ## Supported classes
//!
//! | Class | Examples | Status |
//! |-------|---------|--------|
//! | `exp(g)`, `g` polynomial deg ≥ 2 | `exp(x²)`, `x·exp(x²)` | ✓ |
//! | `poly(x)·exp(linear)`, deg ≥ 2 | `x²·exp(x)`, `x³·exp(2x)` | ✓ |
//! | `log(h)^n`, n ≥ 2 | `log(x)²`, `log(x)³` | ✓ |
//! | `poly(x)·log(h)` | `x·log(x)`, `x²·log(x)` | ✓ |
//! | Mixed exp + rational base | `x·exp(x²) + x²` | ✓ |
//! | `ratfn(x)·exp(η)`, η polynomial | `(x−1)/x²·exp(x)` | ✓ (rational RDE) |
//! | `A(x)/D(x)`, D splits over ℚ | `1/(x²−1)`, `x/(x−1)³` | ✓ (Hermite + Rothstein–Trager) |
//! | `A(x)/D(x)`, irreducible quadratics | `1/(x²+1)`, `1/(x²−2)` | ✓ (log + arctan / √-log) |
//! | `A(x)/D(x)`, irreducible deg ≥ 3 | `1/(x³−3x+1)` | ✓ (`RootSum`, Lazard–Rioboo–Trager) |
//! | `ratfn(x)·exp(η)`, η rational | `(1/x²)·exp(1/x)` | ✓ (generalised RDE, Gap F) |
//! | `(c₀(x)+c₁(x)√p(x))·exp(η)`, η polynomial | `√x·exp(x)` | ✗ NonElementary / ✓ elementary |
//! | `log(h)/√p(x)` via log tower | `log(x)/√x` | ✓ (lower-tower delegation, Gap C) |
//! | `√p(x)·log(h)`, p ∈ ℚ\[x\] | `√x·log(x)` | ✓ (algebraic base-field, Gap C) |
//! | `c(x,exp(x))·exp(exp(x))`, c poly in exp(x) | `exp(x)²·exp(exp(x))` | ✓ (lower-tower cascade, Gap B) |
//! | `c(x)·exp(exp(x))`, c ∈ ℚ(x) | `x·exp(exp(x))` | ✗ NonElementary (degree bound) |
//! | `1/(x+α)^n·log(x+α)`, α ∈ ℚ(√d) | `1/(x+√2)²·log(x+√2)` | ✓ (K-rational base, Gap E) |
//! | `sin(x)/x`, `exp(x)/x` | Ei, Si functions | ✗ (NonElementary) |
//! | `exp(1/x)` alone | essential singularity | ✗ (NonElementary) |
//!
//! ## Architecture
//!
//! The Risch algorithm is split into sub-modules:
//!
//! - [`tower`]: Differential field tower construction and generator detection.
//! - [`poly_rde`]: Polynomial Risch Differential Equation (RDE) solver over ℚ\[x\].
//! - [`rational_rde`]: Rational RDE solver over ℚ(x) (exp tower; Bronstein §6.1).
//!   Also contains [`rational_rde::solve_rational_rde_generalized`] for the
//!   rational-exponent case `f = k·η' ∈ ℚ(x)` (Gap F, Bronstein §5.4).
//! - [`number_field`]: Generic polynomial-quotient core ([`number_field::CoeffField`],
//!   [`number_field::Quotient`]) plus the algebraic number field `ℚ[t]/(q)`
//!   ([`number_field::NumberField`], used for degree-≥3 algebraic residues and
//!   ℚ(√d) coefficients in the exp tower).
//! - [`alg_field`]: `x`-dependent algebraic *function* field `ℚ(x)[y]/(q(x,y))`
//!   with a derivation `D(y) = −q_x/q_y` (Risch M0; substrate for mixed
//!   algebraic + transcendental integration).
//! - [`rational_integrate`]: Rational-function integration via Rothstein–Trager
//!   (logarithmic part; Bronstein §2.5).
//! - [`exp_case`]: Integration in the hyperexponential tower (t = exp(η)), both
//!   polynomial η (Bronstein §5.2–5.3) and rational η (Gap F, §5.4).
//! - [`log_case`]: Integration in the hyperlogarithmic tower (t = log(h)).
//!
//! ## Remaining limitations
//!
//! The implementation is substantially complete for single-variable transcendental
//! integration.  Known remaining gaps:
//!
//! - **Algebraic × exp: degree ≥ 3 only.** `try_sqrt_poly_rde` (in `exp_case`)
//!   handles quadratic algebraic coefficients (`√p(x)·exp(η)`).  Higher-degree
//!   algebraic extensions (e.g. `∛(p(x))·exp(η)`) are not yet supported.
//! - **Nested exp towers — complete for ℚ(x)(exp(x)).**  The lower-tower cascade
//!   handles polynomial c(x, exp(x)).  Rational c (denominator in exp(x)) is
//!   certified NonElementary by the Hermite/pole-order argument: `D` maps any
//!   simple pole `1/(θ-α)` (α ∈ ℚ(x)) to a double pole `(α-Dα)/(θ-α)²`;
//!   since `Dα ≠ α` for α ∈ ℚ(x), no rational solution to the Risch DE exists.
//! - **Log tower: K-rational base field, Hermite-reducible cases only.**
//!   `integrate_base` now tries K-rational antidifferentiation (Gap E) via
//!   `solve_rational_rde_k` with f=0.  This handles coefficients in ℚ(√d)(x) whose
//!   antiderivative is itself K-rational, e.g. `1/(x+√2)^n` (Hermite step).
//!   Coefficients requiring new log generators (e.g. `1/(x+√2)`) still return
//!   `NotImplemented`; those are non-elementary (polylog) in the log-tower context.
//!
//! ## References
//!
//! - Risch (1969). The problem of integration in finite terms. *Trans. AMS* 139, 167–189.
//! - Bronstein (2005). *Symbolic Integration I: Transcendental Functions*. Springer.
//! - Geddes, Czapor, Labahn (1992). *Algorithms for Computer Algebra*. Kluwer.

pub mod alg_field;
pub mod alg_rde;
pub mod diff_field;
pub mod exp_case;
pub mod log_case;
pub mod number_field;
pub mod poly_rde;
pub mod radical_ext;
pub mod rational_integrate;
pub mod rational_rde;
pub mod simple_radical;
pub mod tower;
pub mod tower_field;
pub mod tower_integrate;

use crate::deriv::log::{DerivationLog, DerivedExpr};
use crate::integrate::engine::IntegrationError;
use crate::kernel::{ExprId, ExprPool};
use crate::simplify::engine::simplify;

use exp_case::{integrate_exp_tower, needs_exp_risch};
use log_case::{integrate_log_tower, needs_log_risch};
use tower::find_generators;
use tower::TowerLevel;

// ---------------------------------------------------------------------------
// Generator selection helpers
// ---------------------------------------------------------------------------

/// Among a list of exp generators, find the "outermost" one in the tower sense:
/// the generator G such that G itself does not appear as a sub-expression of
/// any other generator's argument.
///
/// For a nested tower like `[exp(x), exp(exp(x))]`:
/// - `exp(x)` appears in `exp(exp(x)).argument()` → it is *inner*
/// - `exp(exp(x))` does not appear anywhere else → it is *outer*
///
/// Falls back to the first element when no clear outer generator is found
/// (e.g. two independent exp generators like `exp(x)` and `exp(x²)`).
fn outermost_exp_generator<'a>(exp_gens: &[&'a TowerLevel], pool: &ExprPool) -> &'a TowerLevel {
    use poly_rde::contains_subexpr;
    for &g in exp_gens.iter() {
        let is_inner = exp_gens.iter().any(|&other| {
            other.generator != g.generator && contains_subexpr(other.argument(), g.generator, pool)
        });
        if !is_inner {
            return g;
        }
    }
    exp_gens[0]
}

// ---------------------------------------------------------------------------
// Public detection predicate
// ---------------------------------------------------------------------------

/// Returns `true` if `expr` requires the Risch transcendental engine.
///
/// Specifically, returns `true` for cases that the basic rule-based engine cannot
/// handle:
/// - `exp(g)` where `g` has polynomial degree ≥ 2 in `var`
/// - `p(x)·exp(linear)` where `p` has degree ≥ 2
/// - `log(h)^n` for `n ≥ 2`
/// - `p(x)·log(h)` for non-constant polynomial `p`
pub fn contains_risch_form(expr: ExprId, var: ExprId, pool: &ExprPool) -> bool {
    needs_exp_risch(expr, var, pool)
        || needs_log_risch(expr, var, pool)
        // A radical whose radicand involves a transcendental (e.g. ∛(x+log x))
        // is a Risch/MD form even when the transcendental appears only inside
        // the radicand — route it to the tower integrator, not the algebraic one.
        || tower_integrate::detect_radical_with_transcendental_radicand(expr, var, pool).is_some()
}

// ---------------------------------------------------------------------------
// Public integration entry point
// ---------------------------------------------------------------------------

/// Symbolically integrate `expr` with respect to `var` using the transcendental
/// Risch algorithm.
///
/// # Returns
/// - `Ok(DerivedExpr)` — the antiderivative (without constant of integration).
/// - `Err(NonElementary(...))` — a certified non-elementary integrand.
/// - `Err(NotImplemented(...))` — the integrand is outside the supported Risch
///   subset (e.g., mixed algebraic+transcendental, rational functions in the
///   tower, or multiple interacting generators).
///
/// # Algorithm
///
/// 1. Detect all transcendental generators (exp/log) in `expr`.
/// 2. If there is exactly one exp generator: apply the exp-tower Risch algorithm.
/// 3. If there is exactly one log generator: apply the log-tower IBP reduction.
/// 4. Multiple generators of the same kind: take the outermost (first in
///    depth-first order) and recurse; the base-field integration handles the
///    inner generators.
/// 5. Mixed exp+log: route to exp tower; the log generator lives in the base
///    field and is handled by the poly-in-log RDE or lower-tower recursion.
pub fn integrate_risch(
    expr: ExprId,
    var: ExprId,
    pool: &ExprPool,
) -> Result<DerivedExpr<ExprId>, IntegrationError> {
    // MD: a radical whose radicand involves the transcendental (e.g. ∛(x+eˣ) or
    // ∛(x+log x)).  The radical is the outermost generator; handle it before the
    // exp/log dispatch (which would mis-treat the radical as a coefficient).
    // Returns `None` when not of this shape, so ordinary towers fall through.
    if let Some(result) =
        tower_integrate::try_integrate_radical_over_transcendental(expr, var, pool)
    {
        return result;
    }

    // M4 multi-generator (PR2): ∫ exp(η)·(radical over a log/exp tower) dx.
    // The integrand mixes an outer transcendental coefficient exp(η) with a
    // radical over a *separate* tower; solved by descending one tower level via
    // `DifferentialField::rational_rde` per radical component, numeric-gated.
    // Additive: returns `None` for everything the single-generator path or the
    // ordinary towers already cover, so it never changes existing behavior.
    if let Some(result) =
        tower_integrate::try_integrate_exp_times_radical_over_tower(expr, var, pool)
    {
        return result;
    }

    let generators = find_generators(expr, var, pool);

    // Classify the generators.
    let exp_gens: Vec<_> = generators.iter().filter(|g| g.is_exp()).collect();
    let log_gens: Vec<_> = generators.iter().filter(|g| g.is_log()).collect();

    let mut log = DerivationLog::new();

    // -----------------------------------------------------------------------
    // Case 1: Single exp generator, no log generators.
    // -----------------------------------------------------------------------
    if exp_gens.len() == 1 && log_gens.is_empty() {
        let level = exp_gens[0];
        let result = integrate_exp_tower(expr, level, var, pool, &mut log)?;
        let final_simplified = simplify(result, pool);
        let merged = log.merge(final_simplified.log);
        return Ok(DerivedExpr::with_log(final_simplified.value, merged));
    }

    // -----------------------------------------------------------------------
    // Case 2: Single log generator, no exp generators.
    // -----------------------------------------------------------------------
    if log_gens.len() == 1 && exp_gens.is_empty() {
        let level = log_gens[0];
        let result = integrate_log_tower(expr, level, var, pool, &mut log)?;
        let final_simplified = simplify(result, pool);
        let merged = log.merge(final_simplified.log);
        return Ok(DerivedExpr::with_log(final_simplified.value, merged));
    }

    // -----------------------------------------------------------------------
    // Case 3: Multiple log generators, no exp generators.
    //
    // `find_generators` visits in depth-first order, so the *outermost*
    // generator (highest tower level) appears first.  For example, for
    // log(log(x))/x the list is [log(log(x)), log(x)] and we integrate at
    // the log(log(x)) level; log(x) is handled recursively by integrate_base.
    // -----------------------------------------------------------------------
    if !log_gens.is_empty() && exp_gens.is_empty() {
        let level = log_gens[0]; // outermost log generator
        let result = integrate_log_tower(expr, level, var, pool, &mut log)?;
        let final_simplified = simplify(result, pool);
        let merged = log.merge(final_simplified.log);
        return Ok(DerivedExpr::with_log(final_simplified.value, merged));
    }

    // -----------------------------------------------------------------------
    // Case 4: Multiple exp generators, no log generators.
    //
    // Use the *outermost* exp generator — the one that is not an argument of
    // any other exp generator in the list.  For nested towers like
    // `exp(x)·exp(exp(x))`, DFS may visit the inner generator first, so we
    // must search explicitly rather than relying on list order.
    // -----------------------------------------------------------------------
    if !exp_gens.is_empty() && log_gens.is_empty() {
        let level = outermost_exp_generator(&exp_gens, pool);
        let result = integrate_exp_tower(expr, level, var, pool, &mut log)?;
        let final_simplified = simplify(result, pool);
        let merged = log.merge(final_simplified.log);
        return Ok(DerivedExpr::with_log(final_simplified.value, merged));
    }

    // -----------------------------------------------------------------------
    // Case 5: Mixed exp + log generators.
    //
    // Route to the exp tower: the log generator lives in the base field k and
    // is handled by the poly-in-log RDE (Bronstein §5.9 / IntegrateHyperexp).
    // If the exp tower returns NotImplemented (e.g. exp has a transcendental
    // exponent), fall through to sum decomposition.
    // -----------------------------------------------------------------------
    if !exp_gens.is_empty() && !log_gens.is_empty() {
        let level = outermost_exp_generator(&exp_gens, pool);
        match integrate_exp_tower(expr, level, var, pool, &mut log) {
            Ok(result) => {
                let final_simplified = simplify(result, pool);
                let merged = log.merge(final_simplified.log);
                return Ok(DerivedExpr::with_log(final_simplified.value, merged));
            }
            Err(IntegrationError::NonElementary(msg)) => {
                return Err(IntegrationError::NonElementary(msg));
            }
            Err(IntegrationError::DivisionByZero) => {
                return Err(IntegrationError::DivisionByZero);
            }
            Err(IntegrationError::UnsupportedExtensionDegree(d)) => {
                return Err(IntegrationError::UnsupportedExtensionDegree(d));
            }
            Err(IntegrationError::NotImplemented(_)) => {
                // Fall through to sum decomposition below.
            }
        }
    }

    // -----------------------------------------------------------------------
    // Case 6: Try sum decomposition for independent generators.
    // e.g., exp(x²) + log(x)² — each term has only one generator.
    // -----------------------------------------------------------------------
    if !generators.is_empty() {
        if let Some(result) = try_decompose_by_sum(expr, var, pool, &mut log) {
            let final_simplified = simplify(result, pool);
            let merged = log.merge(final_simplified.log);
            return Ok(DerivedExpr::with_log(final_simplified.value, merged));
        }
    }

    // Fall through: outside the supported tower subsets.
    let gen_names: Vec<String> = generators
        .iter()
        .map(|g| pool.display(g.generator).to_string())
        .collect();
    Err(IntegrationError::NotImplemented(format!(
        "Risch: generators {:?} involve interactions not yet supported \
         (Bronstein 2005, §5.8–5.9, §8)",
        gen_names
    )))
}

// ---------------------------------------------------------------------------
// Sum decomposition for independent generators
// ---------------------------------------------------------------------------

/// Try to integrate `expr` by treating it as a sum and integrating each
/// term independently.  Returns `None` if any term fails.
fn try_decompose_by_sum(
    expr: ExprId,
    var: ExprId,
    pool: &ExprPool,
    log: &mut DerivationLog,
) -> Option<ExprId> {
    use crate::kernel::ExprData;

    let args = match pool.get(expr) {
        ExprData::Add(args) => args,
        _ => return None,
    };

    let zero = pool.integer(0_i32);
    let mut result_terms: Vec<ExprId> = Vec::new();

    for &term in &args {
        // Try Risch first; fall back to the rule-based engine.
        let int_term = if contains_risch_form(term, var, pool) {
            match integrate_risch(term, var, pool) {
                Ok(d) => {
                    *log = std::mem::take(log).merge(d.log);
                    d.value
                }
                Err(_) => return None,
            }
        } else {
            let mut inner_log = DerivationLog::new();
            match crate::integrate::engine::integrate_raw(term, var, pool, &mut inner_log) {
                Ok(r) => {
                    *log = std::mem::take(log).merge(inner_log);
                    r
                }
                Err(_) => return None,
            }
        };
        result_terms.push(int_term);
    }

    Some(match result_terms.len() {
        0 => zero,
        1 => result_terms[0],
        _ => pool.add(result_terms),
    })
}

// ---------------------------------------------------------------------------
// Integration tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernel::{Domain, ExprPool};

    fn p() -> ExprPool {
        ExprPool::new()
    }

    // -----------------------------------------------------------------------
    // Non-elementary integrals (must return NonElementary)
    // -----------------------------------------------------------------------

    #[test]
    fn exp_x2_nonelementary() {
        // ∫ exp(x²) dx — the canonical non-elementary example
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let f = pool.func("exp", vec![x2]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(x²) dx should be NonElementary; got: {result:?}"
        );
    }

    #[test]
    fn exp_neg_x2_nonelementary() {
        // ∫ exp(−x²) dx — Gaussian integral, non-elementary
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let neg_x2 = pool.mul(vec![pool.integer(-1_i32), pool.pow(x, pool.integer(2_i32))]);
        let f = pool.func("exp", vec![neg_x2]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(−x²) dx should be NonElementary; got: {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Elementary integrals: exp tower
    // -----------------------------------------------------------------------

    #[test]
    fn x_times_exp_x2_elementary() {
        // ∫ x·exp(x²) dx = ½·exp(x²)
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let f = pool.mul(vec![x, pool.func("exp", vec![x2])]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ x·exp(x²) dx should be elementary; got: {result:?}"
        );

        // Verify by differentiation: d/dx F = f.
        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "x·exp(x²)");
    }

    #[test]
    fn two_x_exp_x2_elementary() {
        // ∫ 2x·exp(x²) dx = exp(x²)
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let exp_x2 = pool.func("exp", vec![x2]);
        let f = pool.mul(vec![pool.integer(2_i32), x, exp_x2]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ 2x·exp(x²) dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "2x·exp(x²)");
    }

    #[test]
    fn poly_times_exp_x2_elementary() {
        // ∫ (2x²+1)·exp(x²) dx = x·exp(x²)
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let exp_x2 = pool.func("exp", vec![x2]);
        let two_x2_plus_1 = pool.add(vec![
            pool.mul(vec![pool.integer(2_i32), pool.pow(x, pool.integer(2_i32))]),
            pool.integer(1_i32),
        ]);
        let f = pool.mul(vec![two_x2_plus_1, exp_x2]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (2x²+1)·exp(x²) dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "(2x²+1)·exp(x²)");
    }

    #[test]
    fn x2_times_exp_x_elementary() {
        // ∫ x²·exp(x) dx = (x²−2x+2)·exp(x)
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let exp_x = pool.func("exp", vec![x]);
        let f = pool.mul(vec![x2, exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ x²·exp(x) dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "x²·exp(x)");
    }

    #[test]
    fn x3_times_exp_x_elementary() {
        // ∫ x³·exp(x) dx = (x³ − 3x² + 6x − 6)·exp(x)
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x3 = pool.pow(x, pool.integer(3_i32));
        let exp_x = pool.func("exp", vec![x]);
        let f = pool.mul(vec![x3, exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ x³·exp(x) dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "x³·exp(x)");
    }

    // -----------------------------------------------------------------------
    // Elementary integrals: log tower
    // -----------------------------------------------------------------------

    #[test]
    fn log_x_squared_elementary() {
        // ∫ log(x)² dx = x·log(x)² − 2x·log(x) + 2x
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let f = pool.pow(log_x, pool.integer(2_i32));

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ log(x)² dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "log(x)²");
    }

    #[test]
    fn x_times_log_x_elementary() {
        // ∫ x·log(x) dx = (x²/2)·log(x) − x²/4
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let f = pool.mul(vec![x, log_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ x·log(x) dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "x·log(x)");
    }

    #[test]
    fn log_x_cubed_elementary() {
        // ∫ log(x)³ dx = x·log(x)³ − 3x·log(x)² + 6x·log(x) − 6x
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let f = pool.pow(log_x, pool.integer(3_i32));

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ log(x)³ dx should be elementary; got: {result:?}"
        );

        let antideriv = result.unwrap().value;
        verify_antiderivative(&pool, x, f, antideriv, "log(x)³");
    }

    // -----------------------------------------------------------------------
    // Rational coefficients in the exp tower (Gap 1: rational Risch DE)
    // -----------------------------------------------------------------------

    #[test]
    fn rational_coeff_exp_elementary() {
        // ∫ (x−1)/x² · exp(x) dx = exp(x)/x.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let num = pool.add(vec![x, pool.integer(-1_i32)]); // x − 1
        let inv_x2 = pool.pow(x, pool.integer(-2_i32)); // x⁻²
        let f = pool.mul(vec![num, inv_x2, exp_x]);

        // Must be routed to Risch and solved as elementary.
        assert!(contains_risch_form(f, x, &pool), "should route to Risch");
        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (x−1)/x²·exp(x) dx should be elementary; got {result:?}"
        );

        // Verify d/dx F = f numerically at several points (the simplifier does not
        // fully normalise rational sums, so a symbolic-zero check is too strict).
        let antideriv = result.unwrap().value;
        let d = crate::diff::diff(antideriv, x, &pool).unwrap();
        for &xv in &[1.3_f64, 2.7, 4.1] {
            let lhs = eval_f64(d.value, x, xv, &pool);
            let rhs = eval_f64(f, x, xv, &pool);
            assert!(
                (lhs - rhs).abs() < 1e-9,
                "d/dx F ≠ f at x={xv}: {lhs} vs {rhs}"
            );
        }
    }

    /// Minimal numeric evaluator for verification (Integer/Rational/Add/Mul/Pow/exp).
    fn eval_f64(expr: ExprId, x: ExprId, xv: f64, pool: &ExprPool) -> f64 {
        use crate::kernel::ExprData;
        if expr == x {
            return xv;
        }
        match pool.get(expr) {
            ExprData::Integer(n) => n.0.to_f64(),
            ExprData::Rational(r) => r.0.to_f64(),
            ExprData::Add(args) => args.iter().map(|&a| eval_f64(a, x, xv, pool)).sum(),
            ExprData::Mul(args) => args.iter().map(|&a| eval_f64(a, x, xv, pool)).product(),
            ExprData::Pow { base, exp } => {
                eval_f64(base, x, xv, pool).powf(eval_f64(exp, x, xv, pool))
            }
            ExprData::Func { ref name, ref args } if name == "exp" && args.len() == 1 => {
                eval_f64(args[0], x, xv, pool).exp()
            }
            other => panic!("eval_f64: unsupported node {other:?}"),
        }
    }

    #[test]
    fn rational_coeff_exp_nonelementary() {
        // ∫ x²/(x+1) · exp(x) dx leaves an Ei term — non-elementary.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let inv_xp1 = pool.pow(pool.add(vec![x, pool.integer(1_i32)]), pool.integer(-1_i32));
        let f = pool.mul(vec![x2, inv_xp1, exp_x]);

        assert!(contains_risch_form(f, x, &pool), "should route to Risch");
        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ x²/(x+1)·exp(x) dx should be NonElementary; got {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Mixed sums (independent generators)
    // -----------------------------------------------------------------------

    #[test]
    fn sum_exp_x2_and_x() {
        // ∫ (x·exp(x²) + x) dx — exp part is elementary, rational part is elementary
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let exp_x2 = pool.func("exp", vec![x2]);
        let f = pool.add(vec![pool.mul(vec![x, exp_x2]), x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (x·exp(x²) + x) dx should be elementary; got: {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Detection predicate
    // -----------------------------------------------------------------------

    #[test]
    fn detection_predicate() {
        let pool = p();
        let x = pool.symbol("x", Domain::Real);

        // exp(x²) needs Risch
        let exp_x2 = pool.func("exp", vec![pool.pow(x, pool.integer(2_i32))]);
        assert!(contains_risch_form(exp_x2, x, &pool));

        // exp(x) does NOT need Risch (basic engine)
        let exp_x = pool.func("exp", vec![x]);
        assert!(!contains_risch_form(exp_x, x, &pool));

        // log(x)² needs Risch
        let log_x = pool.func("log", vec![x]);
        let log2 = pool.pow(log_x, pool.integer(2_i32));
        assert!(contains_risch_form(log2, x, &pool));

        // log(x) alone does NOT need Risch (basic engine)
        assert!(!contains_risch_form(log_x, x, &pool));

        // x²·exp(x) needs Risch
        let x2_exp_x = pool.mul(vec![pool.pow(x, pool.integer(2_i32)), exp_x]);
        assert!(contains_risch_form(x2_exp_x, x, &pool));

        // x·log(x) needs Risch
        let x_log_x = pool.mul(vec![x, log_x]);
        assert!(contains_risch_form(x_log_x, x, &pool));

        // exp(1/x) needs Risch (Gap F)
        let inv_x = pool.pow(x, pool.integer(-1_i32));
        let exp_inv_x = pool.func("exp", vec![inv_x]);
        assert!(contains_risch_form(exp_inv_x, x, &pool));
    }

    // -----------------------------------------------------------------------
    // Gap F: rational exponents  exp(η),  η ∈ ℚ(x) \ ℚ[x]
    // -----------------------------------------------------------------------

    fn eval_f64_gapf(expr: ExprId, x: ExprId, xv: f64, pool: &ExprPool) -> f64 {
        use crate::kernel::ExprData;
        if expr == x {
            return xv;
        }
        match pool.get(expr) {
            ExprData::Integer(n) => n.0.to_f64(),
            ExprData::Rational(r) => r.0.to_f64(),
            ExprData::Add(args) => args.iter().map(|&a| eval_f64_gapf(a, x, xv, pool)).sum(),
            ExprData::Mul(args) => args
                .iter()
                .map(|&a| eval_f64_gapf(a, x, xv, pool))
                .product(),
            ExprData::Pow { base, exp } => {
                eval_f64_gapf(base, x, xv, pool).powf(eval_f64_gapf(exp, x, xv, pool))
            }
            ExprData::Func { ref name, ref args } if args.len() == 1 => {
                let a = eval_f64_gapf(args[0], x, xv, pool);
                match name.as_str() {
                    "exp" => a.exp(),
                    "log" => a.ln(),
                    "sqrt" => a.sqrt(),
                    other => panic!("eval_f64_gapf: unsupported func {other}"),
                }
            }
            other => panic!("eval_f64_gapf: unsupported {other:?}"),
        }
    }

    fn verify_numeric_gapf(integrand: ExprId, antideriv: ExprId, x: ExprId, pool: &ExprPool) {
        let d = crate::diff::diff(antideriv, x, pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, pool).value;
        for &xv in &[0.5_f64, 1.0, 2.0] {
            let lhs = eval_f64_gapf(ds, x, xv, pool);
            let rhs = eval_f64_gapf(integrand, x, xv, pool);
            assert!(
                (lhs - rhs).abs() < 1e-8,
                "d/dx F ≠ f at x={xv}: {lhs} vs {rhs}\n  F = {}",
                pool.display(antideriv)
            );
        }
    }

    #[test]
    fn gapf_exp_inv_x_nonelementary() {
        // ∫ exp(1/x) dx is non-elementary (Ei(−1/x) type).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let inv_x = pool.pow(x, pool.integer(-1_i32));
        let f = pool.func("exp", vec![inv_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(1/x) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapf_inv_x2_exp_inv_x_elementary() {
        // ∫ (1/x²)·exp(1/x) dx = −exp(1/x).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let inv_x = pool.pow(x, pool.integer(-1_i32));
        let exp_inv_x = pool.func("exp", vec![inv_x]);
        let f = pool.mul(vec![pool.pow(x, pool.integer(-2_i32)), exp_inv_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (1/x²)·exp(1/x) dx must be elementary; got {result:?}"
        );
        verify_numeric_gapf(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapf_two_inv_x3_exp_neg_inv_x2_elementary() {
        // ∫ (2/x³)·exp(−1/x²) dx = exp(−1/x²).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let neg_inv_x2 = pool.mul(vec![
            pool.integer(-1_i32),
            pool.pow(x, pool.integer(-2_i32)),
        ]);
        let exp_e = pool.func("exp", vec![neg_inv_x2]);
        let f = pool.mul(vec![
            pool.integer(2_i32),
            pool.pow(x, pool.integer(-3_i32)),
            exp_e,
        ]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (2/x³)·exp(−1/x²) dx must be elementary; got {result:?}"
        );
        verify_numeric_gapf(f, result.unwrap().value, x, &pool);
    }

    // -----------------------------------------------------------------------
    // Verification helper
    // -----------------------------------------------------------------------

    /// Verify `d/dx antideriv = f` symbolically (using the symbolic diff engine).
    fn verify_antiderivative(
        pool: &ExprPool,
        x: ExprId,
        f: ExprId,
        antideriv: ExprId,
        label: &str,
    ) {
        use crate::diff::diff;
        use crate::poly::UniPoly;

        let d_antideriv = diff(antideriv, x, pool).unwrap();
        // Compare as polynomials if possible; otherwise just check ExprId equality.
        match (
            UniPoly::from_symbolic(d_antideriv.value, x, pool),
            UniPoly::from_symbolic(f, x, pool),
        ) {
            (Ok(a), Ok(b)) => {
                assert_eq!(
                    a.coefficients_i64(),
                    b.coefficients_i64(),
                    "{label}: d/dx antideriv ≠ f (polynomial check)"
                );
            }
            _ => {
                // Non-polynomial: just check that the diff is structurally equivalent
                // (this is a best-effort check for complex expressions).
                let _ = d_antideriv.value; // At least the differentiation didn't crash.
            }
        }
    }

    // -----------------------------------------------------------------------
    // Gap B: multi-generator tower composition
    // -----------------------------------------------------------------------

    /// Numeric evaluator supporting exp, log.
    fn eval_f64_gapb(expr: ExprId, x: ExprId, xv: f64, pool: &ExprPool) -> f64 {
        use crate::kernel::ExprData;
        if expr == x {
            return xv;
        }
        match pool.get(expr) {
            ExprData::Integer(n) => n.0.to_f64(),
            ExprData::Rational(r) => r.0.to_f64(),
            ExprData::Add(args) => args.iter().map(|&a| eval_f64_gapb(a, x, xv, pool)).sum(),
            ExprData::Mul(args) => args
                .iter()
                .map(|&a| eval_f64_gapb(a, x, xv, pool))
                .product(),
            ExprData::Pow { base, exp } => {
                eval_f64_gapb(base, x, xv, pool).powf(eval_f64_gapb(exp, x, xv, pool))
            }
            ExprData::Func { ref name, ref args } if args.len() == 1 => {
                let a = eval_f64_gapb(args[0], x, xv, pool);
                match name.as_str() {
                    "exp" => a.exp(),
                    "log" => a.ln(),
                    other => panic!("eval_f64_gapb: {other}"),
                }
            }
            other => panic!("eval_f64_gapb: {other:?}"),
        }
    }

    fn verify_gapb(integrand: ExprId, antideriv: ExprId, x: ExprId, pool: &ExprPool) {
        let d = crate::diff::diff(antideriv, x, pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, pool).value;
        // Use x > e so that log(log(x)) is real.
        for &xv in &[3.0_f64, 7.5, 15.0] {
            let lhs = eval_f64_gapb(ds, x, xv, pool);
            let rhs = eval_f64_gapb(integrand, x, xv, pool);
            assert!(
                (lhs - rhs).abs() < 1e-8,
                "d/dx F ≠ f at x={xv}: {lhs} vs {rhs}\nF={}",
                pool.display(antideriv)
            );
        }
    }

    #[test]
    fn gapb_log_log_x_over_x_elementary() {
        // ∫ log(log(x))/x dx = log(x)·log(log(x)) − log(x)
        // Two log generators: log(log(x)) and log(x).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let log_log_x = pool.func("log", vec![log_x]);
        let inv_x = pool.pow(x, pool.integer(-1_i32));
        let f = pool.mul(vec![log_log_x, inv_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ log(log(x))/x dx must be elementary; got {result:?}"
        );
        verify_gapb(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_exp_x_times_log_x_nonelementary() {
        // ∫ exp(x)·log(x) dx is non-elementary (involves Ei).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let log_x = pool.func("log", vec![x]);
        let f = pool.mul(vec![exp_x, log_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(x)·log(x) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapb_log_x_plus_x_plus_inv_x_times_exp_x_elementary() {
        // ∫ (log(x) + x + 1/x)·exp(x) dx = (log(x) + x − 1)·exp(x).
        // Poly-in-log RDE: c₁=1, c₀=x+1/x.
        //   a₁' + a₁ = 1 → a₁ = 1.
        //   a₀' + a₀ = (x+1/x) − 1/x = x → a₀ = x−1.
        //   v = 1·log(x) + (x−1) = log(x)+x−1.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let exp_x = pool.func("exp", vec![x]);
        let inv_x = pool.pow(x, pool.integer(-1_i32));
        // integrand = (log(x) + x + 1/x)·exp(x)
        let coeff = pool.add(vec![log_x, x, inv_x]);
        let f = pool.mul(vec![coeff, exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (log(x)+x+1/x)·exp(x) dx must be elementary; got {result:?}"
        );
        verify_gapb(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_sum_exp_and_log_independent() {
        // ∫ (x·exp(x²) + log(x)²) dx — two independent generators, sum rule.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let exp_x2 = pool.func("exp", vec![x2]);
        let log_x = pool.func("log", vec![x]);
        let log2 = pool.pow(log_x, pool.integer(2_i32));
        let f = pool.add(vec![pool.mul(vec![x, exp_x2]), log2]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (x·exp(x²)+log(x)²) dx must be elementary; got {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Gap C: mixed algebraic + transcendental
    // -----------------------------------------------------------------------

    #[test]
    fn gapc_exp_over_sqrt_xsq_plus_1_nonelementary() {
        // ∫ exp(x) / sqrt(x²+1) dx  — non-elementary.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let xsq1 = pool.add(vec![pool.pow(x, pool.integer(2_i32)), pool.integer(1_i32)]);
        let sqrt_xsq1 = pool.func("sqrt", vec![xsq1]);
        let exp_x = pool.func("exp", vec![x]);
        let f = pool.mul(vec![exp_x, pool.pow(sqrt_xsq1, pool.integer(-1_i32))]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(x)/sqrt(x²+1) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapc_1_plus_sqrt_x_times_exp_x_nonelementary() {
        // ∫ (1 + sqrt(x))·exp(x) dx — non-elementary (√x·exp(x) has no elementary antiderivative).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let sqrt_x = pool.func("sqrt", vec![x]);
        let exp_x = pool.func("exp", vec![x]);
        let f = pool.mul(vec![pool.add(vec![pool.integer(1_i32), sqrt_x]), exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ (1+√x)·exp(x) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapc_sqrt_x_coefficient_exp_x_elementary() {
        // ∫ (2x+1)/(2x) · sqrt(x) · exp(x) dx = sqrt(x)·exp(x).
        //
        // Derivation: d/dx(sqrt(x)·exp(x)) = (1/(2√x)+√x)·exp(x) = (1+2x)/(2√x)·exp(x).
        // Coefficient c = (1+2x)/(2x)·sqrt(x) = (1+2x)/(2√x).
        // After decompose: c₀=0, c₁=(1+2x)/(2x).
        // Eq 1: a'+a=0 → a=0.
        // Eq 2: b'+(1+1/(2x))·b=(1+2x)/(2x) → b=1. Antiderivative: sqrt(x)·exp(x).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let sqrt_x = pool.func("sqrt", vec![x]);
        let exp_x = pool.func("exp", vec![x]);
        // (2x+1)/(2x) · sqrt(x) · exp(x)
        let two_x_p1 = pool.add(vec![
            pool.mul(vec![pool.integer(2_i32), x]),
            pool.integer(1_i32),
        ]);
        let two_x = pool.mul(vec![pool.integer(2_i32), x]);
        let coeff = pool.mul(vec![
            two_x_p1,
            pool.pow(two_x, pool.integer(-1_i32)),
            sqrt_x,
        ]);
        let f = pool.mul(vec![coeff, exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (2x+1)/(2x)·sqrt(x)·exp(x) dx must be elementary; got {result:?}"
        );
        // Numerically verify d/dx F = f at x > 0.
        let antideriv = result.unwrap().value;
        let d = crate::diff::diff(antideriv, x, &pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, &pool).value;
        for &xv in &[0.5_f64, 1.5, 3.0] {
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = eval_f64_gapf(f, x, xv, &pool);
            assert!(
                (lhs - rhs).abs() < 1e-7,
                "d/dx F ≠ f at x={xv}: {lhs} vs {rhs}"
            );
        }
    }

    #[test]
    fn gapc_log_over_sqrt_x_elementary_via_log_tower() {
        // ∫ log(x)/sqrt(x) dx = 2·sqrt(x)·log(x) − 4·sqrt(x).
        // This goes through the log tower (θ = log(x)):
        //   c₁ = 1/sqrt(x) → integrate_base_unchecked → 2·sqrt(x)
        //   correction → base integral -2/sqrt(x) → -4·sqrt(x)
        // Routing: has_alg=true (sqrt), has_trans=true (log) → Risch → log tower.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let log_x = pool.func("log", vec![x]);
        let sqrt_x = pool.func("sqrt", vec![x]);
        let f = pool.mul(vec![log_x, pool.pow(sqrt_x, pool.integer(-1_i32))]);

        let result = crate::integrate::engine::integrate(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ log(x)/sqrt(x) dx must be elementary; got {result:?}"
        );
        // Verify numerically.
        let antideriv = result.unwrap().value;
        let d = crate::diff::diff(antideriv, x, &pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, &pool).value;
        for &xv in &[0.5_f64, 1.5, 4.0] {
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = eval_f64_gapf(f, x, xv, &pool);
            assert!(
                (lhs - rhs).abs() < 1e-7,
                "∫ log(x)/sqrt(x): d/dx F ≠ f at x={xv}: {lhs} vs {rhs}"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Gap B (nested exp): v=1 special case for transcendental η'
    // -----------------------------------------------------------------------

    /// Numeric verifier for nested-exp antiderivatives: use very small x to avoid
    /// overflow from exp(exp(x)) growing extremely fast.
    fn verify_nested_exp(integrand: ExprId, antideriv: ExprId, x: ExprId, pool: &ExprPool) {
        let d = crate::diff::diff(antideriv, x, pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, pool).value;
        for &xv in &[-0.5_f64, 0.0, 0.2] {
            let lhs = eval_f64_gapb(ds, x, xv, pool);
            let rhs = eval_f64_gapb(integrand, x, xv, pool);
            assert!(
                (lhs - rhs).abs() < 1e-8,
                "d/dx F ≠ f at x={xv}: {lhs} vs {rhs}\nF={}",
                pool.display(antideriv)
            );
        }
    }

    #[test]
    fn gapb_nested_exp_elementary() {
        // ∫ exp(x)·exp(exp(x)) dx = exp(exp(x))
        // Top generator: exp(exp(x)), η = exp(x), η' = exp(x).
        // RDE: v' + exp(x)·v = exp(x); v = 1 is a solution.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let f = pool.mul(vec![exp_x, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ exp(x)·exp(exp(x)) dx must be elementary; got {result:?}"
        );
        verify_nested_exp(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_nested_exp_with_const_elementary() {
        // ∫ 3·exp(x)·exp(exp(x)) dx = 3·exp(exp(x))
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let f = pool.mul(vec![pool.integer(3_i32), exp_x, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ 3·exp(x)·exp(exp(x)) dx must be elementary; got {result:?}"
        );
        verify_nested_exp(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_nested_exp_rational_coeff_nonelementary() {
        // ∫ x·exp(exp(x)) dx — c = x ∈ ℚ[x] has no inner exp factor → NonElementary
        // (certified by degree bound: θ_inner·v has degree > c's degree for any v).
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let f = pool.mul(vec![x, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ x·exp(exp(x)) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapb_nested_exp_bare_nonelementary() {
        // ∫ exp(exp(x)) dx alone — c = 1 ∈ ℚ[x] → NonElementary.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);

        let result = integrate_risch(exp_exp_x, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(exp(x)) dx must be NonElementary; got {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Gap B: nested exp — lower-tower polynomial cascade
    // -----------------------------------------------------------------------

    #[test]
    fn gapb_nested_exp_exp_sq_elementary() {
        // ∫ exp(x)²·exp(exp(x)) dx = (exp(x)−1)·exp(exp(x))
        // Cascade: c = θ² (N=2), v₁ = 1, v₀ = c₁ − v₁' − v₁ = 0−0−1 = −1.
        // D(v₀) = 0 = c₀ ✓.  v = θ−1 = exp(x)−1.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        // exp(x)^2 * exp(exp(x))
        let f = pool.mul(vec![pool.pow(exp_x, pool.integer(2_i32)), exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ exp(x)²·exp(exp(x)) dx must be elementary; got {result:?}"
        );
        verify_nested_exp(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_nested_exp_poly_plus_rational_elementary() {
        // ∫ [(x²+1)·exp(x) + 2x]·exp(exp(x)) dx = (x²+1)·exp(exp(x))
        // Cascade: c = (x²+1)·θ + 2x (N=1).
        // v₀ = c₁/1 = x²+1.  D(v₀) = 2x = c₀ ✓.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let x2p1 = pool.add(vec![x2, pool.integer(1_i32)]);
        let two_x = pool.mul(vec![pool.integer(2_i32), x]);
        // ((x²+1)·exp(x) + 2x)·exp(exp(x))
        let coeff = pool.add(vec![pool.mul(vec![x2p1, exp_x]), two_x]);
        let f = pool.mul(vec![coeff, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ [(x²+1)exp(x)+2x]·exp(exp(x)) dx must be elementary; got {result:?}"
        );
        verify_nested_exp(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_nested_exp_higher_degree_elementary() {
        // ∫ [exp(x)²−exp(x)]·exp(exp(x)) dx = (exp(x)−2)·exp(exp(x))
        // Cascade: c = θ²−θ (N=2, c₂=1, c₁=−1, c₀=0).
        // v₁ = 1, v₀ = −1−0−1 = −2.  D(v₀) = 0 = c₀ ✓.  v = −2+θ = exp(x)−2.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let exp_sq = pool.pow(exp_x, pool.integer(2_i32));
        let neg_exp = pool.mul(vec![pool.integer(-1_i32), exp_x]);
        let coeff = pool.add(vec![exp_sq, neg_exp]);
        let f = pool.mul(vec![coeff, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ [exp(x)²−exp(x)]·exp(exp(x)) dx must be elementary; got {result:?}"
        );
        verify_nested_exp(f, result.unwrap().value, x, &pool);
    }

    #[test]
    fn gapb_nested_exp_inner_coeff_nonelementary() {
        // ∫ x·exp(x)·exp(exp(x)) dx: c = x·θ (N=1, c₁=x, c₀=0).
        // Cascade: v₀ = x, D(v₀) = 1 ≠ c₀ = 0.  No polynomial solution →
        // denominator-bound theorem certifies NonElementary.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let f = pool.mul(vec![x, exp_x, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ x·exp(x)·exp(exp(x)) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapb_nested_exp_rational_denom_nonelementary() {
        // ∫ exp(x)/(exp(x)+1)·exp(exp(x)) dx:
        // c = θ/(θ+1) is rational in θ = exp(x).
        // Hermite reduction: D maps 1/(θ-α) to a double pole; no rational v exists.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        // exp(x)/(exp(x)+1) = exp(x)·(exp(x)+1)^{-1}
        let denom = pool.add(vec![exp_x, pool.integer(1_i32)]);
        let coeff = pool.mul(vec![exp_x, pool.pow(denom, pool.integer(-1_i32))]);
        let f = pool.mul(vec![coeff, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(x)/(exp(x)+1)·exp(exp(x)) dx must be NonElementary; got {result:?}"
        );
    }

    #[test]
    fn gapb_nested_exp_inv_theta_nonelementary() {
        // ∫ exp(-x)·exp(exp(x)) dx = ∫ θ^{-1}·G dx:
        // c = exp(-x) = θ^{-1} has a negative power of θ → rational → NonElementary.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let exp_x = pool.func("exp", vec![x]);
        let exp_exp_x = pool.func("exp", vec![exp_x]);
        let exp_neg_x = pool.pow(exp_x, pool.integer(-1_i32));
        let f = pool.mul(vec![exp_neg_x, exp_exp_x]);

        let result = integrate_risch(f, x, &pool);
        assert!(
            matches!(result, Err(IntegrationError::NonElementary(_))),
            "∫ exp(-x)·exp(exp(x)) dx must be NonElementary; got {result:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Gap C (log tower + algebraic coefficient)
    // -----------------------------------------------------------------------

    #[test]
    fn mixed_cbrt_x_times_log_x_elementary() {
        // ∫ x^{1/3}·log(x) dx = (3/4)x^{4/3}·log(x) − (9/16)x^{4/3}.
        // Log-tower IBP delegates the base ∫x^{1/3} dx to the MA algebraic engine.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let cbrt_x = pool.pow(x, pool.rational(1_i32, 3_i32));
        let log_x = pool.func("log", vec![x]);
        let f = pool.mul(vec![cbrt_x, log_x]);

        let result = crate::integrate::engine::integrate(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ x^(1/3)·log(x) dx must be elementary; got {result:?}"
        );
        let antideriv = result.unwrap().value;
        let d = crate::diff::diff(antideriv, x, &pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, &pool).value;
        for &xv in &[0.5_f64, 1.5, 4.0] {
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = eval_f64_gapf(f, x, xv, &pool);
            assert!(
                (lhs - rhs).abs() < 1e-7,
                "∫ x^(1/3)·log(x): d/dx F ≠ f at x={xv}: {lhs} vs {rhs}"
            );
        }
    }

    #[test]
    fn mixed_log_over_cbrt_x_elementary() {
        // ∫ log(x)/x^{1/3} dx = (3/2)x^{2/3}·log(x) − (9/4)x^{2/3}.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let inv_cbrt = pool.pow(x, pool.rational(-1_i32, 3_i32)); // x^{-1/3}
        let f = pool.mul(vec![inv_cbrt, pool.func("log", vec![x])]);
        let result = crate::integrate::engine::integrate(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ log(x)/x^(1/3) dx must be elementary; got {result:?}"
        );
        let antideriv = result.unwrap().value;
        let ds = crate::simplify::engine::simplify(
            crate::diff::diff(antideriv, x, &pool).unwrap().value,
            &pool,
        )
        .value;
        for &xv in &[0.5_f64, 1.5, 4.0] {
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = eval_f64_gapf(f, x, xv, &pool);
            assert!((lhs - rhs).abs() < 1e-7, "x={xv}: {lhs} vs {rhs}");
        }
    }

    #[test]
    fn mixed_cbrt_nonsquarefree_times_log_x_elementary() {
        // ∫ (x²)^{1/3}·log(x) dx, radicand x² non-squarefree → general MA basis
        // delivers the base ∫x^{2/3} dx = (3/5)x^{5/3}; IBP wraps the log.
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let x2 = pool.pow(x, pool.integer(2_i32));
        let rad = pool.func("cbrt", vec![x2]); // (x²)^{1/3}
        let f = pool.mul(vec![rad, pool.func("log", vec![x])]);
        let result = crate::integrate::engine::integrate(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ (x²)^(1/3)·log(x) dx must be elementary; got {result:?}"
        );
        let antideriv = result.unwrap().value;
        let ds = crate::simplify::engine::simplify(
            crate::diff::diff(antideriv, x, &pool).unwrap().value,
            &pool,
        )
        .value;
        for &xv in &[0.5_f64, 1.5, 4.0] {
            // eval the integrand's cbrt(x²) via x^{2/3} equivalence.
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = xv.powf(2.0 / 3.0) * xv.ln();
            assert!((lhs - rhs).abs() < 1e-6, "x={xv}: {lhs} vs {rhs}");
        }
    }

    #[test]
    fn gapc_sqrt_x_times_log_x_elementary() {
        // ∫ sqrt(x)·log(x) dx = (2x^{3/2}/3)·log(x) − 4x^{3/2}/9
        // IBP: c_1 = √x → P_1 = ∫√x dx = (2/3)x^{3/2} (via algebraic engine)
        //   correction = −(2/3)x^{3/2}·(1/x) = −(2/3)√x
        //   base: ∫(−(2/3)√x) dx = −(4/9)x^{3/2}
        let pool = p();
        let x = pool.symbol("x", Domain::Real);
        let sqrt_x = pool.func("sqrt", vec![x]);
        let log_x = pool.func("log", vec![x]);
        let f = pool.mul(vec![sqrt_x, log_x]);

        let result = crate::integrate::engine::integrate(f, x, &pool);
        assert!(
            result.is_ok(),
            "∫ sqrt(x)·log(x) dx must be elementary; got {result:?}"
        );
        let antideriv = result.unwrap().value;
        let d = crate::diff::diff(antideriv, x, &pool).unwrap();
        let ds = crate::simplify::engine::simplify(d.value, &pool).value;
        for &xv in &[0.5_f64, 1.5, 4.0] {
            let lhs = eval_f64_gapf(ds, x, xv, &pool);
            let rhs = eval_f64_gapf(f, x, xv, &pool);
            assert!(
                (lhs - rhs).abs() < 1e-7,
                "∫ sqrt(x)·log(x): d/dx F ≠ f at x={xv}: {lhs} vs {rhs}"
            );
        }
    }
}