API: Add .mT attribute for arrays#23762
Conversation
The lower dimensional (1d and 2d) tests compare the behaviour of `.mT` against `.T` not swapaxes.
The test generates random shapes of a given length. This was previously used with min_dims=3. This excludes arrays of dimension 0, 1, 2 which we want. Instead we test all dimensions up to 7.
seberg
left a comment
There was a problem hiding this comment.
Looks like a good start, will mark as draft for now if you don't mind, just unmark it when things are settling (not that I think there is much missing)!
This commit reduces the memory required by the test. Instead of explicitly constructing an array of shape `shape`, we now construct only the last dim and broadcast over that.
Hypothesis seems to be overkill for the provided situation. Use explicitly defined values instead.
This commit causes `.mT` to raise a ValueError if ndim < 2. This is because the matrix transpose is undefined for 0 or 1 dimensional arrays and having it implemented may lead to confusion by users.
|
Thanks for working on this Kai!
I'd say yes we want that (because of the analogue to |
|
This will need a release note. |
|
@rgommers I'm happy to work on it in this PR, but I'm still learning about NumPy internals, meaning that the commit history could become quite messy as I learn (of course I'll try my best to keep it clean). As a summary, I think we should match the functions for
The next step for me is to add
I'm not really sure if this is the right direction, or if we even want to add |
|
I would not touch the NumPy C-API with this PR. It is enough to add the attribute to ndarray and ma. If in the future we wish to export it via the C-API, that can be another PR. |
We don't need/want both a |
|
I rather like the idea of an |
This was decided to not be needed in the PR review. Skipping CI as I know it doesn't work and don't need CI to tell me that. [skip ci]
|
I've removed the code adding it to the C-API (I think) and stopped working on adding an I may need some help structuring the code so that I can share it between |
|
You have to add the new function to a header file now and include it, probably. If you check the test failures, you see they fail on compiler warnings. |
|
@seberg I've finally got the masked array implementation working! I'm not sure if it's done correctly, could you take a look at my last commit? If it's right, I think I have three things left:
|
numpy/ma/core.py
Outdated
|
|
||
| if self._mask is nomask: | ||
| # TODO: Should this return a masked_array or regular array? | ||
| return self._data.mT |
There was a problem hiding this comment.
A masked array. Otherwise, seems good. Although .transpose() has some _update_from_self stuff, it may make sense to just stick close to it in the off-chance it matters.
There was a problem hiding this comment.
I'm struggling to replicate .transpose() as it uses the function version, not the attribute. I haven't been figured out how to do this (yet) and will leave it for another PR - maybe I can come back and tidy it up then if there are problems?
This function will be added in another PR.
seberg
left a comment
There was a problem hiding this comment.
LGTM, thanks, just should add the versionadded.
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
|
Thanks, lets put it in. After merging, I noticed that the docs actually render slightly incorrectly. Not a big deal, but maybe you want to follow up? I guess we need |
|
Thanks that's my first C level contribution 👌. Let's hope for many more. I'll follow up with the docs rendering properly. |
This is the initial commit for adding an
mT, or matrix transpose, attribute for arrays. It is still a work in progress and not done yet. Please note that this is my first time working on NumPy's C-API. The PR is mainly being submitted this early to confirm that I am on the right track.Things that are still needed:
[ ]np.transposearray_matrix_transpose_getand exit early if 1d arrayAnother note;
ndarray.Thas a corresponding functionnp.transpose, as part of this PR should we add a corresponding function for.mTe.g.np.matrix_transpose? If this is the case, should I add a function toPyArray_MatrixTransposetoshape.crather than defining the function insidegetset.cas is done with the regular transpose function?