Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork11.9k
API: Add .mT attribute for arrays#23762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from3 commits
23f3651cdf850e13f0a2e086a91995da479e33c0025c346a358a0e0d710aaed0304796bd404058115919716e85c6185d19f5105346a9a938d256239e244a27f43dfcc7d3d37102529f1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -931,6 +931,13 @@ array_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored)) | ||
| return PyArray_Transpose(self, NULL); | ||
| } | ||
| static PyObject * | ||
| array_matrix_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored)) | ||
| { | ||
| int ndim = PyArray_NDIM(self); | ||
| return PyArray_SwapAxes(self, ndim - 2, ndim - 1); | ||
Kai-Striega marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| NPY_NO_EXPORT PyGetSetDef array_getsetlist[] = { | ||
| {"ndim", | ||
| (getter)array_ndim_get, | ||
| @@ -992,6 +999,10 @@ NPY_NO_EXPORT PyGetSetDef array_getsetlist[] = { | ||
| (getter)array_transpose_get, | ||
| NULL, | ||
| NULL, NULL}, | ||
| {"mT", | ||
| (getter)array_matrix_transpose_get, | ||
| NULL, | ||
| NULL, NULL}, | ||
| {"__array_interface__", | ||
| (getter)array_interface_get, | ||
| NULL, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import pytest | ||
Kai-Striega marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| from hypothesis import given | ||
| from hypothesis.extra.numpy import array_shapes | ||
| import numpy as np | ||
| from numpy.testing import assert_array_equal | ||
| def test_matrix_transpose_equals_transpose_1d(): | ||
| arr = np.arange(48) | ||
| assert_array_equal(arr.T, arr.mT) | ||
| def test_matrix_transpose_equals_transpose_2d(): | ||
| arr = np.arange(48).reshape((6, 8)) | ||
| assert_array_equal(arr.T, arr.mT) | ||
| @given(shape=array_shapes(max_dims=7)) | ||
| def test_matrix_transpose_equals_swapaxes(shape): | ||
| num_of_axes = len(shape) | ||
| total_elements = np.prod(shape) | ||
| arr = np.arange(total_elements).reshape(shape) | ||
Kai-Striega marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| tgt = np.swapaxes(arr, num_of_axes - 2, num_of_axes - 1) | ||
| mT = arr.mT | ||
Kai-Striega marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| assert_array_equal(tgt, mT) | ||