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
/
term_monoid.py
3907 lines (2922 loc) · 124 KB
/
term_monoid.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) Term Monoids
This module implements asymptotic term monoids. The elements of these
monoids are used behind the scenes when performing calculations in an
:doc:`asymptotic ring <asymptotic_ring>`.
The monoids build upon the (asymptotic) growth groups. While growth
elements only model the growth of a function as it tends towards
infinity (or tends towards another fixed point; see
:doc:`growth_group` for more details), an
asymptotic term additionally specifies its "type" and performs the
actual arithmetic operations (multiplication and partial
addition/absorption of terms).
Besides an abstract base term :class:`GenericTerm`, this module
implements the following types of terms:
- :class:`OTerm` -- `O`-terms at infinity, see
:wikipedia:`Big_O_notation`.
- :class:`TermWithCoefficient` -- abstract base class for
asymptotic terms with coefficients.
- :class:`ExactTerm` -- this class represents a growth element
multiplied with some non-zero coefficient from a coefficient ring.
A characteristic property of asymptotic terms is that some terms are
able to "absorb" other terms (see
:meth:`~sage.rings.asymptotic.term_monoid.GenericTerm.absorb`). For
instance, `O(x^2)` is able to absorb `O(x)` (with result
`O(x^2)`), and `3\cdot x^5` is able to absorb `-2\cdot x^5` (with result
`x^5`). Essentially, absorption can be interpreted as the
addition of "compatible" terms (partial addition).
.. WARNING::
As this code is experimental, a warning is thrown when a term
monoid 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: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ * log(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.
.. _term_absorption:
Absorption of Asymptotic Terms
==============================
A characteristic property of asymptotic terms is that some terms are
able to "absorb" other terms. This is realized with the method
:meth:`~sage.rings.asymptotic.term_monoid.GenericTerm.absorb`.
For instance, `O(x^2)` is able to absorb `O(x)` (with result
`O(x^2)`). This is because the functions bounded by linear growth
are bounded by quadratic growth as well. Another example would be
that `3x^5` is able to absorb `-2x^5` (with result `x^5`), which
simply corresponds to addition.
Essentially, absorption can be interpreted
as the addition of "compatible" terms (partial addition).
We want to show step by step which terms can be absorbed
by which other terms. We start by defining the necessary
term monoids and some terms::
sage: from sage.rings.asymptotic.term_monoid import OTermMonoid, ExactTermMonoid
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: OT = OTermMonoid(growth_group=G, coefficient_ring=QQ)
sage: ET = ExactTermMonoid(growth_group=G, coefficient_ring=QQ)
sage: ot1 = OT(x); ot2 = OT(x^2)
sage: et1 = ET(x^2, 2)
- Because of the definition of `O`-terms (see
:wikipedia:`Big_O_notation`), :class:`OTerm` are able to absorb all
other asymptotic terms with weaker or equal growth. In our
implementation, this means that :class:`OTerm` is able to absorb
other :class:`OTerm`, as well as :class:`ExactTerm`, as long as the
growth of the other term is less than or equal to the growth of this
element::
sage: ot1, ot2
(O(x), O(x^2))
sage: ot1.can_absorb(ot2), ot2.can_absorb(ot1)
(False, True)
sage: et1
2*x^2
sage: ot1.can_absorb(et1)
False
sage: ot2.can_absorb(et1)
True
The result of this absorption always is the dominant
(absorbing) :class:`OTerm`::
sage: ot1.absorb(ot1)
O(x)
sage: ot2.absorb(ot1)
O(x^2)
sage: ot2.absorb(et1)
O(x^2)
These examples correspond to `O(x) + O(x) = O(x)`,
`O(x^2) + O(x) = O(x^2)`, and `O(x^2) + 2x^2 = O(x^2)`.
- :class:`ExactTerm` can only absorb another
:class:`ExactTerm` if the growth coincides with the
growth of this element::
sage: et1.can_absorb(ET(x^2, 5))
True
sage: any(et1.can_absorb(t) for t in [ot1, ot2])
False
As mentioned above, absorption directly corresponds
to addition in this case::
sage: et1.absorb(ET(x^2, 5))
7*x^2
When adding two exact terms, they might cancel out.
For technical reasons, ``None`` is returned in this
case::
sage: ET(x^2, 5).can_absorb(ET(x^2, -5))
True
sage: ET(x^2, 5).absorb(ET(x^2, -5)) is None
True
- The abstract base terms :class:`GenericTerm` and
:class:`TermWithCoefficient` can neither absorb any
other term, nor be absorbed by any other term.
If ``absorb`` is called on a term that cannot be absorbed, an
:python:`ArithmeticError<library/exceptions.html#exceptions.ArithmeticError>`
is raised::
sage: ot1.absorb(ot2)
Traceback (most recent call last):
...
ArithmeticError: O(x) cannot absorb O(x^2)
This would only work the other way around::
sage: ot2.absorb(ot1)
O(x^2)
Comparison
==========
The comparison of asymptotic terms with `\leq` is implemented as follows:
- When comparing ``t1 <= t2``, the coercion framework first tries to
find a common parent for both terms. If this fails, ``False`` is
returned.
- In case the coerced terms do not have a coefficient in their
common parent (e.g. :class:`OTerm`), the growth of the two terms
is compared.
- Otherwise, if the coerced terms have a coefficient (e.g.
:class:`ExactTerm`), we compare whether ``t1`` has a growth that is
strictly weaker than the growth of ``t2``. If so, we return
``True``. If the terms have equal growth, then we return ``True``
if and only if the coefficients coincide as well.
In all other cases, we return ``False``.
Long story short: we consider terms with different coefficients that
have equal growth to be incomparable.
Various
=======
.. TODO::
- Implementation of more term types (e.g. `L` terms,
`\Omega` terms, `o` terms, `\Theta` terms).
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.
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) 2014--2015 Benjamin Hackl <benjamin.hackl@aau.at>
# 2014--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/
# *****************************************************************************
import sage
class ZeroCoefficientError(ValueError):
pass
def absorption(left, right):
r"""
Let one of the two passed terms absorb the other.
Helper function used by
:class:`~sage.rings.asymptotic.asymptotic_ring.AsymptoticExpansion`.
.. NOTE::
If neither of the terms can absorb the other, an
:python:`ArithmeticError<library/exceptions.html#exceptions.ArithmeticError>`
is raised.
See the :ref:`module description <term_absorption>` for a
detailed explanation of absorption.
INPUT:
- ``left`` -- an asymptotic term.
- ``right`` -- an asymptotic term.
OUTPUT:
An asymptotic term or ``None``.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import (TermMonoid, absorption)
sage: T = TermMonoid('O', GrowthGroup('x^ZZ'), ZZ)
sage: absorption(T(x^2), T(x^3))
O(x^3)
sage: absorption(T(x^3), T(x^2))
O(x^3)
::
sage: T = TermMonoid('exact', GrowthGroup('x^ZZ'), ZZ)
sage: absorption(T(x^2), T(x^3))
Traceback (most recent call last):
...
ArithmeticError: Absorption between x^2 and x^3 is not possible.
"""
try:
return left.absorb(right)
except ArithmeticError:
try:
return right.absorb(left)
except ArithmeticError:
raise ArithmeticError('Absorption between %s and %s is not possible.' % (left, right))
def can_absorb(left, right):
r"""
Return whether one of the two input terms is able to absorb the
other.
Helper function used by
:class:`~sage.rings.asymptotic.asymptotic_ring.AsymptoticExpansion`.
INPUT:
- ``left`` -- an asymptotic term.
- ``right`` -- an asymptotic term.
OUTPUT:
A boolean.
.. NOTE::
See the :ref:`module description <term_absorption>` for a
detailed explanation of absorption.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import (TermMonoid, can_absorb)
sage: T = TermMonoid('O', GrowthGroup('x^ZZ'), ZZ)
sage: can_absorb(T(x^2), T(x^3))
True
sage: can_absorb(T(x^3), T(x^2))
True
"""
return left.can_absorb(right) or right.can_absorb(left)
class GenericTerm(sage.structure.element.MultiplicativeGroupElement):
r"""
Base class for asymptotic terms. Mainly the structure and
several properties of asymptotic terms are handled here.
INPUT:
- ``parent`` -- the parent of the asymptotic term.
- ``growth`` -- an asymptotic growth element.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, QQ)
sage: t1 = T(x); t2 = T(x^2); (t1, t2)
(Generic Term with growth x, Generic Term with growth x^2)
sage: t1 * t2
Generic Term with growth x^3
sage: t1.can_absorb(t2)
False
sage: t1.absorb(t2)
Traceback (most recent call last):
...
ArithmeticError: Generic Term with growth x cannot absorb Generic Term with growth x^2
sage: t1.can_absorb(t1)
False
"""
def __init__(self, parent, growth):
r"""
See :class:`GenericTerm` for more information.
TESTS::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, ZZ)
sage: T(x^2)
Generic Term with growth x^2
::
sage: from sage.rings.asymptotic.term_monoid import GenericTerm
sage: GenericTerm(parent=None, growth=x)
Traceback (most recent call last):
...
ValueError: The parent must be provided
sage: GenericTerm(T, GrowthGroup('y^ZZ').gen())
Traceback (most recent call last):
...
ValueError: y is not in Growth Group x^ZZ
"""
if parent is None:
raise ValueError('The parent must be provided')
try:
self.growth = parent.growth_group(growth)
except (ValueError, TypeError):
raise ValueError("%s is not in %s" % (growth, parent.growth_group))
super(GenericTerm, self).__init__(parent=parent)
def _mul_(self, other):
r"""
Multiplication of this term by another.
INPUT:
- ``other`` -- an asymptotic term.
OUTPUT:
A :class:`GenericTerm`.
.. NOTE::
This method is called by the coercion framework, thus,
it can be assumed that this element, as well as ``other``
are from a common parent.
TESTS::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, ZZ)
sage: t1 = T(x); t2 = T(x^2)
sage: t1, t2
(Generic Term with growth x, Generic Term with growth x^2)
sage: t1 * t2
Generic Term with growth x^3
"""
return self.parent()(self.growth * other.growth)
def __invert__(self):
r"""
Invert this term.
OUTPUT:
A :class:`GenericTerm`.
TESTS::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, QQ)
sage: ~T(x) # indirect doctest
Traceback (most recent call last):
...
NotImplementedError: Inversion of Generic Term with growth x
not implemented (in this abstract method).
::
sage: t1 = T(x); t2 = T(x^2)
sage: t1 / t2
Traceback (most recent call last):
...
NotImplementedError: Inversion of Generic Term with growth x^2
not implemented (in this abstract method).
"""
raise NotImplementedError('Inversion of %s not implemented '
'(in this abstract method).' % (self,))
def __pow__(self, exponent):
r"""
Calculate the power of this element to the given ``exponent``.
INPUT:
- ``exponent`` -- an element.
OUTPUT:
Raise a :python:`NotImplementedError<library/exceptions.html#exceptions.NotImplementedError>`
since it is an abstract base class.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('z^ZZ')
sage: t = GenericTermMonoid(G, ZZ).an_element(); t
Generic Term with growth z
sage: t^3 # indirect doctest
Traceback (most recent call last):
...
NotImplementedError: Taking powers of Generic Term with growth z
not implemented (in this abstract method).
"""
raise NotImplementedError('Taking powers of %s not implemented '
'(in this abstract method).' % (self,))
def _calculate_pow_test_zero_(self, exponent):
r"""
Helper function for :meth:`__pow__` which calculates the power of this
element to the given ``exponent`` only if zero to this exponent is possible.
INPUT:
- ``exponent`` -- an element.
OUTPUT:
A term.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('z^ZZ')
sage: t = GenericTermMonoid(G, ZZ).an_element(); t
Generic Term with growth z
sage: t._calculate_pow_test_zero_(3)
Generic Term with growth z^3
sage: t._calculate_pow_test_zero_(-2)
Traceback (most recent call last):
...
ZeroDivisionError: Cannot take Generic Term with growth z to exponent -2.
> *previous* ZeroDivisionError: rational division by zero
::
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: TermMonoid('O', G, QQ)('z')._calculate_pow_test_zero_(-1)
Traceback (most recent call last):
...
ZeroDivisionError: Cannot take O(z) to exponent -1.
> *previous* ZeroDivisionError: rational division by zero
"""
# This assumes `0 = O(g)` for any `g` in the growth group, which
# is valid in the case of a variable going to `\infty`.
# Once non-standard asymptoptics are supported, this has to be
# rewritten.
# See also #19083, comment 64, 27.
zero = self.parent().coefficient_ring.zero()
try:
zero ** exponent
except (TypeError, ValueError, ZeroDivisionError) as e:
from misc import combine_exceptions
raise combine_exceptions(
ZeroDivisionError('Cannot take %s to exponent %s.' %
(self, exponent)), e)
return self._calculate_pow_(exponent)
def _calculate_pow_(self, exponent, new_coefficient=None):
r"""
Helper function for :meth:`__pow__` which calculates the power of this
element to the given ``exponent``.
INPUT:
- ``exponent`` -- an element.
- ``new_coefficient`` -- if not ``None`` this is passed on to the
construction of the element (in particular, not taken to any power).
OUTPUT:
A term.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('z^ZZ')
sage: t = GenericTermMonoid(G, ZZ).an_element(); t
Generic Term with growth z
sage: t._calculate_pow_(3)
Generic Term with growth z^3
sage: t._calculate_pow_(3, new_coefficient=2)
Traceback (most recent call last):
...
ValueError: Coefficient 2 is not 1, but Generic Term Monoid z^ZZ with
(implicit) coefficients in Integer Ring does not support coefficients.
sage: t._calculate_pow_(-2)
Generic Term with growth z^(-2)
sage: t._calculate_pow_(-2, new_coefficient=2)
Traceback (most recent call last):
...
ValueError: Coefficient 2 is not 1, but Generic Term Monoid z^ZZ with
(implicit) coefficients in Integer Ring does not support coefficients.
"""
try:
g = self.growth ** exponent
except (ValueError, TypeError, ZeroDivisionError) as e:
from misc import combine_exceptions
raise combine_exceptions(
ValueError('Cannot take %s to the exponent %s.' % (self, exponent)), e)
return self.parent()._create_element_in_extension_(g, new_coefficient)
def can_absorb(self, other):
r"""
Check whether this asymptotic term is able to absorb
the asymptotic term ``other``.
INPUT:
- ``other`` -- an asymptotic term.
OUTPUT:
A boolean.
.. NOTE::
A :class:`GenericTerm` cannot absorb any other term.
See the :ref:`module description <term_absorption>` for a
detailed explanation of absorption.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GenericGrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GenericGrowthGroup(ZZ)
sage: T = GenericTermMonoid(G, QQ)
sage: g1 = G(raw_element=21); g2 = G(raw_element=42)
sage: t1 = T(g1); t2 = T(g2)
sage: t1.can_absorb(t2) # indirect doctest
False
sage: t2.can_absorb(t1) # indirect doctest
False
"""
return False
def absorb(self, other, check=True):
r"""
Absorb the asymptotic term ``other`` and return the resulting
asymptotic term.
INPUT:
- ``other`` -- an asymptotic term.
- ``check`` -- a boolean. If ``check`` is ``True`` (default),
then ``can_absorb`` is called before absorption.
OUTPUT:
An asymptotic term or ``None`` if a cancellation occurs. If no
absorption can be performed, an :python:`ArithmeticError<library/exceptions.html#exceptions.ArithmeticError>`
is raised.
.. NOTE::
Setting ``check`` to ``False`` is meant to be used in
cases where the respective comparison is done externally
(in order to avoid duplicate checking).
For a more detailed explanation of the *absorption* of
asymptotic terms see
the :ref:`module description <term_absorption>`.
EXAMPLES:
We want to demonstrate in which cases an asymptotic term
is able to absorb another term, as well as explain the output
of this operation. We start by defining some parents and
elements::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: G_QQ = GrowthGroup('x^QQ'); x = G_QQ.gen()
sage: OT = TermMonoid('O', G_QQ, coefficient_ring=ZZ)
sage: ET = TermMonoid('exact', G_QQ, coefficient_ring=QQ)
sage: ot1 = OT(x); ot2 = OT(x^2)
sage: et1 = ET(x, 100); et2 = ET(x^2, 2)
sage: et3 = ET(x^2, 1); et4 = ET(x^2, -2)
`O`-Terms are able to absorb other `O`-terms and exact terms
with weaker or equal growth. ::
sage: ot1.absorb(ot1)
O(x)
sage: ot1.absorb(et1)
O(x)
sage: ot1.absorb(et1) is ot1
True
:class:`ExactTerm` is able to absorb another
:class:`ExactTerm` if the terms have the same growth. In this
case, *absorption* is nothing else than an addition of the
respective coefficients::
sage: et2.absorb(et3)
3*x^2
sage: et3.absorb(et2)
3*x^2
sage: et3.absorb(et4)
-x^2
Note that, for technical reasons, the coefficient `0` is not
allowed, and thus ``None`` is returned if two exact terms
cancel each other out::
sage: et2.absorb(et4)
sage: et4.absorb(et2) is None
True
TESTS:
When disabling the ``check`` flag, absorb might produce
wrong results::
sage: et1.absorb(ot2, check=False)
O(x)
"""
from sage.structure.element import have_same_parent
if check:
if not self.can_absorb(other):
raise ArithmeticError('%s cannot absorb %s' % (self, other))
if have_same_parent(self, other):
return self._absorb_(other)
from sage.structure.element import get_coercion_model
return get_coercion_model().bin_op(self, other,
lambda left, right:
left._absorb_(right))
def _absorb_(self, other):
r"""
Let this element absorb ``other``.
INPUT:
- ``other`` -- an asymptotic term from the same parent as
this element.
OUTPUT:
An asymptotic term or ``None``.
.. NOTE::
This is not implemented for abstract base classes. For
concrete realizations see, for example, :meth:`OTerm._absorb_`
or :meth:`ExactTerm._absorb_`.
Override this in derived class.
EXAMPLES:
First, we define some asymptotic terms::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, QQ)
sage: t1 = T(x); t2 = T(x^2)
When it comes to absorption, note that the method
:meth:`can_absorb` (which is called before absorption takes
place) does not allow the absorption of generic terms. Thus,
an :python:`ArithmeticError<library/exceptions.html#exceptions.ArithmeticError>`
is raised::
sage: t2.absorb(t1)
Traceback (most recent call last):
...
ArithmeticError: Generic Term with growth x^2 cannot absorb Generic Term with growth x
TESTS::
sage: t2._absorb_(t1)
Traceback (most recent call last):
...
NotImplementedError: Not implemented in abstract base classes
"""
raise NotImplementedError('Not implemented in abstract base classes')
def log_term(self, base=None):
r"""
Determine the logarithm of this term.
INPUT:
- ``base`` -- the base of the logarithm. If ``None``
(default value) is used, the natural logarithm is taken.
OUTPUT:
A tuple of terms.
.. NOTE::
This abstract method raises a
:python:`NotImplementedError<library/exceptions.html#exceptions.NotImplementedError>`.
See :class:`ExactTerm` and :class:`OTerm` for a concrete
implementation.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: T = GenericTermMonoid(GrowthGroup('x^ZZ'), QQ)
sage: T.an_element().log_term()
Traceback (most recent call last):
...
NotImplementedError: This method is not implemented in
this abstract base class.
::
sage: from sage.rings.asymptotic.term_monoid import TermWithCoefficientMonoid
sage: T = TermWithCoefficientMonoid(GrowthGroup('x^ZZ'), QQ)
sage: T.an_element().log_term()
Traceback (most recent call last):
...
NotImplementedError: This method is not implemented in
this abstract base class.
.. SEEALSO::
:meth:`ExactTerm.log_term`,
:meth:`OTerm.log_term`.
"""
raise NotImplementedError('This method is not implemented in this '
'abstract base class.')
def _log_growth_(self, base=None):
r"""
Helper function to calculate the logarithm of the growth of this element.
INPUT:
- ``base`` -- the base of the logarithm. If ``None``
(default value) is used, the natural logarithm is taken.
OUTPUT:
A tuple of terms.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: T = TermMonoid('O', GrowthGroup('x^ZZ * log(x)^ZZ'), QQ)
sage: T(x^2)._log_growth_()
(O(log(x)),)
sage: T(x^1234).log_term() # indirect doctest
(O(log(x)),)
.. SEEALSO::
:meth:`ExactTerm.log_term`,
:meth:`OTerm.log_term`.
"""
return tuple(self.parent()._create_element_in_extension_(g, c)
for g, c in self.growth.log_factor(base=base))
def __le__(self, other):
r"""
Return whether the growth of this term is less than
or equal to the growth of ``other``.
INPUT:
- ``other`` -- an asymptotic term.
OUTPUT:
A boolean.
.. NOTE::
This method **only** compares the growth of the input
terms!
EXAMPLES:
First, we define some asymptotic terms (and their parents)::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import (GenericTermMonoid, TermMonoid)
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: GT = GenericTermMonoid(G, QQ)
sage: OT = TermMonoid('O', G, QQ)
sage: ET_ZZ = TermMonoid('exact', G, ZZ)
sage: ET_QQ = TermMonoid('exact', G, QQ)
sage: g1 = GT(x); g2 = GT(x^2); g1, g2
(Generic Term with growth x, Generic Term with growth x^2)
sage: o1 = OT(x^-1); o2 = OT(x^3); o1, o2
(O(x^(-1)), O(x^3))
sage: t1 = ET_ZZ(x^2, 5); t2 = ET_QQ(x^3, 2/7); t1, t2
(5*x^2, 2/7*x^3)
In order for the comparison to work, the terms have come from
or coerce into the same parent. In particular, comparing
:class:`GenericTerm` to, for example, an :class:`OTerm`
always yields ``False``::
sage: g1 <= g2
True
sage: o1, g1
(O(x^(-1)), Generic Term with growth x)
sage: o1 <= g1
False
If the elements of the common parent do not possess
coefficients, then only the growth is compared::
sage: o1 <= o1
True
sage: o1 <= o2
True
sage: o1 <= t1 and t1 <= o2
True
For terms with coefficient (like exact terms), comparison
works similarly, with the sole exception that terms with
equal growth are considered incomparable. Thus, `\leq`
only holds if the coefficients are equal as well::
sage: t1 <= t2
True
sage: ET_ZZ(x, -5) <= ET_ZZ(x, 42)
False
sage: ET_ZZ(x, 5) <= ET_ZZ(x, 5)
True
"""
from sage.structure.element import have_same_parent
if have_same_parent(self, other):
return self._le_(other)
from sage.structure.element import get_coercion_model
import operator
try:
return get_coercion_model().bin_op(self, other, operator.le)
except TypeError:
return False
def _le_(self, other):
r"""
Return whether this generic term grows at most (i.e. less than
or equal) like ``other``.
INPUT:
- ``other`` -- an asymptotic term.
OUTPUT:
A boolean.
.. NOTE::
This method is called by the coercion framework, thus,
it can be assumed that this element, as well as ``other``
are from the same parent.
Also, this method **only** compares the growth of the
input terms!
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = GenericTermMonoid(G, QQ)
sage: t1 = T(x^-2); t2 = T(x^5); t1, t2
(Generic Term with growth x^(-2), Generic Term with growth x^5)
sage: t1._le_(t2)
True
sage: t2._le_(t1)
False
"""
return self.growth <= other.growth
def __eq__(self, other):
r"""
Return whether this asymptotic term 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: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import (GenericTermMonoid,
....: ExactTermMonoid, OTermMonoid)
sage: GT = GenericTermMonoid(GrowthGroup('x^ZZ'), QQ)
sage: ET = ExactTermMonoid(GrowthGroup('x^ZZ'), ZZ)
sage: OT = OTermMonoid(GrowthGroup('x^ZZ'), QQ)
sage: g = GT.an_element(); e = ET.an_element(); o = OT.an_element()
sage: g, e, o
(Generic Term with growth x, x, O(x))
sage: e == e^2 # indirect doctest
False
sage: e == ET(x,1) # indirect doctest
True