Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add pretty printing to Diagram.

This pretty printing largely relies on the pretty printing routines for
Dict, Morphism, and Object.
  • Loading branch information...
commit 0a2e1320d584a428954b6d4448927acd8b7516f7 1 parent 47403e4
@scolobb authored
View
12 sympy/printing/latex.py
@@ -1208,6 +1208,18 @@ def _print_Morphism(self, morphism):
def _print_Category(self, morphism):
return "\\mathbf{%s}" % self._print(Symbol(morphism.name))
+ def _print_Diagram(self, diagram):
+ if not diagram.premises:
+ # This is an empty diagram.
+ return self._print(S.EmptySet)
+
+ latex_result = self._print(diagram.premises)
+ if diagram.conclusions:
+ latex_result += "\\Longrightarrow %s" % \
+ self._print(diagram.conclusions)
+
+ return latex_result
+
def latex(expr, **settings):
r"""
Convert the given expression to LaTeX representation.
View
16 sympy/printing/pretty/pretty.py
@@ -1480,6 +1480,22 @@ def _print_Morphism(self, morphism):
def _print_Category(self, category):
return self._print(pretty_symbol(category.name))
+ def _print_Diagram(self, diagram):
+ if not diagram.premises:
+ # This is an empty diagram.
+ return self._print(S.EmptySet)
+
+ pretty_result = self._print(diagram.premises)
+ if diagram.conclusions:
+ results_arrow = " ==> "
+ if self._use_unicode:
+ results_arrow = u" \u27f9 "
+
+ pretty_conclusions = self._print(diagram.conclusions)[0]
+ pretty_result = pretty_result.right(results_arrow, pretty_conclusions)
+
+ return prettyForm(pretty_result[0])
+
def pretty(expr, **settings):
"""Returns a string containing the prettified form of expr.
View
21 sympy/printing/pretty/tests/test_pretty.py
@@ -3693,7 +3693,7 @@ def test_complicated_symbol_unchanged():
assert pretty(Symbol(symb_name)) == symb_name
def test_categories():
- from sympy.categories import Object, Morphism, Category
+ from sympy.categories import Object, Morphism, Category, Diagram
A1 = Object("A1")
A2 = Object("A2")
A3 = Object("A3")
@@ -3730,3 +3730,22 @@ def test_categories():
h = Morphism(A2, A3, "f") * Morphism(A1, A2, "")
assert pretty(h) == "A1->A3"
assert upretty(h) == u"A₁→A₃"
+
+ # Test how diagrams are printed.
+ d = Diagram()
+ assert pretty(d) == "EmptySet()"
+ assert upretty(d) == u""
+
+ d = Diagram({f1:"unique", f2:S.EmptySet})
+ assert pretty(d) == "{A1->A1: EmptySet(), A2->A2: EmptySet(), " \
+ "A3->A3: EmptySet(), f1:A1->A2: {unique}, f2*f1:A1->A3: " \
+ "EmptySet(), f2:A2->A3: EmptySet()}"
+ assert upretty(d) == u"{A₁→A₁: ∅, A₂→A₂: ∅, A₃→A₃: ∅, " \
+ u"f₁:A₁→A₂: {unique}, f₂∘f₁:A₁→A₃: ∅, f₂:A₂→A₃: ∅}"
+
+ d = Diagram({f1:"unique", f2:S.EmptySet}, {f2 * f1: "unique"})
+ assert pretty(d) == "{A1->A1: EmptySet(), A2->A2: EmptySet(), " \
+ "A3->A3: EmptySet(), f1:A1->A2: {unique}, f2*f1:A1->A3: " \
+ "EmptySet(), f2:A2->A3: EmptySet()} ==> {f2*f1:A1->A3: {unique}}"
+ assert upretty(d) == u"{A₁→A₁: ∅, A₂→A₂: ∅, A₃→A₃: ∅, f₁:A₁→A₂: {unique}," \
+ u" f₂∘f₁:A₁→A₃: ∅, f₂:A₂→A₃: ∅} ⟹ {f₂∘f₁:A₁→A₃: {unique}}"
View
23 sympy/printing/tests/test_latex.py
@@ -571,7 +571,7 @@ def test_PolynomialRing():
r"S_<^{-1}\mathbb{Q}\left[x, y\right]"
def test_categories():
- from sympy.categories import Object, Morphism, Category
+ from sympy.categories import Object, Morphism, Category, Diagram
A1 = Object("A1")
A2 = Object("A2")
A3 = Object("A3")
@@ -597,3 +597,24 @@ def test_categories():
h = Morphism(A2, A3, "f") * Morphism(A1, A2, "")
assert latex(h) == "A_{1}\\rightarrow A_{3}"
+
+ d = Diagram()
+ assert latex(d) == "\emptyset"
+
+ d = Diagram({f1:"unique", f2:S.EmptySet})
+ assert latex(d) == "\\begin{Bmatrix}A_{1}\\rightarrow A_{1} :" \
+ " \\emptyset, & A_{2}\\rightarrow A_{2} : \\emptyset, " \
+ "& A_{3}\\rightarrow A_{3} : \\emptyset, & f_{1}:A_{1}" \
+ "\\rightarrow A_{2} : \\left\\{unique\\right\\}, & f_{2}\\circ" \
+ " f_{1}:A_{1}\\rightarrow A_{3} : \\emptyset, & f_{2}:A_{2}" \
+ "\\rightarrow A_{3} : \\emptyset\\end{Bmatrix}"
+
+ d = Diagram({f1:"unique", f2:S.EmptySet}, {f2 * f1: "unique"})
+ assert latex(d) == "\\begin{Bmatrix}A_{1}\\rightarrow A_{1} :" \
+ " \\emptyset, & A_{2}\\rightarrow A_{2} : \\emptyset, " \
+ "& A_{3}\\rightarrow A_{3} : \\emptyset, & f_{1}:A_{1}" \
+ "\\rightarrow A_{2} : \\left\\{unique\\right\\}, & f_{2}\\circ" \
+ " f_{1}:A_{1}\\rightarrow A_{3} : \\emptyset, & f_{2}:A_{2}" \
+ "\\rightarrow A_{3} : \\emptyset\\end{Bmatrix}\Longrightarrow " \
+ "\\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}\\rightarrow A_{3} : " \
+ "\\left\\{unique\\right\\}\\end{Bmatrix}"
Please sign in to comment.
Something went wrong with that request. Please try again.