[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power

M.-A. Lemburg mal at egenix.com
Fri Mar 14 11:16:34 CET 2014


On 14.03.2014 02:59, Nathaniel Smith wrote:
> PEP: XXXX
> Title: Dedicated infix operators for matrix multiplication and matrix power
>
> ...
>
> Specification
> =============
> 
> Two new binary operators are added to the Python language, together
> with corresponding in-place versions:
> 
> =======  ========================= ===============================
>  Op      Precedence/associativity     Methods
> =======  ========================= ===============================
> ``@``    Same as ``*``             ``__matmul__``, ``__rmatmul__``
> ``@@``   Same as ``**``            ``__matpow__``, ``__rmatpow__``
> ``@=``   n/a                       ``__imatmul__``
> ``@@=``  n/a                       ``__imatpow__``
> =======  ========================= ===============================
>
> ...
>
> When working with n-dimensional arrays, there are two different ways
> we might want to define multiplication.  One is elementwise
> multiplication::
> 
>   [[1, 2],     [[11, 12],     [[1 * 11, 2 * 12],
>    [3, 4]]  x   [13, 14]]  =   [3 * 13, 4 * 14]]
> 
> and the other is `matrix multiplication`_:
> 
> .. _matrix multiplication: https://en.wikipedia.org/wiki/Matrix_multiplication

I have some questions:

1. Since in math, the operator is usually spelt "·" (the center dot,
   or "." but that's already reserved for methods and attributes in
   Python), why not try to use that instead of "@" (which in Python
   already identifies decorators) ?

2. The PEP should include a section on how other programming languages
   solve this, i.e. what syntax they use for matrix multiplications.

3. Since matrix multiplication is only one type of product you find
   in math, albeit a very frequently used one, how would those other
   products fit into the picture ? Would then have to use methods
   again ? E.g. think of cross product, inner product, outer/tensor
   product.

4. Another very common operation needed in vector/matrix calculation
   is transposition. This is usually written as superscript "T" or "t"
   ("ᵀ" in Unicode). Wouldn't this operator be needed as well, to
   make the picture complete ? OTOH, we currently don't have postfix
   operators in Python, so I guess writing this as A.transpose()
   comes close enough ;-)

Now since this is all about syntactic sugar, we also need to look at
some code examples:


I == A @@ -1 @ A
vs.
I == A ·· -1 · A
vs.
I == A.inverse().dot(A)


(A @ B).transpose() == A.transpose() @ B.transpose()
vs.
(A · B).transpose() == A.transpose() · B.transpose()
vs.
A.dot(B).transpose() == A.transpose().dot(B.transpose())


c = A @ v
vs.
c = A · v
vs.
c = A.dot(v)


Hmm, even though I'd love to see matrix operators in Python,
I don't think they really add clarity to the syntax of matrix
calculations -  a bit disappointing, I must say :-(

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Mar 14 2014)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2014-03-29: PythonCamp 2014, Cologne, Germany ...          15 days to go
2014-04-09: PyCon 2014, Montreal, Canada ...               26 days to go

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list