-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprellesingerODE.mac
2020 lines (1750 loc) · 70.8 KB
/
prellesingerODE.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
/*****************************************************************************/
/* */
/* O D E F I */
/* */
/* Differential Equation solver using First Integrals */
/* Based on the Prelle-Singer Algorithm and the ODEFI code */
/* Original author: R. Shtokhamer, 1988 */
/* Re-implementation: N. Beishuizen, 2014-2015 */
/* To do: 1. run kamke database, check nr of equations solved and time */
/* 2. improve cases that can be solved */
/* May 14, 2015: all cases mentioned in Man,1993 can be solved except the
cases with the highest N and longest running times
additionally, we can solve cases with arbitrary functions, like kamke 1.35
*/
/* 3. extended prelle-singer method
4. system of ode's
5. second order ode's
6. nth order ode's
*/
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Extending the ODE package. */
/* */
/* Includes: The Prelle-Singer method, and a subroutine for finding special */
/* forms of an integrating factor. */
/*****************************************************************************/
/* GLOBALS: */
/* ----- THE DEFAULT SETTINGS ----- */
/* */
define_variable(type_of_intfactor ,3 ,fixnum)$ /* 1=heuristics only, 2=prelle-singer only, 3=1+2*/
define_variable(elementary ,4 ,fixnum)$
define_variable(primitiveflag ,false ,boolean)$
define_variable(LISTOFTERMS ,false ,any)$
define_variable(MAXTERMS ,6 ,fixnum)$ /* the maximum number in the monomials 1+x + y +x^2 + xy + y^2 +..*/
define_variable(INTFACTOR_FI ,false ,any)$ /* global variable containing the integrating factor */
define_variable(METHOD_FI ,false ,any)$ /* global variable containing the method used, prelle-singer or one of the heuristics */
define_variable(ODEFI_GRAD_LIST ,[] ,any)$ /* our own definitions of gradients of functions */
define_variable(Y_X_DEPEND ,false ,boolean)$ /* we have y(x) and diff(y(x),x) instead of y and diff(y,x) */
/* */
define_variable(nil,[],any_check,"nil has to bound to []")$
put('nil,lambda([x],if x # [] then error("Don't reset nil")),'value_check)$
/* *************************************************************************** */
/* *************************************************************************** */
prellesingerODE(expr,_y ,_x):= block([_a:0,_b,_p:1,_q:1,res:false,%x%,%y%,mon2nspace:10,newres,oldres],local(%y%,%x%),declare(mon2nspace,integer),
/* check the input and return the ODE, the dependent variable, the independent variable and the quotient M,N of dy/dx=M/N */
[res,%y%,%x%,[_q,_p]]: checkPSInput(expr,_y,_x,'firstorder),
if (res=false) then return(false),
print("ode = ",expr),
print("y = ",%y%),
print("x = ",%x%),
print("_M = ",_q),
print("_N = ",_p),
/* ----- Get the integrating factor ------- */
/* ---------------------------------------- */
res:intfactor_control(_p,_q,%x%,%y%),
/* ---------------------------------------- */
print("res = ",res),
if res=false or (res[1]=false and res[2]=false)then (remarray(vfunv,vifunv),return(false)),
print("some solution found...",res),
if first(res) #false then ( /* " Rational Solution obtained " */
print("rational solution"),
res: rootscontract(first(res)) - %c
)
else if second(res) # false then ( /* "integrating factor obtained " */
print("integrating factor = ",grind(INTFACTOR_FI)),
/* do some simplification here (should be moved to end of intfactor_control)*/
INTFACTOR_FI: ratsimp(trigsimp(INTFACTOR_FI)),
/* rootscontract converts products of roots into roots of products, simplifies an expression like 1/(sqrt(x-%i)sqrt(x+%i)) = 1/(sqrt(x^2+1)) */
/* note that abs(x) can also be put into the root when x is real*/
INTFACTOR_FI: rootscontract(INTFACTOR_FI),
INTFACTOR_FI: radcan(INTFACTOR_FI), /* will simplify e.g. exp(log(a*x+b)) */
print("MU = ",INTFACTOR_FI),
res: integrate(mysimp(-INTFACTOR_FI*_q),%x%),
print("res = ",res),
res: res + integrate(INTFACTOR_FI*_p,%y%) - integrate(trigsimp(ratsimp(diff(res,%y%))),%y%) +%c,
/* FIXME: merge with cleanupsolution */
if not freeof(log,res) then res:logcontract(mysimp(res))
),
print("treating grad_list"),
if ODEFI_GRAD_LIST # nil then (
res:subst(ODEFI_GRAD_LIST,res),
/*_p:subst(ODEFI_GRAD_LIST,_p),*/ /* we never use this again, why substitute back? */
/*_q:subst(ODEFI_GRAD_LIST,_q),*/
INTFACTOR_FI:subst(ODEFI_GRAD_LIST,INTFACTOR_FI),
remlet(all,defi_let_package)
),
print("before res = ",grind(res)),
res: cleanupODESolution(res,%y%,%x%),
print("after res = ",grind(res)),
/* substitute the dependency back */
if Y_X_DEPEND then (
res:subst(_y,%y%,subst(_x,%x%,res)),
LISTOFTERMS:subst(_y,%y%,subst(_x,%x%,LISTOFTERMS)),
INTFACTOR_FI:subst(_y,%y%,subst(_x,%x%,INTFACTOR_FI))
),
print("returning res = ",grind(res)),
return(res)
)$
/* */
/*****************************************************************************/
intfactor_control(_N,_M,vx,vy):=
/* */
/* This is the controlling routine for finding integrating factors */
/* It chooses type of algorithm applied depending on the flags */
/* Applies heuristics as well as the P-S algorithm */
/* Returns a rational First-Integral if such found using the P-S method */
/* The "Prelle-Singer" algorithm is adopted to treat transcendentals */
/* LOCAL VARIABLES: */
/* degree = bound on the order of monomial in solution of df/f */
/* nterms = number of terms allowed or list (of list) of monomials */
/* trlst = list of the transcendentals appearing in grad */
/* prlst = some other symbolic parameters appearing in p,q, in the */
/* case that prlst # nil consistency conditions can be */
/* obtained to enable solutions of the problem */
/* THIS OPTION IS NOT IMPLEMENTED IN THIS VERSION SO */
/* prlst is set to nil */
/* n2mon and mon2n are two function to be supplied by the user */
/* (a crude version is supplied ) */
/* n2mon is bijection from integers to monomials */
/* mon2n is inverse of n2mon */
/* OUTPUT: */
/* false, if no integrating factor or rational solution are found. */
/* otherwise the result is set to [ratres,integfactor] */
/* where a) in case that rational first-integral is found */
/* then ratres is set to the solution and integfactor is false */
/* b) else ratres is false and integfactor is the INTFACTOR */
/* The global variable METHOD_FI describes the method used */
/* In the case that a "rational" first-integral is found */
/* using the Prelle-Singer method, firstintegral is set to the */
/* solution and METHOD_FI to:" Prelle-Singer rational first integral" */
/* */
block([_f,_g,_p,solutions,_sol,primitive,sol1,heursol:false,lg,defpol,gradnew,
algexact:true,ratalgdenom:true,algebraic:true,
exptsubst:true,a,b,trlst,prlst:nil,degree,lst,nterms,
rootscondmode: 'all,radexpand:'all, rhs,newtr], declare([lg],integer),
/* dy/dx = M/N ==> Ndy = Mdx Mdx - Ndy = 0 */
print("M = ",_M),
print("N = ",_N),
/* ----------------------------------------------------------------------------- */
/* ----- try heuristics, when type_of_intfactor=2, only P-S method is used ----- */
if (type_of_intfactor#2) then (
print("trying heuristics..."),
[METHOD_FI,INTFACTOR_FI]:specialintfactor(_M,_N,vx,vy),
print("method =", METHOD_FI),
print("mu = ", INTFACTOR_FI),
if METHOD_FI#false and INTFACTOR_FI #false then return([false,INTFACTOR_FI])
),
/* ----------------------------------------------------------------------------- */
/* ----- if an integrating factor was found then we can stop now ......... ----- */
/* ----------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------- */
/* ----- compute the basis of functions */
trlst : BasisFunctions(_M,_N,vy,vx),
print("----- BASIS OF FUNCTIONS = ",trlst),
/* ----------------------------------------------------------------------------- */
/* nijso moved out of the loop */
/* Only the trivial extension F(i) is checked, Future releases will include */
/* general treatment of algebraic extensions */
/* */
if freeof(%i,_N) and freeof(%i,_M) then (
primitive:nil,
defpol:nil
) else (
primitive:%i,
block([simp:false],defpol:%i^2+1)
),
/* nijso added loop over degrees */
exittheloop:false,
degree:5,
while degree <= MAXTERMS do (
print("---------------------------"),
print("degree = ",degree),
print("---------------------------"),
/* this finds -(dP/dx + dQ/dy) for the ode Qdx=Pdy or dy/dx=Q/P */
rhs:newgradient([_N,_M],[vx,vy],trlst,degree),
print("gradients: ",rhs),
newtr:first(rhs), /* List of transcendentals */
gradnew:second(rhs) ,
rhs:third(rhs) , /* this is the actual -(dP/dx+dQ/dy) term */
/* */
/* lst is the list of transcendentals */
/*degree:if MAXTERMS < 0 then - MAXTERMS else max(MAXTERMS,length(lst)+1), */
/*degree:if MAXTERMS < 0 then - MAXTERMS else max(MAXTERMS,length(trlst)+1), */
/*degree:if MAXTERMS < 0 then - MAXTERMS else MAXTERMS, */
nterms:if LISTOFTERMS=false then degree else LISTOFTERMS,
/* */
if listp(nterms) then (
/* Transform the list nterms into list of numbers identifying terms */
b:trlst,
for a in newtr do (
nterms:ratsubst(a,first(b),nterms),
b:rest(b)
),
nterms :ordl1(nterms,newtr)
),
print("nterms: ",nterms),
/* nterms is number of terms in polynomial, i.e.
f = f1 + f2*x + f3*y + f4*x^2 + f5*xy + f6*y^2 + f7*x^3 + f8*x^2y + f9*xy^2 + f10*y^3 */
/* when degree is 3 (MAXTERMS) then the above polynomial is the largest that will be tested */
/* */
/* Now solve the Dp/p problem (The Darboux polynomials) */
/* Construct all monic polynomials fi of degree <= MAXTERMS */
/*solutions:prelle_singer_dfovf(degree,nterms,gradnew,newtr,prlst, */
solutions:prelle_singer_dfovf(degree,nterms,gradnew,newtr,prlst,
primitive,defpol,n2mon,mon2n),
print("--------------------------------------------------------------"),
print("solutions: ",solutions),
print("solutions: ",grind(solutions)),
print("--------------------------------------------------------------"),
primitive:second(solutions), /* */
defpol:third(solutions),
/* note that we re-use the variable p == _N here*/
_sol:rest(solutions,3),
solutions:first(solutions),
/*print("heursol = ",heursol),*/ /* we only arrive here when heursol=false*/
if solutions=nil then (
METHOD_FI:concat("No Solution Found to the Dp/p Problem.
Try to increase MAXTERMS. ")
) else (
lg:first(_sol),
/* this is the right hand side -(P_x +Q_y) */
print("r1Df/f + r2Df/f = rhs: ",rhs),
/* this is the list of f's */
print("trlst: ",trlst),
print("newtr: ",newtr),
print("primitive: ",primitive),
print("defpol: ",defpol),
print("lg: ",lg),
/* */
/* Check the dependencies */
/* */
_sol:prelle_singer_dep(rhs,trlst,newtr,vx,vy,solutions,
primitive,defpol,lg,n2mon,mon2n),
print("returning from prelle_singer_dep into intfactor_control "),
if _sol#false then (
/* found a integrating factor */
print("found a integrating factor: N= ",_sol),
sol1:first(_sol),
METHOD_FI: if sol1 # false then " Prelle-Singer Rational Solution" else" Prelle-Singer Integrating Factor",
INTFACTOR_FI:if sol1 # false then -diff(sol1,vx)/_M else second(_sol),
exittheloop:true,
print("exittheloop 1"),
return(false)
),
if exittheloop then ( print("exittheloop 2"),return(false) )
),
if exittheloop then ( print("exittheloop 3"),return(false) ),
degree:degree+1,
remarray(vfunv,vifunv)
), /* end loop over N. If we exit here, then we did not find a solution */
print("exiting function"),
return(_sol)
)$
/* */
matchdeclare([%a%,%b%,%n%,%m%] ,freeof(x,y) ,%c%,freeof(x) ,%e%,freeof(y));
notfreeofxy(x,y,z):=not freeof(x,y,z);
matchdeclare(%d%,notfreeofxy(x,y));
defmatch(defi_type6_0, %a% + %d%, x,y);
defmatch(defi_type6_1, %e%*y^%n%, y);
defmatch(defi_type6_2, %b%*x^%m%, x);
defi_type6(exp,x,y):=block([a,b,c,d],
if (a:defi_type6_0(exp,x,y)) # false and (b:defi_type6_1(%d%,y))# false
and (c:defi_type6_2(%e%,x))# false then
subst(a,subst(b,subst(c,[[%a%,%b%],[%m%,%n%]]))) else false
)$
/*****************************************************************************/
/*****************************************************************************/
/* This function creates the basis of functions for which we take the multivariate polynomials */
/*****************************************************************************/
BasisFunctions(_M,_N,_y,_x):=block([_LBasisFunctions,_TrimmedList:nil,_a,sol1],
/*****************************************************************************/
print("computing basis functions"),
/* ----- We continue with the Prelle-Singer algorithm ... ----- */
/* The field is assumed to be generated by the kernels in p and q and by */
/* the derivatives of those kernels */
/* */
_LBasisFunctions:showratvars([_N,_M]), /* list of all unknowns */
/* now add to the list the derivatives wrt the dependent and independent variable */
/* nijso: actually, we have to do this again to make sure that derivatives of derivatives are now in the list... we dont do this!*/
_LBasisFunctions:append(map(lambda([_a],diff(_a,_x)),_LBasisFunctions), map(lambda([_a],diff(_a,_y)),_LBasisFunctions), _LBasisFunctions),
_LBasisFunctions:showratvars(_LBasisFunctions),
/* remove everything that is not depending on x,y */
for _a in _LBasisFunctions do
if not freeof(_x,_y,_a) or diff(_a,_x)#0 or diff(_a,_y)#0 then
_TrimmedList:cons(_a,_TrimmedList),
/* */
/* This section comes to insure proper substitution (no internal substitutions)*/
/* inside the transcendentals ! */
_LBasisFunctions:nil,
/* check for double entries (we need to keep the correct order) */
while _TrimmedList # nil do(
_a:first(_TrimmedList),
_TrimmedList:rest(_TrimmedList),
if freeof(_a,_TrimmedList) then _LBasisFunctions:endcons(_a,_LBasisFunctions) else _TrimmedList:endcons(_a,_TrimmedList)
),
/* trimmedlist is now empty, basisfunctions contains everything*/
/* */
/* It is assumed that all entries in trlst are algebraically independent! */
/* One may use the Rothstein-Caviness algorithm to determine algebraic */
/* dependence (see Siam.J.Comp. Vol 8 No.3. Aug 1979. ) */
/* */
_TrimmedList:_LBasisFunctions,
/* ------------------------------------------------------------------------------ */
/* we are finished with constructing the list of variables... */
/* */
/* Now the Prelle-Singer method */
/* */
/* Check if a closed differential field */
sol1:map(lambda([z],diff(z,_x)),_TrimmedList),
sol1:append(map(lambda([z],diff(z,_y)), _TrimmedList),sol1),
sol1:showratvars(sol1),
print("sol1 : ",sol1),
for _a in sol1 do if (not freeof(_x,_y,_a) or radcan(diff(_a,_x))#0
or radcan(diff(_a,_y))#0) and (not member(_a,_TrimmedList))
then (print (_a," is non constant in the field R", _TrimmedList),
print(" The result may be WRONG! "),
let([_a,0],defi_let_package)
),
/* we check if it is a closed differential field, but if it isn't then we don't do anything about it! */
/* when does this happen? e.g. with unknown functions f(x) and their derivatives df/dx */
return(_TrimmedList)
)$
/*****************************************************************************/
/*****************************************************************************/
prelle_singer_dfovf(degree,nterms,grad,vrlst,prlst,primitive,defpol,fun,ifun):=
/*****************************************************************************/
/* Solving the Dp/p problem of many variable case */
/* Input : */
/* degree = maximal order of monomial appearing if p */
/* nterms = maximal number of terms allowed */
/* or list of terms to be examined */
/* grad = list of polynomials (D = sum of pi*diff(.,xi)) */
/* vrlst = list of the variables (transcendentals) */
/* prlst = list of parameters appearing in grad */
/* primitive= primitive element of the algebraic extension */
/* defpol = The irreducible polynomial defining primitive */
/* fun = bijection from positive integers onto monomials */
/* ifun = inverse of fun */
/* output: */
/* list [ll,primitive,defpol,lg], where */
/* ll=list of [[f,g], ... ,], where Df=f*g, */
/* primitive = possible a new primitive element */
/* defpol = defining polynomial */
/* ng = maximal number of terms in the g's */
/* */
/* Arrays used : vgrad = vector of distributive representation of grad */
/* vf = .. symbols vf [i] */
/* vg = .. .. vg[i] */
/* veq = .. of equations */
/* vgmon = vector of monomials in g */
/*****************************************************************************/
block([ngr,i,ng,m,nmax,n,nf,
a,b,c,d,e,glist,flist,fpol1,gpol1,cof,var,
monom,fpol,gpol,lindex,nlindex,res,eql,nleql,
nterms1,solvedlist,savenleq,saveleq,list_of_terms,nfm,
zerolist,nozerolist,endloop:false,nofsol:0,lc,
oprimitive,odefpol,overpol,oldofnew:nil,irred] ,
/* */
declare([ngr,i,ng,m,nmax,n,nf,nfm,degree],integer),
/*declare([vg,vgmon,vf,vgrad,veq] ,special), */
print("entering prellesinger_dfovf"),
print("maximal order of monomial : ",degree),
print("maximum number of terms allowed:",nterms),
print("list of polynomials : ",grad),
print("list of transcendentals : ", vrlst),
print("list of parameters in poly list : ",prlst),
print("primitive element of the algebraic extension : ",primitive),
print("irreducible polynomial defining primitive : ", defpol),
/* comment of Rami on nterms:nterms-1: degree-1 (BUG?) */
/* is nterms an integer or a rational number? */
if numberp(nterms) then nterms:nterms-1,
print("nterms = ",nterms),
/* Because the coefficient of the highest term if f (in the df/f problem) */
/* can be set to 1 */
res :nil,
ngr:length(grad),
/* declare the array vgrad */
array(vgrad,ngr),
/* */
print("calling pol2dist"),
for i:1 thru ngr do (
a:first(grad),
vgrad[i]:pol2dist(a,vrlst,vrlst,ifun),
grad:rest(grad)
),
/* */
print("vgrad = ",listarray(vgrad)),
/* vgrad has the following form for the polynomials:
[index,term,coefficient], where index is the index to the vfunv array. term is the poly term in terms of transcendentals
and coefficient is the constant prefactor in Complex C
*/
print("determining highest monomial in g"),
/* note, g is the remainder term of the division: Df/f = g */
/* Nijso: if N=2, then we only have to look in monomials 1..6 */
/* if N=3, then we only have to look in monomials 1..10*/
/* if N=4, then we only have to look in monomials 1..15*/
/* Determine the highest monomial appearing in g */
/* but... we know this, right? */
b:vrlst,
ng:0,
for i:1 thru ngr do (
print("---- i=",i,"/",ngr),
a:first(vgrad[i]),
cof:third(a),
var:first(b),
print("a=",a),
print("cof=",cof),
print("var=",var),
/* nijso: new degree bound for g from Y.K. Man 1993: deg(Df) - deg(f) */
print("degree of f = ",degree),
if cof # 0 then (
monom:second(a), /* this is the maximal monomial in A, M(A)*/
m:first(a), /* the index, or the maximum order in terms of the monomials */
print(" monom=",monom," ,m=",m),
if hipow(monom,var)>0 then m:apply(ifun,[ratsimp(monom/var),vrlst]),
print("m = ",m), /* this is the degree of A, deg(A)=Phi^-1(M(A)) */
ng:max(ng,m)
),
b:rest(b)
),
/* g gives the index in the list of polynomials with the highest monomial occuring in the ode*/
print("highest monomial in g = ",ng),
print("monomial = ",monom),
/* Initialize */
array(vg,ng),array(vgmon,ng),array(vf,degree),
/* */
flist:[vf[1]], /* flist will hold the list of all unknowns vf(1] .. vf[.] */
fpol : vf[1],
glist:nil,
gpol: 0,
/* */
print("glist"),
for i:1 thru ng do (
glist:cons(vg[i],glist),
b:apply(fun,[i,vrlst]),
vgmon[i]:b,
gpol:gpol+b*vg[i]
),
print("glist = ",glist),
print("vgmon = ",listarray(vgmon)),
print("gpol = ",gpol), /* this is the first polynomial f */
/* find nmax the maximal monomial appearing in f*g */
print("find nmax for degree=",degree),
nmax:apply(ifun,[ratsimp(vgmon[ng]*apply(fun,[degree,vrlst])),vrlst]),
print("nmax = ",nmax),
/* */
array(veq,nmax),
/* */
/* initialize lindex = list of integers from 1 to nmax */
/* and set all the equations veq[i] to zero */
lindex: nil,
for i:1 thru nmax do (veq[i] :0,lindex:cons(i,lindex)),
/* */
for i:1 thru ng do veq[i]:vg[i]*vf[1],
/*print("veq = ",listarray(veq)), */
print("lindex = ",lindex),
/* */
/* Start the main loop : */
/* */
/* nijso: we move the loop outside of the darboux function*/
for nf : 2 thru degree do (
monom: apply(fun,[nf,vrlst]),
print("monom = ",monom),
/* */
/* Depending on "nterms" prepare the list "list_of_terms" */
/* */
print("--------------------"),
print("----- N=",nf," -----"),
print("--------------------"),
print("prepare listofterms"),
print("nterms = ",nterms),
if numberp(nterms) then (
print(" --- numberp(nterms) is true"),
solvedlist:nil,
nterms1: nterms ,
nfm:nf-1,
print("nterms1 = ",nterms),
print("nfm = ",nfm),
if nterms1 > nfm then nterms1:nfm,
list_of_terms:combinations(flist,nfm,nterms1),
print("-- list of terms = ",list_of_terms)
) /* rami put cross - wrong here */
else(
print(" --- numberp(nterms) is false"),
list_of_terms:nil,
c:nterms,
while c#nil do(
a:first(c),
/* *rami put a cross through the following code: */
nfm:first(a),
if nfm = nf /* only lists whose highest order term is nf examined */
then(
list_of_terms: cons(combinations1(flist,rest(a)),list_of_terms) ,
nterms: rest (c)
)
else if nfm>nf then c:[nil]
else error(print(" incompatible terms ",a," for nf= ",nf)),
c:rest(c)
),
if nterms=nil then endloop:true
),
print("nterms = ",nterms),
print("endloop = ",endloop),
print("list of terms = ",list_of_terms),
/* rami ends the cross through the code here */
/* update the equations except the one arising from vf[nf]*g */
/* NB: here we start constructing the matrix F */
c:vrlst, /* for example [x,y] */
print("update equations"),
/*print("veqn = ",listarray(veq)),*/
for i:1 thru ngr do (
var:first(c),
b:vgrad[i],
m:hipow(monom,var),
if m>0 then (
d:ratsimp(monom/var),
while b # nil do (
e:first(b),
a:second(e),
cof:third(e),
n:apply(ifun,[a*d,vrlst]),
veq[n] : veq[n]-cof*m*vf[nf] ,
/*print("veq=",listarray(veq)),*/
b:rest(b)
)
),
c:rest(c)
),
/*
print("vrlist of transcendentals = ",vrlst),
print("vgrad = ",listarray(vgrad)),
print("veq = ",veq, listarray(veq)),
*/
/* */
print("pick linear equations"),
/* Pick up the linear equations */
n:apply(ifun,[monom*vgmon[ng],vrlst]),
eql:nil,
nlindex : rest(lindex,nmax-n),
for i:1 thru ng do (
m:apply(ifun,[monom*vgmon[i],vrlst]),
eql:cons(-ratsubst(1,vf[nf],veq[m]),eql),/* we eliminate the highest vf[i], i.e. vf[ng]*/
/* print("eql = ",eql," ",nf," ",vf[nf]," ",veq[m]),*/
nlindex:delete(m,nlindex)
),
/*print("eql = ",eql),*/
saveleq : eql,
/* */
/* Collect the nonlinear equations */
print("pick nonlinear equations"),
nleql:nil ,
while nlindex # nil do (
m:first(nlindex),
a:ratsubst(1,vf[nf] ,veq[m]),
if a#0 then nleql:cons(a,nleql),
nlindex:rest(nlindex)
),
/* print("nleql = ",nleql),*/
/* */
savenleq:nleql,
/* */
/* */
/* Loop on members in the list "list_of_terms" */
/* rami comments: no while*/
/* rami comments: nozerolist:flist ???nolist:nil*/
print("loop over listofterms",list_of_terms),
while list_of_terms # nil do (
print("insert list_of_terms"),
print(first(list_of_terms)),
zerolist: first (list_of_terms),
nozerolist:first(zerolist),
zerolist: second(zerolist),
/* -------------------------------- */
/* -------------------------------- */
/* -------------------------------- */
/* */
/* solve the linear equations */
/* */
print("solve linear equations, ",zerolist," ",saveleq," ",glist),
eql: subst(zerolist,saveleq),
/*print("eql = ",eql), */
eql: lsolveforg(eql,glist),
/*print("eql = ",eql), */
/* -------------------------------- */
/* -------------------------------- */
/* -------------------------------- */
/* */
/* Solve the set of nonlinear equations in nleql */
/* save the old primitive and defining polynomial */
print("solve nonlinear equations"),
oprimitive:primitive,
odefpol:defpol,
/* rami crosses appendzerolist here */
nleql : subst (append(zerolist,eql),savenleq),
/*print("nleql = ",nleql), */
nleql : nlsolverpp(delete(0,nleql),nozerolist,primitive,defpol),
/*print("nleql = ",nleql), */
/* */
/*Resolve the output */
oldofnew:first(rest(nleql,3)),
defpol:third(nleql),
primitive:second(nleql),
nleql:first(nleql),
/*print("nleql,first = ",nleql), */
/* rami comments: not necessary */
if numberp(nterms) then if (terms < (degree-1)) then (
/* examine redundant solutions */
lc : solvedlist,
c : nleql,b:nil,
while c#nil do(
a:first(c),
m:0, i:1,
while a # nil do (
if rhs(first(a))#0 then m:m+2^first(lhs(first(a))) else i:0,
a:rest(a)
),
if i=1 then b:cons(first(c),b)
else if not member(m,lc) then (
solvedlist:cons(m,solvedlist),
b: cons(first(c),b)
),
c:rest(c)
),
nleql:reverse(b)
),
/*print("nleql,reverse = ",nleql), */
/* */
/* Redefine the solutions in terms of the new primitive element */
/* */
/* rami remarks: X bug check!*/
if defpol # nil and primitive # oprimitive and oldofnew #nil
then mysip(subst(oldofnew,oprimitive,nleql)),
/* */
/* Now construct the actual polynomials */
/*print("construct polynomials, nleq = ",nleql),*/
c:res,
print("c-res = ",res,",fpol= ",fpol,",monom= ",monom,",zerolist= ",zerolist),
/* rami crosses zerolist */
/* rami wants to move the while loop to after 'if domainp line */
print("---------- BEGIN eliminate nonlinear equations ----------"),
while nleql # nil do (
print("----- nleql = ",nleql),
a:first(nleql),
print("a = ",a),
fpol1:rat(mysimp(subst(append(a,zerolist),fpol)+monom)),
print("fpol1=",fpol1," ",fpol," ",monom),
b:subst(a,eql),
print("a = ",a),
print("eql= ",eql),
print("b = ",b),
print("gpol = ",gpol),
gpol1:rat(mysimp(subst(b,gpol))),
print("gpol1 = ",gpol1),
/* */
/* Now check for irreducability is performed */
/* Irreducability in Q or Q(i) only, is checked */
/* */
print("check irreducibility"),
b:c,irred:true,
while b # nil do (
a:first(first(b)),
a:mysimp(fpol1/a),
if domainp(denom(a),vrlst) then (b : [1] ,irred:false),
b:rest(b)
),
print("irred = ",irred),
if irred then (
print("irreduceable"),
nofsol :nofsol+1, /* Check Lemma 1 */
print("nofsol = ",nofsol," ",ng),
/* we have removed the loop inside the darboux function */
if nofsol > ng then (endloop:true,nleql:[1]),
print("res = ",res,", ",fpol1,", ",gpol1),
res:cons([fpol1,gpol1],res)
),
nleql:rest(nleql)
),
print("nleql = ",nleql),
print("res = ",res),
/* */
list_of_terms:rest(list_of_terms)
), /* end loop over list_of_terms... rami has crossed this list_of_terms */
print("---------- END eliminate nonlinear equations ----------"),
print("list_of_terms = ",list_of_terms),
/* */
/* Update veq , fpol and flist */
for i:1 thru ng do (
m:apply(ifun,[vgmon[i]*monom,vrlst]),
veq [m] : veq[m]+vf[nf]*vg[i]
),
/*print("veq = ",listarray(veq)),*/
print("fpol=",fpol,", monom=",monom,", vf=",vf[nf]),
fpol:rat(fpol+monom*vf[nf]),
flist:cons(vf[nf],flist),
print("flist= ",flist)
/* but how do we know we can exit the loop when we have found 2 independent solutions? */
/*if endloop then nf:degree */
/* the loop has been moved outside of the darboux funxtion*/
), /* End of the main loop over nf */
print("end of loop over listofterms"),
remarray(vf,vg,veq,vgmon,vgrad),
print("----- exiting prellesinger_dfovf ----- "),
print("result = ",res," ",primitive," ",defpol," ",ng),
return ([reverse(res),primitive,defpol,ng])
)$
/*****************************************************************************/
/* Checking linear dependence of the solutions */
/* returned by the subroutine prelle_singer_dfovf. */
/* Constructing rational first-integral or an integrating factor */
/*****************************************************************************/
prelle_singer_dep(rhs,trlst,newtr,vx,vy,
solutions,primitive,defpol,lg,n2mon,mon2n):=
/* INPUT: */
/* rhs = -(diff(p,vx)+diff(q,vy) (generalization of) the right hand */
/* side of the equation Dp/p = rhs, for transcendentals. */
/* trlst = list of transcendentals */
/* newtr = The new names of transcendentals */
/* solutions = solutions returned by prelle_singer_dfovf */
/*OUTPUT: */
/* false if solution is not found */
/* [first-integral, ... ] if "rational" solution found */
/* [false,integrating-factor] otherwise */
/*****************************************************************************/
block([_a,_b,s,k,i,j,l,fpol,nofsol,exptsubst:true,indlist:nil,
gpol,lm,m,n,solved:false,degreeprim:0],
declare([i,j,k,l,lm,nofsol,m,n,degreeprim] ,integer),
print("----- entering prelle singer dep ----- "),
print("rhs = ",rhs),
print("trlst = ",trlst),
print("newtr = ",newtr),
print("vx = ",vx),
print("vy = ",vx),
print("solutions = ",solutions),
print("primitive = ",primitive),
print("defpol = ",defpol),
print("lg = ",lg),
/* first we check if the g's are linearly dependent. If they are, then sum(c_i*g_i)=0 for some nonzero c_i */
/* so we have to collect all the different terms, i.e. tr2*tr3*tr4 etc and check if we can find coefficients */
/* such that we get a+b+c = 0, b-c+d=0, etc...*/
/* */
degreeprim: 1,
if primitive='%i then (
print("%i is a primitive!"),
degreeprim:2,
block([simp:false] , defpol:%i^2+1)
) else if defpol # nil then
degreeprim:hipow(defpol,primitive),
degreeprim:degreeprim-1,
print("primitiveflag: ",primitiveflag),
print("elementary:",elementary,evenp(elementary)),
if primitiveflag=false or evenp(elementary) then (
defpol=nil,
degreeprim=0,
primitive=nil
),
print("defpol = ",defpol),
print("degreeprim = ",degreeprim),
print("primitive = ",primitive),
nofsol:length(solutions),
array(gvec,nofsol,degreeprim,lg),
array(fun,nofsol),
array (per_vec,degreeprim,lg),
/* set to zero all vectors */
for l:0 thru nofsol do (
fun[l]:0,
for i:0 thru degreeprim do
for j:1 thru lg do gvec[l,i,j]:0
),
/* Prepare the substitution */
trlst:map("=",newtr,trlst),
/* --- construct the gvec --- */
l:0,
while solutions # nil do (
l:l+1,
gpol:first(solutions),
fpol:first(gpol),
gpol:second(gpol),
print("fpol:",fpol),
print("gpol:",gpol),
/* If gpol = 0 then solution found (this is step 2 in Shtokhamer) */
/* Because this means that we have 0*g1 + 0*g2 + ... + c*gpol + ... + 0*gn =0 */
/* At this stage return the result and dont search any further */
/* We find these trivial solutions while we are building the gvec array for further analysis*/
if gpol=0 then /* check if trivial solution or not */
if ((letsimp(diff(fpol:subst(trlst,fpol),vx),defi_let_package) #0) or
(letsimp(diff(fpol,vy),defi_let_package)#0) )
then (
remarray(gvec,fun,per_vec),
solved:true,
return(1) /* we exit the gvec construction routine */
)
else (
l:l-1,
nofsol:nofsol-1
)
else (
fun[l] :fpol,
print("gpol:",gpol),
print("newtr:",gpol),
gpol:pol2dist(gpol,newtr,newtr,mon2n),/* pol2dist rewrites the gpol as a list [[index_i,variable_i,coeff_i],[],[]] */
/* where variable is tr1,tr2,tr1*tr2,etc, coeff is the constant factor and index */
/* is the index to the polynomial array [tr1,tr2,tr3,tr1^2,tr1*tr2,tr2^2,...] */
print("fun:",fun[l]),
print("gpol:",gpol),
while gpol # nil do (
_a:first(gpol), /* takes the first gpol from the list */
i:mode_identity(fixnum,first(_a)), /* takes the index from gpol_i */
_a:third(_a), /* takes the coefficient */
if _a # 0 then
if primitive # nil then
for k:0 thru degreeprim do /* loop over all powers */
gvec[l,k,i]:ratcoeff(_a,primitive,k) /* returns coefficient _a of _a*primitive^k */
/* so gvec[l,k,i] contains for all "l" solutions the */
/* coefficients of the "kth" power */
else
gvec[l,0,i]:_a, /* if primitive is nil then naturally we place it at position 0, but why create an extra else for it? */
gpol:rest(gpol)
)
),
solutions:rest(solutions)
), /* --- end of construction of gvec --- */
print("solutions: ",solutions),
/*print("gvec = ",listarray(gvec)),*/
print("solved = ",solved),
/* at this point we have only found the simple linearly independents solutions where one of the g's is zero*/
if solved then return([fpol,false]),
/* */
/* at this point we are ready to test the linear dependence of all g's (if there is a g=0, then we have a solution and we exited before) */
/* examine now the rhs? -(diff(p,x)+diff(q,y)) */
/* */
_a:rhs,
_a:pol2dist(_a,newtr,newtr,mon2n),
s:first(_a), /* redundant with s:first a immediately down? (only 4 occurences of variable s)*/
/* /* we never enter here...*/
if(first(s) > lg) then (
remarray(gvec,fun,per_vec),
print(" BUG in prelle_singer_dep"), return(false)
),
*/
/* */
while _a # nil do (
s:first(_a),
i:mode_identity(fixnum,first(s)),
rhs:third(s),
if rhs # 0 then
if primitive # nil then
for k:0 thru degreeprim do
gvec[0,k,i]:ratcoef(rhs,primitive,k) /* right-hand-side is put at location 0 */
else gvec[0,0,i]:rhs,
_a:rest(_a)
),
indlist: nil,
for j:1 thru nofsol do(
print("------ j: ",j,"/",nofsol),