Skip to content

Commit

Permalink
Raise ValueError when composing uncomposable morphisms.
Browse files Browse the repository at this point in the history
  • Loading branch information
scolobb committed Jun 15, 2012
1 parent 39ee393 commit 629c916
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
22 changes: 7 additions & 15 deletions sympy/categories/baseclasses.py
Expand Up @@ -97,8 +97,6 @@ class Morphism(Basic):
True True
>>> f == Morphism(A, B, "") >>> f == Morphism(A, B, "")
False False
>>> f * g is None
True
>>> g * f >>> g * f
Morphism(Object("B"), Object("C"), "g") * Morphism(Object("B"), Object("C"), "g") *
Morphism(Object("A"), Object("B"), "f") Morphism(Object("A"), Object("B"), "f")
Expand Down Expand Up @@ -176,7 +174,6 @@ def compose(self, g, new_name=""):
If ``self`` is a morphism from `B` to `C` and ``g`` is a If ``self`` is a morphism from `B` to `C` and ``g`` is a
morphism from `A` to `B`, returns the morphism from `A` to `C` morphism from `A` to `B`, returns the morphism from `A` to `C`
which results from the composition of these morphisms. which results from the composition of these morphisms.
Otherwise, returns ``None``.
If either ``self`` or ``g`` are morphisms resulted from some If either ``self`` or ``g`` are morphisms resulted from some
previous composition, components in the resulting morphism previous composition, components in the resulting morphism
Expand Down Expand Up @@ -204,7 +201,7 @@ def compose(self, g, new_name=""):
""" """
if g.codomain != self.domain: if g.codomain != self.domain:
return None raise ValueError("Uncomponsable morphisms.")


if self.identity: if self.identity:
return g return g
Expand Down Expand Up @@ -438,12 +435,7 @@ def _set_dict_union(dictionary, key, value):
Returns ``True`` if the key already was in the dictionary and Returns ``True`` if the key already was in the dictionary and
``False`` otherwise. ``False`` otherwise.
If ``key`` is ``None``, returns True and does nothing.
""" """
if not key:
return True

if key in dictionary: if key in dictionary:
dictionary[key] = dictionary[key] | value dictionary[key] = dictionary[key] | value
return True return True
Expand Down Expand Up @@ -474,13 +466,13 @@ def _add_morphism(morphisms, morphism, props, add_identities=True):
Diagram._set_dict_union(morphisms, id_cod, empty) Diagram._set_dict_union(morphisms, id_cod, empty)


for existing_morphism, existing_props in morphisms.items(): for existing_morphism, existing_props in morphisms.items():
left = morphism * existing_morphism
right = existing_morphism * morphism

new_props = existing_props & props new_props = existing_props & props

if morphism.domain == existing_morphism.codomain:
Diagram._set_dict_union(morphisms, left, new_props) left = morphism * existing_morphism
Diagram._set_dict_union(morphisms, right, new_props) Diagram._set_dict_union(morphisms, left, new_props)
if morphism.codomain == existing_morphism.domain:
right = existing_morphism * morphism
Diagram._set_dict_union(morphisms, right, new_props)


def __new__(cls, *args): def __new__(cls, *args):
premises = {} premises = {}
Expand Down
4 changes: 2 additions & 2 deletions sympy/categories/tests/test_baseclasses.py
Expand Up @@ -36,8 +36,8 @@ def test_morphism():
assert f == Morphism(A, B, "f") assert f == Morphism(A, B, "f")
assert f != g assert f != g


assert f * g == None raises(ValueError, lambda: f * g)
assert f * f == None raises(ValueError, lambda: f * f)


k = g.compose(f, "k") k = g.compose(f, "k")


Expand Down

0 comments on commit 629c916

Please sign in to comment.