Permalink
Browse files

Forbid anonymous morphisms.

This makes the code even more streamlined and removes some extra checks.
  • Loading branch information...
1 parent 25f746a commit 5e280596252cace7f0a3802598f91a766974db4e @scolobb committed Jun 15, 2012
@@ -38,22 +38,17 @@ class Morphism(Basic):
be the same morphisms. To distinguish between morphisms between
the same objects use :class:`NamedMorphism`.
- Examples
- ========
-
- >>> from sympy.categories import Object, Morphism
- >>> A = Object("A")
- >>> B = Object("B")
- >>> Morphism(A, B)
- Morphism(Object("A"), Object("B"))
+ It is prohibited to instantiate this class. Use one of the
+ derived classes instead.
See Also
========
IdentityMorphism, NamedMorphism, CompositeMorphism
"""
def __new__(cls, domain, codomain):
- return Basic.__new__(cls, domain, codomain)
+ raise(NotImplementedError(
+ "Cannot instantiate Morphism. Use derived classes instead."))
@property
def domain(self):
@@ -63,10 +58,10 @@ def domain(self):
Examples
========
- >>> from sympy.categories import Object, Morphism
+ >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A")
>>> B = Object("B")
- >>> f = Morphism(A, B)
+ >>> f = NamedMorphism(A, B, "f")
>>> f.domain
Object("A")
@@ -81,10 +76,10 @@ def codomain(self):
Examples
========
- >>> from sympy.categories import Object, Morphism
+ >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A")
>>> B = Object("B")
- >>> f = Morphism(A, B)
+ >>> f = NamedMorphism(A, B, "f")
>>> f.codomain
Object("B")
@@ -101,15 +96,15 @@ def compose(self, other):
Examples
========
- >>> from sympy.categories import Object, Morphism
+ >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> g * f
- CompositeMorphism((Morphism(Object("A"), Object("B")),
- Morphism(Object("B"), Object("C"))))
+ CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
+ NamedMorphism(Object("B"), Object("C"), "g")))
>>> (g * f).domain
Object("A")
>>> (g * f).codomain
@@ -143,10 +138,10 @@ class IdentityMorphism(Morphism):
Examples
========
- >>> from sympy.categories import Object, Morphism, IdentityMorphism
+ >>> from sympy.categories import Object, NamedMorphism, IdentityMorphism
>>> A = Object("A")
>>> B = Object("B")
- >>> f = Morphism(A, B)
+ >>> f = NamedMorphism(A, B, "f")
>>> id_A = IdentityMorphism(A)
>>> id_B = IdentityMorphism(B)
>>> f * id_A == f
@@ -173,7 +168,7 @@ class NamedMorphism(Morphism):
Examples
========
- >>> from sympy.categories import Object, Morphism, NamedMorphism
+ >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> f = NamedMorphism(A, B, "f")
@@ -200,7 +195,7 @@ def name(self):
Examples
========
- >>> from sympy.categories import Object, Morphism, NamedMorphism
+ >>> from sympy.categories import Object, NamedMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> f = NamedMorphism(A, B, "f")
@@ -226,15 +221,15 @@ class CompositeMorphism(Morphism):
Examples
========
- >>> from sympy.categories import Object, Morphism, CompositeMorphism
+ >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> g * f
- CompositeMorphism((Morphism(Object("A"), Object("B")),
- Morphism(Object("B"), Object("C"))))
+ CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
+ NamedMorphism(Object("B"), Object("C"), "g")))
>>> CompositeMorphism(f, g) == g * f
True
@@ -307,14 +302,15 @@ def components(self):
Examples
========
- >>> from sympy.categories import Object, Morphism, CompositeMorphism
+ >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> (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]
@@ -330,12 +326,12 @@ def domain(self):
Examples
========
- >>> from sympy.categories import Object, Morphism, CompositeMorphism
+ >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> (g * f).domain
Object("A")
@@ -353,19 +349,19 @@ def codomain(self):
Examples
========
- >>> from sympy.categories import Object, Morphism, CompositeMorphism
+ >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> (g * f).codomain
Object("C")
"""
return self.components[-1].codomain
- def flatten(self, new_name=None):
+ def flatten(self, new_name):
"""
Forgets the composite structure of this morphism.
@@ -378,22 +374,17 @@ def flatten(self, new_name=None):
Examples
========
- >>> from sympy.categories import Object, Morphism, CompositeMorphism
+ >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
- >>> (g * f).flatten()
- Morphism(Object("A"), Object("C"))
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> (g * f).flatten("h")
NamedMorphism(Object("A"), Object("C"), "h")
"""
- if new_name:
- return NamedMorphism(self.domain, self.codomain, new_name)
- else:
- return Morphism(self.domain, self.codomain)
+ return NamedMorphism(self.domain, self.codomain, new_name)
class Category(Basic):
r"""
@@ -430,13 +421,13 @@ class Category(Basic):
Examples
========
- >>> from sympy.categories import Object, Morphism, Diagram, Category
+ >>> from sympy.categories import Object, NamedMorphism, Diagram, Category
>>> from sympy import FiniteSet
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> d = Diagram([f, g])
>>> K = Category("K", commutative_diagrams=[d])
>>> K.commutative_diagrams == FiniteSet(d)
@@ -495,13 +486,13 @@ def commutative_diagrams(self):
Returns the :class:`FiniteSet` of diagrams which are known to
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
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
- >>> f = Morphism(A, B)
- >>> g = Morphism(B, C)
+ >>> f = NamedMorphism(A, B, "f")
+ >>> g = NamedMorphism(B, C, "g")
>>> d = Diagram([f, g])
>>> K = Category("K", commutative_diagrams=[d])
>>> K.commutative_diagrams == FiniteSet(d)
@@ -12,10 +12,10 @@ def test_morphisms():
D = Object("D")
# Test the base morphism.
- f = Morphism(A, B)
+ f = NamedMorphism(A, B, "f")
assert f.domain == A
assert f.codomain == B
- assert f == Morphism(A, B)
+ assert f == NamedMorphism(A, B, "f")
# Test identities.
id_A = IdentityMorphism(A)
@@ -29,7 +29,6 @@ def test_morphisms():
g = NamedMorphism(B, C, "g")
assert g.name == "g"
assert g != f
- assert g != Morphism(B, C)
assert g == NamedMorphism(B, C, "g")
assert g != NamedMorphism(B, C, "f")
@@ -46,7 +45,7 @@ def test_morphisms():
assert CompositeMorphism(g * f) == g * f
# Test the associativity of composition.
- h = Morphism(C, D)
+ h = NamedMorphism(C, D, "h")
p = h * g
u = h * g * f
@@ -56,11 +55,6 @@ def test_morphisms():
assert CompositeMorphism(f, g, h) == u
# Test flattening.
- u1 = u.flatten()
- assert isinstance(u1, Morphism)
- assert u1.domain == A
- assert u1.codomain == D
-
u2 = u.flatten("u")
assert isinstance(u2, NamedMorphism)
assert u2.name == "u"
@@ -84,6 +78,7 @@ def test_morphisms():
raises(TypeError, lambda: CompositeMorphism(f, None, 1))
raises(ValueError, lambda: NamedMorphism(A, B, ""))
+ raises(NotImplementedError, lambda: Morphism(A, B))
def test_diagram():
A = Object("A")
@@ -151,8 +146,8 @@ def test_category():
B = Object("B")
C = Object("C")
- f = Morphism(A, B)
- g = Morphism(B, C)
+ f = NamedMorphism(A, B, "f")
+ g = NamedMorphism(B, C, "g")
d1 = Diagram([f, g])
d2 = Diagram([f])
@@ -1946,6 +1946,7 @@ def test_sympy__categories__baseclasses__Object():
from sympy.categories import Object
assert _test_args(Object("A"))
+@XFAIL
def test_sympy__categories__baseclasses__Morphism():
from sympy.categories import Object, Morphism
assert _test_args(Morphism(Object("A"), Object("B")))
@@ -1959,30 +1960,30 @@ def test_sympy__categories__baseclasses__NamedMorphism():
assert _test_args(NamedMorphism(Object("A"), Object("B"), "f"))
def test_sympy__categories__baseclasses__CompositeMorphism():
- from sympy.categories import Object, Morphism, CompositeMorphism
+ from sympy.categories import Object, NamedMorphism, CompositeMorphism
A = Object("A")
B = Object("B")
C = Object("C")
- f = Morphism(A, B)
- g = Morphism(B, C)
+ f = NamedMorphism(A, B, "f")
+ g = NamedMorphism(B, C, "g")
assert _test_args(CompositeMorphism(f, g))
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")
B = Object("B")
C = Object("C")
- f = Morphism(A, B)
+ f = NamedMorphism(A, B, "f")
d = Diagram([f])
assert _test_args(d)
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")
B = Object("B")
C = Object("C")
- f = Morphism(A, B)
- g = Morphism(B, C)
+ f = NamedMorphism(A, B, "f")
+ g = NamedMorphism(B, C, "g")
d1 = Diagram([f, g])
d2 = Diagram([f])
K = Category("K", commutative_diagrams=[d1, d2])
View
@@ -1202,15 +1202,12 @@ def _print_IdentityMorphism(self, morphism):
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) + ":"
+ # 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
@@ -1478,15 +1478,12 @@ def _print_CompositeMorphism(self, morphism):
if self._use_unicode:
circle = u"\u2218"
- 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) + ":"
+ # 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)
Oops, something went wrong.

0 comments on commit 5e28059

Please sign in to comment.