Permalink
Browse files

Add pretty printing for Object and Morphism.

Pretty printing supports anonymous objects and morphisms.  Also add the
corresponding tests.
  • Loading branch information...
1 parent ff04329 commit 2e812319faa6415a28c160e3bf11453725343a83 @scolobb committed Jun 3, 2012
Showing with 74 additions and 0 deletions.
  1. +37 −0 sympy/printing/pretty/pretty.py
  2. +37 −0 sympy/printing/pretty/tests/test_pretty.py
@@ -1446,6 +1446,43 @@ def _print_DMP(self, p):
def _print_DMF(self, p):
return self._print_DMP(p)
+ def _print_Object(self, object):
+ if object.name:
+ return self._print(pretty_symbol(object.name))
+ else:
+ if self._use_unicode:
+ return self._print(u"\u2022")
+ else:
+ return self._print(".")
+
+ def _print_Morphism(self, morphism):
+ arrow = "->"
+ circle = "*"
+ if self._use_unicode:
+ arrow = u"\u2192"
+ circle = u"\u2218"
+
+ domain = self._print(morphism.domain)
+ codomain = self._print(morphism.codomain)
+ tail = domain.right(arrow, codomain)[0]
+
+ pretty_name = morphism.name
+ if pretty_name:
+ pretty_name = pretty_symbol(pretty_name)
+ else:
+ for component in reversed(morphism.components):
+ if not component.name:
+ # Composition with an anonymous morphism is an
+ # anonymous morphism.
+ return prettyForm(tail)
+
+ pretty_name += pretty_symbol(component.name) + circle
+
+ pretty_name = pretty_name[:-1]
+
+ pretty_name_form = self._print(pretty_name)
+ return prettyForm(pretty_name_form.right(":", tail)[0])
+
def pretty(expr, **settings):
"""Returns a string containing the prettified form of expr.
@@ -3691,3 +3691,40 @@ def test_issue_3186():
def test_complicated_symbol_unchanged():
for symb_name in ["dexpr2_d1tau", "dexpr2^d1tau"]:
assert pretty(Symbol(symb_name)) == symb_name
+
+def test_categories():
+ from sympy.categories import Object, Morphism
+ A1 = Object("A1")
+ A2 = Object("A2")
+ A3 = Object("A3")
+
+ f1 = Morphism(A1, A2, "f1")
+ f2 = Morphism(A2, A3, "f2")
+ anonymous = Morphism(A1, A3, "")
+
+ assert pretty(A1) == "A1"
+ assert upretty(A1) == u"A₁"
+
+ assert pretty(f1) == "f1:A1->A2"
+ assert upretty(f1) == u"f₁:A₁→A₂"
+ assert pretty(anonymous) == "A1->A3"
+ assert upretty(anonymous) == u"A₁→A₃"
+
+ assert pretty(f2*f1) == "f2*f1:A1->A3"
+ assert upretty(f2*f1) == u"f₂∘f₁:A₁→A₃"
+
+ # Some further tests for anonymous objects and morphisms.
+ assert pretty(Object("")) == "."
+ assert upretty(Object("")) == u""
+
+ h = Morphism(A2, A3, "").compose(Morphism(A1, A2, ""), "h")
+ assert pretty(h) == "h:A1->A3"
+ assert upretty(h) == u"h:A₁→A₃"
+
+ h = Morphism(A2, A3, "") * Morphism(A1, A2, "")
+ assert pretty(h) == "A1->A3"
+ assert upretty(h) == u"A₁→A₃"
+
+ h = Morphism(A2, A3, "f") * Morphism(A1, A2, "")
+ assert pretty(h) == "A1->A3"
+ assert upretty(h) == u"A₁→A₃"

0 comments on commit 2e81231

Please sign in to comment.