Skip to content

Commit

Permalink
Drop igcd(), use math.gcd()
Browse files Browse the repository at this point in the history
A partial fix for #839
  • Loading branch information
skirpichev committed Mar 6, 2021
1 parent 52bb6b5 commit bdd058e
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 82 deletions.
4 changes: 2 additions & 2 deletions diofant/__init__.py
Expand Up @@ -14,7 +14,7 @@
cacheit, comp, count_ops, diff, evaluate, expand,
expand_complex, expand_func, expand_log, expand_mul,
expand_multinomial, expand_power_base, expand_power_exp,
expand_trig, factor_nc, factor_terms, gcd_terms, igcd,
expand_trig, factor_nc, factor_terms, gcd_terms,
integer_digits, integer_nthroot, mod_inverse, nan, nfloat,
oo, pi, preorder_traversal, symbols, sympify, var,
vectorize, zoo)
Expand Down Expand Up @@ -179,7 +179,7 @@
'cacheit', 'comp', 'count_ops', 'diff', 'evaluate', 'expand',
'expand_complex', 'expand_func', 'expand_log', 'expand_mul',
'expand_multinomial', 'expand_power_base', 'expand_power_exp',
'expand_trig', 'factor_nc', 'factor_terms', 'gcd_terms', 'igcd',
'expand_trig', 'factor_nc', 'factor_terms', 'gcd_terms',
'integer_digits', 'integer_nthroot', 'mod_inverse', 'nan', 'nfloat',
'oo', 'pi', 'preorder_traversal', 'symbols', 'sympify', 'var',
'vectorize', 'zoo', 'ITE', 'And', 'Equivalent', 'Implies', 'Nand', 'Nor',
Expand Down
6 changes: 3 additions & 3 deletions diofant/core/__init__.py
Expand Up @@ -8,8 +8,8 @@
from .expr import Expr, AtomicExpr
from .symbol import Symbol, Wild, Dummy, symbols, var
from .numbers import (Number, Float, Rational, Integer,
NumberSymbol, igcd, E, I, nan, oo,
pi, zoo, comp, mod_inverse, integer_digits)
NumberSymbol, E, I, nan, oo, pi, zoo, comp,
mod_inverse, integer_digits)
from .power import Pow, integer_nthroot
from .mul import Mul
from .add import Add
Expand Down Expand Up @@ -37,7 +37,7 @@
__all__ = ('sympify', 'SympifyError', 'cacheit', 'Basic', 'Atom',
'preorder_traversal', 'S', 'Expr', 'AtomicExpr', 'Symbol',
'Wild', 'Dummy', 'symbols', 'var', 'Number', 'Float', 'Rational',
'Integer', 'NumberSymbol', 'igcd', 'E', 'I', 'nan', 'oo',
'Integer', 'NumberSymbol', 'E', 'I', 'nan', 'oo',
'pi', 'zoo', 'comp', 'mod_inverse', 'integer_digits', 'Pow',
'integer_nthroot', 'Mul', 'Add', 'Mod', 'Rel', 'Eq', 'Ne',
'Lt', 'Le', 'Gt', 'Ge', 'Equality', 'GreaterThan', 'LessThan',
Expand Down
8 changes: 4 additions & 4 deletions diofant/core/add.py
Expand Up @@ -6,7 +6,7 @@
from .cache import cacheit
from .compatibility import is_sequence
from .logic import _fuzzy_group
from .numbers import Integer, igcd, nan, oo, zoo
from .numbers import Integer, nan, oo, zoo
from .operations import AssocOp


Expand Down Expand Up @@ -724,10 +724,10 @@ def primitive(self):
terms.append((c.numerator, c.denominator, m))

if not inf:
ngcd = functools.reduce(igcd, [t[0] for t in terms], 0)
ngcd = functools.reduce(math.gcd, [t[0] for t in terms], 0)
dlcm = functools.reduce(math.lcm, [t[1] for t in terms], 1)
else:
ngcd = functools.reduce(igcd, [t[0] for t in terms if t[1]], 0)
ngcd = functools.reduce(math.gcd, [t[0] for t in terms if t[1]], 0)
dlcm = functools.reduce(math.lcm, [t[1] for t in terms if t[1]], 1)

if ngcd == dlcm == 1:
Expand Down Expand Up @@ -812,7 +812,7 @@ def as_content_primitive(self, radical=False):
# find the gcd of bases for each q
G = []
for q in common_q:
g = functools.reduce(igcd, [r[q] for r in rads], 0)
g = functools.reduce(math.gcd, [r[q] for r in rads], 0)
if g != 1:
G.append(root(g, q))
if G:
Expand Down
22 changes: 0 additions & 22 deletions diofant/core/numbers.py
Expand Up @@ -96,28 +96,6 @@ def _str_to_Decimal_dps(s):
return num, len(num.as_tuple().digits)


@cacheit
def igcd(*args):
"""Computes positive integer greatest common divisor.
Examples
========
>>> igcd(2, 4)
2
>>> igcd(5, 10, 15)
5
"""
args = map(as_int, args)
a = next(args)
for b in args:
if a == 1:
break
a = math.gcd(a, b)
return a


def igcdex(a, b):
"""Returns x, y, g such that g = x*a + y*b = gcd(a, b).
Expand Down
3 changes: 1 addition & 2 deletions diofant/integrals/transforms.py
Expand Up @@ -450,7 +450,6 @@ def _rewrite_gamma(f, s, a, b):
(([], []), ([], []), 1/2, 1, 8)
"""
from ..core import igcd
from ..functions import cos, cot, exp_polar, gamma, re, sin, tan
from ..polys import Poly, RootOf, roots

Expand Down Expand Up @@ -522,7 +521,7 @@ def left(c, is_numer):
s_multiplier = common_coefficient
else:
s_multiplier = common_coefficient \
* functools.reduce(igcd, [Integer(x.numerator) for x in s_multipliers])
* functools.reduce(math.gcd, [Integer(x.numerator) for x in s_multipliers])

exponent = Integer(1)
fac = Integer(1)
Expand Down
3 changes: 1 addition & 2 deletions diofant/ntheory/modular.py
Expand Up @@ -3,7 +3,6 @@

import mpmath

from ..core import igcd
from ..core.compatibility import as_int
from ..core.numbers import igcdex
from .primetest import isprime
Expand Down Expand Up @@ -216,7 +215,7 @@ def combine(c1, c2):
a1, m1 = c1
a2, m2 = c2
a, b, c = m1, a2 - a1, m2
g = functools.reduce(igcd, [a, b, c])
g = functools.reduce(math.gcd, [a, b, c])
a, b, c = [i//g for i in [a, b, c]]
if a != 1:
inv_a, _, g = igcdex(a, c)
Expand Down
4 changes: 2 additions & 2 deletions diofant/polys/polyroots.py
Expand Up @@ -4,7 +4,7 @@
import math

from ..core import (Dummy, Eq, Float, I, Integer, Rational, Symbol, comp,
factor_terms, igcd, pi, symbols)
factor_terms, pi, symbols)
from ..core.mul import expand_2arg
from ..core.sympify import sympify
from ..domains.compositedomain import CompositeDomain
Expand Down Expand Up @@ -646,7 +646,7 @@ def _integer_basis(poly):
monoms = monoms[:-1]
coeffs = coeffs[:-1]

divs = reversed(divisors(igcd(*coeffs))[1:])
divs = reversed(divisors(math.gcd(*coeffs))[1:])

try:
div = next(divs)
Expand Down
5 changes: 3 additions & 2 deletions diofant/simplify/trigsimp.py
@@ -1,9 +1,10 @@
import functools
import math
from collections import defaultdict

from ..core import (Add, Basic, Dummy, E, Expr, FunctionClass, I, Integer, Mul,
Pow, Rational, Wild, cacheit, count_ops, expand,
expand_mul, factor_terms, igcd, symbols)
expand_mul, factor_terms, symbols)
from ..core.compatibility import iterable
from ..core.function import _mexpand
from ..core.strategies import greedy, identity
Expand Down Expand Up @@ -289,7 +290,7 @@ def analyse_gens(gens, hints):
# from this list.
fns = [x[1] for x in val]
val = [x[0] for x in val]
gcd = functools.reduce(igcd, val)
gcd = functools.reduce(math.gcd, val)
terms = [(fn, v/gcd) for (fn, v) in zip(fns, val)]
fs = set(funcs + fns)
for c, s, t in ([cos, sin, tan], [cosh, sinh, tanh]):
Expand Down
6 changes: 3 additions & 3 deletions diofant/solvers/diophantine.py
@@ -1,6 +1,6 @@
import math

