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

Aaron Meurer asmeurer at gmail.com
Sat Mar 15 18:14:58 CET 2014


On Sat, Mar 15, 2014 at 6:27 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 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.

I write that all the time when working with SymPy. And the
associativity of ** is *very* important. If ** were left associative
it would not be useful, because, at least for positive x, y, and z,
(x**y)**z is the same as x**(y*z). If you really want the stacked
power it has to be right associative.

Aaron Meurer

>
> 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.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/aHVlL6BADLY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


More information about the Python-ideas mailing list