Skip to content

Commit 70662cd

Browse files
Merge pull request #38 from fredrik-johansson/moretests
doctesting & bugfixing
2 parents d6e239d + 80ac97e commit 70662cd

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/flint/fmpq.pyx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ cdef class fmpq(flint_scalar):
281281

282282
@staticmethod
283283
def dedekind_sum(n, k):
284+
"""
285+
Dedekind sum.
286+
287+
>>> fmpq.dedekind_sum(10, 3)
288+
1/18
289+
"""
284290
cdef fmpz nv, kv
285291
cdef fmpq v
286292
nv = fmpz(n)
@@ -290,11 +296,23 @@ cdef class fmpq(flint_scalar):
290296
return v
291297

292298
def floor(self):
299+
"""
300+
Floor function.
301+
302+
>>> fmpq(3,2).floor()
303+
1
304+
"""
293305
cdef fmpz r = fmpz.__new__(fmpz)
294306
fmpz_fdiv_q(r.val, fmpq_numref(self.val), fmpq_denref(self.val))
295307
return r
296308

297309
def ceil(self):
310+
"""
311+
Ceiling function.
312+
313+
>>> fmpq(3,2).ceil()
314+
2
315+
"""
298316
cdef fmpz r = fmpz.__new__(fmpz)
299317
fmpz_cdiv_q(r.val, fmpq_numref(self.val), fmpq_denref(self.val))
300318
return r
@@ -304,6 +322,16 @@ cdef class fmpq(flint_scalar):
304322
return hash(Fraction(int(self.p), int(self.q), _normalize=False))
305323

306324
def height_bits(self, bint signed=False):
325+
"""
326+
Returns the bit length of the maximum of the numerator and denominator.
327+
With signed=True, returns the negative value if the number is
328+
negative.
329+
330+
>>> fmpq(1001,5).height_bits()
331+
10
332+
>>> fmpq(-5,1001).height_bits(signed=True)
333+
-10
334+
"""
307335
cdef long b1, b2
308336
b1 = fmpz_bits(fmpq_numref(self.val))
309337
b2 = fmpz_bits(fmpq_denref(self.val))

src/flint/fmpz_series.pyx

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ cdef fmpz_series_coerce_operands(x, y):
2424
return NotImplemented, NotImplemented
2525

2626
cdef class fmpz_series(flint_series):
27+
"""
28+
Power series with integer coefficients.
29+
30+
>>> fmpz_series([1,2,3])
31+
1 + 2*x + 3*x^2 + O(x^10)
32+
>>> fmpz_series([1,2,3], prec=2)
33+
1 + 2*x + O(x^2)
34+
>>> fmpz_series([1,2,3], prec=2) + fmpz_series([1,2,3,4,5])
35+
2 + 4*x + O(x^2)
36+
>>> a = fmpz_series([1,2,3])
37+
>>> (a+1)*(a-1) - a**2 + 1
38+
0 + O(x^10)
39+
40+
"""
2741

2842
cdef fmpz_poly_t val
2943
cdef long prec
@@ -151,6 +165,17 @@ cdef class fmpz_series(flint_series):
151165
return s * t
152166

153167
cpdef valuation(self):
168+
"""
169+
Returns the valuation of this power series.
170+
If there are no known nonzero coefficients, returns -1.
171+
172+
>>> fmpz_series([1,2,3]).valuation()
173+
0
174+
>>> fmpz_series([0,0,0,1,2,3]).valuation()
175+
3
176+
>>> fmpz_series([]).valuation()
177+
-1
178+
"""
154179
cdef long i
155180
if fmpz_poly_is_zero(self.val):
156181
return -1
@@ -159,7 +184,26 @@ cdef class fmpz_series(flint_series):
159184
i += 1
160185
return i
161186