from ..core import (Add, Eq, Integer, Rational, Symbol, factor_terms, igcd,
from ..core import (Add, Eq, Integer, Rational, Symbol, factor_terms,
integer_nthroot, oo, symbols, sympify)
from ..core.assumptions import check_assumptions
from ..core.compatibility import as_int, is_sequence
Expand Down Expand Up @@ -48,9 +48,9 @@ def _sorted_tuple(*i):

def _remove_gcd(*x):
try:
g = igcd(*x)
g = math.gcd(*x)
return tuple(i//g for i in x)
except ValueError:
except (TypeError, ValueError):
return x


Expand Down
26 changes: 1 addition & 25 deletions diofant/tests/core/test_numbers.py
Expand Up @@ -7,7 +7,7 @@

from diofant import (Catalan, E, EulerGamma, Float, Ge, GoldenRatio, Gt, I,
Integer, Le, Lt, Mul, Number, Pow, Rational, Symbol, cbrt,
comp, cos, exp, factorial, false, igcd, integer_digits,
comp, cos, exp, factorial, false, integer_digits,
integer_nthroot, latex, log, mod_inverse, nan, nextprime,
oo, pi, root, sin, sqrt, true, zoo)
from diofant.core.cache import clear_cache
Expand Down Expand Up @@ -169,30 +169,6 @@ def test_divmod():
assert divmod(Integer(4), Rational(f)) == divmod(4, f)


def test_igcd():
assert igcd(0, 0) == 0
assert igcd(0, 1) == 1
assert igcd(1, 0) == 1
assert igcd(0, 7) == 7
assert igcd(7, 0) == 7
assert igcd(7, 1) == 1
assert igcd(1, 7) == 1
assert igcd(-1, 0) == 1
assert igcd(0, -1) == 1
assert igcd(-1, -1) == 1
assert igcd(-1, 7) == 1
assert igcd(7, -1) == 1
assert igcd(8, 2) == 2
assert igcd(4, 8) == 4
assert igcd(8, 16) == 8
assert igcd(7, -3) == 1
assert igcd(-7, 3) == 1
assert igcd(-7, -3) == 1
assert igcd(*[10, 20, 30]) == 10
pytest.raises(ValueError, lambda: igcd(45.1, 30))
pytest.raises(ValueError, lambda: igcd(45, 30.1))


def test_igcdex():
assert igcdex(0, 0) == (0, 1, 0)
assert igcdex(-2, 0) == (-1, 0, 2)
Expand Down
20 changes: 10 additions & 10 deletions diofant/tests/test_wester.py
Expand Up @@ -6,6 +6,7 @@
each tested system.
"""
import itertools
import math

import pytest

Expand All @@ -26,15 +27,14 @@
exp, expand, expand_func, eye, factor, factorial,
factorial2, factorint, fibonacci, floor,
fourier_transform, gamma, gcd, groebner, hessian, hyper,
hyperexpand, igcd, im, integrate,
inverse_laplace_transform, laplace_transform,
legendre_poly, limit, log, logcombine, maximize,
mellin_transform, minimize, nan, npartitions, oo, pi,
polygamma, polylog, powdenest, powsimp, primerange,
primitive, primitive_root, product, radsimp, re,
reduce_inequalities, residue, resultant, rf, root, rsolve,
sec, series, sign, simplify, sin, sinh, solve, sqrt,
sqrtdenest, summation, symbols, tan, tanh, totient,
hyperexpand, im, integrate, inverse_laplace_transform,
laplace_transform, legendre_poly, limit, log, logcombine,
maximize, mellin_transform, minimize, nan, npartitions,
oo, pi, polygamma, polylog, powdenest, powsimp,
primerange, primitive, primitive_root, product, radsimp,
re, reduce_inequalities, residue, resultant, rf, root,
rsolve, sec, series, sign, simplify, sin, sinh, solve,
sqrt, sqrtdenest, summation, symbols, tan, tanh, totient,
trigsimp, trunc, wronskian, zeta, zoo)
from diofant.abc import a, b, c, s, t, w, x, y, z
from diofant.functions.combinatorial.numbers import stirling
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_C8():


def test_C9():
assert igcd(1776, 1554, 5698) == 74
assert math.gcd(1776, 1554, 5698) == 74


def test_C10():
Expand Down
4 changes: 0 additions & 4 deletions docs/modules/core.rst
Expand Up @@ -115,10 +115,6 @@ NumberSymbol
.. autoclass:: NumberSymbol
:members:

igcd
^^^^
.. autofunction:: igcd

mod_inverse
^^^^^^^^^^^
.. autofunction:: mod_inverse
Expand Down
2 changes: 1 addition & 1 deletion docs/release/notes-0.13.rst
Expand Up @@ -19,7 +19,7 @@ Compatibility breaks
* Renamed ``Ring`` as :class:`~diofant.domains.ring.CommutativeRing`, see :pull:`1123`.
* Removed support for Python 3.7 and 3.8, see :pull:`1118` and :pull:`1124`.
* ``FiniteRing`` renamed to :class:`~diofant.domains.IntegerModRing`, see :pull:`1124`.
* Removed ``ilcm()`` and ``prod()`` functions, see :pull:`1125`.
* Removed ``igcd()``, ``ilcm()`` and ``prod()`` functions, see :pull:`1125`.

Minor changes
=============
Expand Down

0 comments on commit bdd058e

Please sign in to comment.