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

Nathaniel Smith njs at pobox.com
Fri Mar 14 17:20:38 CET 2014


On Fri, Mar 14, 2014 at 3:46 PM, Zachary Ware
<zachary.ware+pyideas at gmail.com> wrote:
> I agree with Paul, @/@@ just look scary as operators.  Here's a few
> multi-character options I've come up with that I would like to see
> shot down in flames before @/@@ are added to Python:
>
>>< (for multiplication, not sure about exponentiation.  I only like it because it's the shortest thing I've come up with that looks somewhat like multiplication)
> [*] / [**] ([] make me think 'matrix', but this might be confusing to
> the parser)
> |*| / |**| (pretty close to [], shouldn't confuse the parser)

I really liked the [*] or |*| "footnote operator" idea, and got all
excited, until I tried it :-). Using the PEP's example of a linear
hypothesis test, compare:

    S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

versus

    S = (H [*] beta - r).T [*] inv(H [*] V [*] H.T) [*] (H [*] beta - r)

    S = (H |*| beta - r).T |*| inv(H |*| V |*| H.T) |*| (H |*| beta - r)

A big part of the motivation for wanting an infix operator for this
kind of expression is that when using function syntax, the visual
clutter from all the required parentheses makes expressions hard to
parse by eye. [*] has this same problem of introducing visual nesting
and making it hard to pick out which vertical shapes are actually
grouping parens, and which are the operators. E.g., try counting how
many sub-expressions there are in each of these expressions -- I find
it much easier to pick out the 3 groups in the top example than in the
bottom two. Sort of a variant of leaning toothpick syndrome...

-n


More information about the Python-ideas mailing list