-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Add np.linalg.matrix_transpose #22767
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Unlike np.transpose(), matrix_transpose() only operates on the last 2 dimensions of an array, and raises an exception if it is 0- or 1-dimensional. This is a function from the array API specification. See https://data-apis.org/array-api/latest/API_specification/generated/signatures.linear_algebra_functions.matrix_transpose.html
LGTM on first look, needs a release note. Is there a particular reason for the name "matrix_transpose"? Something shorter would be nice, but the current name has the advantage of being unambiguous. What about a method, "mT"? I would be in favor of such an addition despite the hesitance to add new methods. |
That's the idea. Ideally just
A |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, it sounds like nobody minded the addition. The only thing is whether we should coerce, since NumPy does that everywhere (this does not affect the internal usage in linalg).
If we don't coerce, the function has little to add over an attribute?
def _matrix_transpose_dispatcher(a): | ||
return (a,) | ||
|
||
@array_function_dispatch(_matrix_transpose_dispatcher) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@array_function_dispatch(_matrix_transpose_dispatcher) | |
@array_function_dispatch(_matrix_transpose_dispatcher, module='numpy.linalg') |
although I guess you want to copy it also into the main namespace, in which case we have to decide for one of the two modules.
""" | ||
if a.ndim < 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, this will effectively not work for many array-likes, which is fairly untypical for NumPy. Is that what we want? swapaxes
itself checks for the attribute or coerces unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we implement .mT
then this function should possibly just try obj.mT
and otherwise coerce?
a = np.array([[[1, 2], [3, 4]], [[-1, -2], [-3, -4]]]) | ||
aT = np.array([[[1, 3], [2, 4]], [[-1, -3], [-2, -4]]]) | ||
assert_array_equal(matrix_transpose(a), aT) | ||
assert_array_equal(matrix_transpose(aT), a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also check for may_share_memory()
or a.base is a
just for completeness sake.
To answer a few questions:
|
This was superseded by gh-25155 I think, we've got a |
Unlike np.transpose(), matrix_transpose() only operates on the last 2 dimensions of an array, and raises an exception if it is 0- or 1-dimensional.
This is a function from the array API specification. See https://data-apis.org/array-api/latest/API_specification/generated/signatures.linear_algebra_functions.matrix_transpose.html
This was mentioned in this issue #13797
Some things left to do, which I would like some help with:
matrix_transpose
should also be added to the mainnp
namespace. What is the correct way to do that? (the spec specifies it should be in both the main namespace and thelinalg
sub-namespace)mT
attribute that can be added to the array object, which is a shorthand formatrix_transpose
. Should I add that in this PR? There was some discussion on the list about it, which is why I haven't done it yet.np.linalg.linalg.transpose
function. Should I re-include that function for backwards compatibility?@array_function_dispatch
to the function? Is there anything else I need to do?np.swapaxes(x, -1, -2)
(with a shape check to give a better error message for ndim < 2)ndim < 2
?np.matrix_transpose
andndarry.mT
.