[sage-devel] Re: making new infix operators

2007-09-28 Thread Bill Page
On 9/27/07, Fernando Perez wrote: > > On 9/27/07, Robert Bradshaw wrote: > > > I agree with your caution, but don't see how the examples above mess > > anything up. If an object B wants to pretend to be immutable, it > > merely has to check that nothing points to it before mutating itself > > (and

[sage-devel] Re: making new infix operators

2007-09-27 Thread Fernando Perez
On 9/27/07, Robert Bradshaw <[EMAIL PROTECTED]> wrote: > I agree with your caution, but don't see how the examples above mess > anything up. If an object B wants to pretend to be immutable, it > merely has to check that nothing points to it before mutating itself > (and, in mutating itself, don't

[sage-devel] Re: making new infix operators

2007-09-27 Thread Chris Chiasson
Well, I don't want to take the thread off topic, but in the standard (documented) way of creating new operators, it is done with an assignment to MakeExpression. This will work in a notebook, where boxes are transformed into expressions before execution, but it will not work in a .m file, because

[sage-devel] Re: making new infix operators

2007-09-27 Thread Robert Bradshaw
On Sep 27, 2007, at 2:47 PM, Fernando Perez wrote: > The problematic issue is that some extensions (numpy first and > foremost, but this buffer API is going into the core of the language > for 2.6/3.0) allow multiple, distinct *python objects* to share the > same (or parts of the same) chunk of r

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Robert Miller wrote: >> The best I've come up with at this point is, given graphs A,B,C,D,E, the >> union is: >> >> A.union(B).union(C).union(D).union(E) >> >> where the union function returns the modified graph each time so I can >> keep chaining union function calls. The union actually modifies

[sage-devel] Re: making new infix operators

2007-09-27 Thread Bill Page
On 9/27/07, Fernando Perez wrote: > ... > In [12]: a = N.arange(10) > > In [13]: sys.getrefcount(a) > Out[13]: 2 > > Here the refcount is 2, as expected: the original array and the name > 'a' pointing to it. > > Now, a naked, unassigned slice of the original array has a refcount of 1: > > In [14]:

[sage-devel] Re: making new infix operators

2007-09-27 Thread Fernando Perez
On 9/27/07, Bill Page <[EMAIL PROTECTED]> wrote: > > On 9/27/07, Fernando Perez wrote: > > > > On 9/27/07, Robert Bradshaw wrote: > > > > > You have a good point, though fortunately we're looking for refcounts > > > so low that (essentially) nothing else can be holding onto it but the > > > curren

[sage-devel] Re: making new infix operators

2007-09-27 Thread Bill Page
On 9/27/07, Fernando Perez wrote: > > On 9/27/07, Robert Bradshaw wrote: > > > You have a good point, though fortunately we're looking for refcounts > > so low that (essentially) nothing else can be holding onto it but the > > current stack frame. If b is pointing to a, it doesn't matter how > > m

[sage-devel] Re: making new infix operators

2007-09-27 Thread Robert Miller
> The best I've come up with at this point is, given graphs A,B,C,D,E, the > union is: > > A.union(B).union(C).union(D).union(E) > > where the union function returns the modified graph each time so I can > keep chaining union function calls. The union actually modifies the > graph A, so if you wa

[sage-devel] Re: making new infix operators

2007-09-27 Thread Robert Bradshaw
On Sep 27, 2007, at 1:45 PM, Jason Grout wrote: > Robert Bradshaw wrote: >> On Sep 27, 2007, at 4:43 AM, Jason Grout wrote: > >>> A=A.union(B) >>> >>> to modify A? >> >> Yes. Or even >> >> A.inplace_union(B) >> >> (or some better name than that) which returns nothing. Otherwise, for >> example, e

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Robert Bradshaw wrote: > On Sep 27, 2007, at 4:43 AM, Jason Grout wrote: > >> A=A.union(B) >> >> to modify A? > > Yes. Or even > > A.inplace_union(B) > > (or some better name than that) which returns nothing. Otherwise, for > example, every function that received a graph as an argument wou

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jaap Spies
Jason Grout wrote: > Mike Hansen wrote: >> Of all the code I've read in lots of different languages, I think code >> written for Mathematica has been the least transparent. > > I guess I've always loved functional languages, so the simple core > principles appealed to me right away. See a thread

