Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DiagramGrid: Handle the situations when growing a pseudopod fails.

While I initially supposed that reaching such situations would be
difficult, it proved quite easy to get there by setting up a "star"
diagram, with many morphisms coming out of a central object.  This
commit adds the code to handle such situations and actually lay out all
objects.
  • Loading branch information...
commit 7821c422b1abf901626c5f8a5d8631a70d1398e4 1 parent 5a3d764
@scolobb authored
View
33 sympy/categories/diagram_drawing.py
@@ -749,6 +749,9 @@ def good_triangle(tri):
return other_obj
+ # This diagram is actually cooler that I can handle. Fail cowardly.
+ return None
+
@staticmethod
def _handle_groups(diagram, groups, merged_morphisms, hints):
"""
@@ -920,9 +923,35 @@ def _generic_layout(diagram, merged_morphisms):
new_obj = DiagramGrid._grow_pseudopod(
triangles, fringe, grid, skeleton, placed_objects)
- placed_objects.add(new_obj)
+ if not new_obj:
+ # No more triangles can be attached, not even by
+ # the edge. We will set up a new diagram out of
+ # what has been left, laid it out independently,
+ # and then attach it to this one.
+
+ remaining_objects = all_objects - placed_objects
+
+ remaining_diagram = diagram.subdiagram_from_objects(
+ FiniteSet(remaining_objects))
+ remaining_grid = DiagramGrid(remaining_diagram)
- # Now, hopefully, a new welding will be found.
+ # Now, let's glue ``remaining_grid`` to ``grid``.
+ final_width = grid.width + remaining_grid.width
+ final_height = max(grid.height, remaining_grid.height)
+ final_grid = _GrowableGrid(final_width, final_height)
+
+ for i in xrange(grid.width):
+ for j in xrange(grid.height):
+ final_grid[i, j] = grid[i, j]
+
+ start_j = grid.width
+ for i in xrange(remaining_grid.height):
+ for j in xrange(remaining_grid.width):
+ final_grid[i, start_j + j] = remaining_grid[i, j]
+
+ return final_grid
+
+ placed_objects.add(new_obj)
triangles = DiagramGrid._drop_irrelevant_triangles(
triangles, placed_objects)
View
39 sympy/categories/tests/test_drawing.py
@@ -522,3 +522,42 @@ def test_DiagramGrid():
assert grid.height == 1
assert grid[0, 0] == A
assert grid[0, 1] == B
+
+ # Test a diagram in which even growing a pseudopod does not
+ # eventually help.
+ F = Object("F")
+ f1 = NamedMorphism(A, B, "f1")
+ f2 = NamedMorphism(A, C, "f2")
+ f3 = NamedMorphism(A, D, "f3")
+ f4 = NamedMorphism(A, E, "f4")
+ f5 = NamedMorphism(A, A_, "f5")
+ f6 = NamedMorphism(A, B_, "f6")
+ f7 = NamedMorphism(A, C_, "f7")
+ f8 = NamedMorphism(A, D_, "f8")
+ f9 = NamedMorphism(A, E_, "f9")
+ f10 = NamedMorphism(A, F, "f10")
+ d = Diagram([f1, f2, f3, f4, f5, f6, f7, f8, f9, f10])
+ grid = DiagramGrid(d)
+
+ assert grid.width == 5
+ assert grid.height == 3
+ assert grid[0, 0] == E
+ assert grid[0, 1] == C
+ assert grid[0, 2] == C_
+ assert grid[0, 3] == E_
+ assert grid[0, 4] == F
+ assert grid[1, 0] == D
+ assert grid[1, 1] == A
+ assert grid[1, 2] == A_
+ assert grid[1, 3] is None
+ assert grid[1, 4] is None
+ assert grid[2, 0] == D_
+ assert grid[2, 1] == B
+ assert grid[2, 2] == B_
+ assert grid[2, 3] is None
+ assert grid[2, 4] is None
+
+ morphisms = {}
+ for f in [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]:
+ morphisms[f] = FiniteSet()
+ assert grid.morphisms == morphisms
Please sign in to comment.
Something went wrong with that request. Please try again.