-
Notifications
You must be signed in to change notification settings - Fork 1
/
Risch.m
1651 lines (1572 loc) · 64.1 KB
/
Risch.m
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
(*******************************************************************************
* Risch Algorithm for Transcendental Functions
*
* Written by Sam Blake in 2007
*
* References:
* * Symbolic Integration 1, Manuel Bronstein.
*
* * Algorithms for Computer Algebra, Geddes et al.
*
* * Indefinite and Definite Integration, Kelly Roach,
* 1992 Mathematica Conference.
*
* * Fast Reduction of the Risch Differential Equation,
* Manuel Bronstein, Journal of Symbolic Computation.
*
* * The Problem of Integration in Finite Terms, Robert Risch,
* Transactions of the American Mathematical Society.
*
* * The Risch Differential Equation Problem, J. H. Davenport,
* SIAM, Vol. 15, No. 4, November 1986.
*
* * Simplification of Real Elementary Functions, Manuel Bronstein,
* IBM Research Division, T. J. Watson Research Center,
* Yorktown Heights, NY 10598
*
* * Algebraic Properties of the Elementary Functions of Analysis,
* Robert H. Risch, American Journal of Mathematics, Vol. 101,
* No. 4, pp. 743-759.
*
* * Algebraic Factoring and Rational Function Integration,
* B. M. Trager, ACM Symposium on Symbolic and Algebraic
* Computation, 1976,
*
* * Symbolic Integration towards Practicle Algorithms, Manuel Bronstein,
* IBM Research Division, T. J. Watson Research Center,
* Yorktown Heights, NY 10598
*
*******************************************************************************)
(* BeginPackage["Risch`"]; *)
<<RischUtilities.m;
Risch::usage = "Risch[f,x] uses the Risch/Bronstein algorithm to find the \
indefinite integral of f wrt x. At present we require f to be a rational or \
transcendental function.";
(* Begin["`Private`"] *)
(*******************************
*
* Main Call to Risch Algorithm
*
********************************)
Risch[0, x_]:= 0;
Risch[expr_, x_]:= expr x /; FreeQ[expr, x];
Risch[f_, x_]:= Module[
{integrand, res, X, internal, theta, tower, Dtower, solution, result},
Info["Enter transcendental Risch integration algorithm"];
(* External call to the Risch integration algorithm *)
RischTrace[Risch, {f, x}, "In"];
(* Try look-up for some integrals which we don't handle too well,
specifically trig integrals and some integrals with symbolic parameters *)
res = RischTable[f, x];
If[FreeQ[res, RischTable], Return[res]];
(* Convert the input to internal form, from here on Log => rischLog and
Exp => rischExp *)
{integrand, X} = InputConvert[f, x];
(* all parameters in the integrand are assumed to be positive *)
RischParams[integrand, X];
(* determine the structure of integrand *)
{internal, theta, tower, Dtower, {}} = RischStructure[integrand, X];
(* call internal integration routine *)
solution = RischInternal[internal, theta, tower, Dtower];
If[Head[solution]===List, solution = First[solution]];
(* is soluton elementary? *)
If[SameQ[solution, $Failed], Return[ HoldForm[Risch[f, x]] ]];
(* convert to external form *)
result = OutputConvert[f, solution, theta, tower, x];
(* Clear assumptions on parameters *)
ClearAll /@ PureVariables[integrand];
RischTrace[Risch, result, "Out"];
result
];
(* special call for trig rational functions *)
Risch[f_, x_]:= Module[{arg, dx, res},
Info["Use Euler substitution on trig rational."];
arg = Cases[f, #[arg_. x] :> arg, {0, Infinity}] & /@ {Sin, Cos, Tan, Cot, Sec, Csc};
arg = First @ Union @ Flatten @ arg;
res = (f*dx) //. {Tan[arg x] -> -((2 Risch`sub)/(-1 + Risch`sub^2)),
Sin[arg x] -> (2*Risch`sub)/(Risch`sub^2 + 1),
Cos[arg x] -> (1 - Risch`sub^2)/(1 + Risch`sub^2),
Tan[arg x] -> (-2 Risch`sub)/(-1 + Risch`sub^2),
Csc[arg x] -> (1 + Risch`sub^2)/(2 Risch`sub),
Sec[arg x] -> (1 + Risch`sub^2)/(1 - Risch`sub^2),
Cot[arg x] -> (-1 + Risch`sub^2)/(-2 Risch`sub),
dx -> 2/(arg (1 + Risch`sub^2))};
Info["Integrand becomes ", res /. Risch`sub -> "t"];
ReplaceAll[Risch[Cancel[res], Risch`sub], Risch`sub -> Tan[arg x/2]]
] /; TrigRationalQ[f, x];
(*special call for hyperbolic rational functions*)
Risch[f_, x_]:= Module[{arg, dx, res},
Info["Use Euler substitution on hyperbolic rational."];
arg = Cases[f, #[arg_. x] :> arg, {0, Infinity}] & /@ {Sinh, Cosh, Tanh, Coth, Csch, Sech};
arg = First@Union@Flatten@arg;
res = (f*dx) /. {Tanh[arg x] -> (2 Risch`sub)/(1 + Risch`sub^2),
Sinh[arg x] -> (2*Risch`sub)/(1 - Risch`sub^2),
Cosh[arg x] -> (1 + Risch`sub^2)/(1 - Risch`sub^2),
Tanh[arg x] -> (2 Risch`sub)/(1 + Risch`sub^2),
Csch[arg x] -> (1 - Risch`sub^2)/(2*Risch`sub),
Sech[arg x] -> (1 - Risch`sub^2)/(1 + Risch`sub^2),
Coth[arg x] -> (1 + Risch`sub^2)/(2*Risch`sub),
dx -> 2/(arg (1 - Risch`sub^2))};
Info["Integrand becomes ", res /. Risch`sub -> "t"];
ReplaceAll[Risch[Cancel[res], Risch`sub], Risch`sub -> Tanh[arg x/2]]
] /; HyperbolicRationalQ[f, x];
(**********************************
*
* Internal call to Risch algorithm
*
**********************************)
Options[RischInternal]:= {"RecursiveCall" -> False, "ExtendThetas" -> True};
RischInternal[0, intheta_, intower_, inDtower_, OptionsPattern[]]:= {0, intheta, intower, inDtower};
RischInternal[integrand_, itheta_, itower_, iDtower_, OptionsPattern[]]:= Module[
{recursive,extendtheta, theta, tower, Dtower,
fp,fs,fn,rationalsoln,simplePart,reducedPart,
polyPart,logPart,polysoln,logsoln,rep,res},
(* Main Algorithm *)
RischTrace[RischInternal, {integrand, itheta, itower, iDtower}, "In"];
recursive = OptionValue["RecursiveCall"];
extendtheta = OptionValue["ExtendThetas"];
If[recursive,
{theta,tower,Dtower} = UpdateExtensionField[integrand, itheta, itower, iDtower],
{theta,tower,Dtower} = {itheta, itower, iDtower}
];
(* Canonical representation *)
{fp, fs, fn} = CanonicalRepresentation[integrand, theta, tower, Dtower];
(* rational part *)
{rationalsoln, simplePart, reducedPart} = HermiteReduction[fn, theta, Dtower];
polyPart = Simplify[fp + fs];
logPart = Together[simplePart + reducedPart];
(* polynomial part *)
polysoln = RischPolynomial[polyPart, theta, tower, Dtower, extendtheta];
If[SameQ[polysoln, $Failed], Return[$Failed]];
(* logarithmic part *)
If[logPart =!= 0,
logsoln = PureLogPart[logPart, theta, tower, Dtower, extendtheta],
logsoln = 0
];
If[SameQ[logsoln, $Failed], Return[$Failed] ];
res = rationalsoln + polysoln + logsoln;
RischTrace[RischInternal, res, "Out"];
If[recursive,
rep = Thread[itower -> itheta];
{res //. rep, theta, tower, Dtower},
res
]
];
(*******************************************
*
* Integration of polynomials
*
********************************************)
RischPolynomial[polyPart_, theta_, tower_, Dtower_, extendtheta_]:= Module[
{b, res, out},
(* Integration of transcendental polynomials *)
RischTrace[RischPolynomial, polyPart, "In"];
Info["Integrating the polynomial ", polyPart];
If[Apply[And, FreeQ[polyPart, #]& /@ theta], Return[polyPart*First[theta]] ];
Which[
SameQ[Head[Last[tower]], rischLog],
out = RischLogarithmicPolynomial[polyPart, theta, tower, Dtower, extendtheta];
{res, b} = If[out === $Failed, {$Failed, False}, out];
If[Not[b],
Message[RischLogarithmicPolynomial::warning];
res = $Failed
],
SameQ[Head[Last[tower]], rischExp],
res = RischExponentialPolynomial[polyPart, theta, tower, Dtower, extendtheta],
SameQ[Length[theta], 1],
res = RischBasePolynomial[polyPart // Together, theta, tower, Dtower, extendtheta],
True,
res = $Failed
];
Info["The polynomial part is ", res];
RischTrace[RischPolynomial, res, "Out"];
res
];
RischBasePolynomial[p_, theta_, tower_, Dtower_, extendtheta_]:= Module[
{x = First[theta], n, d, poly, rat, res},
RischTrace[RischBasePolynomial,{p, theta, tower, Dtower, extendtheta}, "In"];
{n,d} = PolynomialQuotientRemainder[Numerator[p],Denominator[p],x];
poly = rischpoly[n, x];
rat = First[ RischInternal[d, theta, tower, Dtower, {"RecursiveCall" -> True, "ExtendThetas" -> extendtheta}] ];
res = poly + rat;
RischTrace[RischBasePolynomial, res, "Out"];
res
];
rischpoly[0, x_] := 0;
rischpoly[a_: 1, x_] := a x /; FreeQ[a, x];
rischpoly[a_. x_^n_., x_] := a x^(n + 1)/(n + 1) /; n =!= -1 && FreeQ[{a, n}, x];
rischpoly[a_ + b_, x_] := rischpoly[a, x] + rischpoly[b, x];
rischpoly[a_./x_, x_] := a rischLog[x] /; FreeQ[a, x];
(****************************************
*
* Integration of algebraic functions
*
****************************************)
RischAlgebraic[integrand_, x_] := Module[{linearAlgebraic, quadraticAlgebraic},
(* Work in progress *)
Print["Integration of algebraic functions are not yet implemented!"];
Return[$Failed]
];
(****************************************
*
* Integration of special functions
*
*****************************************)
RischSpecialExtension[integrand_, x_] := Module[{},
(* Work to do here, mainly in structure theorem implementation *)
Print["Integration failed, integral may be elementary."];
$Failed
];
(***************************************************
*
* Hermite reduction - quadratic version
*
****************************************************)
HermiteReduction[0,theta_, Dtower_]:={0, 0, 0};
HermiteReduction[f_,theta_,Dtower_]:=Module[
{fp, fs, fn, a, d, dsqf, dlist, dseq, g = 0, v, u, b, c, q, r, result},
RischTrace[HermiteReduction,{f, theta, Dtower}, "In"];
(* Hermite Reduction - quadratic version, reference: Symbolic Integration, Bronstein p. 139 *)
{a, d} = {Numerator[f], Denominator[f]};
dsqf = First/@Squarefree[d,Last[theta]];
If[Length[dsqf] === 1,
Info["Hermite reduction yields ",
HoldForm[Integrate[f, #1]]& [First[theta]] ];
RischTrace[HermiteReduction,{f, theta, Dtower}, "Out"];
Return[ {0, f, 0} ]
];
Do[
If[Exponent[dsqf[[i]], Last[theta]] > 0,
v = dsqf[[i]];
u = Cancel[d/v^i];
Do[
{b, c} = ExtendedEuclidean[u TotalDerivation[v,theta,Dtower], v, -Cancel[a/j], Last[theta]];
g = g + Cancel[b/v^j];
a = Expand[-j c-u TotalDerivation[b, theta, Dtower]]
,{j,i-1,1,-1}
];
d = u v
]
,{i,2,Length[dsqf]}
];
q = PolynomialQuotient[a,Expand[u v],Last[theta]];
r = PolynomialRemainder[a,Expand[u v],Last[theta]];
result = {g, Cancel[r/(u v)], Cancel[q]};
Info["Hermite reduction yields ",
HoldForm[ Integrate[f, #1] ]& [First[theta]] == g +
HoldForm[ Integrate[#1, #2] ]& [result[[2]] + result[[3]], First[theta]] ];
RischTrace[HermiteReduction, result, "Out"];
result
];
(***********************************************************************
*
* Lazard-Rothstein-Trager-Rioboo algorithm
*
************************************************************************)
PureLogPart[a_ + b_, theta_, tower_, Dtower_, extend_] :=
PureLogPart[a, theta, tower, Dtower, extend] + PureLogPart[b, theta, tower, Dtower, extend];
PureLogPart::warning = "Integral not elementary -- roots of resultant \
polynomial are non constant.";
PureLogPart[f_, intheta_, intower_, inDtower_, extend_] := Module[
{theta, tower, Dtower, d, p, res, r, R, n, s, kD, S, M, A, w,
EEA, newarg, sbar, evals, remaining, naivepart, Result = 0},
RischTrace[PureLogPart, {f, intheta, intower, inDtower}, "In"];
Info["Integrate the logarithmic part with the L-R-T-R algorithm ", f];
(* Lazard-Rioboo-Rothstein-Trager resultant algorithm *)
(* reduces a simple rational function with no polynomial part to a sum
of logarithms *)
(* Bronstein p. 153 *)
(* update the extension field if required *)
{theta, tower, Dtower} = UpdateExtensionField[f, intheta, intower, inDtower];
d = Denominator[f];
Which[
Exponent[TotalDerivation[d, theta, Dtower], Last[theta]] <= Exponent[d, Last[theta]],
res = SubResultant[d, Expand[Numerator[f] - rischZ TotalDerivation[d, theta, Dtower]], Last[theta]];
r = Cancel[Together[Resultant[d, Expand[Numerator[f] - rischZ TotalDerivation[d, theta, Dtower]], Last[theta]]]];
(* allow for algebraic numbers to be present *)
If[Cases[r, xx_. cc_^n_?(!IntegerQ[#] &), {0, Infinity}] =!= {},
r = Cancel[Factor[Numerator[r], Extension -> Automatic]/Factor[Denominator[r], Extension -> Automatic]]
],
True,
res = SubResultant[Expand[Numerator[f] - rischZ TotalDerivation[d, theta, Dtower]], d, Last[theta]];
r = Cancel[Together[Resultant[Expand[Numerator[f] - rischZ TotalDerivation[d, theta, Dtower]], d, Last[theta]]]
];
(* allow for algebraic numbers to be present *)
If[Cases[r, xx_. cc_^n_?(!IntegerQ[#] &), {0, Infinity}] =!= {},
r = Cancel[Factor[Numerator[r], Extension -> Automatic]/Factor[Denominator[r], Extension -> Automatic]]]
];(* end if *)
kD = kappaD[r, Flatten[{theta, rischZ}], Flatten[{Dtower, 1}]];
{n, s} = SplitSquarefreeFactor[r, Flatten[{theta, rischZ}], Flatten[{Dtower, kD}]];
S = {};
Do[
If[Exponent[Part[s, i], rischZ] > 0,
If[i == Exponent[d, Last[theta]],
(* make S monic wrt Last[theta] *)
w = lc[Expand[d], Last[theta]];
EEA = ExtendedEuclidean[w, Part[s, i], 1, rischZ];
newarg = primitive[Cancel[Together[PolynomialRemainder[Part[EEA, 1] d, Part[s, i], rischZ]]], theta, Last[theta]];
AppendTo[S, newarg],(* else *)
Do[
If[Exponent[res[[m]], Last[theta]] == i,
M = m;
sbar = Part[res, m]
];(* end if *)
, {m, 2, Length[res] - 1}
];(* end do *)
A = Power @@@ Squarefree[lc[sbar, Last[theta]], Last[theta]];
Do[
sbar = sbar/(PolynomialGCD[Part[A, j], Part[s, i], rischZ, Extension -> Automatic]^j)
, {j, 1, Length[A]}
];
(* make S monic wrt Last[theta] *)
w = lc[Cancel[Together[sbar]], Last[theta]];
EEA = ExtendedEuclidean[w, Part[s, i], 1, rischZ];
sbar = primitive[Cancel[Together[PolynomialRemainder[Part[EEA, 1] sbar, Part[s, i], rischZ]]], theta, Last[theta]];
AppendTo[S, sbar]
](* end if *)
];(* end if *)
, {i, 1, Length[s]}
];
(* remove constant polynomials from RootSum expression *)
evals = Select[s, !PossibleZeroQ[D[#, rischZ]] &];
(* RootSum exrpession for result *)
Which[
Head[Last[tower]] === rischExp,
Do[
Result = Result + rischLogPartE[Part[evals, i], Part[S, i], Last[theta], Last[tower]]
, {i, 1, Length[evals]}
],
True(* log theta or rational *),
Do[
Result = Result + rischLogPartL[Part[evals, i], Part[S, i], Last[theta], Last[tower]]
, {i, 1, Length[evals]}
]
];
If[!FreeQ[Result, Indeterminate | _DirectedInfinity],
Result = RischNaive[f, theta, Dtower]
];
If[!FreeQ[Result, "NaiveSolution"],
Result = Result /. "NaiveSolution" -> 0;
remaining = f - TotalDerivation[Result //. {rischExp -> Exp, rischLog -> Log},theta, Dtower];
naivepart = RischNaive[remaining, theta, Dtower];
Result = (Result + naivepart) //. {Exp -> rischExp, Log -> rischLog};
];
(* Check if the integral is elementary? *)
If[FreeQ[Simplify[Times @@ n], Last[theta]] && FreeQ[Simplify[Times @@ n], rischZ],
(* integral elementary *)
Info["The logarithmic part is ", Result];
RischTrace[PureLogPart, Result, "Out"];
Result,
(* integral not elementary *)
Message[PureLogPart::warning];
RischTrace[PureLogPart, Result, "Out"];
$Failed
]
];
(*****************************************************
*
* Naive form of log part of integral
*
******************************************************)
RischNaive[int_, theta_, Dtower_] := Module[
{expr = Together[int], numf, denf, Ddenf, z, res},
RischTrace[RischNaive, {int, theta, Dtower}, "In"];
(* return naive result *)
Ddenf = TotalDerivation[Denominator[expr], theta, Dtower];
{numf, denf, Ddenf} = {Numerator[expr], Denominator[expr], Ddenf} //. Last[theta] -> z;
res = RootSum[Function[#1, #2], Function[#1, (#3/#4) Log[#5 - z]]] &[z, denf, numf, Ddenf, Last[theta]];
If[Factor[denf] =!= denf, res = ToRadicals[res]];
RischTrace[RischNaive, res, "Out"];
res
];
(*********************************************************
*
* Construction of logarithmic terms
*
**********************************************************)
(* terms for exponential extensions *)
rischLogPartE[r_, v_, theta_, tower_] := Module[
{extension, rfac, exparg, result, e, term, root, roots},
RischTrace[rischLogPartE, {r, v, theta, tower}, "In"];
extension = Quiet[Re[rischZ /. Solve[r == 0, rischZ]]];
If[And @@ (FreeQ[extension, #] & /@ {Re, Log, Root, E, Sin, Cos}) &&
PureVariables[extension] === {}(* may need more here... *),
rfac = TimeConstrained[
Power @@@ Quiet[FactorList[r, Extension -> extension]],
0.5,
Power @@@ Quiet[FactorList[r, Extension -> Automatic]]
],(* else *)
rfac = Power @@@ Quiet[FactorList[r, Extension -> Automatic]]
];
If[Or @@ (! FreeQ[rfac, #] & /@ {Sin, Cos, Tan, Pi, Root, RootSum}) || ! FreeQ[rfac, (-1)^n_Rational],
rfac = Power @@@ Quiet[FactorList[r, Extension -> Automatic]]
];
If[! FreeQ[rfac, Root] || ! FreeQ[rfac, RootSum],
rfac = {r}
];
rfac = rfac /. {rischLog -> Log, rischExp -> Exp};
exparg = tower /.rischExp[arg_] :> arg;
result = 0;
Do[
e = Exponent[rfac[[i]], rischZ];
Which[
e == 0,
term = 0,
e == 1,
root = First[rischZ /.Solve[rfac[[i]] == 0, rischZ]];
term = -root Exponent[v, theta] exparg + root rischLog[primitive[v /.rischZ :> root, theta]],
e == 2,
roots = rischZ /.Solve[rfac[[i]] == 0, rischZ];
term = rischQuadraticExpTerm[v, roots, exparg, theta] +
rischQuadraticLog[rfac[[i]], roots, v, theta],
e > 2,
term = "NaiveSolution"
];
result = result + term
, {i, 1, Length[rfac]}
];
RischTrace[rischLogPartE, result, "Out"];
result
];
(* terms for rational of logarithmic extensions *)
rischLogPartL[r_, v_, theta_, tower_] := Module[
{rfac, extension, exparg, result, e, term, root, roots},
RischTrace[rischLogPartL, {r, v, theta, tower}, "In"];
(* construct elegant form of logarithms for LRTR *)
extension = TimeConstrained[ComplexExpand[Re[rischZ /. Solve[r == 0, rischZ]]], 1];
If[MatchQ[extension, _Solve|$Aborted], Return["NaiveSolution"]];
Which[
BiQuadraticQ[r, rischZ],
rfac = FactorBiQuadratic[r, rischZ],
Or @@ (!FreeQ[extension, #] & /@ {Root, Sin, Cos, Log, E}) || PureVariables[extension] =!= {},
rfac = Power @@@ Quiet[FactorList[r, Extension -> Automatic]],
True,
rfac = TimeConstrained[
Power @@@ Quiet[FactorList[r, Extension -> extension]],
0.5,
Power @@@ Quiet[FactorList[r, Extension -> Automatic]]
]
];
If[Or @@ (! FreeQ[rfac, #] & /@ {Sin, Cos, Tan, Pi, Root, Log, E}) || !FreeQ[rfac, (-1)^n_Rational],
rfac = Power @@@ Quiet[FactorList[r, Extension -> Automatic]]
];
If[! FreeQ[rfac, Root] || ! FreeQ[rfac, RootSum],
rfac = {r}
];
exparg = tower /. rischExp[arg_] :> arg;
result = 0;
Do[
e = Exponent[rfac[[i]], rischZ];
Which[
e == 0,
term = 0,
e == 1,
root = First[rischZ /. Solve[rfac[[i]] == 0, rischZ]];
term = root*rischLog[Simplify[primitive[v /. rischZ :> root, theta]]],
e == 2,
roots = rischZ /. Solve[rfac[[i]] == 0, rischZ];
term = rischQuadraticLog[rfac[[i]], roots, v, theta],
e > 2,
(* Here is it probably better to return naive term if no other
terms in the solution, or if there are other terms then return
Risch[integrand - D[partialsolution],x] *)
term = "NaiveSolution"
];
result = result + term
, {i, 1, Length[rfac]}
];
(* condition for problems caused in Risch[ (2 + Tan[x]^2)/(1 + (x + Tan[x])^2), x] *)
If[term === Indeterminate, result = "NaiveSolution"];
RischTrace[rischLogPartL, result, "Out"];
result
];
rischQuadraticExpTerm[v_, roots_, exparg_, theta_] :=
Which[
Length[roots] == 1,
-First[roots] Exponent[v, theta] exparg,
Length[roots] == 2,
Expand[-First[roots] Exponent[v, theta] exparg - Last[roots] Exponent[v, theta] exparg]
];
rischQuadraticLog[r_, rts_, v_, theta_] := Module[{a, b, c, del, roots, result, S1, S2},
RischTrace[rischQuadraticLog, {r, rts, v, theta}, "In"];
(* r = a z^2 + b z + c *)
roots = FullSimplify /@ rts;
a = Coefficient[r, rischZ, 2];
b = Coefficient[r, rischZ, 1];
c = Coefficient[r, rischZ, 0];
del = b^2 - 4*a*c;
Which[
(* del = 0: 1 root => single log term *)
del === 0,
result = First[roots] rischLog[Simplify[primitive[v /. rischZ -> First[roots], theta]]],
SameQ[Sign[del], 1] || ComplexExpand[Im[del]] =!= 0,
(* the second condition above fixes Risch[Tan[x] Tan[x+1],x] *)
(* del > 0: 2 real roots => 2 log terms*)
result = First[roots] rischLog[Simplify[primitive[v /. rischZ -> First[roots], theta]]] +
Last[roots] rischLog[Simplify[primitive[v /. rischZ -> Last[roots], theta]]],
SameQ[Sign[del], -1],
(* del < 0: logs + atans *)
result = LogToReal[r, v, theta],
True,
result = "NaiveSolution"
];
RischTrace[rischQuadraticLog, result, "Out"];
result
];
(******************************************************
*
* Integration of logarithmic polynomials
*
*******************************************************)
RischLogarithmicPolynomial::warning = "Integral not elementary -- \
non-elementary recursive integration or introduction of new logarithms.";
RischLogarithmicPolynomial[p_, theta_, tower_, Dtower_, extendtheta_]:=Catch[Module[
{P = ExpandNumeratorDenominator[p], result, iszero, remaining},
result = IntegratePrimitivePolynomial[P, theta, tower, Dtower, extendtheta];
If[Not@Last[result], Throw[result]];
iszero = ExpandNumeratorDenominator @ Simplify @ Together @ Expand[P - TotalDerivation[First @ result, theta, Dtower]];
If[iszero=!=0,
remaining = RischInternal[iszero, theta, tower, Dtower, "ExtendThetas" -> True, "RecursiveCall" -> True];
If[remaining===$Failed, Throw[$Failed]];
result = First[result] + First[remaining]
];
{result, True}
]];
IntegratePrimitivePolynomial[0, theta_, tower_, Dtower_, extendtheta_]:= {0, True};
IntegratePrimitivePolynomial[logpoly_, theta_, tower_, Dtower_, extendtheta_]:= Module[
{A0, p0, infield, b, c, m, q0, q, beta, res},
RischTrace[IntegratePrimitivePolynomial, {logpoly, theta, tower}, "In"];
If[FreeQ[logpoly, Last[theta]],
Return[{0, True}]
];
infield = LimitedIntegrate[Simplify @ lc[logpoly, Last[theta]], theta, tower, Dtower];
If[infield === $Failed, Return[{0, False}], {b, c} = infield];
m = Exponent[logpoly, Last[theta]];
q0 = c Last[theta]^(m + 1)/(m + 1) + b Last[theta]^m;
(* we do need to use Simplify below, try Risch[Log[x] Log[2 x] Log[3 x], x] *)
{q, beta} = IntegratePrimitivePolynomial[
Simplify[Together[logpoly - TotalDerivation[q0, theta, Dtower]]],
theta, tower, Dtower, extendtheta
];
res = {Expand[q + q0], beta};
RischTrace[IntegratePrimitivePolynomial, res, "Out"];
res
];
(********************************************************
*
* Limited integration - recursive integration version
*
*********************************************************)
LimitedIntegrate[expr_, theta_, tower_, Dtower_] := Module[
{iexpr, t = Last[theta], a, c, bool, res, result},
RischTrace[LimitedIntegrate, expr, "In"];
(* Limited integrate -- recursive integration version. *)
Info["Recursively integrating ", expr];
iexpr = RischInternal[expr, theta, tower, Dtower, "ExtendThetas" -> False, "RecursiveCall" -> True];
(* We use structure theorems on result. e.g. Risch[x^2 Log[1-x]^2, x] fails without! *)
If[iexpr === $Failed, Return[$Failed]];
{bool, res} = MatchLogs[First[iexpr], theta, tower, Dtower];
If[bool === False (* !FreeQ[res, rischLog | rischExp | ArcTan] *),
Info["Recursive integration introduced new logarithms.", iexpr];
RischTrace[LimitedIntegrate, $Failed, "Out"];
Return[$Failed]
];
result = {Simplify[res - Coefficient[res, t] t], Coefficient[res, t]};
RischTrace[LimitedIntegrate, result, "Out"];
result
];
(*********************************************************
*
* Integration of exponential polynomials
*
**********************************************************)
RischExponentialPolynomial[p_, theta_, tower_, Dtower_, extendtheta_] := Catch[Module[
{poly = p, degMax, degMin, coeff, A0, nonthetaterm, result, rde, intA0},
RischTrace[RischExponentialPolynomial, {p, theta, tower, Dtower}, "In"];
(* Integration of exponential polynomials. Given
P = a_n Theta^n + a_ {n-1} Theta^{n-1} + ... + a_ 0
where a_ {i} are in k and Theta' = f' Theta, then differentiating gives us
P' = (a_ {n}' + n f' a_n) Theta^n + (a_ {n-1}' + (n-1) f' a_ {n-1}) Theta^{n-1} +
... + (a_ {1}' + f' a_ {1}) Theta + a_ {0}.
The following algorithm is based on how P acts under differentiation. *)
degMax = Exponent[poly, Last[theta]];
degMin = Exponent[poly, Last[theta], Min];
nonthetaterm = 0; result = 0;
(* solve a Risch DE for each power. *)
Do[
coeff = Coefficient[poly, Last[theta], n];
If[SameQ[n, 0] || SameQ[coeff, 0], Continue[]];
rde = RischDE[n (Last[Dtower]/Last[theta]), Cancel[coeff], theta, tower, Dtower];
If[SameQ[rde, $Failed],
RischTrace[RischExponentialPolynomial, $Failed, "Out"];
Throw[$Failed],
nonthetaterm = nonthetaterm + coeff Last[theta]^n;
result = result + rde*Last[theta]^n
]
, {n, degMin, degMax}
];
(* Integration of the theta^0 term. *)
A0 = Together[Cancel[Expand[poly - nonthetaterm]]];
If[! SameQ[A0, 0],
(* new logarithms are permitted *)
intA0 = RischInternal[A0, theta, tower, Dtower, "ExtendThetas" -> extendtheta, "RecursiveCall" -> True];
If[SameQ[intA0, $Failed], Throw[$Failed]];
result = result + First[intA0]
];
RischTrace[RischExponentialPolynomial, result, "Out"];
Throw[result]
]];
(******************************************************************
*
* The Risch Differential Equation
*
*******************************************************************)
RischDE::warning = "Integral not elementary - Risch DE: y' + `1` y = `2` has no \
solution.";
RischDE[f_, g_, inTheta_, inTower_, inDtower_] := Catch[Module[
{theta, tower, Dtower, q, fbar, gbar, rischden, a, b, c, den, n,
beta, num, result},
RischTrace[RischDE, {f, g, inTheta, inTower, inDtower}, "In"];
Info["Solve the Risch DE: ", "y'" + f"y" == g];
(* Solve the Risch differential equation using Davenport's weak normalisation,
Bronstein's fast reduction for computing the denominator and Rothstein's
special polynomial differential equation algorithm. *)
(* update the extension field *)
{theta, tower, Dtower} = UpdateExtensionField[{f, g}, inTheta, inTower, inDtower];
(* Test if f is weakly normalised wrt x, otherwise adding an appropriate
logarithmic derivative to f makes it weakly normalised. *)
q = WeakNormalizer[Together[f], theta, Dtower];
fbar = Cancel[Together[f - TotalDerivation[q, theta, Dtower]/q]];
gbar = Cancel[Together[q*g]];
(* If fbar and gbar are constant then trivial solution *)
Which[
MatchQ[gbar, 0] && And @@ (FreeQ[fbar, #] & /@ theta),
Throw[0],
And @@ (FreeQ[fbar, #] & /@ theta) && And @@ (FreeQ[gbar, #] & /@ theta),
Throw[g/f]
];
(* Compute the denominator of the solution. *)
rischden = RDENormalDenominator[fbar, gbar, theta, Dtower];
If[rischden === $Failed,
Message[RischDE::warning, f, g];
RischTrace[RischDE, $Failed, "Out"];
Throw[$Failed],
{a, b, c, den} = rischden;
(* We need this cancellation, try Risch[Exp[1/(x^2-1)]/(x-1)^2, x] *)
{a, b, c} = {Cancel[a], Cancel[b], Cancel[c]};
];
(* Bound the degree of the solution's numerator. *)
Which[
Head[Last[tower]] === rischLog,
n = PolyDEPrimitiveCase[a, b, c, theta, tower, Dtower],
Head[Last[tower]] === rischExp,
{a, b, c, n, beta} = PolyDEExponentialCase[a, b, c, theta, tower, Dtower],
True,
n = PolyDEBaseCase[a, b, c, theta]
];
If[n < 0,
Message[RischDE::warning, f, g];
RischTrace[RischDE, $Failed, "Out"];
Throw[$Failed]
];
(* Solve the polynomial form of the DE *)
num = SPDE[a, b, c, n, theta, tower, Dtower];
(* return solution *)
Which[
num === $Failed,
Message[RischDE::warning, f, g];
result = $Failed,
Head[Last[tower]] === rischLog,
result = num/(den q) // Cancel,
Head[Last[tower]] === rischExp,
result = Expand[Cancel[(num/(den*q))]]*Last[theta]^beta,
True,
result = num/(den q)
];
Info["Solution to the RDE is ", result];
RischTrace[RischDE, result, "Out"];
Throw[result]
]];
(******************************************************
*
* Weak normalisation
*
*******************************************************)
WeakNormalizer[f_, theta_, Dtower_] := Module[
{dn, ds, g, dstar, d1, Dd1, a, b , res, roots, PIR, result, Risch`z},
RischTrace[WeakNormalizer, {f, theta, Dtower}, "In"];
(* Weak normalisation, reference: Symbolic Integration 1, Bronstein p .183 *)
(* Given a derivation D on k[t] and f in k (t), return q in k[t] such that
f - Dq/q is weakly normalised with respect to t. *)
{dn, ds} = SplitFactor[Denominator[f], theta, Dtower];
g = PolynomialGCD[dn, D[dn, Last[theta]] ];
dstar = Cancel[dn/g];
d1 = Cancel[dstar/PolynomialGCD[dstar, g]];
Dd1 = TotalDerivation[d1, theta, Dtower];
{a, b} = ExtendedEuclidean[Cancel[Denominator[f]/d1], d1, Numerator[f], Last[theta]];
res = Resultant[a - Risch`z Dd1, d1, Last[theta]];
roots = Risch`z /. Solve[res == 0, Risch`z];
PIR = Select[roots, IntegerQ[#] && Sign[#] == 1 &];
result = Product[PolynomialGCD[a - PIR[[i]]*Dd1, d1]^PIR[[i]], {i, 1, Length[PIR]}];
RischTrace[WeakNormalizer, result, "Out"];
result
];
(*******************************************************
*
* Normal part of the denominator
*
********************************************************)
RDENormalDenominator[f_, g_, theta_, Dtower_] := Module[
{dn, ds, en, es, p, num, den, h, result},
RischTrace[RDENormalDenominator, {f, g, theta, Dtower}, "In"];
(* Normal part of the denominator, reference: Symbolic Integration 1, Bronstein p .185 *)
(* Given a derivation D on k[t] and f,g in k (t) with f weakly normalised wrt t, return either
no solution, in which case the equation Dy + f*y == g has no solution in k (t), or the
quadruplet (a,b,c,h) such that a,h in k[t], b,c in k<t>,
and for any solution y in k (t) of Dy + f*y == g, q = y*h in k<t> satisfies a*Dq + b*q = c. *)
{dn, ds} = SplitFactor[Denominator[f], theta, Dtower];
{en, es} = SplitFactor[Denominator[g], theta, Dtower];
p = PolynomialGCD[dn, en];
num = PolynomialGCD[en, D[en, Last[theta]]];
den = PolynomialGCD[p, D[p, Last[theta]]];
h = Cancel[num/den];
If[PolynomialRemainder[dn*h^2, en, Last[theta]] =!= 0,
(* Risch DE has no solutions *)
result = $Failed,
(* return normal part of the denominator *)
result = {dn*h, dn*h*f - dn*TotalDerivation[h, theta, Dtower], dn*h^2*g, h}
];
RischTrace[RDENormalDenominator, result, "Out"];
result
];
(**********************************************************
*
* Degree bound -- base case
*
**********************************************************)
PolyDEBaseCase[A_, B_, C_, theta_] := Module[{degA, degB, degC, a, b, r, res},
RischTrace[PolyDEBaseCase, {A, B, C, theta}, "In"];
(* Given A,B,C in K[x],return n such that deg (Q)<=n and Q satisfies A Q'+B Q==C. *)
(* bound the degree of the solution in k[theta] *)
degA = Exponent[A, Last[theta]];
degB = Exponent[B, Last[theta]];
degC = Exponent[C, Last[theta]];
Which[
degA < degB + 1,
res = degC - degB,
degA > degB + 1,
res = Max[0, degC - degA + 1],
degA == degB + 1,
a = lc[A, Last[theta]];
b = lc[B, Last[theta]];
r = Cancel[-b/a];
If[IntegerQ[r],
res = Max[r, degC - degB],
res = degC - degB
]
];
RischTrace[PolyDEBaseCase, res, "Out"];
res
];
(******************************************************
*
* Degree bound -- logarithmic case
*
******************************************************)
PolyDEPrimitiveCase[A_, B_, C_, theta_, tower_, Dtower_] := Catch[Module[
{degA, degB, degC, a, b, int, alpha, struct, J, H, n, r},
RischTrace[PolyDEPrimitiveCase, {A, B, C, theta, tower, Dtower}, "In"];
(* Given A,B,C in k[theta], with A!=0 safisfying A Q' + B Q = C,
where theta is primitive. Return Q in k[theta] satisfying the above DE. *)
degA = Exponent[A, Last[theta]];
degB = Exponent[B, Last[theta]];
degC = Exponent[C, Last[theta]];
Which[
degA == degB && degB != 0,
a = lc[A, Last[theta]];
b = lc[B, Last[theta]];
int = RischInternal[b/a, theta, tower, Dtower, "ExtendThetas" -> True, "RecursiveCall" -> True];
If[int === $Failed,
RischTrace[PolyDEPrimitiveCase, $Failed, "Out"];
Throw[$Failed]
];
struct = IsStructureExtended[rischExp[-int], theta, tower];(* simple cancellation cases *)
If[!First[struct],
alpha = Last[struct];
H = PolyDEPrimitiveCase[alpha*A, TotalDerivation[alpha, theta, Dtower]*A + alpha*B, C, theta, tower];
If[H === $Failed,
RischTrace[PolyDEPrimitiveCase, $Failed, "Out"];
Throw[$Failed]
];
RischTrace[PolyDEPrimitiveCase, alpha H, "Out"];
Throw[alpha*H]
];
RischTrace[PolyDEPrimitiveCase, degC - degB, "Out"];
Throw[degC - degB],
degA < degB,
RischTrace[PolyDEPrimitiveCase, degC - degB, "Out"];
Throw[degC - degB],
degA > degB + 1,
RischTrace[PolyDEPrimitiveCase, Max[0, degC - degA + 1], "Out"];
Throw[Max[0, degC - degA + 1]],
degA == degB + 1,
a = lc[A, Last[theta]];
b = lc[B, Last[theta]];
J = RischInternal[b/a, theta, tower, Dtower, "ExtendThetas" -> True, "RecursiveCall" -> True];
If[J === $Failed,
RischTrace[PolyDEPrimitiveCase, $Failed, "Out"];
Throw[$Failed],
J = First[J]
];
If[!FreeQ[J, Last[theta]] && FreeQ[Denominator[J], Last[theta]],
J = -J;
r = Coefficient[J, Last[theta]];
If[IntegerQ[r],
RischTrace[PolyDEPrimitiveCase, Max[r, degC - degB], "Out"];
Throw[Max[r, degC - degB]],
RischTrace[PolyDEPrimitiveCase, degC - degB, "Out"];
Throw[degC - degB]
],
RischTrace[PolyDEPrimitiveCase, degC - degB, "Out"];
Throw[degC - degB]
]
]
]];
(*****************************************************
*
* Degree bound -- exponential case
*
******************************************************)
PolyDEExponentialCase[C1_, C2_, C3_, theta_, tower_, Dtower_] := Module[
{A = C1, B = C2, C = C3, nb, nc, rem, n, b, m, eta, degA, degB, degC, a, H, alpha, beta, res},
RischTrace[PolyDEExponentialCase, {C1, C2, C3, theta, tower, Dtower}];
(*Given A in k[theta] aith A (0)!=0,B,C in k[theta,theta^-1],
where theta is exponential and A,B,C satisfy A Q'+B Q = C.
Return either $Failed in which case there is no solution in k[theta],
or {m,b} such that m is a bound on the degree of Q and.*)
(*Find a lower bound on the order of 0 at Q*)
nb = OrderFunction[B, Last[theta], Last[theta]];
nc = OrderFunction[C, Last[theta], Last[theta]];
If[MatchQ[nb, 0],
rem = Remainder[-B/A, Last[theta], Last[theta]];
n = ParametricLogarithmicDerivative[rem, theta, tower, Dtower];
If[n === $Failed,
b = Min[0, nc],
b = Min[0, First[n], nc]
],
b = Min[0, nc - Min[0, nb]]
];
m = Max[0, -nb, b - nc];
(*Convert equation to one in k[theta]*)
eta = Last[tower] /. rischExp[arg_] :> arg;
B = (b*TotalDerivation[eta, theta, Dtower]*A + B)*Last[theta]^m;
A = A*Last[theta]^m;
C = C*Last[theta]^(m - b);
(*At this point A,B,C in k[theta],and if H in k[theta] satisfies
A H'+B H=C,then Q=H*theta^b is a solution to the original DE*)
(*Find a bound on deg (H) and solve the polynomial equation*)
degA = Exponent[A, Last[theta]];
degB = Exponent[B, Last[theta]];
degC = Exponent[C, Last[theta]];
Which[
degA < degB,
m = degC - degB,
degA > degB,
m = Max[0, degC - degA],
degA == degB,
alpha = lc[A, Last[theta]];
beta = lc[B, Last[theta]];
n = ParametricLogarithmicDerivative[-beta/alpha, theta, tower, Dtower];
If[n === $Failed,
m = degC - degB,
m = Max[0, First[n], degC - degB]
]
];
res = {A, Expand[B], Expand[C], m, b};
RischTrace[PolyDEExponentialCase, res, "Out"];
res
];
(**********************************************************
*
* Rothstein's SPDE algorithm
*
**********************************************************)
SPDE[c1_, c2_, c3_, n_, theta_, tower_, Dtower_] := Catch[Module[
{A = c1, B = c2, C = c3, G, res1, Q, etheta, etower, H, m,
b, c, derivation, solution, qr, r, bc, Z, res},
RischTrace[SPDE, {c1, c2, c3, n, theta, tower, Dtower}, "In"];
(* Rothstein's special polynomial differential equation algorithm
for solving A Q' + B Q = C, with a degree bound n on the solution Q in
k[theta] and A,B,C in k[theta] *)
If[C == 0,
RischTrace[SPDE, 0, "Out"];
Throw[0]
];
G = PolynomialGCD[A, B];
If[PolynomialRemainder[C, G, Last[theta]] =!= 0,
RischTrace[SPDE, $Failed, "Out"];
Throw[$Failed]
];
A = Cancel[A/G];
B = Cancel[B/G];
C = Cancel[C/G];
Which[
MatchQ[B, 0],
res1 = RischInternal[C, theta, tower, Dtower, "ExtendThetas" -> True, "RecursiveCall" -> True];
If[res1 == $Failed,
RischTrace[SPDE, $Failed, "Out"];
Throw[$Failed]
];
Q = First[res1];
If[Exponent[Q, Last[theta]] <= n,
RischTrace[SPDE, Q, "Out"];
Throw[Q],
RischTrace[SPDE, $Failed, "Out"];
Throw[$Failed]
],
Exponent[A, Last[theta]] > 0,
bc = Simplify/@ExtendedEuclidean[B, A, C, Last[theta]];
{qr, r} = PolynomialQuotientRemainder[First[bc], A, Last[theta]];
Z = B*qr + Last[bc];
If[Exponent[r, Last[theta]] > n,
RischTrace[SPDE, $Failed, "Out"];
Throw[$Failed]
];
H = SPDE[A, B + TotalDerivation[A, theta, Dtower],Simplify[Z - TotalDerivation[r, theta, Dtower]],
n - Exponent[A, Last[theta]], theta, tower, Dtower];
(* I am not sure how one can avoid using Simplify here,
given that we are only simplifying rational expressions
it should be fairly fast... *)
If[H === $Failed,
RischTrace[SPDE, $Failed, "Out"];
Throw[$Failed]
];
If[Exponent[H, Last[theta]] > n,