[Numpy-discussion] New Operators in Python

Sebastian Haase haase@msg.ucsf....
Sat Mar 24 15:59:09 CDT 2007


On 3/24/07, Charles R Harris <charlesr.harris@gmail.com> wrote:
>
>
> On 3/24/07, Travis Oliphant <oliphant@ee.byu.edu> wrote:
> > Every so often the idea of new operators comes up because of the need to
> > do both "matrix-multiplication" and element-by-element multiplication.
> >
> > I think this is one area where the current Python approach is not as
> > nice because we have a limited set of operators to work with.
> >
> > One thing I wonder is if we are being vocal enough with the Python 3000
> > crowd to try and get additional operators into the language itself.
> >
> > What if we could get a few new operators into the language to help us.
> > If we don't ask for it, it certainly won't happen.
> > My experience is that the difficulty of using the '*' operator for both
> > matrix multiplication and element-by-element multiplication depending on
> > the class of the object is not especially robust.  It makes it harder to
> > write generic code, and we still haven't gotten everything completely
> > right.
> >
> > It is somewhat workable as it stands, but I think it would be nicer if
> > we could have some "meta" operator that allowed an alternative
> > definition of major operators.   Something like @*  for example (just
> > picking a character that is already used for decorators).
>
> Yes indeed, this is an old complaint. Just having an infix operator would be
> an improvement:
>
> A dot B dot C
>
> Not that I am suggesting dot in this regard ;) In particular, it wouldn't
> parse without spaces. What about division? Matlab has both / and \ for left
> and right matrix division and something like that could call solve instead
> of inverse, leading to some efficiencies. We also have both dot and
> tensordot, which raises the problem of interpretation when ndim > 2.
>
> Chuck
>
I understand the convenience of more infix operators. And I sometimes
think one should just be able to define new ones at will ....

On the other hand, I'm now playing the devil's advocate:
A "math specific" language like Matlab has obviously an overwhelming
need for a second set of matrix/array operators.  However, a language
-- as broadly used as -- Python might be just better off having a
simple, concise and limited set of infix operators.   I assume that
this is the official argument.

I got especially "worried" when being remember of the "\"
right-to-left  division operator. (As I said, it very useful to have
in Matlab, and I wish sometimes we could add things like this).
It is just important to keep the "con" - argument clearly in mind.
I hope this helps the discussion.

- Sebastian Haase


More information about the Numpy-discussion mailing list