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
/
growth_group.py
4059 lines (3007 loc) · 119 KB
/
growth_group.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) Growth Groups
This module provides support for (asymptotic) growth groups.
Such groups are equipped with a partial order: the elements can be
seen as functions, and the behavior as their argument (or arguments)
gets large (tend to `\infty`) is compared.
Growth groups are used for the calculations done in the
:doc:`asymptotic ring <asymptotic_ring>`. There, take a look at the
:ref:`informal definition <asymptotic_ring_definition>`, where
examples of growth groups and elements are given as well.
.. WARNING::
As this code is experimental, warnings are thrown when a growth
group is created for the first time in a session (see
:class:`sage.misc.superseded.experimental`).
TESTS::
sage: from sage.rings.asymptotic.growth_group import \
....: GenericGrowthGroup, GrowthGroup
sage: GenericGrowthGroup(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.
Growth Group Generic(ZZ)
sage: 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.
Growth Group x^ZZ * log(x)^ZZ
.. _growth_group_description:
Description of Growth Groups
============================
Many growth groups can be described by a string, which can also be used to
create them. For example, the string ``'x^QQ * log(x)^ZZ * QQ^y * y^QQ'``
represents a growth group with the following properties:
- It is a growth group in the two variables `x` and `y`.
- Its elements are of the form
.. MATH::
x^r \cdot \log(x)^s \cdot a^y \cdot y^q
for `r\in\QQ`, `s\in\ZZ`, `a\in\QQ` and `q\in\QQ`.
- The order is with respect to `x\to\infty` and `y\to\infty` independently
of each other.
- To compare such elements, they are split into parts belonging to
only one variable. In the example above,
.. MATH::
x^{r_1} \cdot \log(x)^{s_1} \leq x^{r_2} \cdot \log(x)^{s_2}
if `(r_1, s_1) \leq (r_2, s_2)` lexicographically. This reflects the fact
that elements `x^r` are larger than elements `\log(x)^s` as `x\to\infty`.
The factors belonging to the variable `y` are compared analogously.
The results of these comparisons are then put together using the
:wikipedia:`product order <Product_order>`, i.e., `\leq` if each component
satisfies `\leq`.
Each description string consists of ordered factors---yes, this means
``*`` is noncommutative---of strings describing "elementary" growth
groups (see the examples below). As stated in the example above, these
factors are split by their variable; factors with the same variable are
grouped. Reading such factors from left to right determines the order:
Comparing elements of two factors (growth groups) `L` and `R`, then all
elements of `L` are considered to be larger than each element of `R`.
.. _growth_group_creating:
Creating a Growth Group
=======================
For many purposes the factory ``GrowthGroup`` (see
:class:`GrowthGroupFactory`) is the most convenient way to generate a
growth group.
::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
Here are some examples::
sage: GrowthGroup('z^ZZ')
Growth Group z^ZZ
sage: M = GrowthGroup('z^QQ'); M
Growth Group z^QQ
Each of these two generated groups is a :class:`MonomialGrowthGroup`,
whose elements are powers of a fixed symbol (above ``'z'``).
For the order of the elements it is assumed that `z\to\infty`.
.. NOTE::
Growth groups where the variable tend to some value distinct from
`\infty` are not yet implemented.
To create elements of `M`, a generator can be used::
sage: z = M.gen()
sage: z^(3/5)
z^(3/5)
Strings can also be parsed::
sage: M('z^7')
z^7
Similarly, we can construct logarithmic factors by::
sage: GrowthGroup('log(z)^QQ')
Growth Group log(z)^QQ
which again creates a
:class:`MonomialGrowthGroup`. An :class:`ExponentialGrowthGroup` is generated in the same way. Our factory gives
::
sage: E = GrowthGroup('QQ^z'); E
Growth Group QQ^z
and a typical element looks like this::
sage: E.an_element()
(1/2)^z
More complex groups are created in a similar fashion. For example
::
sage: C = GrowthGroup('QQ^z * z^QQ * log(z)^QQ'); C
Growth Group QQ^z * z^QQ * log(z)^QQ
This contains elements of the form
::
sage: C.an_element()
(1/2)^z*z^(1/2)*log(z)^(1/2)
The group `C` itself is a Cartesian product; to be precise a
:class:`~sage.rings.asymptotic.growth_group_cartesian.UnivariateProduct`. We
can see its factors::
sage: C.cartesian_factors()
(Growth Group QQ^z, Growth Group z^QQ, Growth Group log(z)^QQ)
Multivariate constructions are also possible::
sage: GrowthGroup('x^QQ * y^QQ')
Growth Group x^QQ * y^QQ
This gives a
:class:`~sage.rings.asymptotic.growth_group_cartesian.MultivariateProduct`.
Both these Cartesian products are derived from the class
:class:`~sage.rings.asymptotic.growth_group_cartesian.GenericProduct`. Moreover
all growth groups have the abstract base class
:class:`GenericGrowthGroup` in common.
Some Examples
^^^^^^^^^^^^^
::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G_x = GrowthGroup('x^ZZ'); G_x
Growth Group x^ZZ
sage: G_xy = GrowthGroup('x^ZZ * y^ZZ'); G_xy
Growth Group x^ZZ * y^ZZ
sage: G_xy.an_element()
x*y
sage: x = G_xy('x'); y = G_xy('y')
sage: x^2
x^2
sage: elem = x^21*y^21; elem^2
x^42*y^42
A monomial growth group itself is totally ordered, all elements
are comparable. However, this does **not** hold for Cartesian
products::
sage: e1 = x^2*y; e2 = x*y^2
sage: e1 <= e2 or e2 <= e1
False
In terms of uniqueness, we have the following behaviour::
sage: GrowthGroup('x^ZZ * y^ZZ') is GrowthGroup('y^ZZ * x^ZZ')
True
The above is ``True`` since the order of the factors does not play a role here; they use different variables. But when using the same variable, it plays a role::
sage: GrowthGroup('x^ZZ * log(x)^ZZ') is GrowthGroup('log(x)^ZZ * x^ZZ')
False
In this case the components are ordered lexicographically, which
means that in the second growth group, ``log(x)`` is assumed to
grow faster than ``x`` (which is nonsense, mathematically). See
:class:`CartesianProduct <sage.rings.asymptotic.growth_group_cartesian.CartesianProductFactory>`
for more details or see :ref:`above <growth_group_description>`
for a more extensive description.
Short notation also allows the construction of more complicated
growth groups::
sage: G = GrowthGroup('QQ^x * x^ZZ * log(x)^QQ * y^QQ')
sage: G.an_element()
(1/2)^x*x*log(x)^(1/2)*y^(1/2)
sage: x, y = var('x y')
sage: G(2^x * log(x) * y^(1/2)) * G(x^(-5) * 5^x * y^(1/3))
10^x*x^(-5)*log(x)*y^(5/6)
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) 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
from sage.misc.lazy_import import lazy_import
lazy_import('sage.rings.asymptotic.growth_group_cartesian', 'CartesianProductGrowthGroups')
class Variable(sage.structure.unique_representation.CachedRepresentation,
sage.structure.sage_object.SageObject):
r"""
A class managing the variable of a growth group.
INPUT:
- ``var`` -- an object whose representation string is used as the
variable. It has to be a valid Python identifier. ``var`` can
also be a tuple (or other iterable) of such objects.
- ``repr`` -- (default: ``None``) if specified, then this string
will be displayed instead of ``var``. Use this to get
e.g. ``log(x)^ZZ``: ``var`` is then used to specify the variable `x`.
- ``ignore`` -- (default: ``None``) a tuple (or other iterable)
of strings which are not variables.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: v = Variable('x'); repr(v), v.variable_names()
('x', ('x',))
sage: v = Variable('x1'); repr(v), v.variable_names()
('x1', ('x1',))
sage: v = Variable('x_42'); repr(v), v.variable_names()
('x_42', ('x_42',))
sage: v = Variable(' x'); repr(v), v.variable_names()
('x', ('x',))
sage: v = Variable('x '); repr(v), v.variable_names()
('x', ('x',))
sage: v = Variable(''); repr(v), v.variable_names()
('', ())
::
sage: v = Variable(('x', 'y')); repr(v), v.variable_names()
('x, y', ('x', 'y'))
sage: v = Variable(('x', 'log(y)')); repr(v), v.variable_names()
('x, log(y)', ('x', 'y'))
sage: v = Variable(('x', 'log(x)')); repr(v), v.variable_names()
Traceback (most recent call last):
...
ValueError: Variable names ('x', 'x') are not pairwise distinct.
::
sage: v = Variable('log(x)'); repr(v), v.variable_names()
('log(x)', ('x',))
sage: v = Variable('log(log(x))'); repr(v), v.variable_names()
('log(log(x))', ('x',))
::
sage: v = Variable('x', repr='log(x)'); repr(v), v.variable_names()
('log(x)', ('x',))
::
sage: v = Variable('e^x', ignore=('e',)); repr(v), v.variable_names()
('e^x', ('x',))
::
sage: v = Variable('(e^n)', ignore=('e',)); repr(v), v.variable_names()
('e^n', ('n',))
sage: v = Variable('(e^(n*log(n)))', ignore=('e',)); repr(v), v.variable_names()
('e^(n*log(n))', ('n',))
"""
def __init__(self, var, repr=None, ignore=None):
r"""
See :class:`Variable` for details.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('blub')
blub
sage: Variable('blub') is Variable('blub')
True
::
sage: Variable('(:-)')
Traceback (most recent call last):
...
TypeError: Malformed expression: : !!! -
sage: Variable('(:-)', repr='icecream')
Traceback (most recent call last):
...
ValueError: ':-' is not a valid name for a variable.
"""
from sage.symbolic.ring import isidentifier
from misc import split_str_by_op
if not isinstance(var, (list, tuple)):
var = (var,)
var = tuple(''.join(split_str_by_op(str(v), None)) for v in var) # we strip off parentheses
if ignore is None:
ignore = tuple()
if repr is None:
var_bases = tuple(i for i in sum(iter(
self.extract_variable_names(v)
if not isidentifier(v) else (v,)
for v in var), tuple()) if i not in ignore)
var_repr = ', '.join(var)
else:
for v in var:
if not isidentifier(v):
raise ValueError("'%s' is not a valid name for a variable." % (v,))
var_bases = var
var_repr = str(repr).strip()
if len(var_bases) != len(set(var_bases)):
raise ValueError('Variable names %s are not pairwise distinct.' %
(var_bases,))
self.var_bases = var_bases
self.var_repr = var_repr
def __hash__(self):
r"""
Return the hash of this variable.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: hash(Variable('blub')) # random
-123456789
"""
return hash((self.var_repr,) + self.var_bases)
def __eq__(self, other):
r"""
Compare whether this variable equals ``other``.
INPUT:
- ``other`` -- another variable.
OUTPUT:
A boolean.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('x') == Variable('x')
True
sage: Variable('x') == Variable('y')
False
"""
return self.var_repr == other.var_repr and self.var_bases == other.var_bases
def __ne__(self, other):
r"""
Return whether this variable does not equal ``other``.
INPUT:
- ``other`` -- another variable.
OUTPUT:
A boolean.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('x') != Variable('x')
False
sage: Variable('x') != Variable('y')
True
"""
return not self == other
def _repr_(self):
r"""
Return a representation string of this variable.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('blub') # indirect doctest
blub
"""
return self.var_repr
def variable_names(self):
r"""
Return the names of the variables.
OUTPUT:
A tuple of strings.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('x').variable_names()
('x',)
sage: Variable('log(x)').variable_names()
('x',)
"""
return self.var_bases
def is_monomial(self):
r"""
Return whether this is a monomial variable.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('x').is_monomial()
True
sage: Variable('log(x)').is_monomial()
False
"""
return len(self.var_bases) == 1 and self.var_bases[0] == self.var_repr
@staticmethod
def extract_variable_names(s):
r"""
Determine the name of the variable for the given string.
INPUT:
- ``s`` -- a string.
OUTPUT:
A tuple of strings.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable.extract_variable_names('')
()
sage: Variable.extract_variable_names('x')
('x',)
sage: Variable.extract_variable_names('exp(x)')
('x',)
sage: Variable.extract_variable_names('sin(cos(ln(x)))')
('x',)
::
sage: Variable.extract_variable_names('log(77w)')
('w',)
sage: Variable.extract_variable_names('log(x')
Traceback (most recent call last):
....
TypeError: Bad function call: log(x !!!
sage: Variable.extract_variable_names('x)')
Traceback (most recent call last):
....
TypeError: Malformed expression: x) !!!
sage: Variable.extract_variable_names('log)x(')
Traceback (most recent call last):
....
TypeError: Malformed expression: log) !!! x(
sage: Variable.extract_variable_names('log(x)+y')
('x', 'y')
sage: Variable.extract_variable_names('icecream(summer)')
('summer',)
::
sage: Variable.extract_variable_names('a + b')
('a', 'b')
sage: Variable.extract_variable_names('a+b')
('a', 'b')
sage: Variable.extract_variable_names('a +b')
('a', 'b')
sage: Variable.extract_variable_names('+a')
('a',)
sage: Variable.extract_variable_names('a+')
Traceback (most recent call last):
...
TypeError: Malformed expression: a+ !!!
sage: Variable.extract_variable_names('b!')
('b',)
sage: Variable.extract_variable_names('-a')
('a',)
sage: Variable.extract_variable_names('a*b')
('a', 'b')
sage: Variable.extract_variable_names('2^q')
('q',)
sage: Variable.extract_variable_names('77')
()
::
sage: Variable.extract_variable_names('a + (b + c) + d')
('a', 'b', 'c', 'd')
"""
from sage.symbolic.ring import SR
if s == '':
return ()
return tuple(str(s) for s in SR(s).variables())
def _substitute_(self, rules):
r"""
Substitute the given ``rules`` in this variable.
INPUT:
- ``rules`` -- a dictionary.
OUTPUT:
An object.
TESTS::
sage: from sage.rings.asymptotic.growth_group import Variable
sage: Variable('x^2')._substitute_({'x': SR.var('z')})
z^2
sage: _.parent()
Symbolic Ring
::
sage: Variable('1/x')._substitute_({'x': 'z'})
Traceback (most recent call last):
...
TypeError: Cannot substitute in 1/x in
<class 'sage.rings.asymptotic.growth_group.Variable'>.
> *previous* TypeError: unsupported operand parent(s) for '/':
'Integer Ring' and '<type 'str'>'
sage: Variable('1/x')._substitute_({'x': 0})
Traceback (most recent call last):
...
ZeroDivisionError: Cannot substitute in 1/x in
<class 'sage.rings.asymptotic.growth_group.Variable'>.
> *previous* ZeroDivisionError: rational division by zero
"""
from sage.misc.sage_eval import sage_eval
try:
return sage_eval(self.var_repr, locals=rules)
except (ArithmeticError, TypeError, ValueError) as e:
from misc import substitute_raise_exception
substitute_raise_exception(self, e)
# The following function is used in the classes GenericGrowthElement and
# GenericProduct.Element as a method.
def _is_lt_one_(self):
r"""
Return whether this element is less than `1`.
INPUT:
Nothing.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('x^ZZ'); x = G(x)
sage: (x^42).is_lt_one() # indirect doctest
False
sage: (x^(-42)).is_lt_one() # indirect doctest
True
"""
one = self.parent().one()
return self <= one and self != one
# The following function is used in the classes GenericGrowthElement and
# GenericProduct.Element as a method.
def _log_(self, base=None):
r"""
Return the logarithm of this element.
INPUT:
- ``base`` -- the base of the logarithm. If ``None``
(default value) is used, the natural logarithm is taken.
OUTPUT:
A growth element.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('x^ZZ * log(x)^ZZ')
sage: x, = G.gens_monomial()
sage: log(x) # indirect doctest
log(x)
sage: log(x^5) # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: When calculating log(x^5) a factor 5 != 1 appeared,
which is not contained in Growth Group x^ZZ * log(x)^ZZ.
::
sage: G = GrowthGroup('QQ^x * x^ZZ')
sage: x, = G.gens_monomial()
sage: el = x.rpow(2); el
2^x
sage: log(el) # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: When calculating log(2^x) a factor log(2) != 1
appeared, which is not contained in Growth Group QQ^x * x^ZZ.
sage: log(el, base=2) # indirect doctest
x
::
sage: from sage.rings.asymptotic.growth_group import GenericGrowthGroup
sage: x = GenericGrowthGroup(ZZ).an_element()
sage: log(x) # indirect doctest
Traceback (most recent call last):
...
NotImplementedError: Cannot determine logarithmized factorization of
GenericGrowthElement(1) in abstract base class.
::
sage: x = GrowthGroup('x^ZZ').an_element()
sage: log(x) # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: Cannot build log(x) since log(x) is not in
Growth Group x^ZZ.
TESTS::
sage: G = GrowthGroup("(e^x)^QQ * x^ZZ")
sage: x, = G.gens_monomial()
sage: log(exp(x)) # indirect doctest
x
sage: G.one().log() # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: log(1) is zero, which is not contained in
Growth Group (e^x)^QQ * x^ZZ.
::
sage: G = GrowthGroup("(e^x)^ZZ * x^ZZ")
sage: x, = G.gens_monomial()
sage: log(exp(x)) # indirect doctest
x
sage: G.one().log() # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: log(1) is zero, which is not contained in
Growth Group (e^x)^ZZ * x^ZZ.
::
sage: G = GrowthGroup('QQ^x * x^ZZ * log(x)^ZZ * y^ZZ * log(y)^ZZ')
sage: x, y = G.gens_monomial()
sage: (x * y).log() # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: Calculating log(x*y) results in a sum,
which is not contained in
Growth Group QQ^x * x^ZZ * log(x)^ZZ * y^ZZ * log(y)^ZZ.
"""
from misc import log_string
log_factor = self.log_factor(base=base)
if not log_factor:
raise ArithmeticError('%s is zero, '
'which is not contained in %s.' %
(log_string(self, base), self.parent()))
if len(log_factor) != 1:
raise ArithmeticError('Calculating %s results in a sum, '
'which is not contained in %s.' %
(log_string(self, base), self.parent()))
g, c = log_factor[0]
if c != 1:
raise ArithmeticError('When calculating %s a factor %s != 1 '
'appeared, which is not contained in %s.' %
(log_string(self, base), c, self.parent()))
return g
# The following function is used in the classes GenericGrowthElement and
# GenericProduct.Element as a method.
def _log_factor_(self, base=None):
r"""
Return the logarithm of the factorization 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 pairs, where the first entry is a growth
element and the second a multiplicative coefficient.
ALGORITHM:
This function factors the given element and calculates
the logarithm of each of these factors.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('QQ^x * x^ZZ * log(x)^ZZ * y^ZZ * log(y)^ZZ')
sage: x, y = G.gens_monomial()
sage: (x * y).log_factor() # indirect doctest
((log(x), 1), (log(y), 1))
sage: (x^123).log_factor() # indirect doctest
((log(x), 123),)
sage: (G('2^x') * x^2).log_factor(base=2) # indirect doctest
((x, 1), (log(x), 2/log(2)))
::
sage: G(1).log_factor()
()
::
sage: log(x).log_factor() # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: Cannot build log(log(x)) since log(log(x)) is
not in Growth Group QQ^x * x^ZZ * log(x)^ZZ * y^ZZ * log(y)^ZZ.
.. SEEALSO::
:meth:`~sage.rings.asymptotic.growth_group.GenericGrowthElement.factors`,
:meth:`~sage.rings.asymptotic.growth_group.GenericGrowthElement.log`.
TESTS::
sage: G = GrowthGroup("(e^x)^ZZ * x^ZZ * log(x)^ZZ")
sage: x, = G.gens_monomial()
sage: (exp(x) * x).log_factor() # indirect doctest
((x, 1), (log(x), 1))
"""
log_factor = self._log_factor_(base=base)
for g, c in log_factor:
if hasattr(g, 'parent') and \
isinstance(g.parent(), GenericGrowthGroup):
continue
from misc import log_string
raise ArithmeticError('Cannot build %s since %s '
'is not in %s.' % (log_string(self, base),
g, self.parent()))
return log_factor
# The following function is used in the classes GenericGrowthElement and
# GenericProduct.Element as a method.
def _rpow_(self, base):
r"""
Calculate the power of ``base`` to this element.
INPUT:
- ``base`` -- an element.
OUTPUT:
A growth element.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: G = GrowthGroup('QQ^x * x^ZZ')
sage: x = G('x')
sage: x.rpow(2) # indirect doctest
2^x
sage: x.rpow(1/2) # indirect doctest
(1/2)^x
::
sage: x.rpow(0) # indirect doctest
Traceback (most recent call last):
...
ValueError: 0 is not an allowed base for calculating the power to x.
sage: (x^2).rpow(2) # indirect doctest
Traceback (most recent call last):
...
ArithmeticError: Cannot construct 2^(x^2) in Growth Group QQ^x * x^ZZ
> *previous* TypeError: unsupported operand parent(s) for '*':
'Growth Group QQ^x * x^ZZ' and 'Growth Group ZZ^(x^2)'
::
sage: G = GrowthGroup('QQ^(x*log(x)) * x^ZZ * log(x)^ZZ')
sage: x = G('x')
sage: (x * log(x)).rpow(2) # indirect doctest
2^(x*log(x))
::
sage: n = GrowthGroup('QQ^n * n^QQ')('n')
sage: n.rpow(2)
2^n
sage: _.parent()
Growth Group QQ^n * n^QQ
"""
if base == 0:
raise ValueError('%s is not an allowed base for calculating the '
'power to %s.' % (base, self))
var = str(self)
try:
element = self._rpow_element_(base)
except ValueError:
if base == 'e':
from sage.rings.integer_ring import ZZ
from misc import repr_op
M = MonomialGrowthGroup(ZZ, repr_op('e', '^', var),
ignore_variables=('e',))
element = M(raw_element=ZZ(1))
else:
E = ExponentialGrowthGroup(base.parent(), var)
element = E(raw_element=base)
try:
return self.parent().one() * element
except (TypeError, ValueError) as e:
from misc import combine_exceptions, repr_op
raise combine_exceptions(
ArithmeticError('Cannot construct %s in %s' %
(repr_op(base, '^', var), self.parent())), e)
class GenericGrowthElement(sage.structure.element.MultiplicativeGroupElement):
r"""
A basic implementation of a generic growth element.
Growth elements form a group by multiplication, and (some of) the
elements can be compared to each other, i.e., all elements form a
poset.
INPUT:
- ``parent`` -- a :class:`GenericGrowthGroup`.
- ``raw_element`` -- an element from the base of the parent.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import (GenericGrowthGroup,
....: GenericGrowthElement)
sage: G = GenericGrowthGroup(ZZ)
sage: g = GenericGrowthElement(G, 42); g
GenericGrowthElement(42)
sage: g.parent()
Growth Group Generic(ZZ)
sage: G(raw_element=42) == g
True
"""
def __init__(self, parent, raw_element):
r"""
See :class:`GenericGrowthElement` for more information.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GenericGrowthGroup
sage: G = GenericGrowthGroup(ZZ)
sage: G(raw_element=42)
GenericGrowthElement(42)
TESTS::
sage: G(raw_element=42).category()
Category of elements of Growth Group Generic(ZZ)
::
sage: G = GenericGrowthGroup(ZZ)
sage: G(raw_element=42).category()
Category of elements of Growth Group Generic(ZZ)
::
sage: from sage.rings.asymptotic.growth_group import GenericGrowthElement
sage: GenericGrowthElement(None, 0)
Traceback (most recent call last):
...
ValueError: The parent must be provided
"""
if parent is None:
raise ValueError('The parent must be provided')
super(GenericGrowthElement, self).__init__(parent=parent)
self._raw_element_ = parent.base()(raw_element)
def _repr_(self):
r"""
A representation string for this generic element.
INPUT:
Nothing.
OUTPUT:
A string.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GenericGrowthGroup
sage: G = GenericGrowthGroup(ZZ)
sage: G(raw_element=42) # indirect doctest
GenericGrowthElement(42)
sage: H = GenericGrowthGroup(ZZ, 'h')
sage: H(raw_element=42) # indirect doctest
GenericGrowthElement(42, h)
"""
vars = ', '.join(self.parent()._var_.variable_names())
if vars: