Skip to content

Commit

Permalink
DiagramGrid: Don't use "loop" morphisms in laying out objects.
Browse files Browse the repository at this point in the history
"Loop" morphisms are morphisms which have the same domains and
codomains.  They will be eventually drawn as loops and should not
influence the layout of the diagram.
  • Loading branch information
scolobb committed Aug 4, 2012
1 parent cbfdf55 commit de5a5b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions sympy/categories/diagram_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,25 @@ def place_objects(pt, placed_objects):

return grid

@staticmethod
def _drop_inessential_morphisms(merged_morphisms):
r"""
Removes those morphisms which should appear in the diagram,
but which have no relevance to object layout.
Currently this removes "loop" morphisms: the non-identity
morphisms with the same domains and codomains.
"""
morphisms = [m for m in merged_morphisms if m.domain != m.codomain]
return morphisms

def __init__(self, diagram, groups=None, **hints):
premises = DiagramGrid._simplify_morphisms(diagram.premises)
conclusions = DiagramGrid._simplify_morphisms(diagram.conclusions)
merged_morphisms = DiagramGrid._merge_premises_conclusions(
all_merged_morphisms = DiagramGrid._merge_premises_conclusions(
premises, conclusions)
merged_morphisms = DiagramGrid._drop_inessential_morphisms(
all_merged_morphisms)

if groups and (groups != diagram.objects):
# Lay out the diagram according to the groups.
Expand All @@ -1023,7 +1037,7 @@ def __init__(self, diagram, groups=None, **hints):
self._grid = grid

# Store the merged morphisms for later use.
self._morphisms = merged_morphisms
self._morphisms = all_merged_morphisms

@property
def width(self):
Expand Down
13 changes: 13 additions & 0 deletions sympy/categories/tests/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ def test_DiagramGrid():
assert grid.morphisms == {f:FiniteSet(), g:FiniteSet(),
g * f:FiniteSet("unique")}

# A triangle with a "loop" morphism.
l_A = NamedMorphism(A, A, "l_A")
d = Diagram([f, g, l_A])
grid = DiagramGrid(d)

assert grid.width == 2
assert grid.height == 2
assert grid[0, 0] == A
assert grid[0, 1] == B
assert grid[1, 0] is None
assert grid[1, 1] == C
assert grid.morphisms == {f:FiniteSet(), g:FiniteSet(), l_A:FiniteSet()}

# A simple diagram.
d = Diagram([f, g, h, k])
grid = DiagramGrid(d)
Expand Down

0 comments on commit de5a5b2

Please sign in to comment.