Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make test_args.py pass.

This implies making everything stored in Basic.args to be an instance of
Basic and also adding the corresponding tests to test_args.py.  Instead
of storing string names, instances of Symbol or Dummy are now stored in
Basic.args.
  • Loading branch information...
commit cd926f21fa6e61d1fd75ea8e15a16752840c75ba 1 parent d02283a
@scolobb authored
View
58 sympy/categories/baseclasses.py
@@ -1,4 +1,5 @@
-from sympy.core import Set, Basic, FiniteSet, EmptySet, Dict
+from sympy.core import (Set, Basic, FiniteSet, EmptySet, Dict, Symbol,
+ Dummy, Tuple)
class Class(Set):
"""
@@ -15,6 +16,16 @@ class Class(Set):
"""
is_proper = False
+def _make_symbol(name):
+ """
+ If ``name`` is not empty, creates a :class:`Symbol` with this
+ name. Otherwise creates a :class:`Dummy`.
+ """
+ if name:
+ return Symbol(name)
+ else:
+ return Dummy("")
+
class Object(Basic):
"""
The base class for any kind of object in an abstract category.
@@ -37,29 +48,14 @@ def __new__(cls, name):
if not name:
raise ValueError("Anonymous Objects are not allowed.")
- return Basic.__new__(cls, name)
+ return Basic.__new__(cls, Symbol(name))
@property
def name(self):
"""
Returns the name of this object.
"""
- return self._args[0]
-
- def __eq__(self, obj):
- if not isinstance(obj, Object):
- return False
-
- return self.name == obj.name
-
- def __ne__(self, obj):
- if not isinstance(obj, Object):
- return True
-
- return self.name != obj.name
-
- def __hash__(self):
- return hash(self.name)
+ return self._args[0].name
class Morphism(Basic):
"""
@@ -108,6 +104,7 @@ class Morphism(Basic):
True
"""
+
def __new__(cls, domain, codomain, name="", identity=False):
if identity and (domain != codomain):
raise ValueError(
@@ -118,7 +115,8 @@ def __new__(cls, domain, codomain, name="", identity=False):
if identity:
return IdentityMorphism(domain, name)
else:
- return Basic.__new__(cls, domain, codomain, name, [])
+ return Basic.__new__(cls, domain, codomain,
+ _make_symbol(name), Tuple())
@property
def domain(self):
@@ -139,7 +137,7 @@ def name(self):
"""
Returns the name of this morphism.
"""
- return self.args[2]
+ return self.args[2].name
@property
def identity(self):
@@ -158,18 +156,19 @@ def components(self):
========
>>> from sympy.categories import Object, Morphism
+ >>> from sympy import Tuple
>>> A = Object("A")
>>> B = Object("B")
>>> C = Object("C")
>>> f = Morphism(A, B, "f")
>>> g = Morphism(B, C, "g")
- >>> (g * f).components == [f, g]
+ >>> (g * f).components == Tuple(f, g)
True
"""
components = self.args[3]
if not components:
- return [self]
+ return Tuple(self)
else:
return components
@@ -212,7 +211,8 @@ def compose(self, g, new_name=""):
# (even if g.domain == self.codomain), so let's suppose it's
# not an identity.
return Basic.__new__(Morphism, g.domain, self.codomain,
- new_name, g.components + self.components)
+ _make_symbol(new_name), g.components +
+ self.components)
def __mul__(self, g):
"""
@@ -253,6 +253,7 @@ def flatten(self, new_name=""):
========
compose
"""
+
return Morphism(self.domain, self.codomain, new_name)
def __eq__(self, g):
@@ -308,7 +309,7 @@ class IdentityMorphism(Morphism):
"""
def __new__(cls, domain, name=""):
- return Basic.__new__(cls, domain, name)
+ return Basic.__new__(cls, domain, _make_symbol(name))
@property
def domain(self):
@@ -329,7 +330,7 @@ def name(self):
"""
Returns the name of this identity morphism.
"""
- return self.args[1]
+ return self.args[1].name
@property
def identity(self):
@@ -347,7 +348,7 @@ def components(self):
Since this is an identity morphisms, it always has itself as
the only component.
"""
- return [self]
+ return Tuple(self)
def compose(self, g, new_name=""):
"""
@@ -428,7 +429,8 @@ class Category(Basic):
Diagram
"""
def __new__(cls, name, objects=EmptySet(), commutative=EmptySet()):
- new_category = Basic.__new__(cls, name, objects, FiniteSet(commutative))
+ new_category = Basic.__new__(cls, Symbol(name), objects,
+ FiniteSet(commutative))
return new_category
@property
@@ -436,7 +438,7 @@ def name(self):
"""
Returns the name of this category.
"""
- return self.args[0]
+ return self.args[0].name
@property
def objects(self):
View
15 sympy/categories/tests/test_baseclasses.py
@@ -1,6 +1,6 @@
from sympy.categories import Object, Morphism, IdentityMorphism, Diagram, Category
from sympy.utilities.pytest import XFAIL, raises
-from sympy import FiniteSet, EmptySet, Dict
+from sympy import FiniteSet, EmptySet, Dict, Tuple
def test_object():
A = Object("A")
@@ -29,7 +29,7 @@ def test_morphism():
assert f.name == "f"
assert f.domain == A
assert f.codomain == B
- assert f.components == [f]
+ assert f.components == Tuple(f)
assert f != None
assert f == f
@@ -44,7 +44,7 @@ def test_morphism():
assert k.domain == A
assert k.codomain == C
assert k.name == "k"
- assert k.components == [f, g]
+ assert k.components == Tuple(f, g)
k = g * f
p = h * g
@@ -53,7 +53,7 @@ def test_morphism():
assert k.domain == A
assert k.codomain == C
assert k.name == ""
- assert k.components == [f, g]
+ assert k.components == Tuple(f, g)
assert h * k == u
assert p * f == u
@@ -61,21 +61,21 @@ def test_morphism():
assert u.domain == A
assert u.codomain == D
assert u.name == ""
- assert u.components == [f, g, h]
+ assert u.components == Tuple(f, g, h)
u1 = u.flatten()
assert u1.domain == A
assert u1.codomain == D
assert u1.name == ""
- assert u1.components == [u1]
+ assert u1.components == Tuple(u1)
u1 = u.flatten("u")
assert u1.domain == A
assert u1.codomain == D
assert u1.name == "u"
- assert u1.components == [u1]
+ assert u1.components == Tuple(u1)
assert f == Morphism(A, B, "f")
assert f != g
@@ -91,6 +91,7 @@ def test_morphism():
assert id_A == IdentityMorphism(A, "id_A")
assert id_A.identity == True
+ assert id_A.components == Tuple(id_A)
assert id_A == Morphism(A, A, name="f", identity=True)
assert hash(id_A) == hash(Morphism(A, A, name="f", identity=True))
assert id_A != Morphism(A, A, name="f")
View
37 sympy/core/tests/test_args.py
@@ -1937,3 +1937,40 @@ def test_sympy__differential_geometry__differential_geometry__VectorField():
from sympy.differential_geometry import Manifold, Patch, CoordSystem, VectorField
cs = CoordSystem('name', Patch('name', Manifold('name', 3)))
assert _test_args(VectorField(cs, [x, y], [x, y]))
+
+def test_sympy__categories__baseclasses__Class():
+ from sympy.categories.baseclasses import Class
+ assert _test_args(Class())
+
+def test_sympy__categories__baseclasses__Object():
+ from sympy.categories import Object
+ assert _test_args(Object("A"))
+
+def test_sympy__categories__baseclasses__Morphism():
+ from sympy.categories import Object, Morphism
+ assert _test_args(Morphism(Object("A"), Object("B"), "f"))
+
+def test_sympy__categories__baseclasses__IdentityMorphism():
+ from sympy.categories import Object, IdentityMorphism
+ assert _test_args(IdentityMorphism(Object("A"), "f"))
+
+def test_sympy__categories__baseclasses__Diagram():
+ from sympy.categories import Object, Morphism, Diagram, Category
+ A = Object("A")
+ B = Object("B")
+ C = Object("C")
+ f = Morphism(A, B, "f")
+ d = Diagram([f])
+ assert _test_args(d)
+
+def test_sympy__categories__baseclasses__Category():
+ from sympy.categories import Object, Morphism, Diagram, Category
+ A = Object("A")
+ B = Object("B")
+ C = Object("C")
+ f = Morphism(A, B, "f")
+ g = Morphism(B, C, "g")
+ d1 = Diagram([f, g])
+ d2 = Diagram([f])
+ K = Category("K", commutative=[d1, d2])
+ assert _test_args(K)
Please sign in to comment.
Something went wrong with that request. Please try again.