Skip to content

Commit

Permalink
moving tests.utils to abelian.utils
Browse files Browse the repository at this point in the history
  • Loading branch information
VascoSch92 committed Oct 8, 2023
1 parent 1682ea7 commit a743f2c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
41 changes: 37 additions & 4 deletions abelian/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)

doctest.testmod(verbose=True)
2 changes: 1 addition & 1 deletion tests/test_FGA_factorizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_HomLCA_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_LCA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_linalg/test_HomLCA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions tests/utils.py

This file was deleted.

0 comments on commit a743f2c

Please sign in to comment.