[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power
Steven D'Aprano
steve at pearwood.info
Sat Mar 15 17:26:49 CET 2014
On Sat, Mar 15, 2014 at 04:22:33PM +0100, Antoine Pitrou wrote:
> On Sun, 16 Mar 2014 02:11:43 +1100
> Steven D'Aprano <steve at pearwood.info> wrote:
> > > The possible reason given in the PEP is very weak and amounts to
> > > premature optimization:
> >
> > I don't think it's premature optimization. Sometimes we do know ahead of
> > time that a calculation done one way will be faster than doing it
> > another way: you don't have to "try it and see" to realise that
> > repeatedly adding strings in a for-loop can be O(N**2) versus O(N) for
> > using str.join().
>
> It's premature optimization because the PEP is proposing to enforce it
> at the language level.
The PEP leaves the question of left- versus right-associativity open. It
has to be decided one way or the other. What evidence would you want to
see before saying "It's not premature optimization, it's a justified
optimization"?
> We didn't change *the language* so that "str +="
> allows for O(N) repeated concatenation; instead we tell people that
> "".join() should be used for repeated concatenation. Why would our
> course of action be different for matrix multiplication?
Exactly the same argument applies to left-associativity. Why should the
language enforce optimizing the first case over the second?
row_vector @ matrix @ matrix
vs.
matrix @ matrix @ column_vector
Since infix operators have to have an associativity, the language
implicitly has to optimize one case or the other. We can't sit on the
fence and refuse to favour one over the other.
If the matrix-using community says that both cases are equally common,
(or uncommon, perhaps) and they are indifferent between left- and
right-associativity, then it makes sense to stick to what the majority
of other operators do. I agree with that.
But if the matrix-using community comes down in favour of right-
associativity, because Practicality Beats Purity and the second case is
sufficiently more common than the first case, then I think it is a
*broken design* to force left-associativity on them just to satisfy the
expectations of people who aren't going to use the @ operator.
I hope I've made my position clear here: Guido has given his blessing to
the possibility of making @ right-associative, and I think that the only
people who should care what the associativity of @ ends up being is the
community of matrix-multiplication users. They should make that
decision, based on what it useful to them. I don't care what colour this
bikeshed ends up having, I just want to see that it is the users of that
shed that make the decision.
--
Steven
More information about the Python-ideas
mailing list