forked from fricas/fricas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mantepse.spad
3930 lines (3154 loc) · 157 KB
/
mantepse.spad
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
)if false
\documentclass{article}
\usepackage{axiom, amsthm, amsmath, url}
\newtheorem{ToDo}{ToDo}[section]
\newcommand{\FriCAS}{{\tt FriCAS}}
\newcommand{\Rate}{{\tt Rate}}
\newcommand{\GFUN}{{\tt GFUN}}
\begin{document}
\title{mantepse.spad}
\author{Martin Rubey}
\maketitle
\begin{abstract}
The packages defined in this file enable {\FriCAS} to guess formulas for
sequences of, for example, rational numbers or rational functions, given the
first few terms. It extends and complements Christian Krattenthaler's
program \Rate\ and the relevant parts of Bruno Salvy and Paul Zimmermann's
\GFUN. An article describing this package can be found at
\url{http://arxiv.org/abs/math.CO/0702086}.
\end{abstract}
\tableofcontents
\section{domain UFPS UnivariateFormalPowerSeries}
)endif
)abbrev domain UFPS UnivariateFormalPowerSeries
UnivariateFormalPowerSeries(Coef : Ring) ==
UnivariateTaylorSeries(Coef, 'x, 0$Coef)
)abbrev package UFPS1 UnivariateFormalPowerSeriesFunctions
UnivariateFormalPowerSeriesFunctions(Coef : Ring) : Exports == Implementation
where
UFPS ==> UnivariateFormalPowerSeries Coef
Exports == with
hadamard : (UFPS, UFPS) -> UFPS
Implementation == add
hadamard(f, g) ==
series map((z1 : Coef, z2 : Coef) : Coef +-> z1*z2, coefficients f, coefficients g)
$StreamFunctions3(Coef, Coef, Coef)
)abbrev package STNSR StreamTensor
StreamTensor(R : Type) : with
tensorMap : (Stream R, R -> List R) -> Stream R
++ tensorMap([s1, s2, ...], f) returns the stream consisting of all
++ elements of f(s1) followed by all elements of f(s2) and so on.
== add
tensorMap(s, f) ==
empty? s => empty()
concat([f first s], delay tensorMap(rest s, f))
)if false
\section{domain GOPT GuessOption}
)endif
)abbrev domain GOPT GuessOption
++ Author: Martin Rubey
++ Description: GuessOption is a domain whose elements are various options used
++ by \spadtype{Guess}.
GuessOption() : Exports == Implementation where
Exports == SetCategory with
maxDerivative : Union(NonNegativeInteger, "arbitrary") -> %
++ maxDerivative(d) specifies the maximum derivative in an algebraic
++ differential equation. This option is expressed in the form
++ \spad{maxDerivative == d}.
maxShift : Union(NonNegativeInteger, "arbitrary") -> %
++ maxShift(d) specifies the maximum shift in a recurrence
++ equation. This option is expressed in the form \spad{maxShift == d}.
maxSubst : Union(PositiveInteger, "arbitrary") -> %
++ maxSubst(d) specifies the maximum degree of the monomial substituted
++ into the function we are looking for. That is, if \spad{maxSubst ==
++ d}, we look for polynomials such that $p(f(x), f(x^2), ...,
++ f(x^d))=0$. equation. This option is expressed in the form
++ \spad{maxSubst == d}.
maxPower : Union(PositiveInteger, "arbitrary") -> %
++ maxPower(d) specifies the maximum degree in an algebraic differential
++ equation. For example, the degree of (f'')^3 f' is 4. maxPower(-1)
++ specifies that the maximum exponent can be arbitrary. This option is
++ expressed in the form \spad{maxPower == d}.
homogeneous : Union(PositiveInteger, Boolean) -> %
++ homogeneous(d) specifies whether we allow only homogeneous algebraic
++ differential equations. This option is expressed in the form
++ \spad{homogeneous == d}. If true, then maxPower must be
++ set, too, and ADEs with constant total degree are allowed.
++ If a PositiveInteger is given, only ADE's with this total degree are
++ allowed.
Somos : Union(PositiveInteger, Boolean) -> %
++ Somos(d) specifies whether we want that the total degree of the
++ differential operators is constant, and equal to d, or maxDerivative
++ if true. If true, maxDerivative must be set, too.
maxLevel : Union(NonNegativeInteger, "arbitrary") -> %
++ maxLevel(d) specifies the maximum number of recursion levels operators
++ guessProduct and guessSum will be applied. This option is expressed in
++ the form spad{maxLevel == d}.
maxDegree : Union(NonNegativeInteger, "arbitrary") -> %
++ maxDegree(d) specifies the maximum degree of the coefficient
++ polynomials in an algebraic differential equation or a recursion with
++ polynomial coefficients. For rational functions with an exponential
++ term, \spad{maxDegree} bounds the degree of the denominator
++ polynomial.
++ This option is expressed in the form \spad{maxDegree == d}.
maxMixedDegree : NonNegativeInteger -> %
++ maxMixedDegree(d) specifies the maximum q-degree of the coefficient
++ polynomials in a recurrence with polynomial coefficients, in the case
++ of mixed shifts. Although slightly inconsistent, maxMixedDegree(0)
++ specifies that no mixed shifts are allowed. This option is expressed
++ in the form \spad{maxMixedDegree == d}.
allDegrees : Boolean -> %
++ allDegrees(d) specifies whether all possibilities of the degree vector
++ - taking into account maxDegree - should be tried. This is mainly
++ interesting for rational interpolation. This option is expressed in
++ the form \spad{allDegrees == d}.
safety : NonNegativeInteger -> %
++ safety(d) specifies the number of values reserved for testing any
++ solutions found. This option is expressed in the form \spad{safety ==
++ d}.
check : Union("skip", "MonteCarlo", "deterministic") -> %
++ check(d) specifies how we want to check the solution. If
++ the value is "skip", we return the solutions found by the
++ interpolation routine without checking. If the value is
++ "MonteCarlo", we use a probabilistic check. This option is
++ expressed in the form \spad{check == d}
checkExtraValues : Boolean -> %
++ checkExtraValues(d) specifies whether we want to check the
++ solution beyond the order given by the degree bounds. This
++ option is expressed in the form \spad{checkExtraValues == d}
one : Boolean -> %
++ one(d) specifies whether we are happy with one solution. This option
++ is expressed in the form \spad{one == d}.
debug : Boolean -> %
++ debug(d) specifies whether we want additional output on the
++ progress. This option is expressed in the form \spad{debug == d}.
functionName : Symbol -> %
++ functionName(d) specifies the name of the function given by the
++ algebraic differential equation or recurrence. This option is
++ expressed in the form \spad{functionName == d}.
functionNames : List(Symbol) -> %
++ functionNames(d) specifies the names for the function in
++ algebraic dependence. This option is
++ expressed in the form \spad{functionNames == d}.
variableName : Symbol -> %
++ variableName(d) specifies the variable used in by the algebraic
++ differential equation. This option is expressed in the form
++ \spad{variableName == d}.
indexName : Symbol -> %
++ indexName(d) specifies the index variable used for the formulas. This
++ option is expressed in the form \spad{indexName == d}.
displayKind : Symbol -> %
++ displayKind(d) specifies kind of the result: generating function,
++ recurrence or equation. This option should not be set by the
++ user, but rather by the HP-specification.
option : (List %, Symbol) -> Union(Any, "failed")
++ option(l, option) returns which options are given.
Implementation ==> add
import from AnyFunctions1(Boolean)
import from AnyFunctions1(Symbol)
import from AnyFunctions1(NonNegativeInteger)
import from AnyFunctions1(Union(NonNegativeInteger, "arbitrary"))
import from AnyFunctions1(Union(PositiveInteger, "arbitrary"))
import from AnyFunctions1(Union(PositiveInteger, Boolean))
import from AnyFunctions1(Union("skip", "MonteCarlo", "deterministic"))
Rep := Record(keyword : Symbol, value : Any)
maxLevel d == ['maxLevel, d::Any]
maxDerivative d == ['maxDerivative, d::Any]
maxShift d == maxDerivative d
maxSubst d ==
if d case PositiveInteger
then maxDerivative((d@Integer-1)::NonNegativeInteger)
else maxDerivative d
maxDegree d == ['maxDegree, d::Any]
maxMixedDegree d == ['maxMixedDegree, d::Any]
allDegrees d == ['allDegrees, d::Any]
maxPower d == ['maxPower, d::Any]
safety d == ['safety, d::Any]
homogeneous d == ['homogeneous, d::Any]
Somos d == ['Somos, d::Any]
debug d == ['debug, d::Any]
check d == ['check, d::Any]
checkExtraValues d == ['checkExtraValues, d::Any]
one d == ['one, d::Any]
functionName d == ['functionName, d::Any]
functionNames d ==
['functionNames, coerce(d)$AnyFunctions1(List(Symbol))]
variableName d == ['variableName, d::Any]
indexName d == ['indexName, d::Any]
displayKind d == ['displayKind, d::Any]
coerce(x : %) : OutputForm == x.keyword::OutputForm = x.value::OutputForm
x : % = y : % == x.keyword = y.keyword and x.value = y.value
option(l, s) ==
for x in l repeat
x.keyword = s => return(x.value)
"failed"
)abbrev package GOPT0 GuessOptionFunctions0
++ Author: Martin Rubey
++ Description: GuessOptionFunctions0 provides operations that extract the
++ values of options for \spadtype{Guess}.
GuessOptionFunctions0() : Exports == Implementation where
LGOPT ==> List GuessOption
Exports == SetCategory with
maxDerivative : LGOPT -> Union(NonNegativeInteger, "arbitrary")
++ maxDerivative returns the specified maxDerivative.
maxShift : LGOPT -> Union(NonNegativeInteger, "arbitrary")
++ maxShift returns the specified maxShift.
maxSubst : LGOPT -> Union(PositiveInteger, "arbitrary")
++ maxSubst returns the specified maxSubst.
maxPower : LGOPT -> Union(PositiveInteger, "arbitrary")
++ maxPower returns the specified maxPower.
homogeneous : LGOPT -> Union(PositiveInteger, Boolean)
++ homogeneous returns whether we allow only homogeneous algebraic
++ differential equations, default being false
Somos : LGOPT -> Union(PositiveInteger, Boolean)
++ Somos returns whether we allow only Somos-like operators, default
++ being false
maxLevel : LGOPT -> Union(NonNegativeInteger, "arbitrary")
++ maxLevel returns the specified maxLevel.
maxDegree : LGOPT -> Union(NonNegativeInteger, "arbitrary")
++ maxDegree returns the specified maxDegree.
maxMixedDegree : LGOPT -> NonNegativeInteger
++ maxMixedDegree returns the specified maxMixedDegree.
allDegrees : LGOPT -> Boolean
++ allDegrees returns whether all possibilities of the degree vector
++ should be tried, the default being false.
safety : LGOPT -> NonNegativeInteger
++ safety returns the specified safety or 1 as default.
check : LGOPT -> Union("skip", "MonteCarlo", "deterministic")
++ check(d) specifies how we want to check the solution. If
++ the value is "skip", we return the solutions found by the
++ interpolation routine without checking. If the value is
++ "MonteCarlo", we use a probabilistic check. The default is
++ "deterministic".
checkExtraValues : LGOPT -> Boolean
++ checkExtraValues(d) specifies whether we want to check the
++ solution beyond the order given by the degree bounds. The
++ default is true.
one : LGOPT -> Boolean
++ one returns whether we need only one solution, default being true.
functionName : LGOPT -> Symbol
++ functionName returns the name of the function given by the algebraic
++ differential equation, default being f
functionNames : LGOPT -> List(Symbol)
++ functionNames returns the names for the function in the algebraic
++ dependence, default being %f1, %f2, ...
variableName : LGOPT -> Symbol
++ variableName returns the name of the variable used in by the
++ algebraic differential equation, default being x
indexName : LGOPT -> Symbol
++ indexName returns the name of the index variable used for the
++ formulas, default being n
displayKind : LGOPT -> Symbol
++ displayKind(d) specifies kind of the result: generating function,
++ recurrence or equation. This option should not be set by the
++ user, but rather by the HP-specification.
debug : LGOPT -> Boolean
++ debug returns whether we want additional output on the progress,
++ default being false
checkOptions : LGOPT -> Void
++ checkOptions checks whether the given options are consistent, and
++ yields an error otherwise
Implementation == add
maxLevel l ==
if (opt := option(l, 'maxLevel)) case "failed" then
"arbitrary"
else
retract(opt@Any)$AnyFunctions1(Union(NonNegativeInteger,
"arbitrary"))
maxDerivative l ==
if (opt := option(l, 'maxDerivative)) case "failed" then
"arbitrary"
else
retract(opt@Any)$AnyFunctions1(Union(NonNegativeInteger,
"arbitrary"))
maxShift l == maxDerivative l
maxSubst l ==
d := maxDerivative l
if d case NonNegativeInteger
then (d+1)::PositiveInteger
else d
maxDegree l ==
if (opt := option(l, 'maxDegree)) case "failed" then
"arbitrary"
else
retract(opt@Any)$AnyFunctions1(Union(NonNegativeInteger,
"arbitrary"))
maxMixedDegree l ==
if (opt := option(l, 'maxMixedDegree)) case "failed" then
0
else
retract(opt@Any)$AnyFunctions1(NonNegativeInteger)
allDegrees l ==
if (opt := option(l, 'allDegrees)) case "failed" then
false
else
retract(opt@Any)$AnyFunctions1(Boolean)
maxPower l ==
if (opt := option(l, 'maxPower)) case "failed" then
"arbitrary"
else
retract(opt@Any)$AnyFunctions1(Union(PositiveInteger, "arbitrary"))
safety l ==
if (opt := option(l, 'safety)) case "failed" then
1$NonNegativeInteger
else
retract(opt@Any)$AnyFunctions1(NonNegativeInteger)
check l ==
if (opt := option(l, 'check)) case "failed" then
"deterministic"
else
retract(opt@Any)$AnyFunctions1(Union("skip",
"MonteCarlo", "deterministic"))
checkExtraValues l ==
if (opt := option(l, 'checkExtraValues)) case "failed" then
true
else
retract(opt@Any)$AnyFunctions1(Boolean)
one l ==
if (opt := option(l, 'one)) case "failed" then
true
else
retract(opt@Any)$AnyFunctions1(Boolean)
debug l ==
if (opt := option(l, 'debug)) case "failed" then
false
else
retract(opt@Any)$AnyFunctions1(Boolean)
homogeneous l ==
if (opt := option(l, 'homogeneous)) case "failed" then
false
else
retract(opt@Any)$AnyFunctions1(Union(PositiveInteger, Boolean))
Somos l ==
if (opt := option(l, 'Somos)) case "failed" then
false
else
retract(opt@Any)$AnyFunctions1(Union(PositiveInteger, Boolean))
variableName l ==
if (opt := option(l, 'variableName)) case "failed" then
'x
else
retract(opt@Any)$AnyFunctions1(Symbol)
functionName l ==
if (opt := option(l, 'functionName)) case "failed" then
'f
else
retract(opt@Any)$AnyFunctions1(Symbol)
functionNames l ==
if (opt := option(l, 'functionNames)) case "failed" then
[]
else
retract(opt@Any)$AnyFunctions1(List Symbol)
indexName l ==
if (opt := option(l, 'indexName)) case "failed" then
'n
else
retract(opt@Any)$AnyFunctions1(Symbol)
displayKind l ==
if (opt := option(l, 'displayKind)) case "failed" then
error "GuessOption: displayKind not set"
else
retract(opt@Any)$AnyFunctions1(Symbol)
NNI ==> NonNegativeInteger
PI ==> PositiveInteger
checkOptions l ==
maxD := maxDerivative l
maxP := maxPower l
homo := homogeneous l
Somo := Somos l
if Somo case PI then
if one? Somo then
error "Guess: Somos must be Boolean or at least two"
if maxP case PI and one? maxP then
error "Guess: Somos requires that maxPower is at least two"
if maxD case NNI and maxD > Somo then
error "Guess: if Somos is an integer, it should be larger"
" than maxDerivative/maxShift or at least as big as maxSubst"
else
if Somo then
if maxP case PI and one? maxP then
error "Guess: Somos requires that maxPower is at least two"
if not (maxD case NNI) or zero? maxD or one? maxD then
error "Guess: Somos==true requires that maxDerivative/"
"maxShift is an integer, at least two, or maxSubst is"
" an integer, at least three"
if not (maxP case PI) and homo case Boolean and not homo then
error "Guess: Somos requires that maxPower is set or"
" homogeneous is not false"
if homo case PI then
if maxP case PI and maxP ~= homo then
error "Guess: only one of homogeneous and maxPower may be"
" an integer"
if maxD case NNI and zero? maxD then
error "Guess: homogeneous requires that maxShift/maxDerivative"
" is at least one or maxSubst is at least two"
else
if homo then
if not maxP case PI then
error "Guess: homogeneous==true requires that maxPower"
" is an integer"
if maxD case NNI and zero? maxD then
error "Guess: homogeneous requires that maxShift/"
"maxDerivative is at least one or maxSubst is"
" at least two"
)abbrev package GUESSEB GuessExpBin
)boot $tryRecompileArguments := nil
++ Author: Martin Rubey
++ Description: This package implements guessing GuessExpRat and
++ GuessBinRat functions of guessing package.
GuessExpBin(F, S, EXPRR, retract, coerce) : Exports == Implementation where
-- for example : FRAC POLY PF 5
F : Join(Field, PolynomialFactorizationExplicit)
-- in F we interpolate and check
S : GcdDomain
-- in guessExpRat I would like to determine the roots of polynomials in F. When
-- F is a quotient field, I can get rid of the denominator. In this case F is
-- roughly QFCAT S
-- results are given as elements of EXPRR
EXPRR : Join(FunctionSpace Integer, IntegralDomain,
RetractableTo Symbol,
RetractableTo Integer, CombinatorialOpsCategory,
PartialDifferentialRing Symbol) with
_* : (%, %) -> %
_/ : (%, %) -> %
_^ : (%, %) -> %
numerator : % -> %
denominator : % -> %
ground? : % -> Boolean
-- EXPRR exists, in case at some point there is support for EXPR PF 5.
-- the following I really would like to get rid of
retract : EXPRR -> F -- eg.: i+->i
coerce : F -> EXPRR -- eg.: i+->i
LGOPT ==> List GuessOption
GOPT0 ==> GuessOptionFunctions0
NNI ==> NonNegativeInteger
PI ==> PositiveInteger
GUESSRESULT ==> List EXPRR
SUP ==> SparseUnivariatePolynomial
FFFG ==> FractionFreeFastGaussian
DIFFSPECN ==> EXPRR -> EXPRR -- eg.: i+->q^i
GUESSER ==> (List F, LGOPT) -> GUESSRESULT
FSUPF ==> Fraction SUP F
V ==> OrderedVariableList(['a1, 'A])
POLYF ==> SparseMultivariatePolynomial(F, V)
FPOLYF ==> Fraction POLYF
FSUPFPOLYF ==> Fraction SUP FPOLYF
POLYS ==> SparseMultivariatePolynomial(S, V)
FPOLYS ==> Fraction POLYS
FSUPFPOLYS ==> Fraction SUP FPOLYS
Exports == with
guessExpRat : (List F, LGOPT) -> GUESSRESULT
++ \spad{guessExpRat(l, options)} tries to find a function of the
++ form n+->(a+b n)^n r(n), where r(n) is a rational function, that
++ fits l.
guessBinRat : (List F, LGOPT) -> GUESSRESULT
++ \spad{guessBinRat(l, options)} tries to find a function of the
++ form n+->binomial(a+b n, n) r(n), where r(n) is a rational
++ function, that fits l.
if F has RetractableTo Symbol and S has RetractableTo Symbol then
guessExpRat : Symbol -> GUESSER
++ \spad{guessExpRat q} returns a guesser that tries to find a
++ function of the form n+->(a+b q^n)^n r(q^n), where r(q^n) is a
++ q-rational function, that fits l.
guessBinRat : Symbol -> GUESSER
++ \spad{guessBinRat q} returns a guesser that tries to find a
++ function of the form n+->qbinomial(a+b n, n) r(n), where r(q^n)
++ is a q-rational function, that fits l.
Implementation == add
-- The following expressions for order and degree of the resultants
-- [[res1]] and [[res2]] in [[guessExpRatAux]] were first guessed,
-- partially with the aid of [[guessRat]], and then proven to be correct.
ord1(x : List Integer, i : Integer) : Integer ==
n := #x - 3 - i
x.(n+1)*reduce(_+, [x.j for j in 1..n], 0) + _
2*reduce(_+, [reduce(_+, [x.k*x.j for k in 1..j-1], 0) _
for j in 1..n], 0)
ord2(x : List Integer, i : Integer) : Integer ==
if zero? i then
n := #x - 3 - i
ord1(x, i) + reduce(_+, [x.j for j in 1..n], 0)*(x.(n+2)-x.(n+1))
else
ord1(x, i)
deg1(x : List Integer, i : Integer) : Integer ==
m := #x - 3
(x.(m+3)+x.(m+1)+x.(1+i))*reduce(_+, [x.j for j in 2+i..m], 0) + _
x.(m+3)*x.(m+1) + _
2*reduce(_+, [reduce(_+, [x.k*x.j for k in 2+i..j-1], 0) _
for j in 2+i..m], 0)
deg2(x : List Integer, i : Integer) : Integer ==
m := #x - 3
deg1(x, i) + _
(x.(m+3) + reduce(_+, [x.j for j in 2+i..m], 0)) * _
(x.(m+2)-x.(m+1))
checkResult(res : EXPRR, n : Symbol, l : Integer, list : List F) : NNI ==
for i in l..1 by -1 repeat
den := eval(denominator res, n::EXPRR, (i-1)::EXPRR)
if den = 0 then return i::NNI
num := eval(numerator res, n::EXPRR, (i-1)::EXPRR)
if list.i ~= retract(num/den)@F
then return i::NNI
0$NNI
SUPS2SUPF(p : SUP S) : SUP F ==
if F is S then
p pretend SUP(F)
else if F is Fraction S then
map((z1 : S) : F +-> coerce(z1)$Fraction(S), p)
$SparseUnivariatePolynomialFunctions2(S, F)
else error "Guess: type parameter F should be either equal to S or"
" equal to Fraction S"
-- convertion routines
F2FPOLYS(p : F) : FPOLYS ==
if F is S then
p::POLYF::FPOLYF pretend FPOLYS
else if F is Fraction S then
numer(p)$Fraction(S)::POLYS/denom(p)$Fraction(S)::POLYS
else error "Guess: type parameter F should be either equal to S or"
" equal to Fraction S"
MPCSF ==> MPolyCatFunctions2(V, IndexedExponents V,
IndexedExponents V, S, F,
POLYS, POLYF)
SUPF2EXPRR(xx : Symbol, p : SUP F) : EXPRR ==
zero? p => 0
(coerce(leadingCoefficient p))::EXPRR * (xx::EXPRR)^degree p
+ SUPF2EXPRR(xx, reductum p)
FSUPF2EXPRR(xx : Symbol, p : FSUPF) : EXPRR ==
(SUPF2EXPRR(xx, numer p)) / (SUPF2EXPRR(xx, denom p))
POLYS2POLYF(p : POLYS) : POLYF ==
if F is S then
p pretend POLYF
else if F is Fraction S then
map((z1 : S) : F +-> coerce(z1)$Fraction(S), p)$MPCSF
else error "Guess: type parameter F should be either equal to S or"
" equal to Fraction S"
SUPPOLYS2SUPF(p : SUP POLYS, a1v : F, Av : F) : SUP F ==
zero? p => 0
lc : POLYF := POLYS2POLYF leadingCoefficient(p)
monomial(retract(eval(lc, [index(1)$V, index(2)$V]::List V,
[a1v, Av])),
degree p)
+ SUPPOLYS2SUPF(reductum p, a1v, Av)
SUPFPOLYS2FSUPPOLYS(p : SUP FPOLYS) : Fraction SUP POLYS ==
cden := splitDenominator(p)
$UnivariatePolynomialCommonDenominator(POLYS, FPOLYS, SUP FPOLYS)
pnum : SUP POLYS
:= map((z1 : FPOLYS) : POLYS +-> retract(z1 * cden.den)$FPOLYS, p)
$SparseUnivariatePolynomialFunctions2(FPOLYS, POLYS)
pden : SUP POLYS := (cden.den)::SUP POLYS
pnum/pden
sup_fact(f) ==> factor(f)$SUP(F)
-- mimicking $q$-analoga
defaultD : DIFFSPECN
defaultD(expr : EXPRR) : EXPRR == expr
-- applies n+->q^n or whatever DN is to i
DN2DL : (DIFFSPECN, Integer) -> F
DN2DL(DN, i) == retract(DN(i::EXPRR))@F
evalResultant(p1 : POLYS, p2 : POLYS, o : Integer, d : Integer,
va1 : V, vA : V) : List S ==
res : List S := []
d1 := degree(p1, va1)
d2 := degree(p2, va1)
lead : S
-- Since we also have an lower bound for the order of the resultant,
-- we need to evaluate it only at $d-o+1$ points. Furthermore,
-- we can divide by $k^o$ and still obtain a polynomial.
for k in 1..d-o+1 repeat
p1atk := univariate eval(p1, vA, k::S)
p2atk := univariate eval(p2, vA, k::S)
-- It may happen, that the leading coefficients of one or both of
-- the polynomials changes, when we evaluate it at $k$. In this
-- case, we need to correct this by multiplying with the
-- corresponding power of the leading coefficient of the other
-- polynomial.
-- Consider the Sylvester matrix of the original polynomials. We
-- want to evaluate it at $A = k$. If the first few leading
-- coefficients of $p2$ vanish, the first few columns of the
-- Sylvester matrix have triangular shape, with the leading
-- coefficient of $p1$ on the diagonal. The same thing happens, if
-- we exchange the roles of $p1$ and $p2$, only that we have to
-- take care of the sign, too.
d1atk := degree p1atk
d2atk := degree p2atk
-- output("k: " string(k))$OutputPackage
-- output("d1: " string(d1) " d1atk: " string(d1atk))$OutputPackage
-- output("d2: " string(d2) " d2atk: " string(d2atk))$OutputPackage
if d2atk < d2 then
if d1atk < d1
then lead := 0$S
else lead := (leadingCoefficient p1atk)^((d2-d2atk)::NNI)
else
if d1atk < d1
then lead := (-1$S)^d2 *
(leadingCoefficient p2atk)^((d1-d1atk)::NNI)
else lead := 1$S
if zero? lead
then res := cons(0, res)
else res := cons(lead * (resultant(p1atk, p2atk)$SUP(S) exquo
(k::S)^(o::NNI))::S,
res)
reverse res
-- The degree of [[poly3]] is governed by $(a_0+x_m a_1)^{x_m}$.
-- Therefore, we substitute $A-x_m a1$ for $a_0$, which reduces
-- the degree in $a_1$ by $x_m-x_{i+1}$.
p_subst(xm : Integer, i : Integer, va1 : V, vA : V, basis : DIFFSPECN
) : FPOLYS ==
vA::POLYS::FPOLYS + va1::POLYS::FPOLYS _
* F2FPOLYS(DN2DL(basis, i) - DN2DL(basis, xm))
p2_subst(xm : Integer, i : Symbol, a1v : F, Av : F, basis : DIFFSPECN
) : EXPRR ==
coerce(Av) + coerce(a1v)*(basis(i::EXPRR) - basis(xm::EXPRR))
guessExpRatAux(xx : Symbol, list : List F, basis : DIFFSPECN,
xValues : List Integer, options : LGOPT) : List EXPRR ==
import from Factored(SUP F)
a1 : V := index(1)$V
A : V := index(2)$V
len : NNI := #list
if len < 4 then return []
else len := (len-3)::NNI
xlist := [F2FPOLYS DN2DL(basis, xValues.i) for i in 1..len]
x1 := F2FPOLYS DN2DL(basis, xValues.(len+1))
x2 := F2FPOLYS DN2DL(basis, xValues.(len+2))
x3 := F2FPOLYS DN2DL(basis, xValues.(len+3))
-- We try to fit the data $(s1, s2, \dots)$ to the model
-- $(a+b n)^n y(n)$, $r$ being a rational function. To obtain
-- $y$, we compute $y(n)=s_n*(a+b n)^{-n}$.
y : NNI -> FPOLYS :=
(z1 : NNI) : FPOLYS +-> F2FPOLYS(list.z1) * _
p_subst(last xValues, (xValues.z1)::Integer, a1, A, basis)^_
(-(xValues.z1)::Integer)
ylist : List FPOLYS := [y i for i in 1..len]
y1 := y(len+1)
y2 := y(len+2)
y3 := y(len+3)
res := []::List EXPRR
if (maxD := maxDegree(options)$GOPT0) case NNI
then maxDeg := min(maxD, len-1)
else maxDeg := len-1
for i in 0..maxDeg repeat
if debug(options)$GOPT0 then
output("Guess: degree ExpRat ", i::OutputForm)$OutputPackage
if debug(options)$GOPT0 then
output("Guess: interpolating...")$OutputPackage
ri : FSUPFPOLYS
:= interpolate(xlist, ylist, (len-1-i)::NNI) _
$FFFG(FPOLYS, SUP FPOLYS)
-- for experimental fraction free interpolation
-- ri: Fraction SUP POLYS
-- := interpolate(xlist, ylist, (len-1-i)::NNI) _
-- $FFFG(POLYS, SUP POLYS)
if debug(options)$GOPT0 then
-- output(hconcat("xlist: ", xlist::OutputForm))$OutputPackage
-- output(hconcat("ylist: ", ylist::OutputForm))$OutputPackage
-- output(hconcat("ri: ", ri::OutputForm))$OutputPackage
output("Guess: polynomials...")$OutputPackage
poly1 : POLYS := numer(elt(ri, x1)$SUP(FPOLYS) - y1)
poly2 : POLYS := numer(elt(ri, x2)$SUP(FPOLYS) - y2)
poly3 : POLYS := numer(elt(ri, x3)$SUP(FPOLYS) - y3)
-- for experimental fraction free interpolation
-- ri2: FSUPFPOLYS := map(#1::FPOLYS, numer ri) _
-- $SparseUnivariatePolynomialFunctions2(POLYS, FPOLYS) _
-- /map(#1::FPOLYS, denom ri) _
-- $SparseUnivariatePolynomialFunctions2(POLYS, FPOLYS)
--
-- poly1: POLYS := numer(elt(ri2, x1)$SUP(FPOLYS) - y1)
-- poly2: POLYS := numer(elt(ri2, x2)$SUP(FPOLYS) - y2)
-- poly3: POLYS := numer(elt(ri2, x3)$SUP(FPOLYS) - y3)
-- n : Integer := len - i
o1 : Integer := ord1(xValues, i)
d1 : Integer := deg1(xValues, i)
o2 : Integer := ord2(xValues, i)
d2 : Integer := deg2(xValues, i)
-- another compiler bug: using i as iterator here makes the loop break
if debug(options)$GOPT0 then
output("Guess: interpolating resultants...")$OutputPackage
res1 : SUP S
:= newton(evalResultant(poly1, poly3, o1, d1, a1, A))
$NewtonInterpolation(S)
res2 : SUP S
:= newton(evalResultant(poly2, poly3, o2, d2, a1, A))
$NewtonInterpolation(S)
-- if debug(options)$GOPT0 then
-- res1: SUP S := univariate(resultant(poly1, poly3, a1))
-- res2: SUP S := univariate(resultant(poly2, poly3, a1))
-- if res1 ~= res1res or res2 ~= res2res then
-- output(hconcat("poly1 ", poly1::OutputForm))$OutputPackage
-- output(hconcat("poly2 ", poly2::OutputForm))$OutputPackage
-- output(hconcat("poly3 ", poly3::OutputForm))$OutputPackage
-- output(hconcat("res1 ", res1::OutputForm))$OutputPackage
-- output(hconcat("res2 ", res2::OutputForm))$OutputPackage
-- output("n/i: " string(n) " " string(i))$OutputPackage
-- output("res1 ord: " string(o1) " " string(minimumDegree res1))
-- $OutputPackage
-- output("res1 deg: " string(d1) " " string(degree res1))
-- $OutputPackage
-- output("res2 ord: " string(o2) " " string(minimumDegree res2))
-- $OutputPackage
-- output("res2 deg: " string(d2) " " string(degree res2))
-- $OutputPackage
if debug(options)$GOPT0 then
output("Guess: computing gcd...")$OutputPackage
-- we want to solve over F
-- for polynomial domains S this seems to be very costly!
res3 : SUP F := SUPS2SUPF(primitivePart(gcd(res1, res2)))
if debug(options)$GOPT0 then
output("Guess: solving...")$OutputPackage
-- res3 is a polynomial in A=a0+(len+3)*a1
-- now we have to find the roots of res3
for f in factorList(sup_fact(res3)) | degree f.factor = 1 repeat
-- we are only interested in the linear factors
-- if debug(options)$GOPT0 then
-- output(hconcat("f: ", f::OutputForm))$OutputPackage
Av : F := -coefficient(f.factor, 0)
/ leadingCoefficient f.factor
-- FIXME: in an earlier version, we disregarded vanishing Av
-- maybe we intended to disregard vanishing a1v? Either doesn't really
-- make sense to me right now.
evalPoly := eval(POLYS2POLYF poly3, A, Av)
if zero? evalPoly
then evalPoly := eval(POLYS2POLYF poly1, A, Av)
-- Note that it really may happen that poly3 vanishes when specializing
-- A. Consider for example guessExpRat([1, 1, 1, 1]).
-- FIXME: We check poly1 below, too. I should work out in what cases poly3
-- vanishes.
for g in factorList(sup_fact(univariate evalPoly))
| degree g.factor = 1 repeat
-- if debug(options)$GOPT0 then
-- output("g: ", g::OutputForm)$OutputPackage
a1v : F := -coefficient(g.factor, 0)
/ leadingCoefficient g.factor
-- check whether poly1 and poly2 really vanish. Note that we could have
-- found an extraneous solution, since we only computed the gcd of the
-- two resultants.
t1 := eval(POLYS2POLYF poly1, [a1, A]::List V,
[a1v, Av]::List F)
not(zero? t1) => iterate
t2 := eval(POLYS2POLYF poly2, [a1, A]::List V,
[a1v, Av]::List F)
if zero? t2 then
ri1 : Fraction SUP POLYS
:= SUPFPOLYS2FSUPPOLYS(numer ri)
/ SUPFPOLYS2FSUPPOLYS(denom ri)
-- for experimental fraction free interpolation
-- ri1: Fraction SUP POLYS := ri
numr : SUP F := SUPPOLYS2SUPF(numer ri1, a1v, Av)
denr : SUP F := SUPPOLYS2SUPF(denom ri1, a1v, Av)
if not zero? denr then
res4 : EXPRR := eval(FSUPF2EXPRR(xx, numr/denr),
kernel(xx), basis(xx::EXPRR))
*p2_subst(last xValues, xx, a1v,
Av, basis)^xx::EXPRR
res := cons(res4, res)
else if zero? numr and debug(options)$GOPT0 then
output("Guess: numerator and denominator vanish!"
)$OutputPackage
-- If we are only interested in one solution, we do not try other
-- degrees if we have found already some solutions. I.e., the
-- indentation here is correct.
if not(empty?(res)) and one(options)$GOPT0 then return res
res
guessExpRatAux0(list : List F, basis : DIFFSPECN, options : LGOPT
) : GUESSRESULT ==
if zero? safety(options)$GOPT0 then
error "Guess: guessExpRat does not support zero safety"
-- guesses Functions of the Form (a1*n+a0)^n*rat(n)
xx := indexName(options)$GOPT0
-- restrict to safety
len : Integer := #list
if len-safety(options)$GOPT0+1 < 0 then return []
shortlist : List F := first(list, (len-safety(options)$GOPT0+1)::NNI)
-- remove zeros from list
zeros : EXPRR := 1
newlist : List F
xValues : List Integer
i : Integer := -1
for x in shortlist repeat
i := i+1
if x = 0 then
zeros := zeros * (basis(xx::EXPRR) - basis(i::EXPRR))
i := -1
for x in shortlist repeat
i := i+1
if x ~= 0 then
newlist := cons(x/retract(eval(zeros, xx::EXPRR, i::EXPRR))@F,
newlist)
xValues := cons(i, xValues)
newlist := reverse newlist
xValues := reverse xValues
res : List EXPRR
:= [eval(zeros * f, xx::EXPRR, xx::EXPRR) _
for f in guessExpRatAux(xx, newlist, basis, xValues, options)]
select(z1 +-> checkResult(z1, xx, len, list) <
len-safety(options)$GOPT0, res)
guessExpRat(list : List F, options : LGOPT) : GUESSRESULT ==
guessExpRatAux0(list, defaultD, options)
if F has RetractableTo Symbol and S has RetractableTo Symbol then
guessExpRat(q : Symbol) : GUESSER ==
(z1 : List F, z2 : LGOPT) : GUESSRESULT +->
guessExpRatAux0(z1, (i1 : EXPRR) : EXPRR +-> q::EXPRR^i1, z2)