# Path: openmodes/helpers.py
# def equivalence(relations):
#     """Determine the equivalence classes between objects
# 
#     Following numerical recipes section 8.6
# 
#     Parameters
#     ----------
#     relations: list
#         Each element of the list is a tuple containing the identities of two
#         equivalent items. Each item can be any hashable type
# 
#     Returns
#     -------
#     class_items: list of set
#         Each set
#     """
# 
#     # first put each item in its own equivalence class
#     classes = {}
#     for j, k in relations:
#         classes[j] = j
#         classes[k] = k
# 
#     for relation in relations:
#         j, k = relation
# 
#         # track the anscestor of each
#         while classes[j] != j:
#             j = classes[j]
# 
#         while classes[k] != k:
#             k = classes[k]
# 
#         # if not already related, then relate items
#         if j != k:
#             classes[j] = k
# 
#     # The final sweep
#     for j in classes.keys():
#         while classes[j] != classes[classes[j]]:
#             classes[j] = classes[classes[j]]
# 
#     # Now reverse the storage arrangement, so that all items of the same
#     # class are grouped together into a set
#     classes_reverse = defaultdict(set)
#     for item, item_class in classes.items():
#         classes_reverse[item_class].add(item)
# 
#     # the class names are arbitrary, so just return the list of sets
#     return list(classes_reverse.values())


from openmodes.helpers import equivalence
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
#  OpenModes - An eigenmode solver for open electromagnetic resonantors
#  Copyright (C) 2013 David Powell
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------

from __future__ import print_function


def test_equivalence():
    "Tests for equivalence class code"

    relations1 = ((0, 1), (1, 2), (2, 3), (3, 0),
                 (7, 12), (12, 9), (9, 4))

# Path: openmodes/helpers.py
# def equivalence(relations):
#     """Determine the equivalence classes between objects
# 
#     Following numerical recipes section 8.6
# 
#     Parameters
#     ----------
#     relations: list
#         Each element of the list is a tuple containing the identities of two
#         equivalent items. Each item can be any hashable type
# 
#     Returns
#     -------
#     class_items: list of set
#         Each set
#     """
# 
#     # first put each item in its own equivalence class
#     classes = {}
#     for j, k in relations:
#         classes[j] = j
#         classes[k] = k
# 
#     for relation in relations:
#         j, k = relation
# 
#         # track the anscestor of each
#         while classes[j] != j:
#             j = classes[j]
# 
#         while classes[k] != k:
#             k = classes[k]
# 
#         # if not already related, then relate items
#         if j != k:
#             classes[j] = k
# 
#     # The final sweep
#     for j in classes.keys():
#         while classes[j] != classes[classes[j]]:
#             classes[j] = classes[classes[j]]
# 
#     # Now reverse the storage arrangement, so that all items of the same
#     # class are grouped together into a set
#     classes_reverse = defaultdict(set)
#     for item, item_class in classes.items():
#         classes_reverse[item_class].add(item)
# 
#     # the class names are arbitrary, so just return the list of sets
#     return list(classes_reverse.values())


# Path: test/test_helpers.py
from openmodes.helpers import equivalence

# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
#  OpenModes - An eigenmode solver for open electromagnetic resonantors
#  Copyright (C) 2013 David Powell
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------

from __future__ import print_function


def test_equivalence():
    "Tests for equivalence class code"

    relations1 = ((0, 1), (1, 2), (2, 3), (3, 0),
                 (7, 12), (12, 9), (9, 4))

    equiv1 = equivalence(relations1)