/
trac_10519-v7.patch
3710 lines (3709 loc) · 144 KB
/
trac_10519-v7.patch
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
# HG changeset patch
# User Alex Raichev <alex.raichev@gmail.com>
# Date 1363580174 -46800
# Node ID a04e6d4357197208d7d94f8e33fb6c116c1514df
# Parent 327315909363a5708935d351a81df669a154f9e2
10519: Removed unused modules and variables
diff -r 327315909363 -r a04e6d435719 asymptotics_multivariate_generating_functions.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/asymptotics_multivariate_generating_functions.py Mon Mar 18 17:16:14 2013 +1300
@@ -0,0 +1,3699 @@
+r"""
+Let $F(x) = \sum_{\nu \in \NN^d} F_{\nu} x^\nu$ be a multivariate power series with complex coefficients that converges in a neighborhood of the origin. Assume that $F = G/H$ for some functions $G$ and $H$ holomorphic in a neighborhood of the origin.
+Assume also that $H$ is a polynomial.
+
+This Python module for use within `Sage <http://www.sagemath.org>`_ computes asymptotics for the coefficients $F_{r \alpha}$ as $r \to \infty$ with $r \alpha \in \NN^d$ for $\alpha$ in a permissible subset of $d$-tuples of positive reals.
+More specifically, it computes arbitrary terms of the asymptotic expansion for $F_{r \alpha}$ when the asymptotics are controlled by a strictly minimal multiple point of the alegbraic variety $H = 0$.
+
+The algorithms and formulas implemented here come from [RaWi2008a]_
+and [RaWi2012]_.
+
+.. [AiYu1983] I.A. Aizenberg and A.P. Yuzhakov, "Integral representations and residues in multidimensional complex analysis", Translations of Mathematical Monographs, 58. American Mathematical Society, Providence, RI, 1983. x+283 pp. ISBN: 0-8218-4511-X.
+
+.. [Raic2012] Alexander Raichev, "Leinartas's partial fraction decomposition", `<http://arxiv.org/abs/1206.4740>`_.
+
+.. [RaWi2008a] Alexander Raichev and Mark C. Wilson, "Asymptotics of coefficients of multivariate generating functions: improvements for smooth points", Electronic Journal of Combinatorics, Vol. 15 (2008), R89, `<http://arxiv.org/pdf/0803.2914.pdf>`_.
+
+.. [RaWi2012] Alexander Raichev and Mark C. Wilson, "Asymptotics of coefficients of multivariate generating functions: improvements for smooth points", To appear in 2012 in the Online Journal of Analytic Combinatorics, `<http://arxiv.org/pdf/1009.5715.pdf>`_.
+
+AUTHORS:
+
+- Alexander Raichev (2008-10-01): Initial version
+- Alexander Raichev (2010-09-28): Corrected many functions
+- Alexander Raichev (2010-12-15): Updated documentation
+- Alexander Raichev (2011-03-09): Fixed a division by zero bug in relative_error()
+- Alexander Raichev (2011-04-26): Rewrote in object-oreinted style
+- Alexander Raichev (2011-05-06): Fixed bug in cohomologous_integrand() and fixed _crit_cone_combo() to work in SR
+- Alexander Raichev (2012-08-06): Major rewrite. Created class FFPD and moved functions around.
+- Alexander Raichev (2012-10-03): Fixed whitespace errors, added examples to those six functions missing them (which i overlooked), changed package name to a more descriptive title, made asymptotics methods work for univariate functions.
+
+EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+
+A univariate smooth point example::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: H = (x - 1/2)^3
+ sage: Hfac = H.factor()
+ sage: G = -1/(x + 3)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F
+ (-1/(x + 3), [(x - 1/2, 3)])
+ sage: alpha = [1]
+ sage: decomp = F.asymptotic_decomposition(alpha)
+ sage: print decomp
+ [(0, []), (-1/2*(x^2 + 6*x + 9)*r^2/(x^5 + 9*x^4 + 27*x^3 + 27*x^2) - 1/2*(5*x^2 + 24*x + 27)*r/(x^5 + 9*x^4 + 27*x^3 + 27*x^2) - 3*(x^2 + 3*x + 3)/(x^5 + 9*x^4 + 27*x^3 + 27*x^2), [(x - 1/2, 1)])]
+ sage: F1 = decomp[1]
+ sage: p = {x: 1/2}
+ sage: asy = F1.asymptotics(p, alpha, 3)
+ sage: print asy
+ (8/343*(49*r^2 + 161*r + 114)*2^r, 2, 8/7*r^2 + 184/49*r + 912/343)
+ sage: print F.relative_error(asy[0], alpha, [1, 2, 4, 8, 16], asy[1])
+ Calculating errors table in the form
+ exponent, scaled Maclaurin coefficient, scaled asymptotic values, relative errors...
+ [((1,), 7.555555556, [7.556851312], [-0.0001714971672]), ((2,), 14.74074074, [14.74052478], [0.00001465051901]), ((4,), 35.96502058, [35.96501458], [1.667911934e-7]), ((8,), 105.8425656, [105.8425656], [4.399565380e-11]), ((16,), 355.3119534, [355.3119534], [0.0000000000])]
+
+Another smooth point example (Example 5.4 of [RaWi2008a]_)::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: q = 1/2
+ sage: qq = q.denominator()
+ sage: H = 1 - q*x + q*x*y - x^2*y
+ sage: Hfac = H.factor()
+ sage: G = (1 - q*x)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: alpha = list(qq*vector([2, 1 - q]))
+ sage: print alpha
+ [4, 1]
+ sage: I = F.smooth_critical_ideal(alpha)
+ sage: print I
+ Ideal (y^2 - 2*y + 1, x + 1/4*y - 5/4) of Multivariate Polynomial Ring
+ in x, y over Rational Field
+ sage: s = solve(I.gens(), [SR(x) for x in R.gens()], solution_dict=true)
+ sage: print s
+ [{y: 1, x: 1}]
+ sage: p = s[0]
+ sage: asy = F.asymptotics(p, alpha, 1) # long time
+ Creating auxiliary functions...
+ Computing derivatives of auxiallary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second order differential operator actions...
+ sage: print asy # long time
+ (1/12*2^(2/3)*sqrt(3)*gamma(1/3)/(pi*r^(1/3)), 1,
+ 1/12*2^(2/3)*sqrt(3)*gamma(1/3)/(pi*r^(1/3)))
+ sage: print F.relative_error(asy[0], alpha, [1, 2, 4, 8, 16], asy[1]) # long time
+ Calculating errors table in the form
+ exponent, scaled Maclaurin coefficient, scaled asymptotic values,
+ relative errors...
+ [((4, 1), 0.1875000000, [0.1953794675], [-0.04202382689]), ((8, 2),
+ 0.1523437500, [0.1550727862], [-0.01791367323]), ((16, 4), 0.1221771240,
+ [0.1230813519], [-0.007400959228]), ((32, 8), 0.09739671811,
+ [0.09768973377], [-0.003008475766]), ((64, 16), 0.07744253816,
+ [0.07753639308], [-0.001211929722])]
+
+A multiple point example (Example 6.5 of [RaWi2012]_)::
+
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - 2*x - y)**2 * (1 - x - 2*y)**2
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F
+ (1, [(x + 2*y - 1, 2), (2*x + y - 1, 2)])
+ sage: I = F.singular_ideal()
+ sage: print I
+ Ideal (x - 1/3, y - 1/3) of Multivariate Polynomial Ring in x, y over
+ Rational Field
+ sage: p = {x: 1/3, y: 1/3}
+ sage: print F.is_convenient_multiple_point(p)
+ (True, 'convenient in variables [x, y]')
+ sage: alpha = (var('a'), var('b'))
+ sage: decomp = F.asymptotic_decomposition(alpha); print decomp # long time
+ [(0, []), (-1/9*(2*a^2*y^2 - 5*a*b*x*y + 2*b^2*x^2)*r^2/(x^2*y^2) +
+ 1/9*(5*(a + b)*x*y - 6*a*y^2 - 6*b*x^2)*r/(x^2*y^2) - 1/9*(4*x^2 - 5*x*y
+ + 4*y^2)/(x^2*y^2), [(x + 2*y - 1, 1), (2*x + y - 1, 1)])]
+ sage: F1 = decomp[1]
+ sage: print F1.asymptotics(p, alpha, 2) # long time
+ (-3*((2*a^2 - 5*a*b + 2*b^2)*r^2 + (a + b)*r +
+ 3)*((1/3)^(-b)*(1/3)^(-a))^r, (1/3)^(-b)*(1/3)^(-a), -3*(2*a^2 - 5*a*b +
+ 2*b^2)*r^2 - 3*(a + b)*r - 9)
+ sage: alpha = [4, 3]
+ sage: decomp = F.asymptotic_decomposition(alpha)
+ sage: F1 = decomp[1]
+ sage: asy = F1.asymptotics(p, alpha, 2) # long time
+ sage: print asy # long time
+ (3*(10*r^2 - 7*r - 3)*2187^r, 2187, 30*r^2 - 21*r - 9)
+ sage: print F.relative_error(asy[0], alpha, [1, 2, 4, 8], asy[1]) # long time
+ Calculating errors table in the form
+ exponent, scaled Maclaurin coefficient, scaled asymptotic values,
+ relative errors...
+ [((4, 3), 30.72702332, [0.0000000000], [1.000000000]), ((8, 6),
+ 111.9315678, [69.00000000], [0.3835519207]), ((16, 12), 442.7813138,
+ [387.0000000], [0.1259793763]), ((32, 24), 1799.879232, [1743.000000],
+ [0.03160169385])]
+
+"""
+#*****************************************************************************
+# Copyright (C) 2008 Alexander Raichev <tortoise.said@gmail.com>
+#
+# Distributed under the terms of the GNU General Public License (GPL)
+# http://www.gnu.org/licenses/
+#*****************************************************************************
+
+from functools import total_ordering
+
+# Sage libraries
+from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
+from sage.rings.polynomial.polynomial_ring import is_PolynomialRing
+from sage.rings.polynomial.multi_polynomial_ring_generic import is_MPolynomialRing
+from sage.symbolic.ring import SR
+from sage.geometry.cone import Cone
+from sage.calculus.functional import diff
+from sage.calculus.functions import jacobian
+from sage.calculus.var import function, var
+from sage.combinat.combinat import stirling_number1
+from sage.combinat.permutation import Permutation
+from sage.combinat.tuple import UnorderedTuples
+from sage.functions.log import exp, log
+from sage.functions.other import factorial, gamma, sqrt
+from sage.matrix.constructor import matrix
+from sage.misc.misc import add
+from sage.misc.misc_c import prod
+from sage.misc.mrange import cartesian_product_iterator, mrange
+from sage.modules.free_module_element import vector
+from sage.rings.arith import binomial, xgcd
+from sage.rings.all import CC
+from sage.rings.fraction_field import FractionField
+from sage.rings.integer import Integer
+from sage.rings.rational_field import QQ
+from sage.sets.set import Set
+from sage.symbolic.constants import pi
+from sage.symbolic.relation import solve
+from sage.combinat.subset import Subsets
+
+@total_ordering
+class FFPD(object):
+ r"""
+ Represents a fraction with factored polynomial denominator (FFPD)
+ $p/(q_1^{e_1} \cdots q_n^{e_n})$ by storing the parts $p$ and
+ $[(q_1, e_1), \ldots, (q_n, e_n)]$.
+ Here $q_1, \ldots, q_n$ are elements of a 0- or multi-variate factorial
+ polynomial ring $R$ , $q_1, \ldots, q_n$ are distinct irreducible elements
+ of $R$ , $e_1, \ldots, e_n$ are positive integers, and $p$ is a function
+ of the indeterminates of $R$ (a Sage Symbolic Expression).
+ An element $r$ with no polynomial denominator is represented as $[r, (,)]$.
+
+ AUTHORS:
+
+ - Alexander Raichev (2012-07-26)
+ """
+
+ def __init__(self, numerator=None, denominator_factored=None,
+ quotient=None, reduce_=True):
+ r"""
+ Create a FFPD instance.
+
+ INPUT:
+
+ - ``numerator`` - (Optional; default=None) An element $p$ of a
+ 0- or 1-variate factorial polynomial ring $R$.
+ - ``denominator_factored`` - (Optional; default=None)
+ A list of the form
+ $[(q_1, e_1), \ldots, (q_n, e_n)]$ where the $q_1, \ldots, q_n$ are
+ distinct irreducible elements of $R$ and the $e_i$ are positive
+ integers.
+ - ``quotient`` - (Optional; default=None) An element of a field of
+ fractions of a factorial ring.
+ - ``reduce_`` - (Optional; default=True) If True, then represent
+ $p/(q_1^{e_1} \cdots q_n^{e_n})$ in lowest terms.
+ If False, then won't attempt to divide $p$ by any of the $q_i$.
+
+ OUTPUT:
+
+ A FFPD instance representing the rational expression
+ $p/(q_1^{e_1} \cdots q_n^{e_n})$.
+ To get a non-None output, one of ``numerator`` or ``quotient`` must be
+ non-None.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: print f
+ (1, [(y, 1), (x*y + 1, 1)])
+ sage: ff = FFPD(x, df, reduce_=False)
+ sage: print ff
+ (x, [(y, 1), (x, 1), (x*y + 1, 1)])
+
+ ::
+
+ sage: f = FFPD(x + y, [(x + y, 1)])
+ sage: print f
+ (1, [])
+
+ ::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: print FFPD(quotient=f)
+ (5*x^7 - 5*x^6 + 5/3*x^5 - 5/3*x^4 + 2*x^3 - 2/3*x^2 + 1/3*x - 1/3,
+ [(x - 1, 1), (x, 1), (x^2 + 1/3, 1)])
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: f = 2*y/(5*(x^3 - 1)*(y + 1))
+ sage: print FFPD(quotient=f)
+ (2/5*y, [(y + 1, 1), (x - 1, 1), (x^2 + x + 1, 1)])
+
+ ::
+
+ sage: R.<x, y>= PolynomialRing(QQ)
+ sage: p = 1/x^2
+ sage: q = 3*x**2*y
+ sage: qs = q.factor()
+ sage: f = FFPD(p/qs.unit(), qs)
+ sage: print f
+ (1/(3*x^2), [(y, 1), (x, 2)])
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: f = FFPD(cos(x)*x*y^2, [(x, 2), (y, 1)])
+ sage: print f
+ (x*y^2*cos(x), [(y, 1), (x, 2)])
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = exp(x + y)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: a = FFPD(quotient=G/H)
+ sage: print a
+ (e^(x + y)/(2*x^2 + 5*x*y + 2*y^2 - 3*x - 3*y + 1), [])
+ sage: print a._ring
+ None
+ sage: b = FFPD(G, H.factor())
+ sage: print b
+ (e^(x + y), [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: print b._ring
+ Multivariate Polynomial Ring in x, y over Rational Field
+
+ Singular throws a 'not implemented' error when trying to factor in
+ a multivariate polynomial ring over an inexact field ::
+
+ sage: R.<x, y>= PolynomialRing(CC)
+ sage: f = (x + 1)/(x*y*(x*y + 1)^2)
+ sage: FFPD(quotient=f)
+ Traceback (most recent call last):
+ ...
+ TypeError: Singular error:
+ ? not implemented
+ ? error occurred in or before STDIN line 17:
+ `def sage9=factorize(sage8);`
+
+ """
+ # Attributes are
+ # self._numerator
+ # self._denominator_factored
+ # self._ring
+ if quotient is not None:
+ p = quotient.numerator()
+ q = quotient.denominator()
+ R = q.parent()
+ self._numerator = quotient
+ self._denominator_factored = []
+ if is_PolynomialRing(R) or is_MPolynomialRing(R):
+ self._ring = R
+ if not R(q).is_unit():
+ # Factor q
+ try:
+ df = q.factor()
+ except NotImplementedError:
+ # Singular's factor() needs 'proof=False'.
+ df = q.factor(proof=False)
+ self._numerator = p/df.unit()
+ df = sorted([tuple(t) for t in df]) # Sort for consitency.
+ self._denominator_factored = df
+ else:
+ self._ring = None
+ # Done. No reducing needed, as Sage reduced quotient beforehand.
+ return
+
+ self._numerator = numerator
+ if denominator_factored:
+ self._denominator_factored = sorted([tuple(t) for t in
+ denominator_factored])
+ self._ring = denominator_factored[0][0].parent()
+ else:
+ self._denominator_factored = []
+ self._ring = None
+ R = self._ring
+ if R is not None and numerator in R and reduce_:
+ # Reduce fraction if possible.
+ numer = R(self._numerator)
+ df = self._denominator_factored
+ new_df = []
+ for (q, e) in df:
+ ee = e
+ quo, rem = numer.quo_rem(q)
+ while rem == 0 and ee > 0:
+ ee -= 1
+ numer = quo
+ quo, rem = numer.quo_rem(q)
+ if ee > 0:
+ new_df.append((q, ee))
+ self._numerator = numer
+ self._denominator_factored = new_df
+
+ def numerator(self):
+ r"""
+ Return the numerator of ``self``.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F.numerator()
+ -e^y
+ """
+ return self._numerator
+
+ def denominator(self):
+ r"""
+ Return the denominator of ``self``.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F.denominator()
+ x^3*y^2 + 2*x^3*y + x^2*y^2 + x^3 - 2*x^2*y - x*y^2 - 3*x^2 - 2*x*y
+ - y^2 + 3*x + 2*y - 1
+ """
+ return prod([q**e for q, e in self.denominator_factored()])
+
+ def denominator_factored(self):
+ r"""
+ Return the factorization in ``self.ring()`` of the denominator of
+ ``self`` but without the unit part.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F.denominator_factored()
+ [(x - 1, 1), (x*y + x + y - 1, 2)]
+ """
+ return self._denominator_factored
+
+ def ring(self):
+ r"""
+ Return the ring of the denominator of ``self``, which is
+ None in the case where ``self`` doesn't have a denominator.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F.ring()
+ Multivariate Polynomial Ring in x, y over Rational Field
+ sage: F = FFPD(quotient=G/H)
+ sage: print F
+ (e^y/(x^3*y^2 + 2*x^3*y + x^2*y^2 + x^3 - 2*x^2*y - x*y^2 - 3*x^2 -
+ 2*x*y - y^2 + 3*x + 2*y - 1), [])
+ sage: print F.ring()
+ None
+ """
+ return self._ring
+
+ def dimension(self):
+ r"""
+ Return the number of indeterminates of ``self.ring()``.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F.dimension()
+ 2
+ """
+ R = self.ring()
+ if is_PolynomialRing(R) or is_MPolynomialRing(R):
+ return R.ngens()
+ else:
+ return None
+
+ def list(self):
+ r"""
+ Convert ``self`` into a list for printing.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F # indirect doctest
+ (-e^y, [(x - 1, 1), (x*y + x + y - 1, 2)])
+ """
+ return (self.numerator(), self.denominator_factored())
+
+ def quotient(self):
+ r"""
+ Convert ``self`` into a quotient.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y>= PolynomialRing(QQ)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: print F
+ (-e^y, [(x - 1, 1), (x*y + x + y - 1, 2)])
+ sage: print F.quotient()
+ -e^y/(x^3*y^2 + 2*x^3*y + x^2*y^2 + x^3 - 2*x^2*y - x*y^2 - 3*x^2 -
+ 2*x*y - y^2 + 3*x + 2*y - 1)
+ """
+ return self.numerator()/self.denominator()
+
+ def __str__(self):
+ r"""
+ Returns a string representation of ``self``
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: f = FFPD(x + y, [(y, 1), (x, 1)])
+ sage: print f
+ (x + y, [(y, 1), (x, 1)])
+
+ """
+ return str(self.list())
+
+ def __eq__(self, other):
+ r"""
+ Two FFPD instances are equal iff they represent the same
+ fraction.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y>= PolynomialRing(QQ)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: ff = FFPD(x, df, reduce_=False)
+ sage: f == ff
+ True
+ sage: g = FFPD(y, df)
+ sage: g == f
+ False
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = exp(x + y)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: a = FFPD(quotient=G/H)
+ sage: b = FFPD(G, H.factor())
+ sage: bool(a == b)
+ True
+ """
+ return self.quotient() == other.quotient()
+
+ def __ne__(self, other):
+ r"""
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y>= PolynomialRing(QQ)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: ff = FFPD(x, df, reduce_=False)
+ sage: f != ff
+ False
+ sage: g = FFPD(y, df)
+ sage: g != f # indirect doctest
+ True
+ """
+ return not (self == other)
+
+ def __lt__(self, other):
+ r"""
+ FFPD A is less than FFPD B iff
+ (the denominator factorization of A is shorter than that of B) or
+ (the denominator factorization lengths are equal and
+ the denominator of A is less than that of B in their ring) or
+ (the denominator factorization lengths are equal and the
+ denominators are equal and the numerator of A is less than that of B
+ in their ring).
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y>= PolynomialRing(QQ)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: ff = FFPD(x, df, reduce_=False)
+ sage: g = FFPD(y, df)
+ sage: h = FFPD(exp(x), df)
+ sage: i = FFPD(sin(x + 2), df)
+ sage: print f, ff
+ (1, [(y, 1), (x*y + 1, 1)]) (x, [(y, 1), (x, 1), (x*y + 1, 1)])
+ sage: print f < ff
+ True
+ sage: print f < g
+ True
+ sage: print g < h
+ True
+ sage: print h < i
+ False
+ """
+ sn = self.numerator()
+ on = other.numerator()
+ sdf = self.denominator_factored()
+ odf = other.denominator_factored()
+ sd = self.denominator()
+ od = other.denominator()
+
+ return bool(len(sdf) < len(odf) or\
+ (len(sdf) == len(odf) and sd < od) or\
+ (len(sdf) == len(odf) and sd == od and sn < on))
+
+ def univariate_decomposition(self):
+ r"""
+ Return the usual univariate partial fraction decomposition
+ of ``self`` as a FFPDSum instance.
+ Assume that ``self`` lies in the field of fractions
+ of a univariate factorial polynomial ring.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+
+ One variable::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: print f
+ (15*x^7 - 15*x^6 + 5*x^5 - 5*x^4 + 6*x^3 - 2*x^2 + x - 1)/(3*x^4 -
+ 3*x^3 + x^2 - x)
+ sage: decomp = FFPD(quotient=f).univariate_decomposition()
+ sage: print decomp
+ [(5*x^3, []), (1, [(x - 1, 1)]), (1, [(x, 1)]),
+ (1/3, [(x^2 + 1/3, 1)])]
+ sage: print decomp.sum().quotient() == f
+ True
+
+ One variable with numerator in symbolic ring::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + exp(x)/(3*x^2 + 1)
+ sage: print f
+ e^x/(3*x^2 + 1) + ((5*(x - 1)*x^3 + 2)*x - 1)/((x - 1)*x)
+ sage: decomp = FFPD(quotient=f).univariate_decomposition()
+ sage: print decomp
+ [(e^x/(3*x^2 + 1) + ((5*(x - 1)*x^3 + 2)*x - 1)/((x - 1)*x), [])]
+
+ One variable over a finite field::
+
+ sage: R.<x> = PolynomialRing(GF(2))
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: print f
+ (x^6 + x^4 + 1)/(x^3 + x)
+ sage: decomp = FFPD(quotient=f).univariate_decomposition()
+ sage: print decomp
+ [(x^3, []), (1, [(x, 1)]), (x, [(x + 1, 2)])]
+ sage: print decomp.sum().quotient() == f
+ True
+
+ One variable over an inexact field::
+
+ sage: R.<x> = PolynomialRing(CC)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: print f
+ (15.0000000000000*x^7 - 15.0000000000000*x^6 + 5.00000000000000*x^5
+ - 5.00000000000000*x^4 + 6.00000000000000*x^3 -
+ 2.00000000000000*x^2 + x - 1.00000000000000)/(3.00000000000000*x^4
+ - 3.00000000000000*x^3 + x^2 - x)
+ sage: decomp = FFPD(quotient=f).univariate_decomposition()
+ sage: print decomp
+ [(5.00000000000000*x^3, []), (1.00000000000000,
+ [(x - 1.00000000000000, 1)]), (-0.288675134594813*I,
+ [(x - 0.577350269189626*I, 1)]), (1.00000000000000, [(x, 1)]),
+ (0.288675134594813*I, [(x + 0.577350269189626*I, 1)])]
+ sage: print decomp.sum().quotient() == f # Rounding error coming
+ False
+
+ NOTE::
+
+ Let $f = p/q$ be a rational expression where $p$ and $q$ lie in a
+ univariate factorial polynomial ring $R$.
+ Let $q_1^{e_1} \cdots q_n^{e_n}$ be the
+ unique factorization of $q$ in $R$ into irreducible factors.
+ Then $f$ can be written uniquely as
+
+ (*) $p_0 + \sum_{i=1}^{m} p_i/ q_i^{e_i}$,
+
+ for some $p_j \in R$.
+ I call (*) the *usual partial fraction decomposition* of f.
+
+ AUTHORS:
+
+ - Robert Bradshaw (2007-05-31)
+ - Alexander Raichev (2012-06-25)
+ """
+ if self.dimension() is None or self.dimension() > 1:
+ return FFPDSum([self])
+
+ R = self.ring()
+ p = self.numerator()
+ q = self.denominator()
+ if p in R:
+ whole, p = p.quo_rem(q)
+ else:
+ whole = p
+ p = R(1)
+ df = self.denominator_factored()
+ decomp = [FFPD(whole, [])]
+ for (a, m) in df:
+ numer = p * prod([b**n for (b, n) in df if b != a]).\
+ inverse_mod(a**m) % (a**m)
+ # The inverse exists because the product and a**m
+ # are relatively prime.
+ decomp.append(FFPD(numer, [(a, m)]))
+ return FFPDSum(decomp)
+
+ def nullstellensatz_certificate(self):
+ r"""
+ Let $[(q_1, e_1), \ldots, (q_n, e_n)]$ be the denominator factorization
+ of ``self``.
+ Return a list of polynomials $h_1, \ldots, h_m$ in ``self.ring()``
+ that satisfies $h_1 q_1 + \cdots + h_m q_n = 1$ if it exists.
+ Otherwise return None.
+ Only works for multivariate ``self``.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = sin(x)
+ sage: H = x^2 * (x*y + 1)
+ sage: f = FFPD(G, H.factor())
+ sage: L = f.nullstellensatz_certificate()
+ sage: print L
+ [y^2, -x*y + 1]
+ sage: df = f.denominator_factored()
+ sage: sum([L[i]*df[i][0]**df[i][1] for i in xrange(len(df))]) == 1
+ True
+
+ ::
+
+ sage: f = 1/(x*y)
+ sage: L = FFPD(quotient=f).nullstellensatz_certificate()
+ sage: L is None
+ True
+
+ """
+
+ R = self.ring()
+ if R is None:
+ return None
+
+ df = self.denominator_factored()
+ J = R.ideal([q**e for q, e in df])
+ if R(1) in J:
+ return R(1).lift(J)
+ else:
+ return None
+
+ def nullstellensatz_decomposition(self):
+ r"""
+ Return a Nullstellensatz decomposition of ``self`` as a FFPDSum
+ instance.
+
+ Recursive.
+ Only works for multivariate ``self``.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: f = 1/(x*(x*y + 1))
+ sage: decomp = FFPD(quotient=f).nullstellensatz_decomposition()
+ sage: print decomp
+ [(0, []), (1, [(x, 1)]), (-y, [(x*y + 1, 1)])]
+ sage: decomp.sum().quotient() == f
+ True
+ sage: for r in decomp:
+ ... L = r.nullstellensatz_certificate()
+ ... L is None
+ ...
+ True
+ True
+ True
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = sin(y)
+ sage: H = x*(x*y + 1)
+ sage: f = FFPD(G, H.factor())
+ sage: decomp = f.nullstellensatz_decomposition()
+ sage: print decomp
+ [(0, []), (sin(y), [(x, 1)]), (-y*sin(y), [(x*y + 1, 1)])]
+ sage: bool(decomp.sum().quotient() == G/H)
+ True
+ sage: for r in decomp:
+ ... L = r.nullstellensatz_certificate()
+ ... L is None
+ ...
+ True
+ True
+ True
+
+ NOTE::
+
+ Let $f = p/q$ where $q$ lies in a $d$ -variate polynomial ring $K[X]$ for some field $K$ and $d \ge 1$.
+ Let $q_1^{e_1} \cdots q_n^{e_n}$ be the
+ unique factorization of $q$ in $K[X]$ into irreducible factors and
+ let $V_i$ be the algebraic variety $\{x \in L^d: q_i(x) = 0\}$ of
+ $q_i$ over the algebraic closure $L$ of $K$.
+ By [Raic2012]_, $f$ can be written as
+
+ (*) $\sum p_A/\prod_{i \in A} q_i^{e_i}$,
+
+ where the $p_A$ are products of $p$ and elements in $K[X]$ and
+ the sum is taken over all subsets
+ $A \subseteq \{1, \ldots, m\}$ such that
+ $\cap_{i\in A} T_i \neq \emptyset$.
+
+ I call (*) a *Nullstellensatz decomposition* of $f$.
+ Nullstellensatz decompositions are not unique.
+
+ The algorithm used comes from [Raic2012]_.
+ """
+ L = self.nullstellensatz_certificate()
+ if L is None:
+ # No decomposing possible.
+ return FFPDSum([self])
+
+ # Otherwise decompose recursively.
+ decomp = FFPDSum()
+ p = self.numerator()
+ df = self.denominator_factored()
+ m = len(df)
+ iteration1 = FFPDSum([FFPD(p*L[i],[df[j]
+ for j in xrange(m) if j != i])
+ for i in xrange(m) if L[i] != 0])
+
+ # Now decompose each FFPD of iteration1.
+ for r in iteration1:
+ decomp.extend(r.nullstellensatz_decomposition())
+
+ # Simplify and return result.
+ return decomp.combine_like_terms().whole_and_parts()
+
+ def algebraic_dependence_certificate(self):
+ r"""
+ Return the ideal $J$ of annihilating polynomials for the set
+ of polynomials ``[q**e for (q, e) in self.denominator_factored()]``,
+ which could be the zero ideal.
+ The ideal $J$ lies in a polynomial ring over the field
+ ``self.ring().base_ring()`` that has
+ ``m = len(self.denominator_factored())`` indeterminates.
+ Return None if ``self.ring()`` is None.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: f = 1/(x^2 * (x*y + 1) * y^3)
+ sage: ff = FFPD(quotient=f)
+ sage: J = ff.algebraic_dependence_certificate()
+ sage: print J
+ Ideal (1 - 6*T2 + 15*T2^2 - 20*T2^3 + 15*T2^4 - T0^2*T1^3 -
+ 6*T2^5 + T2^6) of Multivariate Polynomial Ring in
+ T0, T1, T2 over Rational Field
+ sage: g = J.gens()[0]
+ sage: df = ff.denominator_factored()
+ sage: g(*(q**e for q, e in df)) == 0
+ True
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = exp(x + y)
+ sage: H = x^2 * (x*y + 1) * y^3
+ sage: ff = FFPD(G, H.factor())
+ sage: J = ff.algebraic_dependence_certificate()
+ sage: print J
+ Ideal (1 - 6*T2 + 15*T2^2 - 20*T2^3 + 15*T2^4 - T0^2*T1^3 -
+ 6*T2^5 + T2^6) of Multivariate Polynomial Ring in
+ T0, T1, T2 over Rational Field
+ sage: g = J.gens()[0]
+ sage: df = ff.denominator_factored()
+ sage: g(*(q**e for q, e in df)) == 0
+ True
+
+ ::
+
+ sage: f = 1/(x^3 * y^2)
+ sage: J = FFPD(quotient=f).algebraic_dependence_certificate()
+ sage: print J
+ Ideal (0) of Multivariate Polynomial Ring in T0, T1 over
+ Rational Field
+
+ ::
+
+ sage: f = sin(1)/(x^3 * y^2)
+ sage: J = FFPD(quotient=f).algebraic_dependence_certificate()
+ sage: print J
+ None
+ """
+ R = self.ring()
+ if R is None:
+ return None
+
+ df = self.denominator_factored()
+ if not df:
+ return R.ideal() # The zero ideal.
+ m = len(df)
+ F = R.base_ring()
+ Xs = list(R.gens())
+ d = len(Xs)
+
+ # Expand R by 2*m new variables.
+ S = 'S'
+ while S in [str(x) for x in Xs]:
+ S = S + 'S'
+ Ss = [S + str(i) for i in xrange(m)]
+ T = 'T'
+ while T in [str(x) for x in Xs]:
+ T = T + 'T'
+ Ts = [T + str(i) for i in xrange(m)]
+
+ Vs = [str(x) for x in Xs] + Ss + Ts
+ RR = PolynomialRing(F, Vs)
+ Xs = RR.gens()[:d]
+ Ss = RR.gens()[d: d + m]
+ Ts = RR.gens()[d + m: d + 2 * m]
+
+ # Compute the appropriate elimination ideal.
+ J = RR.ideal([ Ss[j] - RR(df[j][0]) for j in xrange(m)] +\
+ [ Ss[j]**df[j][1] - Ts[j] for j in xrange(m)])
+ J = J.elimination_ideal(Xs + Ss)
+
+ # Coerce J into the polynomial ring in the indeteminates Ts[m:].
+ # I choose the negdeglex order because i find it useful in my work.
+ RRR = PolynomialRing(F, [str(t) for t in Ts], order ='negdeglex')
+ return RRR.ideal(J)
+
+ def algebraic_dependence_decomposition(self, whole_and_parts=True):
+ r"""
+ Return an algebraic dependence decomposition of ``self`` as a FFPDSum
+ instance.
+
+ Recursive.
+
+ EXAMPLES::
+
+ sage: from sage.combinat.asymptotics_multivariate_generating_functions import *
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: f = 1/(x^2 * (x*y + 1) * y^3)
+ sage: ff = FFPD(quotient=f)
+ sage: decomp = ff.algebraic_dependence_decomposition()
+ sage: print decomp
+ [(0, []), (-x, [(x*y + 1, 1)]), (x^2*y^2 - x*y + 1,
+ [(y, 3), (x, 2)])]
+ sage: print decomp.sum().quotient() == f
+ True
+ sage: for r in decomp:
+ ... J = r.algebraic_dependence_certificate()
+ ... J is None or J == J.ring().ideal() # The zero ideal
+ ...
+ True
+ True
+ True
+
+ ::
+
+ sage: R.<x, y> = PolynomialRing(QQ)
+ sage: G = sin(x)
+ sage: H = x^2 * (x*y + 1) * y^3
+ sage: f = FFPD(G, H.factor())
+ sage: decomp = f.algebraic_dependence_decomposition()
+ sage: print decomp
+ [(0, []), (x^4*y^3*sin(x), [(x*y + 1, 1)]),
+ (-(x^5*y^5 - x^4*y^4 + x^3*y^3 - x^2*y^2 + x*y - 1)*sin(x),
+ [(y, 3), (x, 2)])]
+ sage: bool(decomp.sum().quotient() == G/H)
+ True
+ sage: for r in decomp:
+ ... J = r.algebraic_dependence_certificate()
+ ... J is None or J == J.ring().ideal()
+ ...
+ True
+ True
+ True
+
+ NOTE::
+
+ Let $f = p/q$ where $q$ lies in a $d$ -variate polynomial ring
+ $K[X]$ for some field $K$.
+ Let $q_1^{e_1} \cdots q_n^{e_n}$ be the
+ unique factorization of $q$ in $K[X]$ into irreducible factors and
+ let $V_i$ be the algebraic variety $\{x\in L^d: q_i(x) = 0\}$ of
+ $q_i$ over the algebraic closure $L$ of $K$.
+ By [Raic2012]_, $f$ can be written as
+
+ (*) $\sum p_A/\prod_{i \in A} q_i^{b_i}$,
+
+ where the $b_i$ are positive integers, each $p_A$ is a products
+ of $p$ and an element in $K[X]$,
+ and the sum is taken over all subsets
+ $A \subseteq \{1, \ldots, m\}$ such that $|A| \le d$ and
+ $\{q_i : i\in A\}$ is algebraically independent.
+
+ I call (*) an *algebraic dependence decomposition* of $f$.
+ Algebraic dependence decompositions are not unique.
+
+ The algorithm used comes from [Raic2012]_.