Skip to content

Commit

Permalink
DiagramGrid: Support any iterable to describe groups.
Browse files Browse the repository at this point in the history
Previously DiagramGrid would only agree if groups were supplied as
FiniteSet's of FiniteSet's.  This commit enables DiagramGrid to cope
with any iterable type.  The test added hereby shows how this can be
utilised.
  • Loading branch information
scolobb committed Aug 4, 2012
1 parent efb8858 commit 0d69ed4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
26 changes: 25 additions & 1 deletion sympy/categories/diagram_drawing.py
Expand Up @@ -87,6 +87,7 @@
NamedMorphism, Diagram)
from sympy.utilities import default_sort_key
from itertools import chain
from sympy.core.compatibility import iterable

class _GrowableGrid(object):
"""
Expand Down Expand Up @@ -787,15 +788,38 @@ def lay_out_group(group, local_hints):
else:
obj_groups[group] = group

def group_to_finiteset(group):
"""
Converts ``group`` to a :class:``FiniteSet`` if it is an
iterable.
"""
if iterable(group):
return FiniteSet(group)
else:
return group

obj_groups = {}
groups_grids = {}

# We would like to support various containers to represent
# groups. To achieve that, before laying each group out, it
# should be converted to a FiniteSet, because that is what the
# following code expects.

if isinstance(groups, dict) or isinstance(groups, Dict):
finiteset_groups = {}
for group, local_hints in groups.items():
finiteset_group = group_to_finiteset(group)
finiteset_groups[finiteset_group] = local_hints
lay_out_group(group, local_hints)
groups = finiteset_groups
else:
finiteset_groups = []
for group in groups:
lay_out_group(group, None)
finiteset_group = group_to_finiteset(group)
finiteset_groups.append(finiteset_group)
lay_out_group(finiteset_group, None)
groups = finiteset_groups

new_morphisms = []
for morphism in merged_morphisms:
Expand Down
26 changes: 26 additions & 0 deletions sympy/categories/tests/test_drawing.py
Expand Up @@ -446,6 +446,32 @@ def test_DiagramGrid():
assert grid[2, 5] == E
assert grid.morphisms == morphisms

# Test the five lemma with object grouping, but mixing containers
# to represent groups.
grid = DiagramGrid(d, [(A, B, C, D, E), set([A_, B_, C_, D_, E_])])

assert grid.width == 6
assert grid.height == 3
assert grid[0, 0] == A_
assert grid[0, 1] == B_
assert grid[0, 2] is None
assert grid[0, 3] == A
assert grid[0, 4] == B
assert grid[0, 5] is None
assert grid[1, 0] is None
assert grid[1, 1] == C_
assert grid[1, 2] == D_
assert grid[1, 3] is None
assert grid[1, 4] == C
assert grid[1, 5] == D
assert grid[2, 0] is None
assert grid[2, 1] is None
assert grid[2, 2] == E_
assert grid[2, 3] is None
assert grid[2, 4] is None
assert grid[2, 5] == E
assert grid.morphisms == morphisms

# Test the five lemma with object grouping and hints.
grid = DiagramGrid(d, {
FiniteSet(A, B, C, D, E): {"layout": "sequential",
Expand Down

0 comments on commit 0d69ed4

Please sign in to comment.