Terry Hancock wrote: > On 19 Jan 2006 13:57:06 +0100 > Anton Vredegoor <[EMAIL PROTECTED]> wrote: > > Some time ago I tried to 'sell' Python to a mathematician. > > The crucial point was that it was not (in standard Python) > > possible to have a matrix A and a matrix B and then do for > > example: > > > > A = A * B > > > > and have a matrix multiplication performed. > > Um, why not? I'm trying to think what would be the stumbling > block. I don't use matrix multiplies much, but I have > implemented 3D vector math so that "*" is the "dot product" > and "%" is the "cross product", which is pretty trivial to > do.
Of course ! And to think that I even have used this trick a few times, for example to implement set operations using long integers. I feel ashamed. In my defense I can only explain what happened. Four years ago (when I knew a lot less of Python) I tried to use Numeric to do A*B for matrices, but that resulted in something else than expected. So I redefined the star operator by subclassing a *numeric python* object but then it didn't work (the subclassing IIRC). Then it turned out there was a Matrix module for Numeric that did exacly what was asked, but by that time I was trying to understand Numeric by reading the docs and 'selling Python' at the same time, which didn't work too well ... The main reason for that was that it was necessary to convince someone not having any Python knowledge to install Python *and* some module that I didn't know about and then that module needed *another* install which I didn't know about, and the docs for Numeric were separate from Python. Just too much at once. I believe if I just had implemented matrix multiplication myself at the time in plain Python I wouldn't have overcomplicated the matter in such a way that I couldn't convince anyone else anymore :-) So I got lost in Numerics complexities and that made me forget the basic option. By now I have used Numeric enough to make it likely that I could explain its use to someone. But even when I cured myself of this deficiency, the memory of failure stayed in my head. Witness a classic freudian fixation phenomenon in a Python learning curve :-) In order to prevent such mental damage for future Python programmers, I propose to add a simple matrix multiplication module to the standard distribution. > > The only obstacle I've run into is that you can't > (easily) define *new* operators and precedence levels. > > There *is* a trick for doing this that was posted on > the list some time back, which involved overloading an > operator to "apply an operator": > > It would've allowed you to do something like this: > > a |dot| b > a |cross| b > > or perhaps > > a <dot> b > a <cross> b > > I don't remember where this is posted. The trick was in > overloading the <, >, or | to interact specially with > "operator" objects. That's very nice. Thanks to you for mentioning this and to Jorge, who provided the link to activestate for this recipe in another message. Anton -- http://mail.python.org/mailman/listinfo/python-list