Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241818 views
1
2
from sage.stats.intlist cimport IntList
3
4
from sage.all import prime_range
5
6
def prime_powers_intlist(Py_ssize_t B):
7
"""
8
Return IntList of the prime powers and corresponding primes, up to
9
B. The list is *not* in the usual order; instead the order is like
10
this: 2,4,8,...,3,9,27,..., 5,25,..., etc.
11
12
INPUT:
13
14
- B -- positive integer
15
16
EXAMPLES::
17
18
sage: from psage.ellcurve.lseries.helper import prime_powers_intlist
19
sage: prime_powers_intlist(10)
20
([1, 2, 4, 8, 3, 9, 5, 7], [1, 2, 2, 2, 3, 3, 5, 7])
21
sage: prime_powers_intlist(10)
22
([1, 2, 4, 8, 3, 9, 5, 7], [1, 2, 2, 2, 3, 3, 5, 7])
23
sage: prime_powers_intlist(20)
24
([1, 2, 4, 8, 16 ... 7, 11, 13, 17, 19], [1, 2, 2, 2, 2 ... 7, 11, 13, 17, 19])
25
sage: list(sorted(prime_powers_intlist(10^6)[0])) == prime_powers(10^6)
26
True
27
sage: set(prime_powers_intlist(10^6)[1][1:]) == set(primes(10^6))
28
True
29
"""
30
v = prime_range(B, py_ints=True)
31
cdef IntList w = IntList(len(v)*2), w0 = IntList(len(v)*2)
32
w[0] = 1
33
w0[0] = 1
34
# Now fill in prime powers
35
cdef Py_ssize_t i=1
36
cdef int p
37
cdef long long q # so that the "q < B" test doesn't fail due to overflow.
38
for p in v:
39
q = p
40
while q < B:
41
w._values[i] = q
42
w0._values[i] = p
43
q *= p
44
i += 1
45
return w[:i], w0[:i]
46
47
def extend_multiplicatively(IntList a):
48
"""
49
Given an IntList a such that the a[p^r] is filled in, for all
50
prime powers p^r, fill in all the other a[n] multiplicatively.
51
52
INPUT:
53
- a -- IntList with prime-power-index entries set; all other
54
entries are ignored
55
56
OUTPUT:
57
- the input object a is modified to have all entries set
58
via multiplicativity.
59
60
EXAMPLES::
61
62
sage: from psage.ellcurve.lseries.helper import extend_multiplicatively
63
sage: B = 10^5; E = EllipticCurve('389a'); an = stats.IntList(B)
64
sage: for pp in prime_powers(B):
65
... an[pp] = E.an(pp)
66
...
67
sage: extend_multiplicatively(an)
68
sage: list(an) == E.anlist(len(an))[:len(an)]
69
True
70
"""
71
cdef Py_ssize_t i, B = len(a)
72
cdef IntList P, P0
73
P, P0 = prime_powers_intlist(B)
74
75
# Known[n] = 1 if a[n] is set. We use this separate array
76
# to avoid using a sentinel value in a.
77
cdef IntList known = IntList(B)
78
for i in range(len(P)):
79
known._values[P[i]] = 1
80
81
cdef int k, pp, p, n
82
# fill in the multiples of pp = prime power
83
for i in range(len(P)):
84
pp = P._values[i]; p = P0._values[i]
85
k = 2
86
n = k*pp
87
while n < B:
88
# only consider n exactly divisible by pp
89
if k % p and known._values[k]:
90
a._values[n] = a._values[k] * a._values[pp]
91
known._values[n] = 1
92
n += pp
93
k += 1
94
95
96
def extend_multiplicatively_generic(list a):
97
"""
98
Given a list a of numbers such that the a[p^r] is filled in, for
99
all prime powers p^r, fill in all the other a[n] multiplicatively.
100
101
INPUT:
102
- a -- list with prime-power-index entries set; all other
103
entries are ignored
104
105
OUTPUT:
106
- the input object a is modified to have all entries set
107
via multiplicativity.
108
109
EXAMPLES::
110
111
sage: from psage.ellcurve.lseries.helper import extend_multiplicatively_generic
112
sage: B = 10^5; E = EllipticCurve('389a'); an = [0]*B
113
sage: for pp in prime_powers(B):
114
... an[pp] = E.an(pp)
115
...
116
sage: extend_multiplicatively_generic(an)
117
sage: list(an) == E.anlist(len(an))[:len(an)]
118
True
119
120
121
A test using large integers::
122
123
sage: v = [0, 1, 2**100, 3**100, 4, 5, 0]
124
sage: extend_multiplicatively_generic(v)
125
sage: v
126
[0, 1, 1267650600228229401496703205376, 515377520732011331036461129765621272702107522001, 4, 5, 653318623500070906096690267158057820537143710472954871543071966369497141477376]
127
sage: v[-1] == v[2]*v[3]
128
True
129
130
A test using variables::
131
132
sage: v = list(var(' '.join('x%s'%i for i in [0..30]))); v
133
[x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30]
134
sage: extend_multiplicatively_generic(v)
135
sage: v
136
[x0, x1, x2, x3, x4, x5, x2*x3, x7, x8, x9, x2*x5, x11, x3*x4, x13, x2*x7, x3*x5, x16, x17, x2*x9, x19, x4*x5, x3*x7, x11*x2, x23, x3*x8, x25, x13*x2, x27, x4*x7, x29, x2*x3*x5]
137
"""
138
cdef Py_ssize_t i, B = len(a)
139
cdef IntList P, P0
140
P, P0 = prime_powers_intlist(B)
141
142
# Known[n] = 1 if a[n] is set. We use this separate array
143
# to avoid using a sentinel value in a.
144
cdef IntList known = IntList(B)
145
for i in range(len(P)):
146
known._values[P[i]] = 1
147
148
cdef int k, pp, p, n
149
# fill in the multiples of pp = prime power
150
for i in range(len(P)):
151
pp = P._values[i]; p = P0._values[i]
152
k = 2
153
n = k*pp
154
while n < B:
155
# only consider n exactly divisible by pp
156
if k % p and known._values[k]:
157
a[n] = a[k] * a[pp]
158
known._values[n] = 1
159
n += pp
160
k += 1
161
162
163
164
165