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

Antoine Pitrou solipsis at pitrou.net
Sat Mar 15 12:27:33 CET 2014


On Sat, 15 Mar 2014 18:35:09 +1100
Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, Mar 14, 2014 at 09:09:02PM -0700, Bruce Leban wrote:
> > I think making @ right associative would make this less suitable for other
> > uses. Example
> > 
> > someclassobject @ somevalue @ somevalue
> > 
> > won't work with right-associativity. 
> 
> Why not? It works with the other right-associative operator:
> 
>     x**y**z
> 
> "Works" depends on what you expect it to do.

The real question is why @ would be right-associative. "**" is very
rarely used in a chained manner as the above, so its associativity
isn't really important (I don't think I have ever written "x**y**z").
@ will be used routinely in a chained manner, so the question is more
important here.

The possible reason given in the PEP is very weak and amounts to
premature optimization:

"""It's been suggested that @ should be right-associative, on the
grounds that for expressions like Mat @ Mat @ vec, the two different
evaluation orders produce the same result, but the right-associative
order Mat @ (Mat @ vec) will be faster and use less memory than the
left-associative order (Mat @ Mat) @ vec. (Matrix-vector multiplication
is much cheaper than matrix-matrix multiplication)."""

If that's the only reason, then I'd like @ to be left-associative.

Regards

Antoine.




More information about the Python-ideas mailing list