Ticket #13433: trac_13433base_patchdarijs_mod.patch
File trac_13433base_patchdarijs_mod.patch, 39.6 KB (added by , 9 years ago) 


sage/combinat/species/series.py
# HG changeset patch # User Mike Hansen <mhansen@gmail.com> # Date 1374443204 3600 # Node ID fa658c53d32c07db906e12255cdae79949849b74 # Parent 58c07f33b3989be386d38e7af538e72b4aacb46f Lazy power series: fix bad handling of base ring and categorification diff git a/sage/combinat/species/series.py b/sage/combinat/species/series.py
a b In particular, the relevant section for 15 15 http://www.risc.unilinz.ac.at/people/hemmecke/AldorCombinat/combinatse9.html. 16 16 """ 17 17 #***************************************************************************** 18 # Copyright (C) 2008 Mike Hansen <mhansen@gmail.com>, 18 # Copyright (C) 2008 Mike Hansen <mhansen@gmail.com>, 19 19 # 20 20 # Distributed under the terms of the GNU General Public License (GPL) 21 21 # … … from sage.rings.all import Integer, prod 34 34 from functools import partial 35 35 from sage.misc.misc import repr_lincomb, is_iterator 36 36 37 from sage.algebras.algebra import Algebra 38 from sage.algebras.algebra_element import AlgebraElement 39 import sage.structure.parent_base 40 from sage.categories.all import Rings 37 from sage.structure.unique_representation import UniqueRepresentation 38 from sage.structure.element import Element 39 from sage.structure.parent_base import ParentWithBase 40 from sage.categories.all import (Rings, CommutativeRings, 41 Algebras, CommutativeAlgebras) 41 42 42 class LazyPowerSeriesRing( Algebra):43 class LazyPowerSeriesRing(UniqueRepresentation, ParentWithBase): 43 44 def __init__(self, R, element_class = None, names=None): 44 45 """ 45 46 TESTS:: 46 47 47 48 sage: from sage.combinat.species.series import LazyPowerSeriesRing 48 sage: L = LazyPowerSeriesRing(QQ) 49 sage: loads(dumps(L)) 49 sage: L = LazyPowerSeriesRing(QQ); L 50 50 Lazy Power Series Ring over Rational Field 51 sage: L.category() 52 Category of commutative algebras over Rational Field 53 54 We try to check that LazyPowerSeries works in non commutative cases (:trac:`13433`):: 55 56 sage: G = SymmetricGroup(3) 57 sage: R = G.algebra(QQ) 58 sage: L = LazyPowerSeriesRing(R) 59 sage: L.category() 60 Category of algebras over Group algebra of Symmetric group of order 3! as a permutation group over Rational Field 61 sage: s = sum(R(p)*L.gen()^p.length() for p in G); s 62 Uninitialized lazy power series 63 sage: s.coefficient(10) 64 0 65 sage: s 66 B[()]*1 + (B[(2,3)]+B[(1,2)])*x + (B[(1,2,3)]+B[(1,3,2)])*x^2 + B[(1,3)]*x^3 + O(x^11) 67 sage: s.aorder 68 0 69 70 sage: p = R.monomial(G.an_element()); p 71 B[(1,2,3)] 72 sage: s1 = s*L(p); s1.coefficient(10); s1 73 0 74 B[(1,2,3)]*1 + (B[(1,2)]+B[(1,3)])*x + (B[()]+B[(1,3,2)])*x^2 + B[(2,3)]*x^3 + O(x^11) 75 76 sage: s2 = L(p)*s; s2.coefficient(10); s2 77 0 78 B[(1,2,3)]*1 + (B[(2,3)]+B[(1,3)])*x + (B[()]+B[(1,3,2)])*x^2 + B[(1,2)]*x^3 + O(x^11) 79 sage: s.aorder 80 0 81 82 Equality is not decidable, so most tests are skipped:: 83 84 sage: TestSuite(L).run( 85 ... skip = ['_test_additive_associativity', '_test_associativity', 86 ... '_test_distributivity', '_test_elements', '_test_one', 87 ... '_test_prod', '_test_zero']) 51 88 """ 52 89 #Make sure R is a ring with unit element 53 90 if not R in Rings(): 54 91 raise TypeError, "Argument R must be a ring." 55 try:56 z = R(Integer(1))57 except StandardError:58 raise ValueError, "R must have a unit element"59 92 60 93 #Take care of the names 61 94 if names is None: 62 95 names = 'x' 63 96 else: 64 97 names = names[0] 65 66 self. _element_class= element_class if element_class is not None else LazyPowerSeries98 99 self.Element = element_class if element_class is not None else LazyPowerSeries 67 100 self._order = None 68 101 self._name = names 69 sage.structure.parent_base.ParentWithBase.__init__(self, R) 102 if R in CommutativeRings(): 103 category = CommutativeAlgebras(R) 104 else: 105 category = Algebras(R) 106 ParentWithBase.__init__( 107 self, R, category = category) 70 108 71 109 def ngens(self): 72 110 """ 73 111 EXAMPLES:: 74 112 75 113 sage: LazyPowerSeriesRing(QQ).ngens() 76 114 1 77 115 """ 78 116 return 1 79 117 80 def _ _repr__(self):118 def _repr_(self): 81 119 """ 82 120 EXAMPLES:: 83 121 84 122 sage: LazyPowerSeriesRing(QQ) 85 123 Lazy Power Series Ring over Rational Field 86 124 """ … … class LazyPowerSeriesRing(Algebra): 89 127 def __cmp__(self, x): 90 128 """ 91 129 EXAMPLES:: 92 130 93 131 sage: LQ = LazyPowerSeriesRing(QQ) 94 132 sage: LZ = LazyPowerSeriesRing(ZZ) 95 133 sage: LQ == LQ … … class LazyPowerSeriesRing(Algebra): 104 142 def _coerce_impl(self, x): 105 143 """ 106 144 EXAMPLES:: 107 145 108 146 sage: L1 = LazyPowerSeriesRing(QQ) 109 147 sage: L2 = LazyPowerSeriesRing(RR) 110 148 sage: L2.has_coerce_map_from(L1) 111 149 True 112 150 sage: L1.has_coerce_map_from(L2) 113 151 False 114 152 115 153 :: 116 154 117 155 sage: a = L1([1]) + L2([1]) 118 156 sage: a.coefficients(3) 119 157 [2.00000000000000, 2.00000000000000, 2.00000000000000] 120 158 """ 121 159 return self(x) 122 160 123 161 124 162 def __call__(self, x=None, order=unk): 125 163 """ 126 164 EXAMPLES:: 127 165 128 166 sage: from sage.combinat.species.stream import Stream 129 167 sage: L = LazyPowerSeriesRing(QQ) 130 168 sage: L() … … class LazyPowerSeriesRing(Algebra): 137 175 [0, 1, 1, 2, 2, 3, 3, 4, 4, 5] 138 176 sage: L(Stream(ZZ)).coefficients(10) 139 177 [0, 1, 1, 2, 2, 3, 3, 4, 4, 5] 140 178 141 179 :: 142 180 143 181 sage: a = L([1,2,3]) 144 182 sage: a.coefficients(3) 145 183 [1, 2, 3] … … class LazyPowerSeriesRing(Algebra): 153 191 Traceback (most recent call last): 154 192 ... 155 193 TypeError: do not know how to coerce ... into self 156 194 157 195 TESTS:: 158 196 159 197 sage: L(pi) 160 198 Traceback (most recent call last): 161 199 ... 162 200 TypeError: do not know how to coerce pi into self 163 201 """ 164 cls = self. _element_class202 cls = self.element_class 165 203 BR = self.base_ring() 166 204 167 205 if x is None: 168 206 res = cls(self, stream=None, order=unk, aorder=unk, 169 207 aorder_changed=True, is_initialized=False) … … class LazyPowerSeriesRing(Algebra): 172 210 173 211 if isinstance(x, LazyPowerSeries): 174 212 x_parent = x.parent() 175 if x_parent.__class__ != self.__class__: 213 214 # Same kind (ordinary/exponential...) of power series ring. 215 # __base__ removes _with_category 216 if x_parent.__class__.__base__ != self.__class__.__base__: 176 217 raise ValueError 177 218 178 219 if x_parent.base_ring() == self.base_ring(): 179 220 return x 180 221 else: 181 222 if self.base_ring().has_coerce_map_from(x_parent.base_ring()): 182 223 return x._new(partial(x._change_ring_gen, self.base_ring()), lambda ao: ao, x, parent=self) 183 224 184 225 185 226 if hasattr(x, "parent") and BR.has_coerce_map_from(x.parent()): 186 227 x = BR(x) … … class LazyPowerSeriesRing(Algebra): 199 240 elif not hasattr(x, "parent"): 200 241 x = BR(x) 201 242 return self.term(x, 0) 202 243 203 244 raise TypeError, "do not know how to coerce %s into self"%x 204 245 205 246 def zero_element(self): 206 247 """ 207 248 Returns the zero power series. 208 249 209 250 EXAMPLES:: 210 251 211 252 sage: L = LazyPowerSeriesRing(QQ) 212 253 sage: L.zero_element() 213 254 0 214 255 """ 215 return self(self.base_ring() (0))256 return self(self.base_ring().zero()) 216 257 217 258 def identity_element(self): 218 259 """ 219 260 Returns the one power series. 220 261 221 262 EXAMPLES:: 222 263 223 264 sage: L = LazyPowerSeriesRing(QQ) 224 265 sage: L.identity_element() 225 266 1 226 267 """ 227 return self(self.base_ring() (1))268 return self(self.base_ring().one()) 228 269 229 def gen(self , i=0):270 def gen(self): 230 271 """ 272 Returns the generator for `self` 273 231 274 EXAMPLES:: 232 275 233 276 sage: L = LazyPowerSeriesRing(QQ) 234 277 sage: L.gen().coefficients(5) 235 278 [0, 1, 0, 0, 0] 279 sage: L.gen()[1].parent() is QQ 280 True 236 281 """ 237 res = self._new_initial(1, Stream([0,1,0])) 282 R0 = self.base_ring().zero() 283 R1 = self.base_ring().one() 284 res = self._new_initial(1, Stream([R0, R1, R0])) 238 285 res._name = self._name 239 286 return res 240 287 288 def gens(self): 289 r""" 290 Returns the generators for `self` 291 292 EXAMPLES:: 293 294 sage: L.<z> = LazyPowerSeriesRing(QQ) 295 sage: z 296 z 297 sage: z.parent() 298 Lazy Power Series Ring over Rational Field 299 300 TESTS:: 301 302 sage: z.parent() is L 303 True 304 """ 305 return [self.gen()] 306 241 307 def term(self, r, n): 242 308 """ 243 309 EXAMPLES:: 244 310 245 311 sage: L = LazyPowerSeriesRing(QQ) 246 312 sage: L.term(0,0) 247 313 0 … … class LazyPowerSeriesRing(Algebra): 255 321 res = self._new_initial(inf, Stream([0])) 256 322 res._name = "0" 257 323 else: 258 zero = BR (0)324 zero = BR.zero() 259 325 s = [zero]*n+[BR(r),zero] 260 326 res = self._new_initial(n, Stream(s)) 261 327 … … class LazyPowerSeriesRing(Algebra): 271 337 def _new_initial(self, order, stream): 272 338 """ 273 339 Returns a new power series with specified order. 274 340 275 341 INPUT: 276 277 342 343 278 344  ``order``  a nonnegative integer 279 345 280 346  ``stream``  a Stream object 281 282 347 348 283 349 EXAMPLES:: 284 350 285 351 sage: from sage.combinat.species.stream import Stream 286 352 sage: L = LazyPowerSeriesRing(QQ) 287 353 sage: L._new_initial(0, Stream([1,2,3,0])).coefficients(5) 288 354 [1, 2, 3, 0, 0] 289 355 """ 290 return self. _element_class(self, stream=stream, order=order, aorder=order,356 return self.element_class(self, stream=stream, order=order, aorder=order, 291 357 aorder_changed=False, is_initialized=True) 292 358 293 359 … … class LazyPowerSeriesRing(Algebra): 295 361 """ 296 362 Returns a generator for the coefficients of the sum the the lazy 297 363 power series in series_list. 298 364 299 365 INPUT: 300 301 366 367 302 368  ``series_list``  a list of lazy power series 303 304 369 370 305 371 EXAMPLES:: 306 372 307 373 sage: L = LazyPowerSeriesRing(QQ) 308 374 sage: series_list = [ L([1]), L([0,1]), L([0,0,1]) ] 309 375 sage: g = L._sum_gen(series_list) … … class LazyPowerSeriesRing(Algebra): 321 387 def sum(self, a): 322 388 """ 323 389 EXAMPLES:: 324 390 325 391 sage: L = LazyPowerSeriesRing(QQ) 326 392 sage: l = [L(ZZ)]*3 327 393 sage: L.sum(l).coefficients(10) … … class LazyPowerSeriesRing(Algebra): 333 399 def _sum_generator_gen(self, g): 334 400 """ 335 401 EXAMPLES:: 336 402 337 403 sage: L = LazyPowerSeriesRing(QQ) 338 404 sage: s = L([1]) 339 405 sage: def f(): … … class LazyPowerSeriesRing(Algebra): 346 412 s = Stream(g) 347 413 n = 0 348 414 while True: 349 r = s[n].coefficient(n) 415 r = s[n].coefficient(n) 350 416 for i in range(len(s)1): 351 417 r += s[i].coefficient(n) 352 418 yield r … … class LazyPowerSeriesRing(Algebra): 355 421 def sum_generator(self, g): 356 422 """ 357 423 EXAMPLES:: 358 424 359 425 sage: L = LazyPowerSeriesRing(QQ) 360 426 sage: g = [L([1])]*6 + [L(0)] 361 427 sage: t = L.sum_generator(g) 362 428 sage: t.coefficients(10) 363 429 [1, 2, 3, 4, 5, 6, 6, 6, 6, 6] 364 430 365 431 :: 366 432 367 433 sage: s = L([1]) 368 434 sage: def g(): 369 435 ... while True: … … class LazyPowerSeriesRing(Algebra): 378 444 def _product_generator_gen(self, g): 379 445 """ 380 446 EXAMPLES:: 381 447 382 448 sage: from itertools import imap 383 449 sage: from sage.combinat.species.stream import _integers_from 384 450 sage: L = LazyPowerSeriesRing(QQ) … … class LazyPowerSeriesRing(Algebra): 405 471 def product_generator(self, g): 406 472 """ 407 473 EXAMPLES:: 408 474 409 475 sage: L = LazyPowerSeriesRing(QQ) 410 476 sage: s1 = L([1,1,0]) 411 477 sage: s2 = L([1,0,1,0]) … … class LazyPowerSeriesRing(Algebra): 420 486 sage: p = L.product_generator(g()) 421 487 sage: p.coefficients(26) 422 488 [1, 1, 1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 4, 4, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0] 423 489 424 490 :: 425 491 426 492 sage: def m(n): 427 493 ... yield 1 428 494 ... while True: … … class LazyPowerSeriesRing(Algebra): 438 504 ... yield 0 439 505 ... yield q 440 506 ... 441 507 442 508 :: 443 509 444 510 sage: def lhs_gen(): 445 511 ... n = 1 446 512 ... while True: 447 513 ... yield L(m(n)) 448 514 ... n += 1 449 515 ... 450 516 451 517 :: 452 518 453 519 sage: def rhs_gen(): 454 520 ... n = 1 455 521 ... while True: … … class LazyPowerSeriesRing(Algebra): 465 531 """ 466 532 return self(self._product_generator_gen(g)) 467 533 468 469 470 class LazyPowerSeries(AlgebraElement): 534 class LazyPowerSeries(Element): 471 535 def __init__(self, A, stream=None, order=None, aorder=None, aorder_changed=True, is_initialized=False, name=None): 472 536 """ 473 537 EXAMPLES:: 474 538 475 539 sage: L = LazyPowerSeriesRing(QQ) 476 540 sage: f = L() 477 541 sage: loads(dumps(f)) 478 542 Uninitialized lazy power series 479 543 """ 480 AlgebraElement.__init__(self, A)544 Element.__init__(self, A) 481 545 self._stream = stream 482 546 self.order = unk if order is None else order 483 547 self.aorder = unk if aorder is None else aorder … … class LazyPowerSeries(AlgebraElement): 491 555 def compute_aorder(*args, **kwargs): 492 556 """ 493 557 The default compute_aorder does nothing. 494 558 495 559 EXAMPLES:: 496 560 497 561 sage: L = LazyPowerSeriesRing(QQ) 498 562 sage: a = L(1) 499 563 sage: a.compute_aorder() is None … … class LazyPowerSeries(AlgebraElement): 504 568 def _get_repr_info(self, x): 505 569 """ 506 570 EXAMPLES:: 507 571 508 572 sage: L = LazyPowerSeriesRing(QQ) 509 573 sage: a = L([1,2,3]) 510 574 sage: a.compute_coefficients(5) … … class LazyPowerSeries(AlgebraElement): 517 581 c = [ self._stream[i] for i in range(n) ] 518 582 return [ (m,c) for m,c in zip(m,c) if c != 0] 519 583 520 def _ _repr__(self):584 def _repr_(self): 521 585 """ 522 586 EXAMPLES:: 523 587 524 588 sage: L = LazyPowerSeriesRing(QQ) 525 589 sage: s = L(); s._name = 's'; s 526 590 s 527 591 528 592 :: 529 593 530 594 sage: L() 531 595 Uninitialized lazy power series 532 596 533 597 :: 534 598 535 599 sage: a = L([1,2,3]) 536 600 sage: a 537 601 O(1) … … class LazyPowerSeries(AlgebraElement): 541 605 sage: a.compute_coefficients(4) 542 606 sage: a 543 607 1 + 2*x + 3*x^2 + 3*x^3 + 3*x^4 + 3*x^5 + ... 544 608 545 609 :: 546 610 547 611 sage: a = L([1,2,3,0]) 548 612 sage: a.compute_coefficients(5) 549 613 sage: a … … class LazyPowerSeries(AlgebraElement): 551 615 """ 552 616 if self._name is not None: 553 617 return self._name 554 618 555 619 if self.is_initialized: 556 620 n = len(self._stream) 557 621 x = self.parent()._name … … class LazyPowerSeries(AlgebraElement): 572 636 """ 573 637 Refines the approximate order of self as much as possible without 574 638 computing any coefficients. 575 639 576 640 EXAMPLES:: 577 641 578 642 sage: L = LazyPowerSeriesRing(QQ) 579 643 sage: a = L([0,0,0,0,1]) 580 644 sage: a.aorder … … class LazyPowerSeries(AlgebraElement): 586 650 sage: a.refine_aorder() 587 651 sage: a.aorder 588 652 3 589 653 590 654 :: 591 655 592 656 sage: a = L([0,0]) 593 657 sage: a.aorder 594 658 0 … … class LazyPowerSeries(AlgebraElement): 597 661 sage: a.refine_aorder() 598 662 sage: a.aorder 599 663 Infinite series order 600 664 601 665 :: 602 666 603 667 sage: a = L([0,0,1,0,0,0]) 604 668 sage: a[4] 605 669 0 … … class LazyPowerSeries(AlgebraElement): 633 697 #aorder can never be infinity since order would have to 634 698 #be infinity as well 635 699 assert self.aorder != inf 636 700 637 701 if self.aorder == unk or not self.is_initialized: 638 702 self.compute_aorder() 639 703 else: … … class LazyPowerSeries(AlgebraElement): 663 727 664 728 if ao < n: 665 729 self.order = ao 666 667 730 731 668 732 if hasattr(self, '_reference') and self._reference is not None: 669 733 self._reference._copy(self) 670 734 671 735 def initialize_coefficient_stream(self, compute_coefficients): 672 736 """ 673 737 Initializes the coefficient stream. 674 738 675 739 INPUT: compute_coefficients 676 740 677 741 TESTS:: 678 742 679 743 sage: from sage.combinat.species.series_order import inf, unk 680 744 sage: L = LazyPowerSeriesRing(QQ) 681 745 sage: f = L() … … class LazyPowerSeries(AlgebraElement): 685 749 sage: f.initialize_coefficient_stream(compute_coefficients) 686 750 sage: f.coefficients(5) 687 751 [0, 0, 0, 0, 0] 688 752 689 753 :: 690 754 691 755 sage: f = L() 692 756 sage: compute_coefficients = lambda ao: iter(ZZ) 693 757 sage: f.order = 1 694 758 sage: f.aorder = 1 695 759 sage: f.initialize_coefficient_stream(compute_coefficients) 696 sage: f.coefficients(5) 760 sage: f.coefficients(5) 697 761 [0, 1, 1, 2, 2] 698 762 """ 699 763 ao = self.aorder … … class LazyPowerSeries(AlgebraElement): 710 774 def compute_coefficients(self, i): 711 775 """ 712 776 Computes all the coefficients of self up to i. 713 777 714 778 EXAMPLES:: 715 779 716 780 sage: L = LazyPowerSeriesRing(QQ) 717 781 sage: a = L([1,2,3]) 718 782 sage: a.compute_coefficients(5) … … class LazyPowerSeries(AlgebraElement): 724 788 def coefficients(self, n): 725 789 """ 726 790 Returns the first n coefficients of self. 727 791 728 792 EXAMPLES:: 729 793 730 794 sage: L = LazyPowerSeriesRing(QQ) 731 795 sage: f = L([1,2,3,0]) 732 796 sage: f.coefficients(5) … … class LazyPowerSeries(AlgebraElement): 737 801 def is_zero(self): 738 802 """ 739 803 Returns True if and only if self is zero. 740 804 741 805 EXAMPLES:: 742 806 743 807 sage: L = LazyPowerSeriesRing(QQ) 744 808 sage: s = L([0,2,3,0]) 745 809 sage: s.is_zero() 746 810 False 747 811 748 812 :: 749 813 750 814 sage: s = L(0) 751 815 sage: s.is_zero() 752 816 True 753 817 754 818 :: 755 819 756 820 sage: s = L([0]) 757 821 sage: s.is_zero() 758 822 False … … class LazyPowerSeries(AlgebraElement): 770 834 """ 771 835 Sets the approximate order of self and returns True if the 772 836 approximate order has changed otherwise it will return False. 773 837 774 838 EXAMPLES:: 775 839 776 840 sage: L = LazyPowerSeriesRing(QQ) 777 841 sage: f = L([0,0,0,3,2,1,0]) 778 842 sage: f.get_aorder() … … class LazyPowerSeries(AlgebraElement): 789 853 def _copy(self, x): 790 854 """ 791 855 EXAMPLES:: 792 856 793 857 sage: L = LazyPowerSeriesRing(QQ) 794 858 sage: f = L.term(2, 2) 795 859 sage: g = L() … … class LazyPowerSeries(AlgebraElement): 809 873 self.compute_aorder = x.compute_aorder 810 874 self.is_initialized = x.is_initialized 811 875 self._stream = x._stream 812 876 813 877 def define(self, x): 814 878 """ 815 879 EXAMPLES: Test Recursive 0 816 880 817 881 :: 818 882 819 883 sage: L = LazyPowerSeriesRing(QQ) 820 884 sage: one = L(1) 821 885 sage: monom = L.gen() … … class LazyPowerSeries(AlgebraElement): 828 892 Unknown series order 829 893 sage: [s.coefficient(i) for i in range(6)] 830 894 [1, 1, 1, 1, 1, 1] 831 895 832 896 Test Recursive 1 833 897 834 898 :: 835 899 836 900 sage: s = L() 837 901 sage: s._name = 's' 838 902 sage: s.define(one+monom*s*s) … … class LazyPowerSeries(AlgebraElement): 842 906 Unknown series order 843 907 sage: [s.coefficient(i) for i in range(6)] 844 908 [1, 1, 2, 5, 14, 42] 845 909 846 910 Test Recursive 1b 847 911 848 912 :: 849 913 850 914 sage: s = L() 851 915 sage: s._name = 's' 852 916 sage: s.define(monom + s*s) 853 917 sage: s.aorder 854 1918 Unknown series order 855 919 sage: s.order 856 920 Unknown series order 857 921 sage: [s.coefficient(i) for i in range(7)] 858 922 [0, 1, 1, 2, 5, 14, 42] 859 923 sage: s.aorder 924 1 925 sage: s.order 926 1 927 860 928 Test Recursive 2 861 929 862 930 :: 863 931 864 932 sage: s = L() 865 933 sage: s._name = 's' 866 934 sage: t = L() … … class LazyPowerSeries(AlgebraElement): 871 939 [1, 1, 3, 9, 34, 132, 546, 2327, 10191] 872 940 sage: [t.coefficient(i) for i in range(9)] 873 941 [1, 1, 2, 7, 24, 95, 386, 1641, 7150] 874 942 875 943 Test Recursive 2b 876 944 877 945 :: 878 946 879 947 sage: s = L() 880 948 sage: s._name = 's' 881 949 sage: t = L() … … class LazyPowerSeries(AlgebraElement): 886 954 [0, 1, 0, 1, 3, 3, 7, 30, 63] 887 955 sage: [t.coefficient(i) for i in range(9)] 888 956 [0, 1, 1, 0, 2, 6, 7, 20, 75] 889 957 890 958 Test Recursive 3 891 959 892 960 :: 893 961 894 962 sage: s = L() 895 963 sage: s._name = 's' 896 964 sage: s.define(one+monom*s*s*s) … … class LazyPowerSeries(AlgebraElement): 903 971 def coefficient(self, n): 904 972 """ 905 973 Returns the coefficient of xn in self. 906 974 907 975 EXAMPLES:: 908 976 909 977 sage: L = LazyPowerSeriesRing(QQ) 910 978 sage: f = L(ZZ) 911 979 sage: [f.coefficient(i) for i in range(5)] 912 980 [0, 1, 1, 2, 2] 913 981 """ 914 982 # The following line must not be written n < self.get_aorder() 915 # because comparison of Integer and OnfinityOrder is not implemented. 983 # because comparison of Integer and OnfinityOrder is not implemented. 916 984 if self.get_aorder() > n: 917 985 return self._zero 918 986 … … class LazyPowerSeries(AlgebraElement): 923 991 def get_aorder(self): 924 992 """ 925 993 Returns the approximate order of self. 926 994 927 995 EXAMPLES:: 928 996 929 997 sage: L = LazyPowerSeriesRing(QQ) 930 998 sage: a = L.gen() 931 999 sage: a.get_aorder() … … class LazyPowerSeries(AlgebraElement): 937 1005 def get_order(self): 938 1006 """ 939 1007 Returns the order of self. 940 1008 941 1009 EXAMPLES:: 942 1010 943 1011 sage: L = LazyPowerSeriesRing(QQ) 944 1012 sage: a = L.gen() 945 1013 sage: a.get_order() … … class LazyPowerSeries(AlgebraElement): 951 1019 def get_stream(self): 952 1020 """ 953 1021 Returns self's underlying Stream object. 954 1022 955 1023 EXAMPLES:: 956 1024 957 1025 sage: L = LazyPowerSeriesRing(QQ) 958 1026 sage: a = L.gen() 959 1027 sage: s = a.get_stream() … … class LazyPowerSeries(AlgebraElement): 998 1066 parent = kwds['parent'] if 'parent' in kwds else self.parent() 999 1067 new_fps = self.__class__(parent, stream=None, order=unk, aorder=self.aorder, 1000 1068 aorder_changed=True, is_initialized=False) 1001 1069 1002 1070 new_fps.compute_aorder = lambda: new_fps._approximate_order(compute_coefficients, order_op, *series) 1003 1071 return new_fps 1004 1072 1005 1073 def _add_(self, y): 1006 1074 """ 1007 1075 EXAMPLES: Test Plus 1 1008 1076 1009 1077 :: 1010 1078 1011 1079 sage: from sage.combinat.species.series import * 1012 1080 sage: from sage.combinat.species.stream import Stream 1013 1081 sage: L = LazyPowerSeriesRing(QQ) … … class LazyPowerSeries(AlgebraElement): 1026 1094 [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] 1027 1095 sage: [sum3.coefficient(i) for i in range(11)] 1028 1096 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 1029 1097 1030 1098 Test Plus 2 1031 1099 1032 1100 :: 1033 1101 1034 1102 sage: gs1 = L([1,2,4,8,0]) 1035 1103 sage: gs2 = L([1, 0,1,9,22,0]) 1036 1104 sage: sum = gs1 + gs2 … … class LazyPowerSeries(AlgebraElement): 1043 1111 [0, 2, 3, 1, 22] 1044 1112 sage: [ sum2.coefficient(i) for i in range(5, 11) ] 1045 1113 [0, 0, 0, 0, 0, 0] 1114 1115 1116 TESTS: 1117 1118 Check that :trac:`13433` is fixed:: 1119 1120 sage: from sage.combinat.species.series import LazyPowerSeriesRing 1121 sage: L = LazyPowerSeriesRing(QQ) 1122 sage: g = L.gen(); z = L.zero() 1123 sage: s = z+g; s 1124 Uninitialized lazy power series 1125 sage: s.coefficients(10) 1126 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] 1127 sage: s 1128 x + O(x^10) 1046 1129 """ 1130 # print self.aorder, y.aorder 1131 if ( self.aorder != unk and 1132 ( y.aorder == unk or self.aorder > y.aorder ) ): 1133 y, self = self, y 1047 1134 return self._new(partial(self._plus_gen, y), min, self, y) 1048 1049 1135 add = _add_ 1050 1136 1051 1137 … … class LazyPowerSeries(AlgebraElement): 1053 1139 def _plus_gen(self, y, ao): 1054 1140 """ 1055 1141 EXAMPLES:: 1056 1142 1057 1143 sage: L = LazyPowerSeriesRing(QQ) 1058 1144 sage: gs1 = L([1]) 1059 1145 sage: g = gs1._plus_gen(gs1, 0) 1060 1146 sage: [g.next() for i in range(5)] 1061 1147 [2, 2, 2, 2, 2] 1062 1148 1063 1149 :: 1064 1150 1065 1151 sage: g = gs1._plus_gen(gs1, 2) 1066 1152 sage: [g.next() for i in range(5)] 1067 1153 [0, 0, 2, 2, 2] … … class LazyPowerSeries(AlgebraElement): 1078 1164 def _mul_(self, y): 1079 1165 """ 1080 1166 EXAMPLES:: 1081 1167 1082 1168 sage: L = LazyPowerSeriesRing(QQ) 1083 1169 sage: gs0 = L(0) 1084 1170 sage: gs1 = L([1]) 1085 1171 1086 1172 :: 1087 1173 1088 1174 sage: prod0 = gs0 * gs1 1089 1175 sage: [prod0.coefficient(i) for i in range(11)] 1090 1176 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1091 1177 1092 1178 :: 1093 1179 1094 1180 sage: prod1 = gs1 * gs0 1095 1181 sage: [prod1.coefficient(i) for i in range(11)] 1096 1182 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1097 1183 1098 1184 :: 1099 1185 1100 1186 sage: prod2 = gs1 * gs1 1101 1187 sage: [prod2.coefficient(i) for i in range(11)] 1102 1188 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 1103 1189 1104 1190 :: 1105 1191 1106 1192 sage: gs1 = L([1,2,4,8,0]) 1107 1193 sage: gs2 = L([1, 0,1,9,22,0]) 1108 1194 1109 1195 :: 1110 1196 1111 1197 sage: prod1 = gs1 * gs2 1112 1198 sage: [prod1.coefficient(i) for i in range(11)] 1113 1199 [1, 2, 5, 19, 0, 0, 16, 176, 0, 0, 0] 1114 1200 1115 1201 :: 1116 1202 1117 1203 sage: prod2 = gs2 * gs1 1118 1204 sage: [prod2.coefficient(i) for i in range(11)] 1119 1205 [1, 2, 5, 19, 0, 0, 16, 176, 0, 0, 0] 1120 1206 """ 1121 1207 1122 return self._new(partial(self._times_gen, y), lambda a,b:a+b, self, y) 1208 return self._new(partial(self._times_gen, y), lambda a,b:a+b, self, y) 1123 1209 1124 1210 times = _mul_ 1125 1211 1126 1212 def _times_gen(self, y, ao): 1127 1213 """ 1128 1214 Returns an iterator for the coefficients of self \* y. 1129 1215 1130 1216 EXAMPLES:: 1131 1217 1132 1218 sage: L = LazyPowerSeriesRing(QQ) 1133 1219 sage: f = L([1,1,0]) 1134 1220 sage: g = f._times_gen(f,0) … … class LazyPowerSeries(AlgebraElement): 1148 1234 nth_coefficient = zero 1149 1235 1150 1236 #Handle the zero series 1151 if low == inf or high == inf: 1237 if low == inf or high == inf: 1152 1238 yield zero 1153 n += 1 1154 continue 1239 n += 1 1240 continue 1155 1241 1156 1242 for k in range(low, high+1): 1157 1243 cx = self._stream[k] … … class LazyPowerSeries(AlgebraElement): 1164 1250 def __pow__(self, n): 1165 1251 """ 1166 1252 EXAMPLES:: 1167 1253 1168 1254 sage: L = LazyPowerSeriesRing(QQ) 1169 1255 sage: f = L([1,1,0]) # 1+x 1170 1256 sage: g = f^3 1171 1257 sage: g.coefficients(4) 1172 1258 [1, 3, 3, 1] 1173 1259 1174 1260 :: 1175 1261 1176 1262 sage: f^0 1177 1263 1 1178 1264 """ … … class LazyPowerSeries(AlgebraElement): 1184 1270 """ 1185 1271 Returns the composition of this power series and the power series 1186 1272 y. 1187 1273 1188 1274 EXAMPLES:: 1189 1275 1190 1276 sage: L = LazyPowerSeriesRing(QQ) 1191 1277 sage: s = L([1]) 1192 1278 sage: t = L([0,0,1]) 1193 1279 sage: u = s(t) 1194 1280 sage: u.coefficients(11) 1195 1281 [1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 1196 1282 1197 1283 Test Compose 2 1198 1284 1199 1285 :: 1200 1286 1201 1287 sage: s = L([1]) 1202 1288 sage: t = L([0,0,1,0]) 1203 1289 sage: u = s(t) … … class LazyPowerSeries(AlgebraElement): 1211 1297 0 1212 1298 sage: u.order 1213 1299 0 1214 1300 1215 1301 Test Compose 3 s = 1/(1x), t = x/(1x) s(t) = (1x)/(12x) 1216 1302 1217 1303 :: 1218 1304 1219 1305 sage: s = L([1]) 1220 1306 sage: t = L([0,1]) 1221 1307 sage: u = s(t) … … class LazyPowerSeries(AlgebraElement): 1230 1316 """ 1231 1317 Returns a iterator for the coefficients of the composition of this 1232 1318 power series with the power series y. 1233 1319 1234 1320 EXAMPLES:: 1235 1321 1236 1322 sage: L = LazyPowerSeriesRing(QQ) 1237 1323 sage: s = L([1]) 1238 1324 sage: t = L([0,1]) … … class LazyPowerSeries(AlgebraElement): 1255 1341 """ 1256 1342 Returns the power series whose coefficients obtained by subtracting 1257 1343 the constant term from this series and then dividing by x. 1258 1344 1259 1345 EXAMPLES:: 1260 1346 1261 1347 sage: from sage.combinat.species.stream import Stream 1262 1348 sage: L = LazyPowerSeriesRing(QQ) 1263 1349 sage: f = L(range(20)) … … class LazyPowerSeries(AlgebraElement): 1270 1356 def iterator(self, n=0, initial=None): 1271 1357 """ 1272 1358 Returns an iterator for the coefficients of self starting at n. 1273 1359 1274 1360 EXAMPLES:: 1275 1361 1276 1362 sage: from sage.combinat.species.stream import Stream 1277 1363 sage: L = LazyPowerSeriesRing(QQ) 1278 1364 sage: f = L(range(10)) … … class LazyPowerSeries(AlgebraElement): 1295 1381 def _power_gen(self): 1296 1382 """ 1297 1383 Returns a generator for all the powers self^k starting with k = 1. 1298 1384 1299 1385 EXAMPLES:: 1300 1386 1301 1387 sage: L = LazyPowerSeriesRing(QQ) 1302 1388 sage: f = L([1,1,0]) 1303 1389 sage: g = f._power_gen() … … class LazyPowerSeries(AlgebraElement): 1316 1402 def derivative(self): 1317 1403 """ 1318 1404 EXAMPLES:: 1319 1405 1320 1406 sage: from sage.combinat.species.stream import Stream 1321 1407 sage: L = LazyPowerSeriesRing(QQ) 1322 1408 sage: one = L(1) … … class LazyPowerSeries(AlgebraElement): 1325 1411 sage: u = s.derivative() 1326 1412 sage: u.coefficients(10) 1327 1413 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1328 1414 1329 1415 :: 1330 1416 1331 1417 sage: s = L() 1332 1418 sage: s._name = 's' 1333 1419 sage: s.define(one+monom*s*s) 1334 1420 sage: u = s.derivative() 1335 1421 sage: u.coefficients(5) #[1*1, 2*2, 3*5, 4*14, 5*42] 1336 1422 [1, 4, 15, 56, 210] 1337 1423 1338 1424 :: 1339 1425 1340 1426 sage: s = L([1]) 1341 1427 sage: t = L([0,1]) 1342 1428 sage: u = s(t).derivative() … … class LazyPowerSeries(AlgebraElement): 1345 1431 [1, 4, 12, 32, 80, 192, 448, 1024, 2304, 5120, 11264] 1346 1432 sage: v.coefficients(11) 1347 1433 [1, 4, 12, 32, 80, 192, 448, 1024, 2304, 5120, 11264] 1348 1434 1349 1435 :: 1350 1436 1351 1437 sage: s = L(); s._name='s' 1352 1438 sage: t = L(); t._name='t' 1353 1439 sage: s.define(monom+t*t*t) … … class LazyPowerSeries(AlgebraElement): 1360 1446 [0, 2, 3, 4, 30, 72, 133, 552, 1791, 4260] 1361 1447 sage: u.coefficients(10) == v.coefficients(10) 1362 1448 True 1363 1449 1364 1450 :: 1365 1451 1366 1452 sage: f = L._new_initial(2, Stream([0,0,4,5,6,0])) 1367 1453 sage: d = f.derivative() 1368 1454 sage: d.get_aorder() … … class LazyPowerSeries(AlgebraElement): 1376 1462 """ 1377 1463 Returns an iterator for the coefficients of the derivative of 1378 1464 self. 1379 1465 1380 1466 EXAMPLES:: 1381 1467 1382 1468 sage: L = LazyPowerSeriesRing(QQ) 1383 1469 sage: f = L([1]) 1384 1470 sage: g = f._diff_gen(0) … … class LazyPowerSeries(AlgebraElement): 1389 1475 while True: 1390 1476 yield n*self._stream[n] 1391 1477 n += 1 1392 1393 ########### 1478 1479 ########### 1394 1480 #Integrals# 1395 1481 ########### 1396 1482 def integral(self, integration_constant = 0): 1397 1483 """ 1398 1484 EXAMPLES:: 1399 1485 1400 1486 sage: L = LazyPowerSeriesRing(QQ) 1401 1487 sage: zero = L(0) 1402 1488 sage: s = zero 1403 1489 sage: t = s.integral() 1404 1490 sage: t.is_zero() 1405 1491 True 1406 1492 1407 1493 :: 1408 1494 1409 1495 sage: s = zero 1410 1496 sage: t = s.integral(1) 1411 1497 sage: t.coefficients(6) 1412 1498 [1, 0, 0, 0, 0, 0] 1413 1499 sage: t._stream.is_constant() 1414 1500 True 1415 1501 1416 1502 :: 1417 1503 1418 1504 sage: s = L.term(1, 0) 1419 1505 sage: t = s.integral() 1420 1506 sage: t.coefficients(6) 1421 1507 [0, 1, 0, 0, 0, 0] 1422 1508 sage: t._stream.is_constant() 1423 1509 True 1424 1510 1425 1511 :: 1426 1512 1427 1513 sage: s = L.term(1,0) 1428 1514 sage: t = s.integral(1) 1429 1515 sage: t.coefficients(6) 1430 1516 [1, 1, 0, 0, 0, 0] 1431 1517 sage: t._stream.is_constant() 1432 1518 True 1433 1519 1434 1520 :: 1435 1521 1436 1522 sage: s = L.term(1, 4) 1437 1523 sage: t = s.integral() 1438 1524 sage: t.coefficients(10) 1439 1525 [0, 0, 0, 0, 0, 1/5, 0, 0, 0, 0] 1440 1526 1441 1527 :: 1442 1528 1443 1529 sage: s = L.term(1,4) 1444 1530 sage: t = s.integral(1) 1445 1531 sage: t.coefficients(10) 1446 1532 [1, 0, 0, 0, 0, 1/5, 0, 0, 0, 0] 1447 1533 1448 1534 TESTS:: 1449 1535 1450 1536 sage: from sage.combinat.species.stream import Stream 1451 1537 sage: f = L._new_initial(2, Stream([0,0,4,5,6,0])) 1452 1538 sage: i = f.derivative().integral() … … class LazyPowerSeries(AlgebraElement): 1469 1555 def _integral_zero_gen(self, ao): 1470 1556 """ 1471 1557 EXAMPLES:: 1472 1558 1473 1559 sage: L = LazyPowerSeriesRing(QQ) 1474 1560 sage: s = L.gen() 1475 1561 sage: g = s._integral_zero_gen(1) … … class LazyPowerSeries(AlgebraElement): 1492 1578 def _integral_nonzero_gen(self, integration_constant): 1493 1579 """ 1494 1580 EXAMPLES:: 1495 1581 1496 1582 sage: from sage.combinat.species.stream import Stream 1497 1583 sage: L = LazyPowerSeriesRing(QQ) 1498 1584 sage: f = L._new_initial(2, Stream([0,0,4,5,6,0])).derivative() … … class LazyPowerSeries(AlgebraElement): 1510 1596 else: 1511 1597 for _ in range(ao1): 1512 1598 yield self._zero 1513 1599 1514 1600 n = max(1,ao) 1515 1601 while True: 1516 1602 c = self.coefficient(n1) … … class LazyPowerSeries(AlgebraElement): 1526 1612 def is_finite(self, n=None): 1527 1613 """ 1528 1614 EXAMPLES:: 1529 1615 1530 1616 sage: L = LazyPowerSeriesRing(QQ) 1531 1617 sage: a = L([0,0,1,0,0]); a 1532 1618 O(1) … … class LazyPowerSeries(AlgebraElement): 1550 1636 1551 1637 if n is None: 1552 1638 n = len(s) 1553 1639 1554 1640 if s.is_constant() and all(s[i] == 0 for i in range(n1, max(n,len(s)))): 1555 1641 return True 1556 1642 … … class LazyPowerSeries(AlgebraElement): 1559 1645 def exponential(self): 1560 1646 """ 1561 1647 TESTS:: 1562 1648 1563 1649 sage: def inv_factorial(): 1564 1650 ... q = 1 1565 1651 ... yield 0 … … class LazyPowerSeries(AlgebraElement): 1589 1675 def __getitem__(self, i): 1590 1676 """ 1591 1677 Returns the ith coefficient of self. 1592 1678 1593 1679 EXAMPLES:: 1594 1680 1595 1681 sage: L = LazyPowerSeriesRing(QQ) 1596 1682 sage: f = L([1,2,3,0]) 1597 1683 sage: [f[i] for i in range(5)] … … class LazyPowerSeries(AlgebraElement): 1609 1695 min and going up to, but not including max. If min is not 1610 1696 specified, then it is assumed to be zero. If max is not specified, 1611 1697 then it is assumed to be infinity. 1612 1698 1613 1699 EXAMPLES:: 1614 1700 1615 1701 sage: L = LazyPowerSeriesRing(QQ) 1616 1702 sage: a = L([1]) 1617 1703 sage: a.restricted().coefficients(10) … … class LazyPowerSeries(AlgebraElement): 1627 1713 if ((min is None and max is None) or 1628 1714 (max is None and self.get_aorder() >= min)): 1629 1715 return self 1630 1716 1631 1717 return self._new(partial(self._restricted_gen, min, max), 1632 1718 lambda ao: __builtin__.max(ao, min), self) 1633 1719 1634 1720 def _restricted_gen(self, mn, mx, ao): 1635 1721 """ 1636 1722 EXAMPLES:: 1637 1723 1638 1724 sage: L = LazyPowerSeriesRing(QQ) 1639 1725 sage: a = L([1]) 1640 1726 sage: g = a._restricted_gen(None, None, 2) … … class LazyPowerSeries(AlgebraElement): 1646 1732 sage: g = a._restricted_gen(3, None, 2) 1647 1733 sage: [g.next() for i in range(10)] 1648 1734 [0, 0, 0, 1, 1, 1, 1, 1, 1, 1] 1649 1735 1650 1736 :: 1651 1737 1652 1738 sage: g = a._restricted_gen(1, 5, 2) 1653 1739 sage: [g.next() for i in range(6)] 1654 1740 [0, 0, 1, 1, 1, 0] 1655 1741 """ 1656 1742 BR = self.parent().base_ring() 1657 1743 for n in range(max(mn,ao)): 1658 yield BR (0)1744 yield BR.zero() 1659 1745 1660 1746 n = max(mn, ao) 1661 1747 while True: 1662 1748 if mx is not None and n >= mx: 1663 yield BR (0)1749 yield BR.zero() 1664 1750 break 1665 1751 else: 1666 1752 yield self._stream[n] … … class LazyPowerSeries(AlgebraElement): 1673 1759 def _change_ring_gen(self, R, ao): 1674 1760 """ 1675 1761 EXAMPLES:: 1676 1762 1677 1763 sage: L = LazyPowerSeriesRing(QQ) 1678 1764 sage: L2 = LazyPowerSeriesRing(RR) 1679 1765 sage: a = L([1]) … … class LazyPowerSeries(AlgebraElement): 1693 1779 1694 1780 ################################# 1695 1781 1696 1697 1698 1782 def uninitialized(): 1699 1783 """ 1700 1784 EXAMPLES:: 1701 1785 1702 1786 sage: from sage.combinat.species.series import uninitialized 1703 1787 sage: uninitialized() 1704 1788 Traceback (most recent call last):