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.
PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
21–30 of 42 posts
Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#22I'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?
Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#23Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#24Seems to me that Haskell's ability to infix named functions is a much nicer solution over-all
Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#25Huh? 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.
Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#26A 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
#27Seems 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 ?
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
#28S = (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
#29Re: PEP 465 – Dedicated infix operators for matrix multiplication and matrix power
#30Otherwise 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.