Skip to content

Commit

Permalink
Revert "gcd, lcm from standard library"
Browse files Browse the repository at this point in the history
This reverts commit 07e4648.
  • Loading branch information
nskeip committed Sep 17, 2021
1 parent 815a7b4 commit ef6b97e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/spectrum/calculations/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""
import itertools
import math

from spectrum.calculations import numeric
from spectrum.calculations.numeric import Integer
Expand Down Expand Up @@ -69,7 +68,7 @@ def _add_element(self, a):
l = len(self._vertices)
for i in range(l):
b = self._vertices[i]
d = math.gcd(a, b)
d = numeric.gcd(a, b)
if d == 1:
continue
bd = numeric.prime_part(b, d)
Expand Down
6 changes: 3 additions & 3 deletions src/spectrum/calculations/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
limitations under the License.
"""
import math
from functools import reduce

from .partition import Partitions
from spectrum.calculations import orders, spectra, numeric
from spectrum.calculations.numeric import Constraints, Integer
from spectrum.tools.tools import doc_inherit, ObjectCache
from .partition import Partitions


__author__ = 'Daniel Lytkin'

Expand Down Expand Up @@ -155,7 +155,7 @@ def apex(self):
n = self._degree
partitions = [x for x in Partitions(n) if (len(x) + n) % 2 == 0]
self._apex = numeric.sort_and_filter(
[reduce(math.lcm, partition) for partition in partitions])
[reduce(numeric.lcm, partition) for partition in partitions])
return self._apex

@doc_inherit
Expand Down
30 changes: 28 additions & 2 deletions src/spectrum/calculations/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@

__author__ = 'Daniel Lytkin'

# Module providing methods to calculate some number theoretic values.
# Module providing methods to calculate GCD and LCM etc.
from functools import reduce


def gcd(a, b):
"""Calculates greatest common divisor of two numbers.
Args:
a, b: Integers
Returns:
GCD(a, b)
"""
while b:
a, b = b, a % b
return a


def lcm(a, b):
"""Calculates least common multiple of two numbers.
Args:
a, b: Integers
Returns:
LCM(a, b)
"""
return a / gcd(a, b) * b


def prime_part(n, b):
"""Calculates b'-part of number n, which is the greatest divisor of n
coprime to d.
Expand All @@ -36,7 +62,7 @@ def prime_part(n, b):
Greatest divisor of n which is relatively prime to d.
"""
while True:
d = math.gcd(n, b)
d = gcd(n, b)
if d <= 1: break
n //= d
return n
Expand Down
4 changes: 1 addition & 3 deletions src/spectrum/calculations/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
limitations under the License.
"""
from math import gcd

from spectrum.calculations.numeric import Integer, prod
from spectrum.calculations.numeric import Integer, gcd, prod

__author__ = 'Daniel Lytkin'

Expand Down
6 changes: 3 additions & 3 deletions src/spectrum/calculations/semisimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"""
import itertools
import math
from functools import reduce

from spectrum.calculations import numeric
from spectrum.calculations.numeric import Integer
from spectrum.calculations.set import MaximalBoundedSets, FullBoundedSets, BoundedSets
from spectrum.tools.tools import ObjectCache
Expand All @@ -40,7 +40,7 @@ def __new__(cls, quotient=1, q=0, partition=None, signs=None,
if signs is None:
signs = []
class_ = SpectraElement if verbose else int
return int.__new__(class_, quotient * reduce(math.lcm,
return int.__new__(class_, quotient * reduce(numeric.lcm,
(q ** ni + ei for (ni, ei) in zip(partition, signs)), 1))

def __init__(self, quotient=1, q=0, partition=None, signs=None,
Expand Down Expand Up @@ -113,7 +113,7 @@ def lcm(self, other):
"""Returns lcm of this and other. 'q' must be the same. Quotients are
multiplied.
"""
elem = int.__new__(SpectraElement, math.lcm(self, other))
elem = int.__new__(SpectraElement, numeric.lcm(self, other))
quotient = self._quotient * other._quotient
elem.__init__(quotient=quotient, q=self._q,
partition=list(self._partition) + list(other._partition),
Expand Down
2 changes: 1 addition & 1 deletion src/spectrum/calculations/spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"""
import itertools
from math import gcd, lcm

from spectrum.calculations import numeric
from spectrum.calculations.numeric import gcd, lcm
from spectrum.calculations.semisimple import MixedElements, SemisimpleElements, SpectraElement
from spectrum.calculations.set import FullBoundedSets

Expand Down
4 changes: 2 additions & 2 deletions src/tests/spectrum_tests/calculations/numeric_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"""
import itertools
import unittest
from math import gcd, lcm

from spectrum.calculations.numeric import *
from spectrum_tests.parametric import parametrized, parameters

from spectrum.calculations.numeric import *

__author__ = 'Daniel Lytkin'

@parametrized
Expand Down
9 changes: 5 additions & 4 deletions src/tests/spectrum_tests/calculations/semisimple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"""
import itertools
import math
import unittest
from functools import reduce

from spectrum_tests.parametric import parameters, parametrized

from spectrum.calculations import numeric
from spectrum.calculations.partition import Partitions
from spectrum.calculations.semisimple import SemisimpleElements, MixedElements, SpectraElement
from spectrum_tests.parametric import parameters, parametrized

__author__ = 'Daniel Lytkin'

Expand All @@ -35,10 +36,10 @@ def evaluate(q, ni, ei=-1):
"""
try:
# for integer ei
return reduce(math.lcm, (q ** n + ei for n in ni))
return reduce(numeric.lcm, (q ** n + ei for n in ni))
except TypeError:
# for sequence ei
return reduce(math.lcm, (q ** n + e for (n, e) in zip(ni, ei)))
return reduce(numeric.lcm, (q ** n + e for (n, e) in zip(ni, ei)))


class Signs:
Expand Down
7 changes: 4 additions & 3 deletions src/tests/spectrum_tests/modules/max_orders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
limitations under the License.
"""
import math
import unittest

from spectrum_tests.parametric import parametrized, parameters

from spectrum.calculations import numeric
from spectrum.calculations.groups import ClassicalGroup
from spectrum.modules import max_orders
from spectrum_tests.parametric import parametrized, parameters

__author__ = 'Daniel Lytkin'

Expand All @@ -46,5 +47,5 @@ def test_symplectic(self, n):
@parameters(list(range(3, 100)))
def test_symplectic_gcd(self, n):
max_elems = max_orders.symplectic_2(n)
expected = math.gcd(*max_elems)
expected = numeric.gcd(*max_elems)
self.assertEqual(expected, max_orders.symplectic_2_gcd(n))

0 comments on commit ef6b97e

Please sign in to comment.