162-
def __div__(s, t):
187+
def _div_(s, t):
188+
"""
189+
Power series division.
190+
191+
>>> fmpz_series([1]) / fmpz_series([1,-1,-1])
192+
1 + x + 2*x^2 + 3*x^3 + 5*x^4 + 8*x^5 + 13*x^6 + 21*x^7 + 34*x^8 + 55*x^9 + O(x^10)
193+
>>> fmpz_series([0,1,2]) / fmpz_series([0,1,2])
194+
1 + O(x^9)
195+
>>> fmpz_series([1,2]) / fmpz_series([0,1,2])
196+
Traceback (most recent call last):
197+
...
198+
ValueError: quotient would not be a power series
199+
>>> fmpz_series([1,2]) / fmpz_series([2,3])
200+
Traceback (most recent call last):
201+
...
202+
ValueError: leading term in denominator is not a unit
203+
>>> fmpz_series([]) / fmpz_series([1,2,3], prec=5)
204+
0 + O(x^5)
205+
206+
"""
163207
cdef long cap, sval, tval
164208
cdef fmpz_poly_t stmp, ttmp
165209
if type(s) is type(t):
@@ -173,7 +217,7 @@ cdef class fmpz_series(flint_series):
173217
u = fmpz_series.__new__(fmpz_series)
174218

175219
if fmpz_poly_is_zero((<fmpz_series>s).val):
176-
u.cap = cap
220+
(<fmpz_series>u).prec = cap
177221
return u
178222

179223
sval = (<fmpz_series>s).valuation()
@@ -205,10 +249,19 @@ cdef class fmpz_series(flint_series):
205249
return s
206250
return s / t
207251

252+
def __div__(s, t):
253+
return fmpz_series._div_(s, t)
254+
208255
def __truediv__(s, t):
209-
return fmpz_series.__div__(s, t)
256+
return fmpz_series._div_(s, t)
210257

211258
def __pow__(fmpz_series s, ulong exp, mod):
259+
"""
260+
Power series exponentiation.
261+
262+
>>> fmpz_series([3,4,5], prec=4) ** 5
263+
243 + 1620*x + 6345*x^2 + 16560*x^3 + O(x^4)
264+
"""
212265
cdef long cap
213266
if mod is not None:
214267
raise NotImplementedError("fmpz_series modular exponentiation")
@@ -220,6 +273,17 @@ cdef class fmpz_series(flint_series):
220273
return u
221274

222275
def __call__(s, t):
276+
"""
277+
Power series composition.
278+
279+
>>> fmpz_series([1,2,3])(fmpz_series([0,1,2]))
280+
1 + 2*x + 7*x^2 + 12*x^3 + 12*x^4 + O(x^10)
281+
>>> fmpz_series([1,2,3])(fmpz_series([1,1,2]))
282+
Traceback (most recent call last):
283+
...
284+
ValueError: power series composition with nonzero constant term
285+
286+
"""
223287
cdef long cap
224288
if typecheck(t, fmpz_series):
225289
u = fmpz_series.__new__(fmpz_series)
@@ -234,6 +298,19 @@ cdef class fmpz_series(flint_series):
234298
raise TypeError("cannot call fmpz_series with input of type %s", type(t))
235299

236300
def reversion(s):
301+
"""
302+
Power series reversion.
303+
304+
>>> fmpz_series([0,1,-2,-3]).reversion()
305+
x + 2*x^2 + 11*x^3 + 70*x^4 + 503*x^5 + 3864*x^6 + 31092*x^7 + 258654*x^8 + 2206655*x^9 + O(x^10)
306+
>>> fmpz_series([0,1,-2,-3]).reversion()(fmpz_series([0,1,-2,-3]))
307+
x + O(x^10)
308+
>>> fmpz_series([1,2,3]).reversion()
309+
Traceback (most recent call last):
310+
...
311+
ValueError: power series reversion must have valuation 1
312+
313+
"""
237314
cdef long cap
238315
cap = getcap()
239316
cap = min(cap, (<fmpz_series>s).prec)

0 commit comments

Comments
 (0)