[Numpy-discussion] adding a .M attribute to the array.
eric at enthought.com
Wed Mar 6 22:05:02 CST 2002
Boy did this one get a rise! Nice to hear so many voices.
I also feel we need a more compact notation for linear algebra and would like to
be able to do it without explicitly casting arrays to Matrix.Matrix objects.
This attribute approach will work, but I wonder if trying the "adding an
operator to Python" approach one more time would be worth while. At Python10
developer's day, Guido explicitly mentioned the linear algebra operator in a
short comment saying something to the affect that, if the numeric community
could agree on an appropriate operator, he would strongly consider the addition.
He also mentioned the strangness of the 2 PEPs proposed on the topic at a coffee
break... I noticed the status of both PEPs is "deferred."
This one proposes the @ operator for outer products.
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
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.
----- Original Message -----
From: "Travis Oliphant" <oliphant.travis at ieee.org>
To: <numpy-discussion at lists.sourceforge.net>
Sent: Tuesday, March 05, 2002 11:44 PM
Subject: [Numpy-discussion] adding a .M attribute to the array.
> Recently there has been discussion on the list about the awkwardness of
> matrix syntax when using Numeric Python.
> Matrix expressions can be awkard to express in Numeric which is a negative
> mark on an otherwise excellent computing environment.
> Currently part of the problem can be solved by working with Matrix objects
> a = Matrix.Matrix("[1 2 3; 4 5 6]") # Notice the strings.
> However, most operations return arrays which have to be recast to matrices
> using at best a character with parenthesis:
> M = Matrix.Matrix
> M(sin(a)) * M(cos(a)).T
> The suggestion was made to add ".M" as an attribute of arrays which returns a
> matrix. Thus, the code above can be written:
> sin(a).M * cos(a).M.T
> While some aesthestic simplicity is obtained, the big advantage is in
> consistency. Somebody else may decide that
> P = Matrix.Matrix is a better choice. But, if we establish that
> .M always returns a matrix for arrays < 2d, then we gain consistency.
> I've made this change and am ready to commit the change to the Numeric tree,
> unless there are strong objections. I know some people do not like the
> proliferation of attributes, but in this case the notational convenience it
> affords to otherwise overly burdened syntax and the consistency it allows
> Numeric to deal with Matrix equations may be worth it.
> What do you think?
> -Travis Oliphant
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
More information about the Numpy-discussion