forked from flintlib/python-flint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmpq.pyx
More file actions
502 lines (432 loc) · 14.5 KB
/
fmpq.pyx
File metadata and controls
502 lines (432 loc) · 14.5 KB
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
from flint.flint_base.flint_base cimport flint_scalar
from flint.utils.typecheck cimport typecheck
from flint.types.fmpz cimport fmpz_set_any_ref
from flint.types.fmpz cimport fmpz
from flint.types.fmpz cimport any_as_fmpz
from flint.flintlib.flint cimport FMPZ_UNKNOWN, FMPZ_TMP, FMPZ_REF
from flint.flintlib.fmpz cimport fmpz_set, fmpz_one, fmpz_t
from flint.flintlib.fmpz cimport fmpz_is_zero, fmpz_sgn
from flint.flintlib.fmpz cimport fmpz_fdiv_q, fmpz_bits
from flint.flintlib.fmpz cimport fmpz_cdiv_q
from flint.flintlib.fmpz cimport fmpz_tdiv_q
from flint.flintlib.fmpz cimport fmpz_clear
from flint.flintlib.fmpq cimport *
from flint.flintlib.bernoulli cimport *
cdef int fmpq_set_any_ref(fmpq_t x, obj):
cdef int status
fmpq_init(x)
if typecheck(obj, fmpq):
x[0] = (<fmpq>obj).val[0]
return FMPZ_REF
if typecheck(obj, fmpz):
fmpz_set(fmpq_numref(x), (<fmpz>obj).val)
fmpz_one(fmpq_denref(x))
return FMPZ_TMP
status = fmpz_set_any_ref(fmpq_numref(x), obj)
if status != FMPZ_UNKNOWN:
fmpz_one(fmpq_denref(x))
return FMPZ_TMP
fmpq_clear(x)
return FMPZ_UNKNOWN
cdef any_as_fmpq(obj):
cdef fmpq_t x
cdef int status
cdef fmpq q
status = fmpq_set_any_ref(x, obj)
if status == FMPZ_REF:
q = fmpq.__new__(fmpq)
fmpq_set(q.val, x)
return q
elif status == FMPZ_TMP:
q = fmpq.__new__(fmpq)
fmpq_clear(q.val)
q.val[0] = x[0]
return q
else:
return NotImplemented
cdef class fmpq(flint_scalar):
"""
The fmpq type represents multiprecision rational numbers.
>>> fmpq(1,7) + fmpq(50,51)
401/357
"""
# cdef fmpq_t val
def __cinit__(self):
fmpq_init(self.val)
def __dealloc__(self):
fmpq_clear(self.val)
def __init__(self, *args):
if not args:
return # zero
elif len(args) == 2:
p, q = args
elif len(args) == 1:
p = args[0]
if typecheck(p, fmpq):
fmpq_set(self.val, (<fmpq>p).val)
return
elif typecheck(p, str):
if "/" in p:
p, q = p.split("/")
p = fmpz(p)
q = fmpz(q)
else:
p = fmpz(p)
q = fmpz(1)
else:
p2 = any_as_fmpq(p)
if p2 is NotImplemented:
raise TypeError("cannot create fmpq from object of type %s" % type(p))
fmpq_set(self.val, (<fmpq>p2).val)
return
else:
raise TypeError("fmpq() takes at most 2 arguments (%d given)" % len(args))
p2 = any_as_fmpz(p)
if p2 is NotImplemented:
raise TypeError("cannot create fmpq from object of type %s" % type(p))
q2 = any_as_fmpz(q)
if q2 is NotImplemented:
raise TypeError("cannot create fmpq from object of type %s" % type(q))
if fmpz_is_zero((<fmpz>q2).val):
raise ZeroDivisionError("cannot create rational number with zero denominator")
fmpz_set(fmpq_numref(self.val), (<fmpz>p2).val)
fmpz_set(fmpq_denref(self.val), (<fmpz>q2).val)
fmpq_canonicalise(self.val)
def __richcmp__(s, t, int op):
cdef bint res
s = any_as_fmpq(s)
if s is NotImplemented:
return s
t = any_as_fmpq(t)
if t is NotImplemented:
return t
if op == 2 or op == 3:
res = fmpq_equal((<fmpq>s).val, (<fmpq>t).val)
if op == 3:
res = not res
return res
else:
# todo: use fmpq_cmp when available
if op == 0: res = (s-t).p < 0
elif op == 1: res = (s-t).p <= 0
elif op == 4: res = (s-t).p > 0
elif op == 5: res = (s-t).p >= 0
else: raise ValueError
return res
def numer(self):
"""
Returns the numerator of *self* as an *fmpz*.
"""
cdef fmpz x = fmpz.__new__(fmpz)
fmpz_set(x.val, fmpq_numref(self.val))
return x
def denom(self):
"""
Returns the denominator of *self* as an *fmpz*.
"""
cdef fmpz x = fmpz.__new__(fmpz)
fmpz_set(x.val, fmpq_denref(self.val))
return x
p = property(numer)
q = property(denom)
# These are the property names in the numeric tower.
numerator = property(numer)
denominator = property(denom)
def __reduce__(self):
return (fmpq, (int(self.p), int(self.q)))
def repr(self):
if self.q == 1:
return "fmpq(%s)" % self.p
else:
return "fmpq(%s,%s)" % (self.p, self.q)
def str(self, **kwargs):
"""
Converts *self* to a string, forwarding optional keyword arguments
to :meth:`.fmpz.str`.
>>> fmpq.bernoulli(12).str()
'-691/2730'
>>> fmpq.bernoulli(100).str(base=2, condense=10)
'-110001110{...257 digits...}0011011111/1000001000110010'
"""
if self.q == 1:
return self.p.str(**kwargs)
else:
return "%s/%s" % (self.p.str(**kwargs), self.q.str(**kwargs))
def __int__(self):
return int(self.trunc())
def __floor__(self):
return self.floor()
def __ceil__(self):
return self.ceil()
def __trunc__(self):
return self.trunc()
def __bool__(self):
return not fmpq_is_zero(self.val)
def __round__(self, ndigits=None):
return self.round(ndigits)
def __pos__(self):
return self
def __neg__(self):
cdef fmpq r = fmpq.__new__(fmpq)
fmpq_neg(r.val, self.val)
return r
def __abs__(self):
cdef fmpq r
if fmpz_sgn(fmpq_numref(self.val)) >= 0:
return self
r = fmpq.__new__(fmpq)
fmpq_neg(r.val, self.val)
return r
def __add__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_add(r.val, (<fmpq>s).val, (<fmpq>t).val)
return r
def __radd__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_add(r.val, (<fmpq>t).val, (<fmpq>s).val)
return r
def __sub__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_sub(r.val, (<fmpq>s).val, (<fmpq>t).val)
return r
def __rsub__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_sub(r.val, (<fmpq>t).val, (<fmpq>s).val)
return r
def __mul__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_mul(r.val, (<fmpq>s).val, (<fmpq>t).val)
return r
def __rmul__(s, t):
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
return t
r = fmpq.__new__(fmpq)
fmpq_mul(r.val, (<fmpq>t).val, (<fmpq>s).val)
return r
@staticmethod
def _div_(s, t):
cdef fmpq r
s = any_as_fmpq(s)
if s is NotImplemented:
return s
t = any_as_fmpq(t)
if t is NotImplemented:
return t
if fmpq_is_zero((<fmpq>t).val):
raise ZeroDivisionError("fmpq division by zero")
r = fmpq.__new__(fmpq)
fmpq_div(r.val, (<fmpq>s).val, (<fmpq>t).val)
return r
def __truediv__(s, t):
return fmpq._div_(s, t)
def __rtruediv__(s, t):
return fmpq._div_(t, s)
def next(s, bint signed=True, bint minimal=True):
"""
Returns the next rational number after *s* as ordered by
minimal height (if *minimal* is True) or following the Calkin-Wilf
sequence (if *minimal* is False). If *signed* is set to False,
only the nonnegative rational numbers are considered.
>>> fmpq(23456789,98765432).next()
-23456789/98765432
>>> fmpq(23456789,98765432).next(signed=False)
98765432/23456789
>>> fmpq(23456789,98765432).next(signed=False, minimal=False)
98765432/75308643
>>> a, b, c, d = [fmpq(0)], [fmpq(0)], [fmpq(0)], [fmpq(0)]
>>> for i in range(20):
... a.append(a[-1].next())
... b.append(b[-1].next(signed=False))
... c.append(c[-1].next(minimal=False))
... d.append(d[-1].next(signed=False, minimal=False))
...
>>> a
[0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3, -3, 2/3, -2/3, 3/2, -3/2, 1/4, -1/4, 4, -4, 3/4, -3/4]
>>> b
[0, 1, 1/2, 2, 1/3, 3, 2/3, 3/2, 1/4, 4, 3/4, 4/3, 1/5, 5, 2/5, 5/2, 3/5, 5/3, 4/5, 5/4, 1/6]
>>> c
[0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3/2, -3/2, 2/3, -2/3, 3, -3, 1/4, -1/4, 4/3, -4/3, 3/5, -3/5]
>>> d
[0, 1, 1/2, 2, 1/3, 3/2, 2/3, 3, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4, 1/5, 5/4, 4/7, 7/3, 3/8]
"""
u = fmpq.__new__(fmpq)
if signed:
if minimal:
fmpq_next_signed_minimal((<fmpq>u).val, (<fmpq>s).val)
else:
fmpq_next_signed_calkin_wilf((<fmpq>u).val, (<fmpq>s).val)
else:
if fmpz_sgn(fmpq_numref(s.val)) < 0:
raise ValueError("s must be nonnegative")
if minimal:
fmpq_next_minimal((<fmpq>u).val, (<fmpq>s).val)
else:
fmpq_next_calkin_wilf((<fmpq>u).val, (<fmpq>s).val)
return u
@staticmethod
def bernoulli(ulong n, bint cache=False):
"""
Returns the Bernoulli number `B_n` as an *fmpq*.
>>> [fmpq.bernoulli(n) for n in range(8)]
[1, -1/2, 1/6, 0, -1/30, 0, 1/42, 0]
>>> fmpq.bernoulli(50)
495057205241079648212477525/66
If *cache* is set to *True*, all the Bernoulli numbers up to *n* are
computed and cached for fast subsequent retrieval. This feature should
be used with caution if *n* is large. Calling
:func:`ctx.cleanup()` frees cached Bernoulli numbers.
"""
if cache:
assert n <= 1000000
bernoulli_cache_compute(n+1)
u = fmpq.__new__(fmpq)
bernoulli_fmpq_ui((<fmpq>u).val, n)
return u
@staticmethod
def harmonic(ulong n):
"""
Returns the harmonic number `H_n` as an *fmpq*.
>>> [fmpq.harmonic(n) for n in range(6)]
[0, 1, 3/2, 11/6, 25/12, 137/60]
>>> fmpq.harmonic(50)
13943237577224054960759/3099044504245996706400
"""
cdef fmpq v = fmpq()
fmpq_harmonic_ui(v.val, n)
return v
@staticmethod
def dedekind_sum(n, k):
"""
Dedekind sum.
>>> fmpq.dedekind_sum(10, 3)
1/18
"""
cdef fmpz nv, kv
cdef fmpq v
nv = fmpz(n)
kv = fmpz(k)
v = fmpq()
fmpq_dedekind_sum(v.val, nv.val, kv.val)
return v
def floor(self):
"""
Floor function.
>>> fmpq(3,2).floor()
1
"""
cdef fmpz r = fmpz.__new__(fmpz)
fmpz_fdiv_q(r.val, fmpq_numref(self.val), fmpq_denref(self.val))
return r
def ceil(self):
"""
Ceiling function.
>>> fmpq(3,2).ceil()
2
"""
cdef fmpz r = fmpz.__new__(fmpz)
fmpz_cdiv_q(r.val, fmpq_numref(self.val), fmpq_denref(self.val))
return r
def trunc(self):
"""
Truncation function.
>>> fmpq(3,2).trunc()
1
>>> fmpq(-3,2).trunc()
-1
"""
cdef fmpz r = fmpz.__new__(fmpz)
fmpz_tdiv_q(r.val, fmpq_numref(self.val), fmpq_denref(self.val))
return r
def round(self, ndigits=None):
"""
Rounding function.
>>> fmpq(3,2).round()
2
>>> fmpq(-3,2).round()
-2
"""
from fractions import Fraction
fself = Fraction(int(self.p), int(self.q))
if ndigits is not None:
fround = round(fself, ndigits)
return fmpq(fround.numerator, fround.denominator)
else:
fround = round(fself)
return fmpz(fround)
def __hash__(self):
import sys
from fractions import Fraction
if sys.version_info < (3, 12):
return hash(Fraction(int(self.p), int(self.q), _normalize=False))
else:
return hash(Fraction._from_coprime_ints(int(self.p), int(self.q)))
def height_bits(self, bint signed=False):
"""
Returns the bit length of the maximum of the numerator and denominator.
With signed=True, returns the negative value if the number is
negative.
>>> fmpq(1001,5).height_bits()
10
>>> fmpq(-5,1001).height_bits(signed=True)
-10
"""
cdef long b1, b2
b1 = fmpz_bits(fmpq_numref(self.val))
b2 = fmpz_bits(fmpq_denref(self.val))
if signed and fmpz_sgn(fmpq_numref(self.val)) < 0:
return -max(b1, b2)
else:
return max(b1, b2)
def __pow__(self, n, z):
cdef fmpz_struct nval[1]
cdef int ntype = FMPZ_UNKNOWN
cdef fmpq v
cdef int success
cdef long e
assert z is None
ntype = fmpz_set_any_ref(nval, n)
if ntype == FMPZ_UNKNOWN:
return NotImplemented
if fmpq_is_zero((<fmpq>self).val) and fmpz_sgn(nval) == -1:
if ntype == FMPZ_TMP: fmpz_clear(nval)
raise ZeroDivisionError
v = fmpq.__new__(fmpq)
success = fmpq_pow_fmpz(v.val, (<fmpq>self).val, nval)
if ntype == FMPZ_TMP: fmpz_clear(nval)
if success:
return v
else:
raise OverflowError("fmpq_pow_fmpz(): exponent too large")
def sqrt(self):
"""
Return exact rational square root of self or raise an error.
>>> fmpq(9, 4).sqrt()
3/2
>>> fmpq(8).sqrt()
Traceback (most recent call last):
...
flint.utils.flint_exceptions.DomainError: not a square number
"""
return fmpq(self.numer().sqrt(), self.denom().sqrt())