[SciPy-User] Matrix multiplication operator PEP

Geordie McBain gdmcbain at freeshell.org
Sat Feb 22 18:09:02 EST 2014


I like this idea and draft PEP a lot.  Having this feature would ease my
programming and greatly improve the readability of my SciPy programs.
   Having been trained in Matlab and then having used GNU Octave for many
years, I thought about adapting their pair of infix multiplication
operators, * and .*, but quickly realized that this wouldn't translate as
they use the lone star for the linear algebraic product.
   A small suggestion is an additional mnemonic motivation for the at-sign:
it slightly resembles the centred dot (LaTeX \cdot) often used in printed
mathematics to represent an algebraic product.
   A bolder one that I haven't thought through yet: were there such an
operator, besides making heavy use of its intended purpose, how might I
overload it?  The first thing that springs to mind is functional
composition and application.  The fundamental idea of a matrix is as a
function on vectors; matrix-matrix multiplication is defined subsequently
by associativity.  If I had arbitrary compatible functions f and g and an
argument x, would I like to write 'f @ x' for f (x) and 'g @ f' for lambda
x: g (f (x))?  Maybe not the former so much but the latter definitely.
Haskell uses a dot for this as in 'g . f'.

   http://www.haskell.org/haskellwiki/Function_composition

Of course the dot's unpalatable here as it's an accessor in Python.  The
use of @ for composition might go some way to addressing the concern about
adding an operator with no stdlib uses: I presume it does contain a few
functional compositions.
Then turning that argument around: what about (should the PEP not get up,
or even before it gets up) defining ndarray.__call__ as dot?  So we'd have
A (x) instead of A.dot (x) or A @ x.  An advantage of this approach would
be that it wouldn't require changing Python, only NumPy.  Matlab/Octave
can't do that as they use parentheses for indexing as well as application;
NumPy has left parentheses free by using brackets for the former.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140223/03472ff6/attachment.html>


More information about the SciPy-User mailing list