-
Notifications
You must be signed in to change notification settings - Fork 1
/
kovacicODE.mac
1162 lines (967 loc) · 39 KB
/
kovacicODE.mac
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
/* kovacicODE.mac
Solve second order linear ODEs with Liouvillian solutions using Kovacic' algorithm
References:
[1] Carolyn J. Smith A discussion and implementation of Kovacic' algorithm,
MSC thesis university of waterloo, 1984
https://cs.uwaterloo.ca/research/tr/1984/CS-84-35.pdf
[2] B.D. Saunders An implementation of Kovacic's algorithm for solving second order linear homogeneous differential equations
SYMSAC '81 Proceedings of the fourth ACM symposium on Symbolic and algebraic computation
Pages 105-108
[3] Jerald J. Kovacic, An algorithm for solving second order linear homogeneous differential equations, Journal of Symbolic Computation, v.2 n.1, p.3-43, March 1986
Copyright (C) 2014 Nijso Beishuizen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*****************************************************************************************************/
put('kovacicODE,002,'version)$
/*****************************************************************************************************/
/* ----- print everything with flag lower than DEBUGFLAG ----- */
_DEBUGFLAG:0$
/* load absimp to get rid of abs(a) when a>0 */
if get('absimp,'version)=false then
load(absimp)$
/* NOTES */
/* remember: one of the necessary conditions might be too strict, see kovacic, compare with Saunders !!!*/
/* we should check for linear independence by checking that the wronskian has nonzero determinant*/
/* y1 y2 */
/* W = y1' y2'*/
/* */
/* (det(W)) = (y1y2' -y1'y2') */
/* (det(W))' = (y1y2' -y1'y2')' */
/* so detW is in C */
/* 1a. check the paper of Ulmer & Weil */
/* 1b. check paper of Man, combining with Prelle-Singer algorithm */
/* 2: check if linearizable by lie point transformation */
/* 3. check if linearizable by generalized Sundman transformation */
/* 4. check if linearizable by euler-Liouville transformation */
/* 5. check if linearizable by cartan equivalence */
/* 6. check if transformable from linear second order without liouvillian */
/* _solutions to linear second order with liouvillian _solutions */
/* for this we need to have a mapping to a set of standard linear equations */
/* for instance, how does y"=xy transform into y"=0 ?*/
/*****************************************************************************************************/
/* ----- Check if y(x)=_solution is a _solution of (differential) equation ----- */
/*****************************************************************************************************/
isSolution(_solution,_equation):=block([_eq,_subs1],
/* assumes equation contains "=" */
_eq:rhs(_equation)-lhs(_equation),
/* assumes _solution has the form y(x)=expression */
_subs1:subst(_solution,_equation),
return(ratsimp(ev(_subs1,diff)))
)$
/*****************************************************************************************************/
/*****************************************************************************************************/
/* ----- Check if expr is an equation (containing a "=" ) ----- */
/*****************************************************************************************************/
isEquation(_expr) := block([],
if freeof("=",_expr) then return(false) else return(true)
)$
/* rules for linear second order equation: */
/* y'' = b(x)*y'+c(x)*y + d(x) */
/*****************************************************************************************************/
matchdeclare ([_b,_c,_d],freeof(_dy))$
matchdeclare ([_c1,_c2],freeof(_x))$
/* match for linear second order equation - can match dependencies:
depends(y,x)
ode2_linear(a*diff(y,x),y,x)
as well as regular variables:
ode2_linear(a*diff(y(x),x),y(x),x) */
defmatch (ode2_linear, _b*'diff(_dy,_x)+_c*_dy+_d,_dy,_x)$
defmatch (linear, _c1*_x+_c2,_x)$
/*****************************************************************************************************/
/*****************************************************************************************************/
/* input: expr: a second order linear ode of the form y"=f(x,y,y') */
/* output: the solution of the ode [solution] or false when no Liouvillian solution exists */
/*****************************************************************************************************/
kovacicODE(_expr,_y,_x) := block([_i,_ode_order,_ddy,_ode,_phi,_b,_c,_d,_s,_t,_r,_ord_inf,_l,_m,_n,_j,_trial,_squo,_srem,_tcont,_nu],
/*****************************************************************************************************/
/*****************************************************************************************************/
/* ----- first check if it is an equation (we only check for equal sign) ----- */
if isEquation(_expr)=false then (
dprint(0,_expr, " is not an equation."),
return(false)
),
/* ----- find the order of the ODE ----- */
_ode_order : derivdegree(_expr,_y,_x),
if (_ode_order=2) then (
dprint(1,"Second order ODE found.")
)
else (
dprint(0,_expr," is not a second order ODE."),
return(false)
),
/* ----- make sure the equation is in the form y'' = F(x,y,y') ----- */
_ddy: 'diff(_y,_x,2),
/* we only take the first _solution, in general, loop over the _solutions! */
_ode: solve(_expr,_ddy)[1],
_phi : rhs(_ode),
if (lhs(_ode)=_ddy) and (freeof(_ddy,_phi)) then (
dprint(1,grind(_ode))
)
else (
dprint(0,"could not separate second order differential operator ",_ode),
return(false)
),
/* ----- check if the equation is linear ----- */
if (ode2_linear(_phi,_y,_x)=false) then (
dprint(0,"ODE is not linear!"),
return(false)
)
else (
dprint(1,"ODE is linear"),
dprint(2,"b=",_b,", c=",_c,", d=",_d),
_b:-_b,
_c:-_c,
_d:-_d
),
/* ----- check if equation is homogeneous ----- */
/*if (ev(_phi,_y = 0)=0) then (*/
if (_d = 0) then (
dprint(1,"ODE is homogeneous:")
)
else (
dprint(1,"equation is nonhomogeneous, first trying homogeneous case")
),
/* ----- Transform to normal form y''= (s/t)*y, ----- */
_s : 2*diff(_b,_x) + _b*_b - 4*_c,
_t : 4,
/* remember, maple's normal(in expanded form) is like maxima's ratsimp */
_r : ratsimp(_s/_t),
_s : ratexpand(num(_r)),
_t : ratexpand(denom(_r)),
dprint(1,"y'' = s/t = ",_r),
dprint(1,"s = ",_s),
dprint(1,"t = ",_t),
/* ----- determine the quotient and remainder of _s/_t for the main variable _x ----- */
/* catch this because divide is not exactly polynomial long division and the quotient is not uniquely determined */
if freeof(_x,_s) and freeof(_x,_t) and (_t#0) then (
[_squo,_srem] : [_s/_t,0]
)
else (
[_squo,_srem] : divide(_s,_t,_x),
_squo:ratexpand(_squo), /* we need to do a ratexpand because we want to get the coefficient correctly*/
_srem:ratexpand(_srem) /* we need to do a ratexpand because we want to get the coefficient correctly*/
),
dprint(5,"quo = ",_squo),
dprint(5,"rem = ",_srem),
/* ----- compute a square free factorization ----- */
[_sdec,_tcont] : sqfree(_t,_x),
dprint(5,"_sdec square free = ",_sdec),
/* this is the order*/
_m : length(_sdec),
dprint(5,"m = ",_m),
_t : _tcont,
/* TODO: can we replace part(_sdec,i) with _sdec[i] ? */
for _i:1 thru _m do (
_t : _t*part(_sdec,_i)^_i
),
if _m>0 then (
_t1 : part(_sdec,1)
)
else (
_t1 : 1
),
if _m>1 then (
_t2: part(_sdec,2)
)
else (
_t2 : 1
),
dprint(5,"_t2 = ",_t2),
dprint(5,"t = ",_t),
dprint(5,"_t1 = ",_t1),
dprint(5,"_t2 = ",_t2),
dprint(5,"s = ",_s),
/* this does not give the same results as kovacic examples in the 1986 paper: is Smith wrong? */
_ord_inf : hipow(ratexpand(_t),_x) - hipow(ratexpand(_s),_x),
dprint(1,"order at infinity = ",_ord_inf),
_listl : [],
_oddti : true,
for _i from 3 step 2 thru _m do (
if (part(_sdec,_i)#1) then (
dprint(2,"_oddti is false, it cannot be case 1"),
_oddti : false
)
),
if _oddti and (featurep(_ord_inf/2,integer) or (_ord_inf>2)) then (
dprint(2,"case 1 applies"),
_listl : append(_listl,[1])
),
if not _oddti or (_t2 # 1) then (
dprint(2,"case 2 applies"),
_listl : append(_listl,[2])
),
if (_m <= 2) and (_ord_inf >= 2) then (
_listl : append(_listl,[4,6,12])
),
dprint(1,"list of cases that apply: L=",_listl),
if length(_listl)=0 then (
dprint(0,"No Liouvillian solutions exist!"),
return(false)
),
/* ************************************************* */
dprint(3,"end of preliminaries..."),
/* ************************************************* */
/* STEP 1 PART (a) */
_dfix : (min(_ord_inf,2) - hipow(ratexpand(_t),_x) - 3*hipow(ratexpand(_t1),_x))/4,
_thetafix : ratsimp((diff(_t,_x)/_t + 3*diff(_t1,_x)/_t1)/4),
dprint(5,"_dfix=",_dfix),
dprint(5,"_thetafix=",_thetafix),
/* STEP 1 PART (b) */
/* Poles of order 2: find the roots c1,...,c_k2 of _t2 */
/* first, get a list of all the unique roots */
_rlist2 : rootz(_t2,_x),
dprint(5,"_rlist2 = ",_rlist2),
/* find the leading coefficient of _t2 */
/*lcoeff : ratcoef(ratexpand(_t2),_x,hipow(ratexpand(_t2),_x)),*/
_t2 : _t2/lcoeff(_t2,_x),
dprint(5,"_t2=",_t2),
/* k2 is the number of roots of _t2*/
_k2:length(_rlist2),
dprint(5,"_k2 = ",_k2),
/* loop over all roots */
for _i:1 thru _k2 do (
dprint(5,"i=",_i),
trest : _t/_t2^2 * product((_x-_rlist2[_j])^2,_j,1,_i-1) * product((_x-_rlist2[_j])^2,_j,_i+1,_k2),
dprint(5,"srem = ",_srem),
dprint(5,"trest = ",trest),
dprint(5,"var = ",_x),
dprint(5,"_rlist2 = ",_rlist2[_i]),
_sol:undetcoeff(_srem,trest,_x,_rlist2[_i],2,2),
dprint(5,"_sol:",_sol),
_dd[_i] : ratsimp(1+4*_sol)^(1/2),
_theta[_i] : ratsimp(_dd[_i]/(_x-_rlist2[_i])),
dprint(5,"d1 = ",_dd[_i]),
dprint(5,"_theta1 = ",_theta[_i])
),
dprint(5,"m = ",_m),
/* high order poles */
/* STEP 3 PART (c) */
_k1 : _k2,
if member(1,_listl) then (
dprint(1,"1 is in L so we calculate higher order poles"),
for _i:4 thru _m step 2 do (
dprint(5,"_i = ",_i, "/",_m),
L_i : _sdec[_i],
dprint(5,"L_i=",L_i),
_rlisthigher : rootz(L_i,_x),
/*lcoeff : ratcoef(ratexpand(L_i),_x,hipow(ratexpand(L_i),_x)),*/
dprint(5,"sdec = ",_sdec),
dprint(5,"_i-1:",_i-1),
dprint(5,"_m:",_m),
dprint(5,"part1 = ",create_list(_sdec[ii],ii,1,_i-1)),
dprint(5,"part2 = ",L_i/lcoeff(L_i,_x)),
dprint(5,"part3 = ",create_list(_sdec[ii],ii,_i+1,_m)),
/* */
_sdec : append(create_list(_sdec[ii],ii,1,_i-1), [L_i/lcoeff(L_i,_x)], create_list(_sdec[ii],ii,_i+1,_m)),
dprint(5,"sdec = ",_sdec),
_nu : _i/2,
/* we could do for rt in _rlisthigher ??? */
for _j from 1 thru length(_rlisthigher) do (
_k1 : _k1 + 1,
rt : _rlisthigher[_j], /* !!! */
dprint(5,_t,_sdec[_i],_i),
trest : _t/_sdec[_i]^_i * product((_x-_rlisthigher[_l])^_i,_l,1,_j-1) * product((_x-_rlisthigher[_l])^_i,_l,_j+1,length(_rlisthigher)),
dprint(5,_srem,trest),
/* method of undetermined coefficients */
_sol:undetcoeff(_srem,trest,_x,rt,2*_nu,2*_nu),
if (_sol=0) then (
dprint(0,"No Liouvillian solutions exist"),
return(false)
),
dprint(3,"_sol = ",_sol),
_ac[_nu] : ratsimp(_sol^(1/2)),
for _k from _nu-1 thru 2 step -1 do (
_ac[_k] : _vtemp,
result : sum(_ac[_l]*_ac[_nu+_k-_l],_l,_k,_nu),
/* can we do _ac[k]=undetcoeff directly because result is already solved?*/
undet : undetcoeff(_srem,trest,_x,rt,2*_nu,_k+_nu),
dprint(5,"result:",result),
dprint(5,"undet:",undet),
_ac[_k] : rhs(solve(result=undet,_vtemp)[1]),
dprint(5,"_ac[_k]=",_ac[_k])
),
/* sum of _ac[k]*_ac[_nu+1-k] */
result : sum(_ac[_k]*_ac[_nu+1-_k],_k,2,_nu-1),
_dd[_k1] : (undetcoeff(_srem,trest,_x,rt,2*_nu,_nu+1)-result)/_ac[_nu],
result : sum(_ac[_k]/(_x-rt)^_k,_k,2,_nu),
_theta[_k1] : 2*result + _dd[_k1]/(_x-rt)
)
)
),
dprint(3,"_dd = ",_dd),
if _ord_inf>2 then (
dprint(1,"_ord_inf > 2"),
_dd[0]:1,
_theta[0]:0
) else
if _ord_inf = 2 then (
dprint(1,"_ord_inf=2"),
_sol:lcoeff(_s,_x)/lcoeff(_t,_x),
_dd[0]:ratsimp((1+4*_sol)^(1/2)),
_theta[0]:0
)
else if member(1,_listl) then (
dprint(1,"1 is in L"),
_nu:(-_ord_inf)/2,
dprint(5,"_nu = ",_nu),
dprint(5,"_squo = ",_squo),
dprint(5,"_x = ",_x),
dprint(5,"coeff=",coeff(_squo,_x,2*_nu)), /* should we just use lcoeff? it uses ratexpand..*/
_ac[_nu]:ratsimp(coeff(_squo,_x,2*_nu)^(1/2)),
dprint(5,"_ac[_nu]=",_ac[_nu]),
for _i from _nu-1 thru 0 step -1 do (
_ac[_i]:_vtemp,
dprint(5,"_i=",_i),
dprint(5,"_ac[_i]=",_ac[_i]),
_temp_x : sum(_ac[_j]*_ac[_i+_nu-_j],_j,_i,_nu),
dprint(5,"_temp_x = ",_temp_x),
dprint(5,"coeff = ",coeff(_squo,_x,_i+_nu)),
_ac[_i]:rhs(solve(_temp_x=coeff(_squo,_x,_i+_nu),_vtemp)[1]),
dprint(5,"_ac[_i]=",_ac[_i])
),
/* note: ac is not a list, we have just defined ac[1],ac[2]*/
dprint(5,"1.ac = ",_ac),
dprint(5,"_nu = ",_nu),
_temp_x : sum(_ac[_l]*_ac[_nu-_l-1],_l,0,_nu-1),
dprint(5,"tempvar = ",_temp_x),
if _nu=0 then (
_t : ratexpand(_t),
_srem : ratexpand(_srem),
_squo : ratexpand(_squo),
dprint(5,"lcoeff=",lcoeff(_t,_x)),
dprint(5,"degree = ",hipow(_t,_x)),
dprint(5,"coeff=",coeff(_srem,_x,hipow(_t,_x)-1)),
_aa: coeff(_srem,_x,hipow(_t,_x)-1),
_bb: lcoeff(_t,_x),
if (_bb#0) then (
_tmp1:_aa/_bb - _temp_x
)
else (
/* or should we simply bail out when the denominator=0?*/
_tmp1: -_temp_x
)
)
else (
_tmp1:coeff(_squo,_x,_nu-1) - _temp_x
),
dprint(5,"_tmp1=",_tmp1),
/* the denominator can be zero - we catch it by saying that d[0] should be zero then */
/* but is this true?*/
if (_tmp1=0 or _ac[_nu]=0) then (
_dd[0]:0
)
else (
_dd[0]:_tmp1/_ac[_nu]
),
dprint(3,"_dd[0] = ",_dd[0]),
_tmp2 : sum(_ac[_l]*_x^_l,_l,0,_nu),
_theta[0]:2*_tmp2
)
else (
dprint(2,"otherwise"),
_dd[0]:0,
_theta[0]:0
),
dprint(3,"d0 = ",_dd[0]),
dprint(3,"_theta0 = ",_theta[0]),
/* ************************************************* */
/* ***** step 2 - form trial d's and _theta's ***** */
/* ************************************************* */
dprint(1,"Now entering step 2: forming trial ds and _thetas, length= ",length(_listl)),
_solfound:0,
for i from 1 thru length(_listl) do (
dprint(3,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),
dprint(3,"!!! CASE ",_listl[i], " ",i,"/",length(_listl)," !!!"),
dprint(3,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),
_n:_listl[i],
/* Saunders */
if _n = 1 then (
_m : _k1
)
else (
_m : _k2
),
dprint(3,"m = ",_m),
if (_n=2) and (_ord_inf<2) then (
_dd[0]:0,
_theta[0]:0
),
/* s = (-n/2,-n/2+1,...n/2)*/
for _j from 0 thru _m do (
/* -n/2, -n/2, -n/2, -n/2, ... */
sq[_j]:-1/2 * _n
),
/* in case of n=1 (case 1) this is (-1/2, +1/2) */
_solution:[],
alls:false,
/* for all sequences s={-n/2, -n/2+1, ..., n/2}*/
/* preliminary: d0 should be */
_dslist:[],
while (alls # true) do (
/*tezt : sq[0]*d[0] - sum(sq[_l]*d[_l],_l,1,_m),*/
tezt : sq[0]*_dd[0],
for _l from 1 thru _m do (
tezt : tezt - sq[_l]*_dd[_l]
),
ds:ratsimp(_n*_dfix+tezt),
if not member(ds,_dslist) then (
_dslist:append(_dslist,[ds]),
dprint(3,"dslist:",_dslist),
dprint(3,"ds = ",ds, ", should be integer and >=0",featurep(ds,integer), " ",is(ds>=0)),
if (integerp(ds)=true) then (
if (ds>=0) then (
dprint(1,"ds is a positive or zero integer"),
tmp : sum(sq[_l]*_theta[_l],_l,0,_m),
dprint(5,"tmp : ",tmp),
_thetas:ratsimp(_n*_thetafix+tmp),
dprint(5,"_thetas = ",_thetas),
/* step3 - determine polynomial P if possible and hence omega and _solution */
dprint(5,"step 3"),
dprint(5,"facts of var:",facts(_x)),
_soln:step3(_n,ds,_thetas,_s/_t,_x),
dprint(5,"finished step 3"),
_soln : radcan(_soln), /* to simplify */
dprint(3,"_soln = ",_soln,length([_soln])),
if _soln#false then (
dprint(3,"finding sol2"),
ratio:_b,
/*gamma_expand:true, */ /* to get rid of incomplete gamma in saunders ex. 1 */
/* actually, better to work with gamma to prevent complex logarithms like kamke196 */
/* kamke 2.111: we should get exp(-x)/x here */
_soln1:exp(integrate(-(1/2)*ratio,_x)), /* list [soln] necessary?*/
dprint(3,"_soln1 = ",_soln1),
_soln1:_soln1*_soln,
dprint(3,"_soln1 = ",_soln1),
/* kill the impaginary part so we don't have to cancel out all the imaginary bits */
_soln1:trigsimp(radcan(_soln1)),
if not(freeof(%i,_soln1)) then (
_soln1 : trigsimp(realpart(_soln1))
),
_soln1:factor(_soln1),
_soln1:ev(_soln1,integrate),
/* logarc gets rid of arcsinh stuff inside exponentials */
/* sometimes this increases the complexity of the solution, so do a check afterwards */
_soln11:radcan(logarc(_soln1)),
/* if logarc has lead to a better solution, then use it */
_nrOps11 : nrOps(_soln11),
_nrOps1 : nrOps(_soln1),
/* use the radcan-logarc solution when the expression contains less operations
or less types of operations */
if ((length(_nrOps11) <= length(_nrOps1)) or (length(unique(_nrOps11))<=length(unique(_nrOps1))) ) then (
_soln1 : _soln11
),
_soln1:radcan(_soln1),
dprint(3,"x. _soln1 = ",_soln1),
/*soln2 :radcan(soln1*integrate((exp(-integrate(ratio,_x))/(soln1*soln1)),_x)),*/
_soln2:-integrate(ratio,_x),
_soln2:exp(_soln2)/(_soln1*_soln1),
dprint(3,"_soln2 = ",_soln2),
_soln22:radcan(logarc(_soln2)),
dprint(3,"_soln2 = ",_soln22),
/* if logarc has lead to a better solution, then use it */
_nrOps22 : nrOps(_soln22),
_nrOps2 : nrOps(_soln2),
/* use the radcan-logarc solution when the expression contains less operations
or less types of operations */
if ((length(_nrOps22) <= length(_nrOps2)) or (length(unique(_nrOps22))<=length(unique(_nrOps2))) ) then (
_soln2 : _soln22
),
dprint(3,"x1. _soln2 = ",_soln2,", ",_x),
_soln2:integrate(_soln2,_x),
dprint(3,"x2. _soln2 = ",_soln2),
_soln2:trigsimp(radcan(_soln1*_soln2)),
dprint(3,"x3. _soln2 = ",_soln2),
if not(freeof(%i,_soln2)) then (
dprint(3,"x3a. _soln2 = ",_soln2),
_soln2:trigsimp(realpart(_soln2)) /* take the real part and drop the imaginary part */
),
dprint(3,"x4. _soln2 = ",_soln2),
_soln2 : radcan(_soln2), /* to further simplify - we still have exp(2^(3/2)*x-sqrt(2)*x) here */
dprint(3,"x5. _soln2 = ",_soln2),
_soln2 : ev(_soln2,nouns),
dprint(3,"length=",length([_soln])),
if length([_soln]) = 1 then (
dprint(3,"_soln 1"),
_solution: [ratsimp(_soln1),ratsimp(_soln2)], /* realpart because we can have imaginary solutions for y'' + y = 0 and we don't want to deal with the trouble of cancelling them out elegantly by itself, so we just delete them*/
dprint(3,"homogeneous solution:",_solution),
_solfound : 1,
return(true)
)
else (
dprint(3,"_soln 2"),
_solution: [ratsimp(_soln1),ratsimp(_soln2)],op(2,[_soln]),
_solfound:1,
return(true)
)
)
else (
dprint(0,"No Liouvillian solutions exist"),
_solution: false
),
dprint(5,"alls:",alls)
)
)
), /* end of dsloop*/
_jj : _m-1,
dprint(5,"sq_jj = ",sq),
for _j from _m thru 0 step -1 do (
if (sq[_j] = (1/2 * _n)) then (
_jj : _jj - 1,
sq[_j]: -1/2 * _n
)
else (
_jj : 0,
sq[_j] : sq[_j] + 1,
/* exit (the for loop) */
return(true)
)
),
if (_jj < 0) then (
dprint(0,"No Liouvillian solutions exist"),
_solution: false,
alls:true
)
),
if _solfound=1 then (
return(true)
)
),
/* see if we canget rid of abs() terms in the solution */
_solution : abs_remove(_solution),
if (alls=false) then (
/* ----- nonhomogeneous _solution -----*/
dprint(1,"checking if a nonhomogeneous part exists"),
_P:0,
if (_d#0) then (
/* 2 independent _solutions of the homogeneous equation */
_s : _solution,
dprint(1,"nonhomogeneous part found - looking for particular solution"),
/* -------------------------------------------------- */
/* ----- METHOD 1 */
/* -------------------------------------------------- */
_E : exp(integrate(_b,_x)),
_PP : integrate(_E*_s[1]*_d,_x),
/* we need radcan because of the quotient by zero bug */
_PP : (_PP/(_E*_s[1]*_s[1])),
_PP : radcan(_PP),
/* there is an error somewhere - see kamke 184*/
_PP : -_s[1]*integrate(_PP,_x),
dprint(1,"00. particular solution : ", _PP),
/* perform the simplification after we have compared the two solutions */
/* we do this because simplification can lead to expression swell, like for */
/* example kamke 2.234 */
/* so we hope that simplification of the smallest term will still lead to */
/* a simpler expression than simplification of the largest term */
/* -------------------------------------------------- */
/* ----- METHOD 2 */
/* -------------------------------------------------- */
/* Determinant of the Wronskian */
_W : _s[2]*diff(_s[1],_x) - diff(_s[2],_x)*_s[1],
_W : ratexpand(_W),
_W : radcan(_W),
_W : trigsimp(_W),
_W : trigreduce(_W),
_W : ratsimp(_W),
dprint(5,"5. wronskian : ", _W),
dprint(3,"nonhomogeneous part : ", _d),
/* particular _solution of the ODE */
_P : _s[2]*integrate(_s[1]*_d/_W,_x) - _s[1]*integrate(_s[2]*_d/_W,_x),
dprint(1,"0. particular solution : ", _P),
/*_P : ratexpand(_P),*/
/*_P : trigexpand(_P),*/
/*
_P : trigreduce(_P),
print("1. particular solution : ", _P),
_P : trigsimp(_P),
print("2. particular solution : ", _P),
_P : radcan(_P),
print("3. particular solution : ", _P),
*/
/* substitute back the original variables */
/* -------------------------------------------------- */
/*_P : subst([_x=_x1],_P),*/
/* use method one, because it supposedly can give less complicated particular solutions */
/*print("x=",_x),*/
/*print("x1=",_x1),*/
/*
print("complexity of method 1 for particular solution: ",length(nrOps(_PP))),
print("complexity of method 2 for particular solution: ",length(nrOps(_P))),
*/
/* if method 1 has less operation than method 2, use method 1 */
if (length(nrOps(_PP)) <= length(nrOps(_P)) ) then (
_P : _PP,
dprint(3,"1: P = ",_P)
)
else (
/*_P : subst([_x=_x1],_P),*/
dprint(3,"2: P = ",_P)
),
dprint(3,"starting simplification"),
if freeof(abs,_P) then(
_P : trigreduce(_P),
_P : trigsimp(_P),
_P : radcan(_P),
_P : ratsimp(_P)
),
dprint(5,"3. particular solution : ", _P)
),
dprint(3,"0. solution[1]:",_solution[1]),
dprint(3,"0. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
_C1 : constant_factors(factor(_solution[1]),_x),
_C2 : constant_factors(factor(_solution[2]),_x),
_C3 : constant_factors(factor(_P),_x), /* combine constants into the integration constant */
_solution[1] : ratsimp(_solution[1]/_C1), /* ratsimp necessary for kamke 2.286*/
_solution[2] : ratsimp(_solution[2]/_C2),
/*_P : ratsimp(_P/_C3), */ /* divide particular solution by constant factor, absorbed by integration constants */
dprint(3,"1. P:",_P),
/* for some reason, we need to do ratsimp again to simplify, e.g. for the simple problem of y''+ay'+by=c */
/* lets also ratsimp the solution:*/
_solution[1]:ratsimp(_solution[1]),
_solution[2]:ratsimp(_solution[2]),
_P:ratsimp(_P),
dprint(3,"0. solution[1]:",_solution[1]),
dprint(3,"0. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
/* try to get rid of unevaluated integrals */
_solution[1]:ev(_solution[1],nouns),
_solution[2]:ev(_solution[2],nouns),
_P:ev(_P,nouns),
dprint(3,"0. solution[1]:",_solution[1]),
dprint(3,"0. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
_solution[1]:trigsimp(trigreduce(_solution[1])),
_solution[2]:trigsimp(trigreduce(_solution[2])),
_P:trigsimp(trigreduce(_P)),
dprint(3,"0. solution[1]:",_solution[1]),
dprint(3,"0. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
/*
if not lfreeof([log,%e],_solution[1]) then _solution[1]:logcontract(_solution[1]),
if not lfreeof([log,%e],_solution[2]) then _solution[2]:logcontract(_solution[2]),
if not lfreeof([log,%e],_P) then _P:logcontract(_P),
*/
dprint(3,"0. solution[1]:",_solution[1]),
dprint(3,"0. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
_solution[1]:ratsimp(_solution[1]),
_solution[2]:ratsimp(_solution[2]),
_P:ratsimp(_P),
dprint(3,"1. solution[1]:",_solution[1]),
dprint(3,"1. solution[2]:",_solution[2]),
dprint(3,"1. P:",_P),
_solution: [_y = _solution[1]*%k1 + _solution[2]*%k2 + _P]
),
dprint(1,"solution:",grind(_solution)),
/* also return the method used to solve the ode */
method : 'kovacic,
return(_solution)
)$
/*****************************************************************************************************/
step3(_n,_d,_theta,rhs1,_x):=block([_sol,_solution,_p,_listv,_i,_a,_a0,_a1,_a2,_a3,_a4,_a5,_pr,_sete,_soln,_trial,_w,_lset],
/*****************************************************************************************************/
/* ************************************************* */
/* ***** step 3 - ***** */
/* ************************************************* */
/*# form P in terms of undetermined coefficients a_i */
/*# P = a-{d}*x^{d} + a_{d-1}*x^{d-1} + ... + a_0*/
dprint(3,"_x=",_x),
dprint(3,"facts(_x)=",facts(_x)),
dprint(3,"degree of polynomial = ",_d),
dprint(3,"n=",_n),
dprint(3,"_theta=",_theta),
dprint(3,"rhs1=",rhs1),
/* start with highest order*/
_p:_x^_d,
dprint(3,"P = ",_p),
/* [a_d-1, a_d-2, ..., a_2, a_1, a_0] */
_listv : makelist(concat(_a,_d-1 -_i),_i,_d-1,0,-1),
_p : _p + sum(_listv[_i]*_x^(_i-1),_i,1,length(_listv)),
dprint(3,"_listv=",_listv),
/*# generate recursive relations P_i */
_pr[_n]: -_p,
for _i from _n thru 0 step -1 do (
_pr[_i-1]: ratsimp(-diff(_pr[_i],_x) - _theta * _pr[_i] - (_n-_i)*(_i+1)*rhs1*_pr[_i+1])
),
dprint(3,"P-1 = ",_pr[-1]),
_trial:ratexpand(num(ratsimp(_pr[-1]))),
dprint(3,"_trial = ",_trial),
if _trial # 0 then (
_sete : [], /* */
dprint(3,"low degree = ",lopow(_trial,_x)),
dprint(3,"high degree = ",hipow(_trial,_x)),
for _i from lopow(_trial,_x) thru hipow(_trial,_x) do (
_lset:coeff(_trial,_x,_i),
dprint(3,"lset:",_lset),
if (_lset # []) then (
_sete:endcons(_lset,_sete) /* create list as [sete,lset] */
)
),
dprint(5,"sete = ",_sete),
dprint(5,"_listv = ",_listv),
if(length(_listv)=0) then (
dprint(0,"No Liouvillian solutions exist"),
return(false)
),
dprint(5,"a0 = ",_a0),
_soln:solve(_sete,_listv),
dprint(1,"_soln=",_soln),
/*if (_soln[1]=[] or length(_soln)=0 ) then (*/
if (length(_soln)=0 ) then (
dprint(1,"no solutions"),
return(false)
),
/* for more than 1 element, the solution has two brackets: [[1,2,3,...]]*/
if (length(_listv)#1) then (
_soln:_soln[1]
),
/*_soln:_soln[1],*/
dprint(5,"determining ais"),
/* ai = _soln[d-_i] */
/* finally, we are getting some stuff here for kamke 2.129 */
for _i from _d-1 thru 0 step -1 do(
map(":",[concat(_a,_i)],[rhs(_soln[_d-_i])])
)
),
dprint(5,"a0 = ",_a0),
dprint(5,"P = ",_p),
_trial : sum(_pr[_i]*_w^_i/(_n-_i)!,_i,0,_n),
dprint(3,"_trial = ",grind(_trial)),
dprint(3,"w = ",_w),
_solution:solve(ev(_trial),_w),
if _solution=[] then (
dprint(2,"w is empty: ",_w),
dprint(2,"NOTE: we still need to correct the return value!"),
return(exp(int(_w,_x)),_trial=0)
),
dprint(1,"we have a genuine _solution:",_solution),
_w:ratsimp(factor(_solution[1])), /* we need factor, it's like bringing it in normal form */
/* this call to ratexpand causes radcan to behave strangely (doesn't simplify certain things) later on */
/* print("expanded = ",ratexpand(_w)),*/
dprint(1,"w=",_w),
_sol:exp(integrate(rhs(_w),_x)),
dprint(1,"sol=",_sol),
/*if(imagpart(sol)#0) then (*/ /* this also detects log(x) as imaginary */
if not freeof(%i,_sol) then (
dprint(1,"solution has an imaginary part!"),
_sol:realpart(_sol),
dprint(1,"sol=",_sol)
),
return(_sol)
)$
/*****************************************************************************************************/
/*****************************************************************************************************/
lcoeff(_t2,_x):=block([],
return(ratcoef(ratexpand(_t2),_x,hipow(ratexpand(_t2),_x)))
)$
/*****************************************************************************************************/
/* ----- calculate the roots and expand the multiple roots ----- */
/*****************************************************************************************************/
rootz(_expr,_x):=block([_sol,_mult,_rlist2,_j,_i],
/* note: this has the potential to fail */
_sol:solve(_expr,_x),
/* get the multiplicities of the roots*/
_mult:multiplicities,
/* create a list of roots including multiple roots */
_rlist2: flatten( makelist( makelist(rhs(_sol[_j]),_i,1,_mult[_j]),_j,1,length(_sol))),
return(_rlist2)
)$
/*****************************************************************************************************/
/* ----- determine coefficient of a factor in a partial fraction expansion ----- */
/*****************************************************************************************************/
undetcoeff(_num,_rden,_x,_root,_m,_ex):=block([_k,_p,_er],
dprint(3,"undetcoeff:",_m-_ex,_rden,_num),
_k : _m - _ex,
_p : _num/_rden,
/* differentiate p with respect to _x k times */
_p : ratsimp(diff(_p,_x,_k)),
dprint(5,"_p:",_p),
/* catch errors, it now returns empty when an error occurs (e.g. when we have non-polynomial expressions) */
_er: errcatch(subst(_root,_x,_p)/_k!),
if (_er=[]) then (
_er:0
) else (
_er:_er[1]
),
dprint(3,"_er:",_er),
return(_er)
)$
/*****************************************************************************************************/
/* ----- Transform second order ode y"=f(x)y'+g(x)y to normal form y"=F(x)y ----- */
/*****************************************************************************************************/
Normalform(_phi,_y,_x) := block([_f,_g,_df,_F],
_f : coeff(ratexpand(_phi),_y),
_g : coeff(ratexpand(_phi),'diff(_y,_x)),
_df : diff(_f,_x),
_F: radcan(ratsimp(_g - _f*_f/4 - _df/2)),
return(_F)
)$
/*****************************************************************************************************/
/* ----- transforms _expr from normal form solution to actual solution using rhs of the ode ----- */
/*****************************************************************************************************/
NormalSolutionToActualSolution(_expr,_phi) :=block([_f,_g,_df,_sol],
_f : coeff(ratexpand(_phi),_y),
_g : coeff(ratexpand(_phi),'diff(_y,_x)),
_df : diff(_f,_x),
_sol : radcan(ratsimp(_expr*exp(integrate(_f,_x)/2))),
return(_sol)
)$
/*****************************************************************************************************/
/* ----- return the primary part of the polynomial -----*/
/*****************************************************************************************************/
primpart(_expr,_x):=block([],
return(_expr/content(_expr,_x))
)$
/*****************************************************************************************************/
/* ----- Squar-free decomposition (Yun, On square free decomposition algorithms) ----- */
/*****************************************************************************************************/
sqfree(_expr,_x):=block([_i,_signp,_tc,_tlist1,_c,_d,_n,_cc,_re,_pp,_cont,_g,_PPP],