Permalink
Browse files

Update the printing system to fit the new Morphism class hierarchy.

Printing has become easier with the new class hierarchy.  This commit
also updates the relevant tests.
  • Loading branch information...
1 parent 6de72a1 commit 8eecc16d544ce4496e33a39ec702b3f36dc79c7c @scolobb committed Jun 14, 2012
View
@@ -1187,23 +1187,33 @@ def _print_Object(self, object):
def _print_Morphism(self, morphism):
domain = self._print(morphism.domain)
codomain = self._print(morphism.codomain)
- tail = "%s\\rightarrow %s" % (domain, codomain)
-
- pretty_name = morphism.name
- if pretty_name:
- pretty_name = self._print(Symbol(pretty_name))
- else:
- for component in reversed(morphism.components):
- if not component.name:
- # Composition with an anonymous morphism is an
- # anonymous morphism.
- return tail
-
- pretty_name += self._print(Symbol(component.name)) + "\\circ "
-
- pretty_name = pretty_name[:-6]
-
- return "%s:%s" % (pretty_name, tail)
+ return "%s\\rightarrow %s" % (domain, codomain)
+
+ def _print_NamedMorphism(self, morphism):
+ pretty_name = self._print(Symbol(morphism.name))
+ pretty_morphism = self._print_Morphism(morphism)
+ return "%s:%s" % (pretty_name, pretty_morphism)
+
+ def _print_IdentityMorphism(self, morphism):
+ from sympy.categories import NamedMorphism
+ return self._print_NamedMorphism(NamedMorphism(
+ morphism.domain, morphism.codomain, "id"))
+
+ def _print_CompositeMorphism(self, morphism):
+ from sympy.categories import NamedMorphism
+
+ component_names = ""
+ if all([isinstance(component, NamedMorphism) for component in \
+ morphism.components]):
+ # All components of the morphism have names and it is thus
+ # possible to build the name of the composite.
+ component_names_list = [self._print(Symbol(component.name)) for \
+ component in morphism.components]
+ component_names_list.reverse()
+ component_names = "\\circ ".join(component_names_list) + ":"
+
+ pretty_morphism = self._print_Morphism(morphism)
+ return component_names + pretty_morphism
def _print_Category(self, morphism):
return "\\mathbf{%s}" % self._print(Symbol(morphism.name))
@@ -1451,7 +1451,6 @@ def _print_Object(self, object):
def _print_Morphism(self, morphism):
arrow = "->"
- circle = "*"
if self._use_unicode:
arrow = u"\u2192"
circle = u"\u2218"
@@ -1460,22 +1459,38 @@ def _print_Morphism(self, morphism):
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)
+ return prettyForm(tail)
+
+ def _print_NamedMorphism(self, morphism):
+ pretty_name = self._print(pretty_symbol(morphism.name))
+ pretty_morphism = self._print_Morphism(morphism)
+ return prettyForm(pretty_name.right(":", pretty_morphism)[0])
+
+ def _print_IdentityMorphism(self, morphism):
+ from sympy.categories import NamedMorphism
+ return self._print_NamedMorphism(
+ NamedMorphism(morphism.domain, morphism.codomain, "id"))
- pretty_name += pretty_symbol(component.name) + circle
+ def _print_CompositeMorphism(self, morphism):
+ from sympy.categories import NamedMorphism
- pretty_name = pretty_name[:-1]
+ circle = "*"
+ if self._use_unicode:
+ circle = u"\u2218"
- pretty_name_form = self._print(pretty_name)
- return prettyForm(pretty_name_form.right(":", tail)[0])
+ component_names = ""
+ if all([isinstance(component, NamedMorphism) for component in \
+ morphism.components]):
+ # All components of the morphism have names and it is thus
+ # possible to build the name of the composite.
+ component_names_list = [pretty_symbol(component.name) for \
+ component in morphism.components]
+ component_names_list.reverse()
+ component_names = circle.join(component_names_list) + ":"
+
+ pretty_name = self._print(component_names)
+ pretty_morphism = self._print_Morphism(morphism)
+ return prettyForm(pretty_name.right(pretty_morphism)[0])
def _print_Category(self, category):
return self._print(pretty_symbol(category.name))
@@ -3693,14 +3693,18 @@ def test_complicated_symbol_unchanged():
assert pretty(Symbol(symb_name)) == symb_name
def test_categories():
- from sympy.categories import Object, Morphism, Category, Diagram
+ from sympy.categories import (Object, Morphism, IdentityMorphism,
+ NamedMorphism, CompositeMorphism,
+ Category, Diagram)
+
A1 = Object("A1")
A2 = Object("A2")
A3 = Object("A3")
- f1 = Morphism(A1, A2, "f1")
- f2 = Morphism(A2, A3, "f2")
- anonymous = Morphism(A1, A3, "")
+ f1 = NamedMorphism(A1, A2, "f1")
+ f2 = NamedMorphism(A2, A3, "f2")
+ anonymous = Morphism(A1, A3)
+ id_A1 = IdentityMorphism(A1)
K1 = Category("K1")
@@ -3711,6 +3715,8 @@ def test_categories():
assert upretty(f1) == u"f₁:A₁→A₂"
assert pretty(anonymous) == "A1->A3"
assert upretty(anonymous) == u"A₁→A₃"
+ assert pretty(id_A1) == "id:A1->A1"
+ assert upretty(id_A1) == u"id:A₁→A₁"
assert pretty(f2*f1) == "f2*f1:A1->A3"
assert upretty(f2*f1) == u"f₂∘f₁:A₁→A₃"
@@ -3719,15 +3725,11 @@ def test_categories():
assert upretty(K1) == u"K₁"
# Some further tests for anonymous morphisms.
- 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, "")
+ 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, "")
+ h = NamedMorphism(A2, A3, "f") * Morphism(A1, A2)
assert pretty(h) == "A1->A3"
assert upretty(h) == u"A₁→A₃"
@@ -3737,15 +3739,18 @@ def test_categories():
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₃: ∅}"
+ assert pretty(d) == "{f2*f1:A1->A3: EmptySet(), id:A1->A1: " \
+ "EmptySet(), id:A2->A2: EmptySet(), id:A3->A3: " \
+ "EmptySet(), f1:A1->A2: {unique}, f2:A2->A3: EmptySet()}"
+
+ assert upretty(d) == u"{f₂∘f₁:A₁→A₃: ∅, id:A₁→A₁: ∅, " \
+ u"id:A₂→A₂: ∅, id:A₃→A₃: ∅, f₁:A₁→A₂: {unique}, 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}}"
+ assert pretty(d) == "{f2*f1:A1->A3: EmptySet(), id:A1->A1: " \
+ "EmptySet(), id:A2->A2: EmptySet(), id:A3->A3: " \
+ "EmptySet(), f1:A1->A2: {unique}, f2:A2->A3: EmptySet()}" \
+ " ==> {f2*f1:A1->A3: {unique}}"
+ assert upretty(d) == u"{f₂∘f₁:A₁→A₃: ∅, id:A₁→A₁: ∅, id:A₂→A₂: " \
+ u"∅, id:A₃→A₃: ∅, f₁:A₁→A₂: {unique}, f₂:A₂→A₃: ∅}" \
+ u" ⟹ {f₂∘f₁:A₁→A₃: {unique}}"
View
@@ -529,16 +529,12 @@ def _print_DMF(self, expr):
def _print_Object(self, object):
return 'Object("%s")' % object.name
- def _print_Morphism(self, morphism):
- result = ""
- for component in reversed(morphism.components):
- result += 'Morphism(%s, %s, "%s") * ' % \
- (component.domain, component.codomain, component.name)
- return result[:-3]
-
def _print_IdentityMorphism(self, morphism):
- return 'IdentityMorphism(%s, "%s")' % \
- (morphism.domain, morphism.name)
+ return 'IdentityMorphism(%s)' % morphism.domain
+
+ def _print_NamedMorphism(self, morphism):
+ return 'NamedMorphism(%s, %s, "%s")' % \
+ (morphism.domain, morphism.codomain, morphism.name)
def _print_Category(self, category):
return 'Category("%s")' % category.name
@@ -571,50 +571,52 @@ def test_PolynomialRing():
r"S_<^{-1}\mathbb{Q}\left[x, y\right]"
def test_categories():
- from sympy.categories import Object, Morphism, Category, Diagram
+ from sympy.categories import (Object, Morphism, IdentityMorphism,
+ NamedMorphism, CompositeMorphism,
+ Category, Diagram)
+
A1 = Object("A1")
A2 = Object("A2")
A3 = Object("A3")
- f1 = Morphism(A1, A2, "f1")
- f2 = Morphism(A2, A3, "f2")
- anonymous = Morphism(A1, A3, "")
+ f1 = NamedMorphism(A1, A2, "f1")
+ f2 = NamedMorphism(A2, A3, "f2")
+ anonymous = Morphism(A1, A3)
+ id_A1 = IdentityMorphism(A1)
K1 = Category("K1")
assert latex(A1) == "A_{1}"
assert latex(f1) == "f_{1}:A_{1}\\rightarrow A_{2}"
assert latex(anonymous) == "A_{1}\\rightarrow A_{3}"
+ assert latex(id_A1) == "id:A_{1}\\rightarrow A_{1}"
assert latex(f2*f1) == "f_{2}\\circ f_{1}:A_{1}\\rightarrow A_{3}"
assert latex(K1) == "\mathbf{K_{1}}"
- h = Morphism(A2, A3, "").compose(Morphism(A1, A2, ""), "h")
- assert latex(h) == "h:A_{1}\\rightarrow A_{3}"
-
- h = Morphism(A2, A3, "") * Morphism(A1, A2, "")
+ h = Morphism(A2, A3) * Morphism(A1, A2)
assert latex(h) == "A_{1}\\rightarrow A_{3}"
- h = Morphism(A2, A3, "f") * Morphism(A1, A2, "")
+ h = NamedMorphism(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}"
+ assert latex(d) == "\\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}" \
+ "\\rightarrow A_{3} : \\emptyset, & id:A_{1}\\rightarrow " \
+ "A_{1} : \\emptyset, & id:A_{2}\\rightarrow A_{2} : " \
+ "\\emptyset, & id:A_{3}\\rightarrow A_{3} : \\emptyset, " \
+ "& f_{1}:A_{1}\\rightarrow A_{2} : \\left\\{unique\\right\\}, " \
+ "& 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}"
+ assert latex(d) == "\\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}" \
+ "\\rightarrow A_{3} : \\emptyset, & id:A_{1}\\rightarrow " \
+ "A_{1} : \\emptyset, & id:A_{2}\\rightarrow A_{2} : " \
+ "\\emptyset, & id:A_{3}\\rightarrow A_{3} : \\emptyset, " \
+ "& f_{1}:A_{1}\\rightarrow A_{2} : \\left\\{unique\\right\\}," \
+ " & 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}"
@@ -473,22 +473,21 @@ def test_PrettyPoly():
assert sstr(R.convert(x + y)) == sstr(x + y)
def test_categories():
- from sympy.categories import Object, Morphism, Category
+ from sympy.categories import (Object, Morphism, NamedMorphism,
+ IdentityMorphism, Category)
A = Object("A")
B = Object("B")
- C = Object("C")
- f = Morphism(A, B, "f")
- g = Morphism(B, C, "g")
- id_A = Morphism(A, A, "id_A", identity=True)
+ f = NamedMorphism(A, B, "f")
+ g = Morphism(A, B)
+ id_A = IdentityMorphism(A)
K = Category("K")
assert str(A) == 'Object("A")'
- assert str(f) == 'Morphism(Object("A"), Object("B"), "f")'
- assert str(id_A) == 'IdentityMorphism(Object("A"), "id_A")'
- assert str(g * f) == 'Morphism(Object("B"), Object("C"), "g") * ' \
- 'Morphism(Object("A"), Object("B"), "f")'
+ assert str(f) == 'NamedMorphism(Object("A"), Object("B"), "f")'
+ assert str(g) == 'Morphism(Object("A"), Object("B"))'
+ assert str(id_A) == 'IdentityMorphism(Object("A"))'
assert str(K) == 'Category("K")'

0 comments on commit 8eecc16

Please sign in to comment.