Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 184574
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
48
def extend_multiplicatively(IntList a):
49
"""
50
Given an IntList a such that the a[p^r] is filled in, for all
51
prime powers p^r, fill in all the other a[n] multiplicatively.
52
53
INPUT:
54
- a -- IntList with prime-power-index entries set; all other
55
entries are ignored
56
57
OUTPUT:
58
- the input object a is modified to have all entries set
59
via multiplicativity.
60
61
EXAMPLES::
62
63
sage: from psage.ellcurve.lseries.helper import extend_multiplicatively
64
sage: B = 10^5; E = EllipticCurve('389a'); an = stats.IntList(B)
65
sage: for pp in prime_powers(B):
66
... an[pp] = E.an(pp)
67
...
68
sage: extend_multiplicatively(an)
69
sage: list(an) == E.anlist(len(an))[:len(an)]
70
True
71
"""
72
cdef Py_ssize_t i, B = len(a)
73
cdef IntList P, P0
74
P, P0 = prime_powers_intlist(B)
75
76
# Known[n] = 1 if a[n] is set. We use this separate array
77
# to avoid using a sentinel value in a.
78
cdef IntList known = IntList(B)
79
for i in range(len(P)):
80
known._values[P[i]] = 1
81
82
cdef int k, pp, p, n
83
# fill in the multiples of pp = prime power
84
for i in range(len(P)):
85
pp = P._values[i]; p = P0._values[i]
86
k = 2
87
n = k*pp
88
while n < B:
89
# only consider n exactly divisible by pp
90
if k % p and known._values[k]:
91
a._values[n] = a._values[k] * a._values[pp]
92
known._values[n] = 1
93
n += pp
94
k += 1
95
96
97
def extend_multiplicatively_generic(list a):
98
"""
99
Given a list a of numbers such that the a[p^r] is filled in, for
100
all prime powers p^r, fill in all the other a[n] multiplicatively.
101
102
INPUT:
103
- a -- list with prime-power-index entries set; all other
104
entries are ignored
105
106
OUTPUT:
107
- the input object a is modified to have all entries set
108
via multiplicativity.
109
110
EXAMPLES::
111
112
sage: from psage.ellcurve.lseries.helper import extend_multiplicatively_generic
113
sage: B = 10^5; E = EllipticCurve('389a'); an = [0]*B
114
sage: for pp in prime_powers(B):
115
... an[pp] = E.an(pp)
116
...
117
sage: extend_multiplicatively_generic(an)
118
sage: list(an) == E.anlist(len(an))[:len(an)]
119
True
120
121
122
A test using large integers::
123
124
sage: v = [0, 1, 2**100, 3**100, 4, 5, 0]
125
sage: extend_multiplicatively_generic(v)
126
sage: v
127
[0, 1, 1267650600228229401496703205376, 515377520732011331036461129765621272702107522001, 4, 5, 653318623500070906096690267158057820537143710472954871543071966369497141477376]
128
sage: v[-1] == v[2]*v[3]
129
True
130
131
A test using variables::
132
133
sage: v = list(var(' '.join('x%s'%i for i in [0..30]))); v
134
[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]
135
sage: extend_multiplicatively_generic(v)
136
sage: v
137
[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]
138
"""
139
cdef Py_ssize_t i, B = len(a)
140
cdef IntList P, P0
141
P, P0 = prime_powers_intlist(B)
142
143
# Known[n] = 1 if a[n] is set. We use this separate array
144
# to avoid using a sentinel value in a.
145
cdef IntList known = IntList(B)
146
for i in range(len(P)):
147
known._values[P[i]] = 1
148
149
cdef int k, pp, p, n
150
# fill in the multiples of pp = prime power
151
for i in range(len(P)):
152
pp = P._values[i]; p = P0._values[i]
153
k = 2
154
n = k*pp
155
while n < B:
156
# only consider n exactly divisible by pp
157
if k % p and known._values[k]:
158
a[n] = a[k] * a[pp]
159
known._values[n] = 1
160
n += pp
161
k += 1
162
163
164
165
166