[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power
Steven D'Aprano
steve at pearwood.info
Fri Mar 14 13:32:39 CET 2014
On Fri, Mar 14, 2014 at 11:24:26AM +0100, Antoine Pitrou wrote:
> > This PEP proposes two new binary operators dedicated to matrix
> > multiplication and matrix power, spelled ``@`` and ``@@``
> > respectively. (Mnemonic: ``@`` is ``*`` for mATrices.)
>
> That sounds like a strange choice to me. There's nothing in "@" or "@@"
> that suggests "matrices" or "multiplication".
The PEP even gives a mnemonic for it: @ is for mATrix multiplication.
There's nothing in @ that suggests "decorator", nor is there anything
about * that suggests scalar multiplication.
Out of the common symbols used in Python, & is possibly the only
operator symbol with a non-arbitrary connection to its meaning. & was
originally a ligature of "et", the Latin word for "and". Everything else
is pretty much arbitrary, and it's only habit and long use that makes
them seem normal.
> AFAIK, other languages sometimes use ".*" for matrix multiplication (as
> opposed to element-wise multiplication). Why not re-use that
> convention? Would that lead to parsing difficulties?
"Syntax should not look like grit on Tim Peters' monitor."
.* is, in my opinion, pretty ugly, and leads to even ugly
extensions:
a .* b # matrix multiplication
a .*= b # in place matrix multiplication
a .** b # matrix exponentiation
a .**= b # in-place matrix exponentiation
And it suffers from exactly the same lack of connection to matrix
multiplication as @ does: there is nothing about . that spells "matrix".
It also has the problem that it could cause confusion with vector dot
product. Read out A .* B aloud and you get something that sounds like it
might mean dot product, "A dot times B".
> That said, I'm rather sympathetic to the general idea, and if "@" is
> the least contentious option, then I'm ok with it too ("@@" doesn't
> sound like a good idea at all, though).
I don't see why. Since Python uses * and ** for scalar multiplication
and exponentiation, and given that @ is accepted as matrix
multiplication, then it is completely logical to follow the same
pattern, which gives us @@ for matrix exponentiation. That's no sillier
than using ** for exponentiation, instead of ^ as the Gods intended :-)
--
Steven
More information about the Python-ideas
mailing list