Skip to content

Commit

Permalink
Forbid anonymous morphisms.
Browse files Browse the repository at this point in the history
This makes the code even more streamlined and removes some extra checks.
  • Loading branch information
scolobb committed Jun 15, 2012
1 parent 25f746a commit 5e28059
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 112 deletions.
97 changes: 44 additions & 53 deletions sympy/categories/baseclasses.py
Expand Up @@ -38,22 +38,17 @@ class Morphism(Basic):
be the same morphisms. To distinguish between morphisms between be the same morphisms. To distinguish between morphisms between
the same objects use :class:`NamedMorphism`. the same objects use :class:`NamedMorphism`.
Examples It is prohibited to instantiate this class. Use one of the
======== derived classes instead.
>>> from sympy.categories import Object, Morphism
>>> A = Object("A")
>>> B = Object("B")
>>> Morphism(A, B)
Morphism(Object("A"), Object("B"))
See Also See Also
======== ========
IdentityMorphism, NamedMorphism, CompositeMorphism IdentityMorphism, NamedMorphism, CompositeMorphism
""" """
def __new__(cls, domain, codomain): def __new__(cls, domain, codomain):
return Basic.__new__(cls, domain, codomain) raise(NotImplementedError(
"Cannot instantiate Morphism. Use derived classes instead."))


