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

Antoine Pitrou solipsis at pitrou.net
Fri Mar 14 13:57:40 CET 2014


On Fri, 14 Mar 2014 23:32:39 +1100
Steven D'Aprano <steve at pearwood.info> wrote:
> 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.

Well, "@" is not pronounced "at" in every country, so your mnemonic
will only work for a subset of Python users.

> There's nothing in @ that suggests "decorator", nor is there anything 
> about * that suggests scalar multiplication.

Apart from the fact that "*" is commonly used for multiplication in
most programming languages, you mean?

("@" either spells "decorator" or "in reply to" or "e-mail address" to
me, depending on the context)

> 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 is true, and I realize I might have got the convention reversed :-)

But, again, "dot product" is an English term which has no direct
equivalent in other languages. In French, "dot product" doesn't exist,
we only say "scalar product" and therefore the confusion doesn't exist.

> > 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.

It's logical, but it's much less useful, and it's also starting to look
really weird: since the operator space is scarce, it makes sense to
only provide those that have common use.

Regards

Antoine.




More information about the Python-ideas mailing list