Skip to content

Commit

Permalink
Merge pull request #250 from oscarbenjamin/pr_fmpq_gcd
Browse files Browse the repository at this point in the history
fmpq: Add fmpq.gcd() method
  • Loading branch information
oscarbenjamin authored Jan 14, 2025
2 parents 7c154a4 + 890f020 commit 7b2c76d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,10 @@ def test_fmpq():
assert raises(lambda: Q(1,2) / Q(0), ZeroDivisionError)
assert raises(lambda: Q(1,2) / 0, ZeroDivisionError)

assert Q(2,3).gcd(Q(4,9)) == Q(2,9)
assert Q(2,3).gcd(5) == Q(1,3)
assert raises(lambda: Q(2,3).gcd([]), TypeError)

assert Q(5,3).floor() == flint.fmpz(1)
assert Q(-5,3).floor() == flint.fmpz(-2)
assert Q(5,3).ceil() == flint.fmpz(2)
Expand Down
18 changes: 18 additions & 0 deletions src/flint/types/fmpq.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ cdef class fmpq(flint_scalar):
def __rtruediv__(s, t):
return fmpq._div_(t, s)

def gcd(s, t):
"""GCD of two rational numbers.
>>> fmpq(1,2).gcd(fmpq(3,4))
1/4
The GCD is defined as the GCD of the numerators divided by the LCM of
the denominators. This is consistent with ``fmpz.gcd()`` but not with
``fmpq_poly.gcd()``.
"""
cdef fmpq r
t = any_as_fmpq(t)
if t is NotImplemented:
raise TypeError("fmpq expected")
r = fmpq.__new__(fmpq)
fmpq_gcd(r.val, (<fmpq>s).val, (<fmpq>t).val)
return r

def next(s, bint signed=True, bint minimal=True):
"""
Returns the next rational number after *s* as ordered by
Expand Down

0 comments on commit 7b2c76d

Please sign in to comment.