This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Implement matrix multiplication operator (PEP 465)
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: benjamin.peterson Nosy List: belopolsky, benjamin.peterson, jcea, njs, python-dev
Priority: normal Keywords: patch

Created on 2014-04-08 02:51 by belopolsky, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mat-mult1.patch benjamin.peterson, 2014-04-08 04:31 review
mat-mult2.patch benjamin.peterson, 2014-04-08 04:41 review
mat-mult3.patch benjamin.peterson, 2014-04-08 05:01 review
mat-mult4.patch benjamin.peterson, 2014-04-08 05:26 review
mat-mult5.patch benjamin.peterson, 2014-04-08 14:42 review
Messages (15)
msg215724 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-04-08 02:51
[Nathaniel Smith at numpy-discussion]

Guido just formally accepted PEP 465:
  https://mail.python.org/pipermail/python-dev/2014-April/133819.html
  http://legacy.python.org/dev/peps/pep-0465/#implementation-details

Yay.

The next step is to implement it, in CPython and in numpy. I have time
to advise on this, but not to do it myself, so, any volunteers? Ever
wanted to hack on the interpreter itself, with BDFL guarantee your
patch will be accepted (if correct)?

The todo list for CPython is here:
    http://legacy.python.org/dev/peps/pep-0465/#implementation-details
There's one open question which is where the type slots should be
added. I'd just add them to PyNumberMethods and then if someone
objects during patch review it can be changed.
msg215726 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 04:31
Here's a first patch. It works, but needs more tests (associativity, precedence, __rmatmul__, etc...) and also docs. I can work on that tomorrow.
msg215730 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 04:41
Here are changes to the operator module...
msg215731 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-04-08 04:57
I've got to the point where I can do

>>> import ast
>>> ast.dump(ast.parse('a @ b'))
"Module(body=[Expr(value=BinOp(left=Name(id='a', ctx=Load()), op=MatMult(), right=Name(id='b', ctx=Load())))])"


I'll post a link to my bitbucket clone shortly.
msg215732 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 05:01
Here is a nearly complete patch...
msg215734 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-04-08 05:09
Wow! That was quick. And I am still fighting with bitbucket.  Maybe I should stop duplicating the effort at this point.
msg215735 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 05:14
On Mon, Apr 7, 2014, at 22:09, Alexander Belopolsky wrote:
> 
> Alexander Belopolsky added the comment:
> 
> Wow! That was quick. And I am still fighting with bitbucket.  Maybe I
> should stop duplicating the effort at this point.

Sorry about that. I only saw the first message, where Nathaniel invites
an implementation. I didn't realize that was a direct quote from the
mailing list (not by you) and didn't notice you also assigned it to
yourself.
msg215736 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-04-08 05:23
Thanks for stepping in.  From a quick look at your patch I don't see anything that I would do much differently.

I noticed some whitespace issues in Include/token.h:

-#define AT              49	
-#define RARROW          50
-#define ELLIPSIS        51
+#define AT              49
+#define ATEQUAL		50
+#define RARROW          51
+#define ELLIPSIS        52

(Are you sure you want to re-enumerate RARROW and ELLIPSIS? Why not just give ATEQUAL the value of 52?)
msg215737 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-04-08 05:25
+   .. versionadded:: 3.4

Are you planning to use the time machine? :-)
msg215738 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 05:25
On Mon, Apr 7, 2014, at 22:23, Alexander Belopolsky wrote:
> 
> Alexander Belopolsky added the comment:
> 
> Thanks for stepping in.  From a quick look at your patch I don't see
> anything that I would do much differently.
> 
> I noticed some whitespace issues in Include/token.h:
> 
> -#define AT              49     
> -#define RARROW          50
> -#define ELLIPSIS        51
> +#define AT              49
> +#define ATEQUAL                50
> +#define RARROW          51
> +#define ELLIPSIS        52
> 
> (Are you sure you want to re-enumerate RARROW and ELLIPSIS? Why not just
> give ATEQUAL the value of 52?)

I thought it would be nicer if ATEQUAL was right after AT.
msg215739 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 05:25
On Mon, Apr 7, 2014, at 22:25, Alexander Belopolsky wrote:
> 
> Alexander Belopolsky added the comment:
> 
> +   .. versionadded:: 3.4
> 
> Are you planning to use the time machine? :-)

Good catch. :)
msg215740 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 05:26
Here's my latest and greatest version. I'll finish up after some sleep.
msg215762 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-08 14:42
Here's what I consider a complete patch.
msg215863 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-04-10 03:53
I think this patch is fairly straightforward, and I don't want it to rot, so here we go...
msg215865 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-10 03:56
New changeset c553d8f72d65 by Benjamin Peterson in branch 'default':
PEP 465: a dedicated infix operator for matrix multiplication (closes #21176)
http://hg.python.org/cpython/rev/c553d8f72d65
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65375
2014-06-12 00:57:19jceasetnosy: + jcea
2014-04-10 03:56:05python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg215865

resolution: fixed
stage: needs patch -> resolved
2014-04-10 03:53:14benjamin.petersonsetmessages: + msg215863
2014-04-09 12:55:14njssetnosy: + njs
2014-04-08 14:42:13benjamin.petersonsetfiles: + mat-mult5.patch
assignee: belopolsky -> benjamin.peterson
messages: + msg215762
2014-04-08 05:26:55benjamin.petersonsetfiles: + mat-mult4.patch

messages: + msg215740
2014-04-08 05:25:52benjamin.petersonsetmessages: + msg215739
2014-04-08 05:25:32benjamin.petersonsetmessages: + msg215738
2014-04-08 05:25:30belopolskysetmessages: + msg215737
2014-04-08 05:23:04belopolskysetmessages: + msg215736
2014-04-08 05:14:42benjamin.petersonsetmessages: + msg215735
2014-04-08 05:09:26belopolskysetmessages: + msg215734
2014-04-08 05:01:24benjamin.petersonsetfiles: + mat-mult3.patch

messages: + msg215732
2014-04-08 04:57:24belopolskysetmessages: + msg215731
2014-04-08 04:41:21benjamin.petersonsetfiles: + mat-mult2.patch

messages: + msg215730
2014-04-08 04:32:03benjamin.petersonsetfiles: + mat-mult1.patch

nosy: + benjamin.peterson
messages: + msg215726

keywords: + patch
2014-04-08 02:51:34belopolskycreate