The sage coercion model guarantees that when x.__add__(y) is called (well, technically x._add_ or x._add_c_impl), the argument y is of the same type and parent as x. For example
sage: R.<x> = ZZ[] sage: x+1/2 x + 1/2 sage: parent(x) Univariate Polynomial Ring in x over Integer Ring sage: parent(1/2) Rational Field sage: parent(x+1/2) Univariate Polynomial Ring in x over Rational Field In your case, someone could type sage: g = CompleteGraph(5) sage: g+1 # will call g.__add__(1) ??? which you have to deal with. Typically, just throw an error if it's not a graph. But what about graph + digraph? Digraph + digraph? Etc. Should graph + x be a new graph with a new (unconnected) vertex 'x'? - Robert On Sep 25, 2007, at 4:59 PM, Hamptonio wrote: > Thanks, now I understand what to do. However, I don't understand your > comment about "...the SAGE coercion model". What is an example of > that - the sage integers? > > -M. Hampton > > On Sep 25, 12:10 pm, "Mike Hansen" <[EMAIL PROTECTED]> wrote: >> 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/ -~----------~----~----~----~------~----~------~--~---