@property @property
def domain(self): def domain(self):
Expand All @@ -63,10 +58,10 @@ def domain(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> f.domain >>> f.domain
Object("A") Object("A")
Expand All @@ -81,10 +76,10 @@ def codomain(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> f.codomain >>> f.codomain
Object("B") Object("B")
Expand All @@ -101,15 +96,15 @@ def compose(self, other):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> g * f >>> g * f
CompositeMorphism((Morphism(Object("A"), Object("B")), CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
Morphism(Object("B"), Object("C")))) NamedMorphism(Object("B"), Object("C"), "g")))
>>> (g * f).domain >>> (g * f).domain
Object("A") Object("A")
>>> (g * f).codomain >>> (g * f).codomain
Expand Down Expand Up @@ -143,10 +138,10 @@ class IdentityMorphism(Morphism):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, IdentityMorphism >>> from sympy.categories import Object, NamedMorphism, IdentityMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> id_A = IdentityMorphism(A) >>> id_A = IdentityMorphism(A)
>>> id_B = IdentityMorphism(B) >>> id_B = IdentityMorphism(B)
>>> f * id_A == f >>> f * id_A == f
Expand All @@ -173,7 +168,7 @@ class NamedMorphism(Morphism):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, NamedMorphism >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> f = NamedMorphism(A, B, "f") >>> f = NamedMorphism(A, B, "f")
Expand All @@ -200,7 +195,7 @@ def name(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, NamedMorphism >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> f = NamedMorphism(A, B, "f") >>> f = NamedMorphism(A, B, "f")
Expand All @@ -226,15 +221,15 @@ class CompositeMorphism(Morphism):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, CompositeMorphism >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> g * f >>> g * f
CompositeMorphism((Morphism(Object("A"), Object("B")), CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
Morphism(Object("B"), Object("C")))) NamedMorphism(Object("B"), Object("C"), "g")))
>>> CompositeMorphism(f, g) == g * f >>> CompositeMorphism(f, g) == g * f
True True
Expand Down Expand Up @@ -307,14 +302,15 @@ def components(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, CompositeMorphism >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> (g * f).components >>> (g * f).components
(Morphism(Object("A"), Object("B")), Morphism(Object("B"), Object("C"))) (NamedMorphism(Object("A"), Object("B"), "f"),
NamedMorphism(Object("B"), Object("C"), "g"))
""" """
return self.args[0] return self.args[0]
Expand All @@ -330,12 +326,12 @@ def domain(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, CompositeMorphism >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> (g * f).domain >>> (g * f).domain
Object("A") Object("A")
Expand All @@ -353,19 +349,19 @@ def codomain(self):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, CompositeMorphism >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> (g * f).codomain >>> (g * f).codomain
Object("C") Object("C")
""" """
return self.components[-1].codomain return self.components[-1].codomain


def flatten(self, new_name=None): def flatten(self, new_name):
""" """
Forgets the composite structure of this morphism. Forgets the composite structure of this morphism.
Expand All @@ -378,22 +374,17 @@ def flatten(self, new_name=None):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, CompositeMorphism >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> (g * f).flatten()
Morphism(Object("A"), Object("C"))
>>> (g * f).flatten("h") >>> (g * f).flatten("h")
NamedMorphism(Object("A"), Object("C"), "h") NamedMorphism(Object("A"), Object("C"), "h")
""" """
if new_name: return NamedMorphism(self.domain, self.codomain, new_name)
return NamedMorphism(self.domain, self.codomain, new_name)
else:
return Morphism(self.domain, self.codomain)


class Category(Basic): class Category(Basic):
r""" r"""
Expand Down Expand Up @@ -430,13 +421,13 @@ class Category(Basic):
Examples Examples
======== ========
>>> from sympy.categories import Object, Morphism, Diagram, Category >>> from sympy.categories import Object, NamedMorphism, Diagram, Category
>>> from sympy import FiniteSet >>> from sympy import FiniteSet
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> d = Diagram([f, g]) >>> d = Diagram([f, g])
>>> K = Category("K", commutative_diagrams=[d]) >>> K = Category("K", commutative_diagrams=[d])
>>> K.commutative_diagrams == FiniteSet(d) >>> K.commutative_diagrams == FiniteSet(d)
Expand Down Expand Up @@ -495,13 +486,13 @@ def commutative_diagrams(self):
Returns the :class:`FiniteSet` of diagrams which are known to Returns the :class:`FiniteSet` of diagrams which are known to
be commutative in this category. be commutative in this category.
>>> from sympy.categories import Object, Morphism, Diagram, Category >>> from sympy.categories import Object, NamedMorphism, Diagram, Category
>>> from sympy import FiniteSet >>> from sympy import FiniteSet
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B) >>> f = NamedMorphism(A, B, "f")
>>> g = Morphism(B, C) >>> g = NamedMorphism(B, C, "g")
>>> d = Diagram([f, g]) >>> d = Diagram([f, g])
>>> K = Category("K", commutative_diagrams=[d]) >>> K = Category("K", commutative_diagrams=[d])
>>> K.commutative_diagrams == FiniteSet(d) >>> K.commutative_diagrams == FiniteSet(d)
Expand Down
17 changes: 6 additions & 11 deletions sympy/categories/tests/test_baseclasses.py
Expand Up @@ -12,10 +12,10 @@ def test_morphisms():
D = Object("D") D = Object("D")


# Test the base morphism. # Test the base morphism.
f = Morphism(A, B) f = NamedMorphism(A, B, "f")
assert f.domain == A assert f.domain == A
assert f.codomain == B assert f.codomain == B
assert f == Morphism(A, B) assert f == NamedMorphism(A, B, "f")


# Test identities. # Test identities.
id_A = IdentityMorphism(A) id_A = IdentityMorphism(A)
Expand All @@ -29,7 +29,6 @@ def test_morphisms():
g = NamedMorphism(B, C, "g") g = NamedMorphism(B, C, "g")
assert g.name == "g" assert g.name == "g"
assert g != f assert g != f
assert g != Morphism(B, C)
assert g == NamedMorphism(B, C, "g") assert g == NamedMorphism(B, C, "g")
assert g != NamedMorphism(B, C, "f") assert g != NamedMorphism(B, C, "f")


Expand All @@ -46,7 +45,7 @@ def test_morphisms():
assert CompositeMorphism(g * f) == g * f assert CompositeMorphism(g * f) == g * f


# Test the associativity of composition. # Test the associativity of composition.
h = Morphism(C, D) h = NamedMorphism(C, D, "h")


p = h * g p = h * g
u = h * g * f u = h * g * f
Expand All @@ -56,11 +55,6 @@ def test_morphisms():
assert CompositeMorphism(f, g, h) == u assert CompositeMorphism(f, g, h) == u


# Test flattening. # Test flattening.
u1 = u.flatten()
assert isinstance(u1, Morphism)
assert u1.domain == A
assert u1.codomain == D

u2 = u.flatten("u") u2 = u.flatten("u")
assert isinstance(u2, NamedMorphism) assert isinstance(u2, NamedMorphism)
assert u2.name == "u" assert u2.name == "u"
Expand All @@ -84,6 +78,7 @@ def test_morphisms():
raises(TypeError, lambda: CompositeMorphism(f, None, 1)) raises(TypeError, lambda: CompositeMorphism(f, None, 1))


raises(ValueError, lambda: NamedMorphism(A, B, "")) raises(ValueError, lambda: NamedMorphism(A, B, ""))
raises(NotImplementedError, lambda: Morphism(A, B))


def test_diagram(): def test_diagram():
A = Object("A") A = Object("A")
Expand Down Expand Up @@ -151,8 +146,8 @@ def test_category():
B = Object("B") B = Object("B")
C = Object("C") C = Object("C")


f = Morphism(A, B) f = NamedMorphism(A, B, "f")
g = Morphism(B, C) g = NamedMorphism(B, C, "g")


d1 = Diagram([f, g]) d1 = Diagram([f, g])
d2 = Diagram([f]) d2 = Diagram([f])
Expand Down
17 changes: 9 additions & 8 deletions sympy/core/tests/test_args.py
Expand Up @@ -1946,6 +1946,7 @@ def test_sympy__categories__baseclasses__Object():
from sympy.categories import Object from sympy.categories import Object
assert _test_args(Object("A")) assert _test_args(Object("A"))


@XFAIL
def test_sympy__categories__baseclasses__Morphism(): def test_sympy__categories__baseclasses__Morphism():
from sympy.categories import Object, Morphism from sympy.categories import Object, Morphism
assert _test_args(Morphism(Object("A"), Object("B"))) assert _test_args(Morphism(Object("A"), Object("B")))
Expand All @@ -1959,30 +1960,30 @@ def test_sympy__categories__baseclasses__NamedMorphism():
assert _test_args(NamedMorphism(Object("A"), Object("B"), "f")) assert _test_args(NamedMorphism(Object("A"), Object("B"), "f"))


def test_sympy__categories__baseclasses__CompositeMorphism(): def test_sympy__categories__baseclasses__CompositeMorphism():
from sympy.categories import Object, Morphism, CompositeMorphism from sympy.categories import Object, NamedMorphism, CompositeMorphism
A = Object("A") A = Object("A")
B = Object("B") B = Object("B")
C = Object("C") C = Object("C")
f = Morphism(A, B) f = NamedMorphism(A, B, "f")
g = Morphism(B, C) g = NamedMorphism(B, C, "g")
assert _test_args(CompositeMorphism(f, g)) assert _test_args(CompositeMorphism(f, g))


def test_sympy__categories__baseclasses__Diagram(): def test_sympy__categories__baseclasses__Diagram():
from sympy.categories import Object, Morphism, Diagram, Category from sympy.categories import Object, NamedMorphism, Diagram, Category
A = Object("A") A = Object("A")
B = Object("B") B = Object("B")
C = Object("C") C = Object("C")
f = Morphism(A, B) f = NamedMorphism(A, B, "f")
d = Diagram([f]) d = Diagram([f])
assert _test_args(d) assert _test_args(d)


def test_sympy__categories__baseclasses__Category(): def test_sympy__categories__baseclasses__Category():
from sympy.categories import Object, Morphism, Diagram, Category from sympy.categories import Object, NamedMorphism, Diagram, Category
A = Object("A") A = Object("A")
B = Object("B") B = Object("B")
C = Object("C") C = Object("C")
f = Morphism(A, B) f = NamedMorphism(A, B, "f")
g = Morphism(B, C) g = NamedMorphism(B, C, "g")
d1 = Diagram([f, g]) d1 = Diagram([f, g])
d2 = Diagram([f]) d2 = Diagram([f])
K = Category("K", commutative_diagrams=[d1, d2]) K = Category("K", commutative_diagrams=[d1, d2])
Expand Down
15 changes: 6 additions & 9 deletions sympy/printing/latex.py
Expand Up @@ -1202,15 +1202,12 @@ def _print_IdentityMorphism(self, morphism):
def _print_CompositeMorphism(self, morphism): def _print_CompositeMorphism(self, morphism):
from sympy.categories import NamedMorphism from sympy.categories import NamedMorphism


component_names = "" # All components of the morphism have names and it is thus
if all([isinstance(component, NamedMorphism) for component in \ # possible to build the name of the composite.
morphism.components]): component_names_list = [self._print(Symbol(component.name)) for \
# All components of the morphism have names and it is thus component in morphism.components]
# possible to build the name of the composite. component_names_list.reverse()
component_names_list = [self._print(Symbol(component.name)) for \ component_names = "\\circ ".join(component_names_list) + ":"
component in morphism.components]
component_names_list.reverse()
component_names = "\\circ ".join(component_names_list) + ":"


pretty_morphism = self._print_Morphism(morphism) pretty_morphism = self._print_Morphism(morphism)
return component_names + pretty_morphism return component_names + pretty_morphism
Expand Down
15 changes: 6 additions & 9 deletions sympy/printing/pretty/pretty.py
Expand Up @@ -1478,15 +1478,12 @@ def _print_CompositeMorphism(self, morphism):
if self._use_unicode: if self._use_unicode:
circle = u"\u2218" circle = u"\u2218"


component_names = "" # All components of the morphism have names and it is thus
if all([isinstance(component, NamedMorphism) for component in \ # possible to build the name of the composite.
morphism.components]): component_names_list = [pretty_symbol(component.name) for \
# All components of the morphism have names and it is thus component in morphism.components]
# possible to build the name of the composite. component_names_list.reverse()
component_names_list = [pretty_symbol(component.name) for \ component_names = circle.join(component_names_list) + ":"
component in morphism.components]
component_names_list.reverse()
component_names = circle.join(component_names_list) + ":"


pretty_name = self._print(component_names) pretty_name = self._print(component_names)
pretty_morphism = self._print_Morphism(morphism) pretty_morphism = self._print_Morphism(morphism)
Expand Down

0 comments on commit 5e28059

Please sign in to comment.