Since I don't think that graphs and polytopes fall under the SAGE
coercion model, overloading operators is pretty straightforward.  You
just need to define the __add__ method in your class.  x + y will call
x.__add__(y).

sage: class Foo:
....:     def __add__(self, y):
....:         return 42
....:
sage: a = Foo()
sage: b = Foo()
sage: a + b
42
sage: b + a
42

Note that you'll want to do some type-checking so that y is what you
actually think it should be.

--Mike

On 9/25/07, Hamptonio <[EMAIL PROTECTED]> wrote:
>
> I would appreciate any tips on how to extend the + operator in this
> way, since I would like to implement Minkowski sums of polytopes and
> this is natural notation for that.
> Marshall
>
> On Sep 25, 10:37 am, "Mike Hansen" <[EMAIL PROTECTED]> wrote:
> > In SAGE, '+' is used for union of sets.  For example,
> >
> > sage: a = Set([1,2])
> > sage: b = Set([2,3])
> > sage: a+b
> > {1, 2, 3}
> >
> > Since currently, + is not defined for graphs, it'd be a natural choice.
> >
> > --Mike
> >
> > On 9/25/07, Jason Grout <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > I'm thinking more about how to make the Graph class easy to use.  One
> > > thing that crops up is that the operations that combine graphs only
> > > combine two graphs at a time (e.g., g.union(h), where g and h are graphs).
> >
> > > Is there a way to define an infix operator that would allow one to say:
> >
> > > g union h union i union j union k?
> >
> > > I could do it with something like:
> >
> > > reduce(lambda x,y: x.union(y), [g,h,i,j,k])
> >
> > > But that doesn't seem as clear as the infix things above.
> >
> > > For reference, Mathematica allows an operator in backticks to be applied
> > > to its surrounding arguments, so the equivalent operation above would be:
> >
> > > g `union` h `union` i `union` j `union` k
> >
> > > And of course, you can set whether the operator is left-associative or
> > > right-associative.
> >
> > > Of course, one solution is to use a for loop:
> >
> > > newgraph=Graph()
> > > for graph in [g,h,i,j,k]:
> > >      newgraph.union(graph)
> >
> > > But that seems a lot clunkier than the infix expression above.
> >
> > > I guess another solution is to return the new graph from the union, so
> > > that you could do:
> >
> > > g.union(h).union(i).union(j)
> >
> > > Thoughts?
> >
> > > -Jason
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to