This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
asymptotic_ring.py
3347 lines (2502 loc) · 104 KB
/
asymptotic_ring.py
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
r"""
Asymptotic Ring
This module provides a ring (called :class:`AsymptoticRing`) for
computations with :wikipedia:`asymptotic expansions <Asymptotic_expansion>`.
.. _asymptotic_ring_definition:
(Informal) Definition
=====================
An asymptotic expansion is a sum such as
.. MATH::
5z^3 + 4z^2 + O(z)
as `z \to \infty` or
.. MATH::
3x^{42}y^2 + 7x^3y^3 + O(x^2) + O(y)
as `x` and `y` tend to `\infty`. It is a truncated series (after a
finite number of terms), which approximates a function.
The summands of the asymptotic expansions are partially ordered. In
this module these summands are the following:
- Exact terms `c\cdot g` with a coefficient `c` and an element `g` of
a growth group (:ref:`see below <asymptotic_ring_growth>`).
- `O`-terms `O(g)` (see :wikipedia:`Big O notation <Big_O_notation>`;
also called *Bachmann--Landau notation*) for a growth group
element `g` (:ref:`again see below <asymptotic_ring_growth>`).
See
:wikipedia:`the Wikipedia article on asymptotic expansions <Asymptotic_expansion>`
for more details.
Further examples of such elements can be found :ref:`here <asymptotic_ring_intro>`.
.. _asymptotic_ring_growth:
Growth Groups and Elements
--------------------------
The elements of a :doc:`growth group <growth_group>` are equipped with
a partial order and usually contain a variable. Examples---the order
is described below these examples---are
- elements of the form `z^q` for some integer or rational `q`
(growth groups with :ref:`description strings <growth_group_description>`
``z^ZZ`` or ``z^QQ``),
- elements of the form `\log(z)^q` for some integer or rational `q`
(growth groups ``log(z)^ZZ`` or ``log(z)^QQ``),
- elements of the form `a^z` for some
rational `a` (growth group ``QQ^z``), or
- more sophisticated constructions like products
`x^r \cdot \log(x)^s \cdot a^y \cdot y^q`
(this corresponds to an element of the growth group
``x^QQ * log(x)^ZZ * QQ^y * y^QQ``).
The order in all these examples is induced by the magnitude of the
elements as `x`, `y`, or `z` (independently) tend to `\infty`. For
elements only using the variable `z` this means that `g_1 \leq g_2` if
.. MATH::
\lim_{z\to\infty} \frac{g_1}{g_2} \leq 1.
.. NOTE::
Asymptotic rings where the variable tend to some value distinct from
`\infty` are not yet implemented.
To find out more about
- growth groups,
- on how they are created and
- about the above used *descriptions strings*
see the top of the module :doc:`growth group <growth_group>`.
.. WARNING::
As this code is experimental, a warning is thrown when an
asymptotic ring (or an associated structure) is created for the
first time in a session (see
:class:`sage.misc.superseded.experimental`).
TESTS::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('x^ZZ')
doctest:...: FutureWarning: This class/method/function is marked as
experimental. It, its functionality or its interface might change
without a formal deprecation.
See http://trac.sagemath.org/17601 for details.
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: T = GenericTermMonoid(G, ZZ)
sage: R.<x, y> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ)
doctest:...: FutureWarning: This class/method/function is marked as
experimental. It, its functionality or its interface might change
without a formal deprecation.
See http://trac.sagemath.org/17601 for details.
.. _asymptotic_ring_intro:
Introductory Examples
=====================
We start this series of examples by defining two asymptotic rings.
Two Rings
---------
A Univariate Asymptotic Ring
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
First, we construct the following (very simple) asymptotic ring in the variable `z`::
sage: A.<z> = AsymptoticRing(growth_group='z^QQ', coefficient_ring=ZZ); A
Asymptotic Ring <z^QQ> over Integer Ring
A typical element of this ring is
::
sage: A.an_element()
z^(3/2) + O(z^(1/2))
This element consists of two summands: the exact term with coefficient
`1` and growth `z^{3/2}` and the `O`-term `O(z^{1/2})`. Note that the
growth of `z^{3/2}` is larger than the growth of `z^{1/2}` as
`z\to\infty`, thus this expansion cannot be simplified (which would
be done automatically, see below).
Elements can be constructed via the generator `z` and the function
:func:`~sage.rings.big_oh.O`, for example
::
sage: 4*z^2 + O(z)
4*z^2 + O(z)
A Multivariate Asymptotic Ring
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Next, we construct a more sophisticated asymptotic ring in the
variables `x` and `y` by
::
sage: B.<x, y> = AsymptoticRing(growth_group='x^QQ * log(x)^ZZ * QQ^y * y^QQ', coefficient_ring=QQ); B
Asymptotic Ring <x^QQ * log(x)^ZZ * QQ^y * y^QQ> over Rational Field
Again, we can look at a typical (nontrivial) element::
sage: B.an_element()
1/8*x^(3/2)*log(x)^3*(1/8)^y*y^(3/2) + O(x^(1/2)*log(x)*(1/2)^y*y^(1/2))
Again, elements can be created using the generators `x` and `y`, as well as
the function :func:`~sage.rings.big_oh.O`::
sage: log(x)*y/42 + O(1/2^y)
1/42*log(x)*y + O((1/2)^y)
Arithmetical Operations
-----------------------
In this section we explain how to perform various arithmetical
operations with the elements of the asymptotic rings constructed
above.
The Ring Operations Plus and Times
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
We start our calculations in the ring
::
sage: A
Asymptotic Ring <z^QQ> over Integer Ring
Of course, we can perform the usual ring operations `+` and `*`::
sage: z^2 + 3*z*(1-z)
-2*z^2 + 3*z
sage: (3*z + 2)^3
27*z^3 + 54*z^2 + 36*z + 8
In addition to that, special powers---our growth group ``z^QQ`` allows
the exponents to be out of `\QQ`---can also be computed::
sage: (z^(5/2)+z^(1/7)) * z^(-1/5)
z^(23/10) + z^(-2/35)
The central concepts of computations with asymptotic expansions is
that the `O`-notation can be used. For example, we have
::
sage: z^3 + z^2 + z + O(z^2)
z^3 + O(z^2)
where the result is simplified automatically. A more sophisticated example is
::
sage: (z+2*z^2+3*z^3+4*z^4) * (O(z)+z^2)
4*z^6 + O(z^5)
Division
^^^^^^^^
The asymptotic expansions support division. For example, we can
expand `1/(z-1)` to a geometric series::
sage: 1 / (z-1)
z^(-1) + z^(-2) + z^(-3) + z^(-4) + ... + z^(-20) + O(z^(-21))
A default precision (parameter ``default_prec`` of
:class:`AsymptoticRing`) is predefined. Thus, only the first `20`
summands are calculated. However, if we only want the first `5` exact
terms, we cut of the rest by using
::
sage: (1 / (z-1)).truncate(5)
z^(-1) + z^(-2) + z^(-3) + z^(-4) + z^(-5) + O(z^(-6))
or
::
sage: 1 / (z-1) + O(z^(-6))
z^(-1) + z^(-2) + z^(-3) + z^(-4) + z^(-5) + O(z^(-6))
Of course, we can work with more complicated expansions as well::
sage: (4*z+1) / (z^3+z^2+z+O(z^0))
4*z^(-2) - 3*z^(-3) - z^(-4) + O(z^(-5))
Not all elements are invertible, for instance,
::
sage: 1 / O(z)
Traceback (most recent call last):
...
ZeroDivisionError: Cannot invert O(z).
is not invertible, since it includes `0`.
Powers, Expontials and Logarithms
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It works as simple as it can be; just use the usual operators ``^``,
``exp`` and ``log``. For example, we obtain the usual series expansion
of the logarithm
::
sage: -log(1-1/z)
z^(-1) + 1/2*z^(-2) + 1/3*z^(-3) + ... + O(z^(-21))
as `z \to \infty`.
Similarly, we can apply the exponential function of an asymptotic expansion::
sage: exp(1/z)
1 + z^(-1) + 1/2*z^(-2) + 1/6*z^(-3) + 1/24*z^(-4) + ... + O(z^(-20))
Arbitrary powers work as well; for example, we have
::
sage: (1 + 1/z + O(1/z^5))^(1 + 1/z)
1 + z^(-1) + z^(-2) + 1/2*z^(-3) + 1/3*z^(-4) + O(z^(-5))
Multivariate Arithmetic
^^^^^^^^^^^^^^^^^^^^^^^
Now let us move on to arithmetic in the multivariate ring
::
sage: B
Asymptotic Ring <x^QQ * log(x)^ZZ * QQ^y * y^QQ> over Rational Field
.. TODO::
write this part
More Examples
=============
The mathematical constant e as a limit
--------------------------------------
The base of the natural logarithm `e` satisfies the equation
.. MATH::
e = \lim_{n\to\infty} \left(1+\frac{1}{n}\right)^n
By using asymptotic expansions, we obtain the more precise result
::
sage: E.<n> = AsymptoticRing(growth_group='n^ZZ', coefficient_ring=SR, default_prec=5); E
Asymptotic Ring <n^ZZ> over Symbolic Ring
sage: (1 + 1/n)^n
e - 1/2*e*n^(-1) + 11/24*e*n^(-2) - 7/16*e*n^(-3) + 2447/5760*e*n^(-4) + O(n^(-5))
Selected Technical Details
==========================
Coercions and Functorial Constructions
--------------------------------------
The :class:`AsymptoticRing` fully supports
`coercion <../../../../coercion/index.html>`_. For example, the coefficient ring is automatically extended when needed::
sage: A
Asymptotic Ring <z^QQ> over Integer Ring
sage: (z + 1/2).parent()
Asymptotic Ring <z^QQ> over Rational Field
Here, the coefficient ring was extended to allow `1/2` as a
coefficent. Another example is
::
sage: C.<c> = AsymptoticRing(growth_group='c^ZZ', coefficient_ring=ZZ['e'])
sage: C.an_element()
e^3*c^3 + O(c)
sage: C.an_element() / 7
1/7*e^3*c^3 + O(c)
Here the result's coefficient ring is the newly found
::
sage: (C.an_element() / 7).parent()
Asymptotic Ring <c^ZZ> over
Univariate Polynomial Ring in e over Rational Field
Not only the coefficient ring can be extended, but the growth group as
well. For example, we can add/multiply elements of the asymptotic
rings ``A`` and ``C`` to get an expansion of new asymptotic ring::
sage: r = c*z + c/2 + O(z); r
c*z + 1/2*c + O(z)
sage: r.parent()
Asymptotic Ring <c^ZZ * z^QQ> over
Univariate Polynomial Ring in e over Rational Field
Data Structures
---------------
The summands of an
:class:`asymptotic expansion <AsymptoticExpansion>` are wrapped
:doc:`growth group elements <growth_group>`.
This wrapping is done by the
:doc:`term monoid module <term_monoid>`.
However, inside an
:class:`asymptotic expansion <AsymptoticExpansion>` these summands
(terms) are stored together with their growth-relationship, i.e., each
summand knows its direct predecessors and successors. As a data
structure a special poset (namely a
:mod:`mutable poset <sage.data_structures.mutable_poset>`)
is used. We can have a look at this::
sage: b = x^3*y + x^2*y + x*y^2 + O(x) + O(y)
sage: print b.summands.repr_full(reverse=True)
poset(x*y^2, x^3*y, x^2*y, O(x), O(y))
+-- oo
| +-- no successors
| +-- predecessors: x*y^2, x^3*y
+-- x*y^2
| +-- successors: oo
| +-- predecessors: O(x), O(y)
+-- x^3*y
| +-- successors: oo
| +-- predecessors: x^2*y
+-- x^2*y
| +-- successors: x^3*y
| +-- predecessors: O(x), O(y)
+-- O(x)
| +-- successors: x*y^2, x^2*y
| +-- predecessors: null
+-- O(y)
| +-- successors: x*y^2, x^2*y
| +-- predecessors: null
+-- null
| +-- successors: O(x), O(y)
| +-- no predecessors
Various
=======
AUTHORS:
- Benjamin Hackl (2015)
- Daniel Krenn (2015)
ACKNOWLEDGEMENT:
- Benjamin Hackl, Clemens Heuberger and Daniel Krenn are supported by the
Austrian Science Fund (FWF): P 24644-N26.
- Benjamin Hackl is supported by the Google Summer of Code 2015.
Classes and Methods
===================
"""
# *****************************************************************************
# Copyright (C) 2015 Benjamin Hackl <benjamin.hackl@aau.at>
# 2015 Daniel Krenn <dev@danielkrenn.at>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
# *****************************************************************************
from sage.rings.ring import Algebra
from sage.structure.element import CommutativeAlgebraElement
from sage.structure.unique_representation import UniqueRepresentation
from sage.misc.superseded import experimental
class AsymptoticExpansion(CommutativeAlgebraElement):
r"""
Class for asymptotic expansions, i.e., the elements of an
:class:`AsymptoticRing`.
INPUT:
- ``parent`` -- the parent of the asymptotic expansion.
- ``summands`` -- the summands as a
:class:`~sage.data_structures.mutable_poset.MutablePoset`, which
represents the underlying structure.
- ``simplify`` -- a boolean (default: ``True``). It controls
automatic simplification (absorption) of the asymptotic expansion.
- ``convert`` -- a boolean (default: ``True``). If set, then the
``summands`` are converted to the asymptotic ring (the parent of this
expansion). If not, then the summands are taken as they are. In
that case, the caller must ensure that the parent of the terms is
set correctly.
EXAMPLES:
There are several ways to create asymptotic expansions; usually
this is done by using the corresponding :class:`asymptotic rings <AsymptoticRing>`::
sage: R_x.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ); R_x
Asymptotic Ring <x^QQ> over Rational Field
sage: R_y.<y> = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=ZZ); R_y
Asymptotic Ring <y^ZZ> over Integer Ring
At this point, `x` and `y` are already asymptotic expansions::
sage: type(x)
<class 'sage.rings.asymptotic.asymptotic_ring.AsymptoticRing_with_category.element_class'>
The usual ring operations, but allowing rational exponents (growth
group ``x^QQ``) can be performed::
sage: x^2 + 3*(x - x^(2/5))
x^2 + 3*x - 3*x^(2/5)
sage: (3*x^(1/3) + 2)^3
27*x + 54*x^(2/3) + 36*x^(1/3) + 8
One of the central ideas behind computing with asymptotic
expansions is that the `O`-notation (see
:wikipedia:`Big_O_notation`) can be used. For example, we have::
sage: (x+2*x^2+3*x^3+4*x^4) * (O(x)+x^2)
4*x^6 + O(x^5)
In particular, :meth:`~sage.rings.big_oh.O` can be used to
construct the asymptotic expansions. With the help of the
:meth:`summands`, we can also have a look at the inner structure
of an asymptotic expansion::
sage: expr1 = x + 2*x^2 + 3*x^3 + 4*x^4; expr2 = O(x) + x^2
sage: print(expr1.summands.repr_full())
poset(x, 2*x^2, 3*x^3, 4*x^4)
+-- null
| +-- no predecessors
| +-- successors: x
+-- x
| +-- predecessors: null
| +-- successors: 2*x^2
+-- 2*x^2
| +-- predecessors: x
| +-- successors: 3*x^3
+-- 3*x^3
| +-- predecessors: 2*x^2
| +-- successors: 4*x^4
+-- 4*x^4
| +-- predecessors: 3*x^3
| +-- successors: oo
+-- oo
| +-- predecessors: 4*x^4
| +-- no successors
sage: print(expr2.summands.repr_full())
poset(O(x), x^2)
+-- null
| +-- no predecessors
| +-- successors: O(x)
+-- O(x)
| +-- predecessors: null
| +-- successors: x^2
+-- x^2
| +-- predecessors: O(x)
| +-- successors: oo
+-- oo
| +-- predecessors: x^2
| +-- no successors
sage: print((expr1 * expr2).summands.repr_full())
poset(O(x^5), 4*x^6)
+-- null
| +-- no predecessors
| +-- successors: O(x^5)
+-- O(x^5)
| +-- predecessors: null
| +-- successors: 4*x^6
+-- 4*x^6
| +-- predecessors: O(x^5)
| +-- successors: oo
+-- oo
| +-- predecessors: 4*x^6
| +-- no successors
In addition to the monomial growth elements from above, we can
also compute with logarithmic terms (simply by constructing the
appropriate growth group)::
sage: R_log = AsymptoticRing(growth_group='log(x)^QQ', coefficient_ring=QQ)
sage: lx = R_log(log(SR.var('x')))
sage: (O(lx) + lx^3)^4
log(x)^12 + O(log(x)^10)
.. SEEALSO::
:doc:`growth_group`,
:doc:`term_monoid`,
:mod:`~sage.data_structures.mutable_poset`.
"""
def __init__(self, parent, summands, simplify=True, convert=True):
r"""
See :class:`AsymptoticExpansion` for more information.
TESTS::
sage: R_x.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ)
sage: R_y.<y> = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=ZZ)
sage: R_x is R_y
False
sage: ex1 = x + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5
sage: ex2 = x + O(R_x(1))
sage: ex1 * ex2
5*x^6 + O(x^5)
::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: OT = TermMonoid('O', G, ZZ); ET = TermMonoid('exact', G, ZZ)
sage: R = AsymptoticRing(G, ZZ)
sage: lst = [ET(x, 1), ET(x^2, 2), OT(x^3), ET(x^4, 4)]
sage: expr = R(lst, simplify=False); expr # indirect doctest
4*x^4 + O(x^3) + 2*x^2 + x
sage: print expr.summands.repr_full()
poset(x, 2*x^2, O(x^3), 4*x^4)
+-- null
| +-- no predecessors
| +-- successors: x
+-- x
| +-- predecessors: null
| +-- successors: 2*x^2
+-- 2*x^2
| +-- predecessors: x
| +-- successors: O(x^3)
+-- O(x^3)
| +-- predecessors: 2*x^2
| +-- successors: 4*x^4
+-- 4*x^4
| +-- predecessors: O(x^3)
| +-- successors: oo
+-- oo
| +-- predecessors: 4*x^4
| +-- no successors
sage: expr._simplify_(); expr
4*x^4 + O(x^3)
sage: print expr.summands.repr_full()
poset(O(x^3), 4*x^4)
+-- null
| +-- no predecessors
| +-- successors: O(x^3)
+-- O(x^3)
| +-- predecessors: null
| +-- successors: 4*x^4
+-- 4*x^4
| +-- predecessors: O(x^3)
| +-- successors: oo
+-- oo
| +-- predecessors: 4*x^4
| +-- no successors
sage: R(lst, simplify=True) # indirect doctest
4*x^4 + O(x^3)
::
sage: R.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ)
sage: e = R(x^2 + O(x))
sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticExpansion
sage: S = AsymptoticRing(growth_group='x^QQ', coefficient_ring=ZZ)
sage: for s in AsymptoticExpansion(S, e.summands).summands.elements_topological():
....: print s.parent()
O-Term Monoid x^QQ with implicit coefficients in Integer Ring
Exact Term Monoid x^QQ with coefficients in Integer Ring
sage: for s in AsymptoticExpansion(S, e.summands,
....: convert=False).summands.elements_topological():
....: print s.parent()
O-Term Monoid x^QQ with implicit coefficients in Rational Field
Exact Term Monoid x^QQ with coefficients in Rational Field
::
sage: AsymptoticExpansion(S, R(1/2).summands)
Traceback (most recent call last):
...
ValueError: Cannot include 1/2 with parent
Exact Term Monoid x^QQ with coefficients in Rational Field in
Asymptotic Ring <x^QQ> over Integer Ring
> *previous* ValueError: 1/2 is not a coefficient in
Exact Term Monoid x^QQ with coefficients in Integer Ring.
"""
super(AsymptoticExpansion, self).__init__(parent=parent)
from sage.data_structures.mutable_poset import MutablePoset
if not isinstance(summands, MutablePoset):
raise TypeError('Summands %s are not in a mutable poset as expected '
'when creating an element of %s.' % (summands, parent))
if convert:
from misc import combine_exceptions
from term_monoid import TermMonoid
def convert_terms(element):
T = TermMonoid(term=element.parent(), asymptotic_ring=parent)
try:
return T(element)
except (ValueError, TypeError) as e:
raise combine_exceptions(
ValueError('Cannot include %s with parent %s in %s' %
(element, element.parent(), parent)), e)
new_summands = summands.copy()
new_summands.map(convert_terms, topological=True, reverse=True)
self._summands_ = new_summands
else:
self._summands_ = summands
if simplify:
self._simplify_()
@property
def summands(self):
r"""
The summands of this asymptotic expansion stored in the
underlying data structure (a
:class:`~sage.data_structures.mutable_poset.MutablePoset`).
EXAMPLES::
sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ)
sage: expr = 7*x^12 + x^5 + O(x^3)
sage: expr.summands
poset(O(x^3), x^5, 7*x^12)
.. SEEALSO::
:class:`sage.data_structures.mutable_poset.MutablePoset`
"""
return self._summands_
def __hash__(self):
r"""
A hash value for this element.
.. WARNING::
This hash value uses the string representation and might not be
always right.
TESTS::
sage: R_log = AsymptoticRing(growth_group='log(x)^QQ', coefficient_ring=QQ)
sage: lx = R_log(log(SR.var('x')))
sage: elt = (O(lx) + lx^3)^4
sage: hash(elt) # random
-4395085054568712393
"""
return hash(str(self))
def __nonzero__(self):
r"""
Return whether this asymptotic expansion is not identically zero.
INPUT:
Nothing.
OUTPUT:
A boolean.
TESTS::
sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ)
sage: bool(R(0)) # indirect doctest
False
sage: bool(x) # indirect doctest
True
sage: bool(7*x^12 + x^5 + O(x^3)) # indirect doctest
True
"""
return bool(self._summands_)
def __eq__(self, other):
r"""
Return whether this asymptotic expansion is equal to ``other``.
INPUT:
- ``other`` -- an object.
OUTPUT:
A boolean.
.. NOTE::
This function uses the coercion model to find a common
parent for the two operands.
EXAMPLES::
sage: R.<x> = AsymptoticRing('x^ZZ', QQ)
sage: (1 + 2*x + 3*x^2) == (3*x^2 + 2*x + 1) # indirect doctest
True
sage: O(x) == O(x)
False
TESTS::
sage: x == None
False
::
sage: x == 'x'
False
"""
if other is None:
return False
try:
return not bool(self - other)
except (TypeError, ValueError):
return False
def __ne__(self, other):
r"""
Return whether this asymptotic expansion is not equal to ``other``.
INPUT:
- ``other`` -- an object.
OUTPUT:
A boolean.
.. NOTE::
This function uses the coercion model to find a common
parent for the two operands.
EXAMPLES::
sage: R.<x> = AsymptoticRing('x^ZZ', QQ)
sage: (1 + 2*x + 3*x^2) != (3*x^2 + 2*x + 1) # indirect doctest
False
sage: O(x) != O(x)
True
TESTS::
sage: x != None
True
"""
return not self == other
def has_same_summands(self, other):
r"""
Return whether this asymptotic expansion and ``other`` have the
same summands.
INPUT:
- ``other`` -- an asymptotic expansion.
OUTPUT:
A boolean.
.. NOTE::
While for example ``O(x) == O(x)`` yields ``False``,
these expansions *do* have the same summands and this method
returns ``True``.
Moreover, this method uses the coercion model in order to
find a common parent for this asymptotic expansion and
``other``.
EXAMPLES::
sage: R_ZZ.<x_ZZ> = AsymptoticRing('x^ZZ', ZZ)
sage: R_QQ.<x_QQ> = AsymptoticRing('x^ZZ', QQ)
sage: sum(x_ZZ^k for k in range(5)) == sum(x_QQ^k for k in range(5)) # indirect doctest
True
sage: O(x_ZZ) == O(x_QQ)
False
TESTS::
sage: x_ZZ.has_same_summands(None)
False
"""
if other is None:
return False
from sage.structure.element import have_same_parent
if have_same_parent(self, other):
return self._has_same_summands_(other)
from sage.structure.element import get_coercion_model
return get_coercion_model().bin_op(self, other,
lambda self, other:
self._has_same_summands_(other))
def _has_same_summands_(self, other):
r"""
Return whether this :class:`AsymptoticExpansion` has the same
summands as ``other``.
INPUT:
- ``other`` -- an :class:`AsymptoticExpansion`.
OUTPUT:
A boolean.
.. NOTE::
This method compares two :class:`AsymptoticExpansion`
with the same parent.
EXAMPLES::
sage: R.<x> = AsymptoticRing('x^ZZ', QQ)
sage: O(x).has_same_summands(O(x))
True
sage: (1 + x + 2*x^2).has_same_summands(2*x^2 + O(x)) # indirect doctest
False
"""
if len(self.summands) != len(other.summands):
return False
from itertools import izip
return all(s == o for s, o in
izip(self.summands.elements_topological(),
other.summands.elements_topological()))
def _simplify_(self):
r"""
Simplify this asymptotic expansion.
INPUT:
Nothing.
OUTPUT:
Nothing, but modifies this asymptotic expansion.
.. NOTE::
This method is usually called during initialization of
this asymptotic expansion.
.. NOTE::
This asymptotic expansion is simplified by letting
`O`-terms that are included in this expansion absorb all
terms with smaller growth.
TESTS::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: G = GrowthGroup('x^ZZ')
sage: OT = TermMonoid('O', G, ZZ); ET = TermMonoid('exact', G, ZZ)
sage: R = AsymptoticRing(G, ZZ)
sage: lst = [ET(x, 1), ET(x^2, 2), OT(x^3), ET(x^4, 4)]
sage: expr = R(lst, simplify=False); expr # indirect doctest
4*x^4 + O(x^3) + 2*x^2 + x
sage: expr._simplify_(); expr
4*x^4 + O(x^3)
sage: R(lst) # indirect doctest
4*x^4 + O(x^3)
"""
self._summands_.merge(reverse=True)
def _repr_(self):
r"""
A representation string for this asymptotic expansion.
INPUT:
Nothing.
OUTPUT:
A string.
EXAMPLES::
sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ)
sage: (5*x^2+12*x) * (x^3+O(x)) # indirect doctest
5*x^5 + 12*x^4 + O(x^3)
sage: (5*x^2-12*x) * (x^3+O(x)) # indirect doctest
5*x^5 - 12*x^4 + O(x^3)
"""
s = ' + '.join(repr(elem) for elem in
self.summands.elements_topological(reverse=True))
s = s.replace('+ -', '- ')
if not s:
return '0'
return s
def _add_(self, other):
r"""
Add ``other`` to this asymptotic expansion.
INPUT:
- ``other`` -- an :class:`AsymptoticExpansion`.
OUTPUT:
The sum as an :class:`AsymptoticExpansion`.
EXAMPLES::
sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ)
sage: expr1 = x^123; expr2 = x^321
sage: expr1._add_(expr2)
x^321 + x^123
sage: expr1 + expr2 # indirect doctest
x^321 + x^123