First of all, I am not good at mathematics, a little into trigonometry is all my brain could handle. However, this new PEP caught my eyes because I never thought of having Python performing matrix operations, especially there is a well-known NumPy which probably every Python coder is aware of that library.

Secondly, I only skimmed through PEP 465 by Nathaniel J. Smith, but I think it’s a quite important one to let people know about it. It’s only a draft and it’s not the first one to try. 14 years ago, in 2000, PEP 211 by Greg Wilson was submitted and current status is deferred. Fiver years before that, a Matrix Object Proposal was posted by James Hugunin, the first attempt.

In short, some developers have been trying to get matrix into Python for nearly two decades.

This PEP goes from why choosing @ (at-sign) to why it’s needed to the status of many aspects. It proposes the following dedicated operators for two matrix operations:

Op Precedence/associativity Methods
@ Same as * __matmul__, __rmatmul__
@@ Same as ** __matpow__, __rmatpow__
@= n/a __imatmul__
@@= n/a __imatpow__

The operator symbol can be memorized as “@ is * for mATrices.”

It lists libraries have intentions to utilize the operators, such as NumPy or Pandas. It also lists the choices from other languages which have matrix operations. Matlab: * and .*, APL: +.×, or R: %*%.

An example to showcase why having the operators would improve readability:

S=(Hβ-r)T(HVHT) -1(Hβ-r)

The code would go from dots everywhere:

import numpy as np
from numpy.linalg import inv, solve

# Using dot function:
S = np.dot((np.dot(H, beta) - r).T,
           np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))

# Using dot method:
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)

To a very clear presentation of code:

S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

The only thing really comes to my mind of this is transpose operation as well as the inverse, the code above still requires NumPy for .T and inv.

If this PEP gets accepted, would there be implementation for these two operations? But how about other matrix operations like transpose? Which is very common.

Like I said in the beginning, I only skimmed through, so if you have the need or you are already a NumPy or those libraries users, you might want to check this PEP out and this thread.