Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/rings/big_oh.py
4072 views
1
"""
2
Big O for various types (power series, p-adics, etc.)
3
"""
4
5
import sage.rings.arith as arith
6
import laurent_series_ring_element
7
import sage.rings.padics.factory as padics_factory
8
import sage.rings.padics.padic_generic_element as padic_generic_element
9
import power_series_ring_element
10
import integer
11
import rational
12
from sage.rings.polynomial.polynomial_element import Polynomial
13
import multi_power_series_ring_element
14
15
def O(*x):
16
"""
17
Big O constructor for various types.
18
19
EXAMPLES:
20
This is useful for writing power series elements.
21
sage: R.<t> = ZZ[['t']]
22
sage: (1+t)^10 + O(t^5)
23
1 + 10*t + 45*t^2 + 120*t^3 + 210*t^4 + O(t^5)
24
25
A power series ring is created implicitly if a polynomial element is passed in.
26
sage: R.<x> = QQ['x']
27
sage: O(x^100)
28
O(x^100)
29
sage: 1/(1+x+O(x^5))
30
1 - x + x^2 - x^3 + x^4 + O(x^5)
31
sage: R.<u,v> = QQ[[]]
32
sage: 1 + u + v^2 + O(u, v)^5
33
1 + u + v^2 + O(u, v)^5
34
35
This is also useful to create p-adic numbers.
36
sage: O(7^6)
37
O(7^6)
38
sage: 1/3 + O(7^6)
39
5 + 4*7 + 4*7^2 + 4*7^3 + 4*7^4 + 4*7^5 + O(7^6)
40
41
It behaves well with respect to adding negative powers of p:
42
sage: a = O(11^-32); a
43
O(11^-32)
44
sage: a.parent()
45
11-adic Field with capped relative precision 20
46
47
There are problems if you add a rational with very negative valuation to a big_oh.
48
sage: 11^-12 + O(11^15)
49
11^-12 + O(11^8)
50
51
The reason that this fails is that the O function doesn't know the right precision cap to use. If you cast explicitly or use other means of element creation, you can get around this issue.
52
sage: K = Qp(11, 30)
53
sage: K(11^-12) + O(11^15)
54
11^-12 + O(11^15)
55
sage: 11^-12 + K(O(11^15))
56
11^-12 + O(11^15)
57
sage: K(11^-12, absprec = 15)
58
11^-12 + O(11^15)
59
sage: K(11^-12, 15)
60
11^-12 + O(11^15)
61
62
"""
63
if len(x) > 1:
64
if isinstance(x[0], multi_power_series_ring_element.MPowerSeries):
65
return multi_power_series_ring_element.MO(x)
66
x = x[0]
67
68
if isinstance(x, power_series_ring_element.PowerSeries):
69
return x.parent()(0, x.degree())
70
71
elif isinstance(x, Polynomial):
72
if x.parent().ngens() != 1:
73
raise NotImplementedError, "completion only currently defined for univariate polynomials"
74
if not x.is_monomial():
75
raise NotImplementedError, "completion only currently defined for the maximal ideal (x)"
76
return x.parent().completion(x.parent().gen())(0, x.degree())
77
78
elif isinstance(x, laurent_series_ring_element.LaurentSeries):
79
return laurent_series_ring_element.LaurentSeries(x.parent(), 0).add_bigoh(x.valuation())
80
81
elif isinstance(x, (int,long,integer.Integer,rational.Rational)): # p-adic number
82
if x <= 0:
83
raise ArithmeticError, "x must be a prime power >= 2"
84
F = arith.factor(x)
85
if len(F) != 1:
86
raise ArithmeticError, "x must be prime power"
87
p, r = F[0]
88
if r >= 0:
89
return padics_factory.Zp(p, prec = max(r, 20), type = 'capped-rel')(0, absprec = r)
90
else:
91
return padics_factory.Qp(p, prec = max(r, 20), type = 'capped-rel')(0, absprec = r)
92
93
elif isinstance(x, padic_generic_element.pAdicGenericElement):
94
return x.parent()(0, absprec = x.valuation())
95
raise ArithmeticError, "O(x) not defined"
96
97
98