TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python 3.5 to Ship with Matrix Multiplication Operator

268 pointsby madisonmayabout 11 years ago

22 comments

zurnabout 11 years ago
To everyone arguing against special-casing matrix multiplication, please base your arguments on the PEP's "Motivation" section to avoid too much of rehashing the obvious. It even has a subsection "But isn't matrix multiplication a pretty niche requirement?"
评论 #7552183 未加载
评论 #7553018 未加载
评论 #7556872 未加载
iskanderabout 11 years ago
This is surprisingly detailed design rationale which weighs many alternatives and gives careful consideration to possible tradeoffs. Great job by Nathaniel Smith and the numerical Python community.
willvarfarabout 11 years ago
The nice thing is that it makes a nice symbol `@` available to objects that aren&#x27;t matrices, to do with as they want ...<p>The (ab)use seems endless :)
评论 #7552916 未加载
评论 #7552548 未加载
评论 #7552016 未加载
评论 #7552043 未加载
评论 #7555330 未加载
madisonmayabout 11 years ago
Guido has accepted the proposal and work has now begun to add support for the new operator: <a href="http://bugs.python.org/issue21176" rel="nofollow">http:&#x2F;&#x2F;bugs.python.org&#x2F;issue21176</a>
lmmabout 11 years ago
After a few years in Scala, where all operators are methods and vice versa (2 + 3 is just sugar for 2.+(3)), the method-operator distinction just seems <i>weird</i>.
评论 #7552593 未加载
评论 #7552584 未加载
评论 #7552227 未加载
lutormabout 11 years ago
Since this is just a special case of a multiply-and-add indexing loop, maybe they should just introduce some form of tensor notation, so that A[i,j]*B[j,k] is the matrix product of A and B? That would extend to so many more use cases than just a 2d matrix product.
评论 #7555497 未加载
JadeNBabout 11 years ago
The PEP claims that:<p>&gt; Matrix multiplication is more of a special case. It&#x27;s only defined on 2d arrays (also known as &quot;matrices&quot;)<p>but this is not true. Matrix multiplication is just a special case of contraction of indices in a tensor (<a href="https://en.wikipedia.org/wiki/Tensor_contraction)—probably" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tensor_contraction)—probably</a> the most frequently used case, but not the only one. I&#x27;m certainly not arguing for the inclusion of general tensor-manipulating operators in Python, but it does seem to suggest a sensible alternative to:<p>&gt; For inputs with more than 2 dimensions, we treat the last two dimensions as being the dimensions of the matrices to multiply, and &#x27;broadcast&#x27; across the other dimensions.<p>namely, just contract on the inner indices. That is, arr(n1, ..., nk, m) @ arr(m, p1, ... pl) = arr(n1, ..., nk, p1, ..., pl).<p>EDIT: scythe (<a href="https://news.ycombinator.com/item?id=7554013" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7554013</a>) already pointed this out in passing.
评论 #7556915 未加载
JadeNBabout 11 years ago
My issue is not with the matrix-multiplication operator—I&#x27;m a mathematician before I&#x27;m a programmer, and so am all for it—but with the vector-to-matrix promotions: why not just consistently promote vectors to <i>columns</i> (or rows, if BDFL swings that way)? This would achieve almost the same effect as promoting to rows or columns as needed, and would avoid the non-associativity problem that the PEP mentions.<p>This PEP seems to imply that the cost would be a profusion of superfluous `newaxis`s, but I can&#x27;t see that: it seems to me that you would need only to remember which kind of promotion works by default, and sprinkle in a `.T` whenever you need the other kind. (Anyone who&#x27;s uncomfortable with lots of `.T`s in matrix-crunching code is not, I submit, someone who writes or reads lots of matrix-crunching code.)
segphaultabout 11 years ago
There are a few handy things that I&#x27;d like to see added to Python&#x27;s syntax. A dedicated operator for matrix multiplication isn&#x27;t one of them.
评论 #7551932 未加载
patternabout 11 years ago
I really like this snippet from the justification for the symbol chosen: &quot;APL apparently used +.×, which by combining a multi-character token, confusing attribute-access-like . syntax, and a unicode character, ranks somewhere below U+2603 SNOWMAN on our candidate list&quot;.<p>One of the reasons I have been such a fan of Python for so long is the relatively no-nonsense approach to design decisions that many others would have rushed through.
pekkabout 11 years ago
Python&#x27;s syntax is already pretty ponderous, this kind of thing pushes it rapidly toward bash-like incomprehensibility. I&#x27;ve been a big fan of the language for years but this kind of thing makes me consider jumping to Lua or something which is more sparing with new complicated sigils.
评论 #7555073 未加载
评论 #7555764 未加载
NamTafabout 11 years ago
Matlab&#x27;s approach to using * as the matrix multiplier makes sense, because every numerical variable is an array with integers&#x2F;floats simply being a 1x1 array. Using .* then as the elementwise version works.<p>I&#x27;d have personally preferred to see Python do type testing on the variables - it already does. For example:<p>&#x27;quick&#x27; + &#x27;fox&#x27; = &#x27;quickfox&#x27; 3 + 5 = 8 [3] + [5] = [3,5]<p>So why not make it a case where * on an int or float does the &#x27;standard&#x27; multiplication that already exists whereas * on an array does matrix multiplication?<p>You arrive at the problem of then not having an element-wise version of the multiplication but it&#x27;s not as if this solves that problem anyway.<p>What am I missing to make that a problem?
评论 #7552556 未加载
评论 #7552551 未加载
BoppreHabout 11 years ago
I think &quot;Matrix Multiplication&quot; is a little too specific for a syntax change, but I welcome the addition of a new operator to be overloaded.<p>Am I missing some useful applications of matrix multiplication on a general purpose language?
评论 #7551959 未加载
评论 #7551904 未加载
评论 #7554815 未加载
评论 #7551876 未加载
评论 #7556119 未加载
Aardwolfabout 11 years ago
They gave a rationale for @ compared to other not yet used ascii characters.<p>But they didn&#x27;t consider a combination of multiple characters, like e.g. 3 * s?<p>(NOTE: Hacker news does not allow typing 3 * in a row)
评论 #7552265 未加载
JoshTriplettabout 11 years ago
Personally, I&#x27;d really like to see an equivalent in Python to Haskell&#x27;s `binop` , to turn an arbitrary two-argument function into a binary operator.
评论 #7556047 未加载
mckossabout 11 years ago
Why not use the existing convention for names of infix operators like __mmul__ but allow then to be infix as well.. So, we could write A __mmul__ B.<p>I think this is more clear than @ and is already familiar to programmers that deal with operator overloading methods. It would also enable the addition of arbitrary infix operators to python.
评论 #7554105 未加载
festabout 11 years ago
I hope this is not a first step towards transforming python syntax into perl.
评论 #7552048 未加载
beefsackabout 11 years ago
You&#x27;ve got to wonder, does matrix multiplication happen so often and is it so inconvenient to call as a function that it&#x27;s worth complicating the syntax? I&#x27;m really not sure about this one.
评论 #7551950 未加载
评论 #7551963 未加载
评论 #7553828 未加载
评论 #7552086 未加载
评论 #7552161 未加载
评论 #7554279 未加载
riffraffabout 11 years ago
one question I could not see from the PEP page: why not use a spelling &quot;dot&quot; instead of a symbol? i.e. &quot;a dot b&quot;
评论 #7552687 未加载
评论 #7552624 未加载
waitingkuoabout 11 years ago
Currently, we can use * to multiply two numpy matrix. We&#x27;ll have two choices, * and @, to do the matrix multiply things in numpy. Is it pythonic? Or should we deprecate * ?
评论 #7552571 未加载
评论 #7552581 未加载
评论 #7553912 未加载
bsaulabout 11 years ago
from the pep as to why not using a type that defines _mul_ as matrix multiplication : &quot; The result is a strong consensus among both numpy developers and developers of downstream packages that numpy.matrix should essentially never be used, because of the problems caused by having conflicting duck types for arrays &quot;<p>looks to me like an issue with python lack of static type checking. if you need to define an operator every time two types conflict, you&#x27;re in for a long list...
0xdeadbeefbabeabout 11 years ago
Dear Guido,<p>You lost me at 3.