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

Oscar Benjamin oscar.j.benjamin at gmail.com
Sat Mar 15 15:25:10 CET 2014


On 15 March 2014 13:47, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Mar 15, 2014 at 12:42:51PM +0000, Oscar Benjamin wrote:
>
>> Just to add to that: I personally would almost always use brackets
>> rather than rely on left- or right- associativity for something like
>> this.
>
>> A similar way that it can come up is with scalar-scalar vs
>> scalar-array multiplication e.g.:
>>
>> 2 * pi * x / L * A  # A is a big array
>>
>> I would rewrite that as
>>
>> (2 * pi * x / L) * A
>>
>> rather than rely on precedence/associativity.
>
> It seems to me that you actually are relying on precedence/
> associativity, otherwise you would have written it in fully-bracketed
> form like this:
>
> (((2 * pi) * x) / L) * A

The point is that I don't care about the order of evaluation for the
scalar part. Wherever you put the brackets works for me:

(2 * pi) * (x / L)
((2 * pi) * x) / L
(2 * (pi * x)) / L
2 * ((pi * x) / L)
2 * (pi * (x / L))

The reason I care about it with A is because A is a big array. Every
operation on A involves an expensive pass over all the elements of the
array as well as a large temporary allocation.

> It's your choice to include redundant brackets in an expression, but I
> try hard to avoid the extra visual noise.

I don't see those brackets as noise. To me it clearly shows that I'm
only doing one pass over the array which is useful information.


Oscar


More information about the Python-ideas mailing list