Hello.
I am new to SymPy and I don't know if you have the Graph class in
SymPy, but I am playing with my Graph.py anyway (see below).

I have a question in the use of self in  defining __init__: in a class.

I want to define a graph, and first I define vertex objects and edge objects.
The edges should be attributed their endpoints (which are vertex objects)
and the vertices should be attributed their incident edges (which are
edge objects)

In the following, I try to define the vertex object first, then define
the edges (containing
incidence information).  In the process of defining the edges, I try
to update the vertex objects
with the new incidence information with following snippet:

<code>
class Edge:
    def 
__init__(self,endpoint1="",endpoint2="",label='f',endpoints=set([]),beingObject='itself'):
...
...
            for i in self.endpoints:         #usually we define the
vertices before the edges
...
                """jazz up the incident endpoints"""
                i.edges.add(self)
</code>

And it went rather wrong - all the vertices V ended up having
incident edges (the V.edges attribute) being
the set of all my edges.

So did I do something wrong?

The following is the entire module, which can be run byitself:
Thank you very much.


<code>
#Graph.py
#trying to define a graph (actually, a finite graph) in python

"""usually we define the vertices before the edges
    but we may sometimes want to do the opposite"""

class Vertex:
    def  __init__(self,label='x',edges=set([]),beingObject='itself'):
  #e.g. beingObject=sl2 (a Lie algebra object)
        """to create a vertex 'wrapper' out of the beingObject"""
        self.label=label
        self.edges=edges   #initialise
        self.being=beingObject
        if edges!=set([]):
            for e in edges:         #usually we define the vertices
before the edges
                                        #but we may sometimes want to
do the opposite
                """jazz up the incident edges"""
                e.endpoints.add(self)


class Edge:
    """an edge can be defined as a pair of arrows but here
    we do it the direct and simple way"""
    def 
__init__(self,endpoint1="",endpoint2="",label='f',endpoints=set([]),beingObject='itself'):
        """may be called by e=Edge('e',a,b) or
e=Edge('e',endpoints=set([a,b]))"""
        if endpoints==set([]) and endpoint1 !="" :
            endpoints=set([endpoint1,endpoint2])
            #print label, [v.label for v in endpoints]
        self.endpoints = endpoints
        print label, [e.label for e in endpoints]
        self.vertices = endpoints #alias
        self.label=label
        self.being=beingObject
        if self.endpoints!=set([]):
            for i in self.endpoints:         #usually we define the
vertices before the edges
                                        #but we may sometimes want to
do the opposite
                """jazz up the incident endpoints"""
                i.edges.add(self)
                #if isinstance(W,Vertex): W.edges=set([])   #  just a
test, for i have no idea what happened - did i do something wrong with
the object constructions?
                print "updating i=", i.label, "with edge", self.label
                print i.label, "now has edges", [e.label for e in i.edges]
                if isinstance(W,Vertex): print "and W has
edges:",[x.label for x in W.edges] #-why does it return a?
                if isinstance(U,Vertex): print "and U has
edges:",[x.label for x in U.edges] #-why does it return a?



class Graph:
    """we allow multiple edges and loops for simplicity"""
    def __init__(self,vertices=set([]),edges=set([]), label="No label"):
        self.edges=edges
        self.vertices=vertices
        """a preliminary check"""
        for e in edges:
            if not e.endpoints < vertices:
                print "Error! The endpoints of the edge " + e.label +
" are not in the set of vertices " , [v.label for v in vertices]
                return None
            else:
                for v in e.endpoints:
                    if e not in v.edges:
                        print "Error!  Vertex "+ v.label +" doesn't
appear to recognise edge "+ e.label +", which cites it as an endpoint"


if __name__=="__main__":
    U=Vertex('U')
    V=Vertex('V')
    W=Vertex('W')
    #print W.edges
    a=Edge(U,V,'a')
    print "...creating the edge b: V-W............."
    b=Edge(V,W,'b')
    Ver=set([U,V,W])
    Edg=set([a,b])
    for i in Ver:
        print "\n..............."
        print i.label
        print [e.label for e in i.edges]
    for j in Edg:
        print "\n..............."
        print j.label
        print [v.label for v in j.endpoints]
    print "creating the two edge graph U-a-V-b-W"
    G=Graph(Ver,Edg,"two-edge-graph")
    for e in G.edges:
        print 
list(e.endpoints)[0].label+"-"+e.label+"-"+list(e.endpoints)[1].label
    for v in G.vertices:
        print "\n......" + v.label
        for e in v.edges:
            print e.label
</code>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to