Live data from Hacker News

PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

python.org

21–30 of 42 posts

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#21
Guido has indicated that he is ready to accept the PEP after a few details are worked out: https://groups.google.com/forum/#!msg/python-ideas/aHVlL6BAD...

So it appears that we will indeed have @ for matrix multiplication in Python 3.5! This is a feature the numeric python computing has been hoping for for a long, long time.

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#22

I'm kind of baffled by this. Python has operator overloading, so what's wrong with using * for matrix multiplication? I know there's a bit in the PEP that claims to answer this, but I can't understand their argument. Can someone explain?

It looks like it says there are enough cases where libraries crave two multiplication operators, elementwise multiplication and matrix multiplication, that it makes sense to add an operator to the language.

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#25
post #9

Huh? Isn't it a question of whether you are doing matrix or array ops, so the type system is the issue? I have rarely needed to do elementwide multiply on matrices, and if I do, in Eigenproblem I can just call mat.array() to get an ArrayView. Really, the problem is that NumPy is a crappily designed library primarily intended for array ops, and is just not well suited for linear algebra.

Indeed, especially as the functions that are + and * can vary based on type too, as you can have them defined over any ring.

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#26
It kind of seems to me that it's actually vectored multiplication that's the odd one out that should maybe have a special syntax, maybe even one that applies to many operations and not just multiplication.

A matrix is a Thing that happens to also be a collection. A vector you want to multiply is just a collection. Really vector multiplication is just a map operation, so make some syntactic sugar to generate an operator comprehension:

    a = [1,2,3]
    b = [4,5,6]
    c = a [*] b # or something
    # becomes
    c = [x * y for (x,y) in zip(a,b)]
    # or
    c = a.__vecmul__(b)
This would make more sense to me.

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#27

Seems to me that Haskell's ability to infix named functions is a much nicer solution over-all

I agree. I wish python had the capability to infix functions. Maybe the topic for a new PEP ?

This is this[1] workaround.

I wonder if a whole set of infix matrix operations could be added to reduce the need to load numpy for simple tasks.

    A @* B
    A @. B
    A @+ B
[1] http://code.activestate.com/recipes/384122/

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#28
This looks bad:

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

but this:

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

is close enough to:

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

although bit less readable due to being bit longer.

Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power

#30
The bloody associativity issue is unresolved...? Of course it should be right associative.

Otherwise ABx means (AB)x which is idiotic for numerical work. One would have to write A(Bx) to get reasonable performance which isn't far enough away from A.dot(B.dot(x)) to justify the implementation overhead.

Lastly, as awful as it sounds, it is nice when awfully expensive things like 10K by 10K matmats have some visual weight. It makes people think about how they're written down.

Post reply on HN