[Numpy-discussion] adding a .M attribute to the array.

Huaiyu Zhu huaiyu_zhu at yahoo.com
Mon Mar 18 00:40:05 CST 2002


I'm a little late to this discussion, but it gives me a chance to read all
the existing comments.  I'd like to offer some background to one possible
solution.

On Thu, 7 Mar 2002, eric wrote:
> 
> http://python.sourceforge.net/peps/pep-0225.html
> 
>     This one proposes decorating the current binary ops with some
>     symbols to indicate that they have different behavior than
>     the standard binary ops.  This is similar to Matlab's use of
>     * for matrix multiplication and .* for element-wise multiplication
>     or to R's use of * for element-wise multiplication and %*% for
>     "object-wise" multiplication.
> 
>     It proposes prepending ~ to operators to change their behavior so
>     that ~* would become matrix multiply.
> 
>     The PEP is a little more general, but this gives the flavor.
> 
> My hunch is that some form of the second (perhaps drastically reduced) would
> meet with more success.  The suggested ~* or even the %*% operator are both
> palitable.  Such details can be decided later.  The question is whether there is
> sufficient interest to try and push the operator idea through?  It would take
> much longer than choosing something we can do ourselves (like .M), but the
> operator solution seems more desirable to me.
> 

I'm one of the coauthor of this PEP.  I'm very glad to see additional
interest in this proposal.  It is not just a proposal - there was actually
a patch made by Gregory Lielens for the ~op operators for Python 2.0.  It
redefines ~ so that it can be combined with + - * / ** to form new
operators.  It is quite ingenious in that all the original bitwise
operations on ~ alone are still valid.  The new operator can be assigned
any semantics with hooks like __tmul__ and __rtmul__.  The idea is that a
matrix class would define __mul__ so that * is matrix multiplication and
define __tmul__ so that ~* is elementwise operation.  There is a test
implementation on the MatPy homepage (matpy.sourceforge.net).

So what was holding it back?  Well, last time around when this was
discussed, it appears that most of the heavy weights in the Numeric
community favored either keeping the status quo, or using ~* symbol for
arrays.  We hoped to use the MatPy package as a test case to show that it
is possible to have two entirely different kinds of objects, where the
meaning of * and ~* are switched.  However, for various reasons I was not
able to act upon it for months, and Python evolved into 2.1 and 2.2.  I
never had much time to update the patch, and felt the attempt was futile
as 1) Python was evolving quite fast, 2) I did not heard much about this
issue since then.  I often feel guilty about the lapse.

Now it might be a good time to revive this proposal, as the idea of having
matrices and arrays with independent semantics but possibly related
implementation appears to be gaining some additional acceptance.  Some
ancillary issues that hindered the implementation at that time have also
been solved.  For example, using .I for inverse, .T for transpose, etc,
was costly because of the need to override __getattr__ and __coerce__,
making a matrix class less attractive in practice. These can now be
implemented efficiently using the new set/get mechanism.

I'd like to hear any suggestions on how to proceed.  My own favorite would
be to have separate array and matrix classes with easy but explicit
conversions between them.  Without conversions, arrays and matrices would
be completely independent semantically.  In other words, I'm mostly in
favor of Konrad Hinsen's position, with the addition of using ~ operators
for elementwise operations for matrix-like classes.  The PEP itself also
discussed ideas of extending the meaning of ~ to other parts of Python for
elementwise operations on aggregate types, but my impressions of people's
impressions is that it has a better chance without that part.

Huaiyu





More information about the Numpy-discussion mailing list