[sage-devel] Re: making new infix operators

2007-09-27 Thread Fernando Perez
On 9/27/07, Robert Bradshaw <[EMAIL PROTECTED]> wrote: > You have a good point, though fortunately we're looking for refcounts > so low that (essentially) nothing else can be holding onto it but the > current stack frame. If b is pointing to a, it doesn't matter how > many other things are (direc

[sage-devel] Re: making new infix operators

2007-09-27 Thread Robert Bradshaw
On Sep 27, 2007, at 7:36 AM, Fernando Perez wrote: > On 9/27/07, Bill Page <[EMAIL PROTECTED]> wrote: > >> There is another very relevant thread started by Robert Bradshaw on >> this list concerning the safety of in-place modifications: >> >> http://groups.google.com/group/sage-devel/browse_thre

[sage-devel] Re: making new infix operators

2007-09-27 Thread Robert Bradshaw
On Sep 27, 2007, at 4:43 AM, Jason Grout wrote: > Mike Hansen wrote: >>> The best I've come up with at this point is, given graphs >>> A,B,C,D,E, the >>> union is: >>> >>> A.union(B).union(C).union(D).union(E) >>> >>> where the union function returns the modified graph each time so >>> I can

[sage-devel] Re: making new infix operators

2007-09-27 Thread Hamptonio
I disagree. Maybe its because I grew up learning BASIC, but in-place modification generally confuses and annoys me. Here's an example that I think illustrates one of my issues: Code version A: a = something b = [a,something_else] c = a.a_method() important_result(b) Code version B: a = somethin

[sage-devel] Re: making new infix operators

2007-09-27 Thread Fernando Perez
On 9/27/07, Bill Page <[EMAIL PROTECTED]> wrote: > There is another very relevant thread started by Robert Bradshaw on > this list concerning the safety of in-place modifications: > > http://groups.google.com/group/sage-devel/browse_thread/thread/806cd958eb28ac3b/46655d7572d11ee6?#46655d7572d11e

[sage-devel] Re: making new infix operators

2007-09-27 Thread Bill Page
On 9/27/07, Jason Grout wrote: > ... > > > >> where the union function returns the modified graph each time so I can > >> keep chaining union function calls. The union actually modifies the > >> graph A, so if you want A left alone and a new graph returned, you do: > >> > >> newgraph=A.copy().uni

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Robert Bradshaw wrote: > On Sep 26, 2007, at 8:59 PM, Jason Grout wrote: > >> As for graphs, there isn't a standard definition for the "+" operation >> that is agreed upon, even for graphs of the same type (e.g., should it >> be disjoint union or union?). That's why I was asking about custom >>

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Chris Chiasson wrote: > It might be worth pointing out that adding "new" syntax in Mathematica > is (usually) done by assignments to the function that transforms > general two dimensional input into source code. That isn't really the > same thing as adding a new operator to the language itself. A

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Craig Citro wrote: >>> The best I've come up with at this point is, given graphs >>> A,B,C,D,E, the >>> union is: >>> >>> A.union(B).union(C).union(D).union(E) >> I actually think that this looks very clear, despite the lack of >> infix operators. >> > > I agree -- especially since it looks like

[sage-devel] Re: making new infix operators

2007-09-27 Thread Jason Grout
Mike Hansen wrote: >> The best I've come up with at this point is, given graphs A,B,C,D,E, the >> union is: >> >> A.union(B).union(C).union(D).union(E) >> >> where the union function returns the modified graph each time so I can >> keep chaining union function calls. The union actually modifies t

[sage-devel] Re: making new infix operators

2007-09-26 Thread Craig Citro
>> The best I've come up with at this point is, given graphs >> A,B,C,D,E, the >> union is: >> >> A.union(B).union(C).union(D).union(E) > > I actually think that this looks very clear, despite the lack of > infix operators. > I agree -- especially since it looks like a literal translation of th

[sage-devel] Re: making new infix operators

2007-09-26 Thread Jason Grout
Mike Hansen wrote: > Of all the code I've read in lots of different languages, I think code > written for Mathematica has been the least transparent. I guess I've always loved functional languages, so the simple core principles appealed to me right away. Some things I love include the consistenc

[sage-devel] Re: making new infix operators

2007-09-26 Thread Robert Bradshaw
On Sep 26, 2007, at 8:59 PM, Jason Grout wrote: > Robert Bradshaw wrote: >> 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. = ZZ[] >> sage: x+1/2

[sage-devel] Re: making new infix operators

2007-09-26 Thread Mike Hansen
> The best I've come up with at this point is, given graphs A,B,C,D,E, the > union is: > > A.union(B).union(C).union(D).union(E) > > where the union function returns the modified graph each time so I can > keep chaining union function calls. The union actually modifies the > graph A, so if you wa

[sage-devel] Re: making new infix operators

2007-09-26 Thread Jason Grout
Robert Bradshaw wrote: > 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. = ZZ[] > sage: x+1/2 > x + 1/2 > sage: parent(x) > Univariate Polynomial

[sage-devel] Re: making new infix operators

2007-09-26 Thread Robert Bradshaw
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. = ZZ[] sage: x+1/2 x + 1/2 sage: parent(x) Univariate Polynomial Ring in x over Integer Ring sage: parent(

[sage-devel] Re: making new infix operators

2007-09-26 Thread Mike Hansen
Of all the code I've read in lots of different languages, I think code written for Mathematica has been the least transparent. --Mike On 9/26/07, Chris Chiasson <[EMAIL PROTECTED]> wrote: > > It might be worth pointing out that adding "new" syntax in Mathematica > is (usually) done by assignment

[sage-devel] Re: making new infix operators

2007-09-26 Thread Chris Chiasson
It might be worth pointing out that adding "new" syntax in Mathematica is (usually) done by assignments to the function that transforms general two dimensional input into source code. That isn't really the same thing as adding a new operator to the language itself. On Sep 26, 2:07 pm, Jason Grout

[sage-devel] Re: making new infix operators

2007-09-26 Thread Jason Grout
cwitty wrote: > On Sep 26, 7:31 am, Jason Grout <[EMAIL PROTECTED]> wrote: >> Can we define custom infix operators? Suppose I'd like "boxproduct" to >> be an infix operator. Could I make that work? >> >> Thanks, >> >> Jason > > If you're willing to put a lot of effort into the project, you migh

[sage-devel] Re: making new infix operators

2007-09-26 Thread cwitty
On Sep 26, 7:31 am, Jason Grout <[EMAIL PROTECTED]> wrote: > Can we define custom infix operators? Suppose I'd like "boxproduct" to > be an infix operator. Could I make that work? > > Thanks, > > Jason If you're willing to put a lot of effort into the project, you might be able to do something

[sage-devel] Re: making new infix operators

2007-09-26 Thread Nick Alexander
On 26-Sep-07, at 10:13 AM, John Cremona wrote: > > Nostalgia for Algol68 ! The line > > OP OVER = (INT n,d)RAT: cancel(RAT(n,d)); > > is taken from my implementation of rational numbers so "one half" > could be written > > 1 OVER 2 > > and I could also write > > DET M The past is the present:

[sage-devel] Re: making new infix operators

2007-09-26 Thread John Cremona
Nostalgia for Algol68 ! The line OP OVER = (INT n,d)RAT: cancel(RAT(n,d)); is taken from my implementation of rational numbers so "one half" could be written 1 OVER 2 and I could also write DET M for the determinant of a matrix (ok, that's prefix not infix). Don't mock, my original tables

[sage-devel] Re: making new infix operators

2007-09-26 Thread Mike Hansen
No, I don't think you can make custom infix operators in Python. With something as long as 'boxproduct', you'd probably just be better off making it a method. --Mike On 9/26/07, Jason Grout <[EMAIL PROTECTED]> wrote: > > Mike Hansen wrote: > > Since I don't think that graphs and polytopes fall

[sage-devel] Re: making new infix operators

2007-09-26 Thread Jason Grout
Mike Hansen 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

[sage-devel] Re: making new infix operators

2007-09-25 Thread Hamptonio
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

[sage-devel] Re: making new infix operators

2007-09-25 Thread Jason Grout
Mike Hansen 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

[sage-devel] Re: making new infix operators

2007-09-25 Thread Mike Hansen
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-devel] Re: making new infix operators

2007-09-25 Thread Hamptonio
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-devel] Re: making new infix operators

2007-09-25 Thread Mike Hansen
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 Grap