summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-04-10 03:55:56 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-04-10 03:55:56 (GMT)
commitd51374ed78a3e3145911a16cdf3b9b84b3ba7d15 (patch)
tree31f9086f20f5b8923604f41f1a4d139fa809aaed /Objects
parent2aad6ef77419887f5875ba942e9369b4bdd34a5e (diff)
downloadcpython-d51374ed78a3e3145911a16cdf3b9b84b3ba7d15.zip
cpython-d51374ed78a3e3145911a16cdf3b9b84b3ba7d15.tar.gz
cpython-d51374ed78a3e3145911a16cdf3b9b84b3ba7d15.tar.bz2
PEP 465: a dedicated infix operator for matrix multiplication (closes #21176)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c14
-rw-r--r--Objects/typeobject.c10
-rw-r--r--Objects/typeslots.inc2
3 files changed, 26 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 38ddb0f..aeb8634 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -932,6 +932,12 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
}
PyObject *
+PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
+{
+ return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
+}
+
+PyObject *
PyNumber_FloorDivide(PyObject *v, PyObject *w)
{
return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
@@ -1012,6 +1018,7 @@ INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
+INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
PyObject *
PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
@@ -1078,6 +1085,13 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
}
PyObject *
+PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
+{
+ return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
+ NB_SLOT(nb_matrix_multiply), "@=");
+}
+
+PyObject *
PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
{
return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index c21b397..6129523 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4469,6 +4469,8 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
COPYNUM(nb_inplace_true_divide);
COPYNUM(nb_inplace_floor_divide);
COPYNUM(nb_index);
+ COPYNUM(nb_matrix_multiply);
+ COPYNUM(nb_inplace_matrix_multiply);
}
if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
@@ -5605,6 +5607,7 @@ slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
+SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, "__matmul__", "__rmatmul__")
SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
@@ -5698,6 +5701,7 @@ SLOT0(slot_nb_float, "__float__")
SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
+SLOT1(slot_nb_inplace_matrix_multiply, "__imatmul__", PyObject *, "O")
SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
/* Can't use SLOT1 here, because nb_inplace_power is ternary */
static PyObject *
@@ -6278,6 +6282,12 @@ static slotdef slotdefs[] = {
"__index__($self, /)\n--\n\n"
"Return self converted to an integer, if self is suitable"
"for use as an index into a list."),
+ BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply,
+ "@"),
+ RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply,
+ "@"),
+ IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply,
+ wrap_binaryfunc, "@="),
MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
"__len__($self, /)\n--\n\nReturn len(self)."),
MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
diff --git a/Objects/typeslots.inc b/Objects/typeslots.inc
index caa1e03..2ed99d8 100644
--- a/Objects/typeslots.inc
+++ b/Objects/typeslots.inc
@@ -73,3 +73,5 @@ offsetof(PyHeapTypeObject, ht_type.tp_traverse),
offsetof(PyHeapTypeObject, ht_type.tp_members),
offsetof(PyHeapTypeObject, ht_type.tp_getset),
offsetof(PyHeapTypeObject, ht_type.tp_free),
+offsetof(PyHeapTypeObject, as_number.nb_matrix_multiply),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_matrix_multiply),