Skip to content

Commit

Permalink
Make test_args.py pass.
Browse files Browse the repository at this point in the history
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
scolobb committed Jun 15, 2012
1 parent d02283a commit cd926f2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
58 changes: 30 additions & 28 deletions 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): class Class(Set):
""" """
Expand All @@ -15,6 +16,16 @@ class Class(Set):
""" """
is_proper = False 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): class Object(Basic):
""" """
The base class for any kind of object in an abstract category. The base class for any kind of object in an abstract category.
Expand All @@ -37,29 +48,14 @@ def __new__(cls, name):
if not name: if not name:
raise ValueError("Anonymous Objects are not allowed.") raise ValueError("Anonymous Objects are not allowed.")


return Basic.__new__(cls, name) return Basic.__new__(cls, Symbol(name))


@property @property
def name(self): def name(self):
""" """
Returns the name of this object. Returns the name of this object.
""" """
return self._args[0] return self._args[0].name

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)


class Morphism(Basic): class Morphism(Basic):
""" """
Expand Down Expand Up @@ -108,6 +104,7 @@ class Morphism(Basic):
True True
""" """

def __new__(cls, domain, codomain, name="", identity=False): def __new__(cls, domain, codomain, name="", identity=False):
if identity and (domain != codomain): if identity and (domain != codomain):
raise ValueError( raise ValueError(
Expand All @@ -118,7 +115,8 @@ def __new__(cls, domain, codomain, name="", identity=False):
if identity: if identity:
return IdentityMorphism(domain, name) return IdentityMorphism(domain, name)
else: else:
return Basic.__new__(cls, domain, codomain, name, []) return Basic.__new__(cls, domain, codomain,
_make_symbol(name), Tuple())


@property @property
def domain(self): def domain(self):
Expand All @@ -139,7 +137,7 @@ def name(self):
""" """
Returns the name of this morphism. Returns the name of this morphism.
""" """
return self.args[2] return self.args[2].name


@property @property
def identity(self): def identity(self):
Expand All @@ -158,18 +156,19 @@ def components(self):
======== ========
>>> from sympy.categories import Object, Morphism >>> from sympy.categories import Object, Morphism
>>> from sympy import Tuple
>>> A = Object("A") >>> A = Object("A")
>>> B = Object("B") >>> B = Object("B")
>>> C = Object("C") >>> C = Object("C")
>>> f = Morphism(A, B, "f") >>> f = Morphism(A, B, "f")
>>> g = Morphism(B, C, "g") >>> g = Morphism(B, C, "g")
>>> (g * f).components == [f, g] >>> (g * f).components == Tuple(f, g)
True True
""" """
components = self.args[3] components = self.args[3]
if not components: if not components:
return [self] return Tuple(self)
else: else:
return components return components


Expand Down Expand Up @@ -212,7 +211,8 @@ def compose(self, g, new_name=""):
# (even if g.domain == self.codomain), so let's suppose it's # (even if g.domain == self.codomain), so let's suppose it's
# not an identity. # not an identity.
return Basic.__new__(Morphism, g.domain, self.codomain, 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): def __mul__(self, g):
""" """
Expand Down Expand Up @@ -253,6 +253,7 @@ def flatten(self, new_name=""):
======== ========
compose compose
""" """

return Morphism(self.domain, self.codomain, new_name) return Morphism(self.domain, self.codomain, new_name)


def __eq__(self, g): def __eq__(self, g):
Expand Down Expand Up @@ -308,7 +309,7 @@ class IdentityMorphism(Morphism):
""" """
def __new__(cls, domain, name=""): def __new__(cls, domain, name=""):
return Basic.__new__(cls, domain, name) return Basic.__new__(cls, domain, _make_symbol(name))


@property @property
def domain(self): def domain(self):
Expand All @@ -329,7 +330,7 @@ def name(self):
""" """
Returns the name of this identity morphism. Returns the name of this identity morphism.
""" """
return self.args[1] return self.args[1].name


@property @property
def identity(self): def identity(self):
Expand All @@ -347,7 +348,7 @@ def components(self):
Since this is an identity morphisms, it always has itself as Since this is an identity morphisms, it always has itself as
the only component. the only component.
""" """
return [self] return Tuple(self)


def compose(self, g, new_name=""): def compose(self, g, new_name=""):
""" """
Expand Down Expand Up @@ -428,15 +429,16 @@ class Category(Basic):
Diagram Diagram
""" """
def __new__(cls, name, objects=EmptySet(), commutative=EmptySet()): 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 return new_category


@property @property
def name(self): def name(self):
""" """
Returns the name of this category. Returns the name of this category.
""" """
return self.args[0] return self.args[0].name


@property @property
def objects(self): def objects(self):
Expand Down
15 changes: 8 additions & 7 deletions sympy/categories/tests/test_baseclasses.py
@@ -1,6 +1,6 @@
from sympy.categories import Object, Morphism, IdentityMorphism, Diagram, Category from sympy.categories import Object, Morphism, IdentityMorphism, Diagram, Category
from sympy.utilities.pytest import XFAIL, raises from sympy.utilities.pytest import XFAIL, raises
from sympy import FiniteSet, EmptySet, Dict from sympy import FiniteSet, EmptySet, Dict, Tuple


def test_object(): def test_object():
A = Object("A") A = Object("A")
Expand Down Expand Up @@ -29,7 +29,7 @@ def test_morphism():
assert f.name == "f" assert f.name == "f"
assert f.domain == A assert f.domain == A
assert f.codomain == B assert f.codomain == B
assert f.components == [f] assert f.components == Tuple(f)


assert f != None assert f != None
assert f == f assert f == f
Expand All @@ -44,7 +44,7 @@ def test_morphism():
assert k.domain == A assert k.domain == A
assert k.codomain == C assert k.codomain == C
assert k.name == "k" assert k.name == "k"
assert k.components == [f, g] assert k.components == Tuple(f, g)


k = g * f k = g * f
p = h * g p = h * g
Expand All @@ -53,29 +53,29 @@ def test_morphism():
assert k.domain == A assert k.domain == A
assert k.codomain == C assert k.codomain == C
assert k.name == "" assert k.name == ""
assert k.components == [f, g] assert k.components == Tuple(f, g)


assert h * k == u assert h * k == u
assert p * f == u assert p * f == u


assert u.domain == A assert u.domain == A
assert u.codomain == D assert u.codomain == D
assert u.name == "" assert u.name == ""
assert u.components == [f, g, h] assert u.components == Tuple(f, g, h)


u1 = u.flatten() u1 = u.flatten()


assert u1.domain == A assert u1.domain == A
assert u1.codomain == D assert u1.codomain == D
assert u1.name == "" assert u1.name == ""
assert u1.components == [u1] assert u1.components == Tuple(u1)


u1 = u.flatten("u") u1 = u.flatten("u")


assert u1.domain == A assert u1.domain == A
assert u1.codomain == D assert u1.codomain == D
assert u1.name == "u" assert u1.name == "u"
assert u1.components == [u1] assert u1.components == Tuple(u1)


assert f == Morphism(A, B, "f") assert f == Morphism(A, B, "f")
assert f != g assert f != g
Expand All @@ -91,6 +91,7 @@ def test_morphism():
assert id_A == IdentityMorphism(A, "id_A") assert id_A == IdentityMorphism(A, "id_A")


assert id_A.identity == True assert id_A.identity == True
assert id_A.components == Tuple(id_A)
assert id_A == Morphism(A, A, name="f", identity=True) assert id_A == Morphism(A, A, name="f", identity=True)
assert hash(id_A) == hash(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") assert id_A != Morphism(A, A, name="f")
Expand Down
37 changes: 37 additions & 0 deletions sympy/core/tests/test_args.py
Expand Up @@ -1937,3 +1937,40 @@ def test_sympy__differential_geometry__differential_geometry__VectorField():
from sympy.differential_geometry import Manifold, Patch, CoordSystem, VectorField from sympy.differential_geometry import Manifold, Patch, CoordSystem, VectorField
cs = CoordSystem('name', Patch('name', Manifold('name', 3))) cs = CoordSystem('name', Patch('name', Manifold('name', 3)))
assert _test_args(VectorField(cs, [x, y], [x, y])) 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)

0 comments on commit cd926f2

Please sign in to comment.