diff --git a/abelian/utils.py b/abelian/utils.py index 695be09..ac5b9e3 100644 --- a/abelian/utils.py +++ b/abelian/utils.py @@ -1,10 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import itertools import functools +import itertools +import random import types + import numpy as np +from sympy import Integer, Float, Rational def mod(a, b): @@ -183,9 +186,39 @@ def copy_func(f): return g -argmin = functools.partial(arg, min_or_max = min) -argmax = functools.partial(arg, min_or_max = max) +argmin = functools.partial(arg, min_or_max=min) +argmax = functools.partial(arg, min_or_max=max) + + +def close(a, b): + numeric_types = (float, int, complex, Integer, Float, Rational) + if isinstance(a, numeric_types) and isinstance(a, numeric_types): + return abs(a - b) < 10e-10 + return sum(abs(i - j) for (i, j) in zip(a, b)) + + +def random_zero_heavy(low, high): + """ + Draw a random number, with approx 50% probability of zero. + """ + return random.choice(list(range(low, high)) + [0] * (high - low)) + + +def frob_norm(A, B): + """ + Frobenius norm. + """ + return sum(abs(i - j) for (i, j) in zip(A, B)) + + +def random_from_list(number, list_to_take_from): + """ + Draw several random values from the same list. + """ + return [random.choice(list_to_take_from) for i in range(number)] + if __name__ == "__main__": import doctest - doctest.testmod(verbose = True) \ No newline at end of file + + doctest.testmod(verbose=True) diff --git a/tests/test_FGA_factorizations.py b/tests/test_FGA_factorizations.py index 9e621cc..499b0d9 100644 --- a/tests/test_FGA_factorizations.py +++ b/tests/test_FGA_factorizations.py @@ -7,7 +7,7 @@ from sympy import Matrix from abelian.morphisms import HomLCA -from utils import random_zero_heavy +from abelian.utils import random_zero_heavy @pytest.fixture diff --git a/tests/test_HomLCA_real.py b/tests/test_HomLCA_real.py index 7dc2a83..883d0dc 100644 --- a/tests/test_HomLCA_real.py +++ b/tests/test_HomLCA_real.py @@ -9,7 +9,7 @@ from sympy import Matrix from abelian.morphisms import HomLCA, LCA -from utils import frob_norm +from abelian.utils import frob_norm @pytest.fixture diff --git a/tests/test_LCA.py b/tests/test_LCA.py index 1cb7aa8..a40216c 100644 --- a/tests/test_LCA.py +++ b/tests/test_LCA.py @@ -10,7 +10,7 @@ import pytest from abelian.groups import LCA -from utils import random_zero_heavy, random_from_list +from abelian.utils import random_zero_heavy, random_from_list def random_LCA(length): diff --git a/tests/test_examples.py b/tests/test_examples.py index 8236acc..e997f9a 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -9,7 +9,7 @@ from abelian import HomLCA, LCA from abelian import LCAFunc, voronoi -from utils import close +from abelian.utils import close def test_example_1_HomFGA(): diff --git a/tests/test_linalg/test_HomLCA.py b/tests/test_linalg/test_HomLCA.py index e2252c1..c39c819 100644 --- a/tests/test_linalg/test_HomLCA.py +++ b/tests/test_linalg/test_HomLCA.py @@ -6,7 +6,7 @@ from sympy import Matrix import pytest from abelian import LCA, HomLCA -from tests.utils import random_from_list +from abelian.utils import random_from_list @pytest.fixture diff --git a/tests/utils.py b/tests/utils.py deleted file mode 100644 index bb46c49..0000000 --- a/tests/utils.py +++ /dev/null @@ -1,31 +0,0 @@ -import random - -from sympy import Integer, Float, Rational - - -def close(a, b): - numeric_types = (float, int, complex, Integer, Float, Rational) - if isinstance(a, numeric_types) and isinstance(a, numeric_types): - return abs(a - b) < 10e-10 - return sum(abs(i - j) for (i, j) in zip(a, b)) - - -def random_zero_heavy(low, high): - """ - Draw a random number, with approx 50% probability of zero. - """ - return random.choice(list(range(low, high)) + [0] * (high - low)) - - -def frob_norm(A, B): - """ - Frobenius norm. - """ - return sum(abs(i - j) for (i, j) in zip(A, B)) - - -def random_from_list(number, list_to_take_from): - """ - Draw several random values from the same list. - """ - return [random.choice(list_to_take_from) for i in range(number)]