[Numpy-discussion] Re: Making `T` property Array API compatible
As one of the ones who argued (perhaps too) vociferously previously that .T transposing all axes was a mistake and that it should just be the last two, yes, let's deprecate for all but 2-d arrays (i.e., also warning for 0 and 1). Ideally, eventually (numpy 3.0?) it can replace `.mT`, but also fine if it ends up being removed. For the PR, be sure to include a pointer to .mT in the warning message. -- Marten Sebastian Berg writes: > On Sat, 2025-04-12 at 10:10 +, Mateusz Sokol wrote: >> Hi all! >> >> The Array API standard states that `T` property should only be >> applied to 2-dimensional arrays, in all other cases it should raise >> an error: >> https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.T >> >> To ensure that NumPy also follows this rule, I opened a PR that >> raises a warning for `arr.T` for non-2-dimensional arrays and >> scalars: https://github.com/numpy/numpy/pull/28678 > > > There was once a surprising amount of resistance to doing this exact > change a long time ago in NumPy and that is exactly why it never > happened earlier and that is why we have `.mT` and not just `.T` to > begin with. > > I am still happy with slowly deprecating it with a message to use > `arr.transpose()`, `np.moveaxis()`, or `.mT` when it is applies. > Maybe making sure that `.T` keeps working at least for 2-D, and think > about what to do for 1-D (probably an error), although I am not sure > about this unless there is a long term plan to consider allowing `.T` > to mean the same as `.mT`. > > > That sais, it is not correct to say there is any incompatibility! This > is an opinionated recommendation at most and if it was more it would > probably be a mistake there. > > - Sebastian > > > >> >> For non-2-dimensional arrays, the replacement for `arr.T` can be >> either: Array API compatible, namely `np.permute_dims(arr, >> range(arr.ndim)[::-1])`, or shorter, NumPy specific: >> `arr.transpose()`. >> >> The new discrepancy between `arr.T` and `arr.transpose()` is >> justified, as `T` is defined by the Array API, where `transpose` >> isn't and should retain the existing behavior. >> >> Please share your thoughts! >> >> Best regards, >> Mateusz >> ___ >> NumPy-Discussion mailing list -- numpy-discussion@python.org >> To unsubscribe send an email to numpy-discussion-le...@python.org >> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ >> Member address: sebast...@sipsolutions.net >> > > ___ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: m...@astro.utoronto.ca ___ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com
[Numpy-discussion] Re: Making `T` property Array API compatible
If NumPy were to make a move on the deprecation, then I think it would be reasonable to change the standard from > If the array instance is not two-dimensional, an error should be raised. to “if the array instance is not two-dimensional, behaviour should match `.mT`, or an error should be raised.” > On 14 Apr 2025, at 08:35, Sebastian Berg wrote: > > On Sat, 2025-04-12 at 10:10 +, Mateusz Sokol wrote: >> Hi all! >> >> The Array API standard states that `T` property should only be >> applied to 2-dimensional arrays, in all other cases it should raise >> an error: >> https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.T >> >> To ensure that NumPy also follows this rule, I opened a PR that >> raises a warning for `arr.T` for non-2-dimensional arrays and >> scalars: https://github.com/numpy/numpy/pull/28678 > > > There was once a surprising amount of resistance to doing this exact > change a long time ago in NumPy and that is exactly why it never > happened earlier and that is why we have `.mT` and not just `.T` to > begin with. > > I am still happy with slowly deprecating it with a message to use > `arr.transpose()`, `np.moveaxis()`, or `.mT` when it is applies. > Maybe making sure that `.T` keeps working at least for 2-D, and think > about what to do for 1-D (probably an error), although I am not sure > about this unless there is a long term plan to consider allowing `.T` > to mean the same as `.mT`. > > > That sais, it is not correct to say there is any incompatibility! This > is an opinionated recommendation at most and if it was more it would > probably be a mistake there. > > - Sebastian > > > >> >> For non-2-dimensional arrays, the replacement for `arr.T` can be >> either: Array API compatible, namely `np.permute_dims(arr, >> range(arr.ndim)[::-1])`, or shorter, NumPy specific: >> `arr.transpose()`. >> >> The new discrepancy between `arr.T` and `arr.transpose()` is >> justified, as `T` is defined by the Array API, where `transpose` >> isn't and should retain the existing behavior. >> >> Please share your thoughts! >> >> Best regards, >> Mateusz >> ___ >> NumPy-Discussion mailing list -- numpy-discussion@python.org >> To unsubscribe send an email to numpy-discussion-le...@python.org >> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ >> Member address: sebast...@sipsolutions.net >> > > ___ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: lucas.coll...@gmail.com ___ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com
[Numpy-discussion] Re: Making `T` property Array API compatible
On Sat, 2025-04-12 at 10:10 +, Mateusz Sokol wrote: > Hi all! > > The Array API standard states that `T` property should only be > applied to 2-dimensional arrays, in all other cases it should raise > an error: > https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.T > > To ensure that NumPy also follows this rule, I opened a PR that > raises a warning for `arr.T` for non-2-dimensional arrays and > scalars: https://github.com/numpy/numpy/pull/28678 There was once a surprising amount of resistance to doing this exact change a long time ago in NumPy and that is exactly why it never happened earlier and that is why we have `.mT` and not just `.T` to begin with. I am still happy with slowly deprecating it with a message to use `arr.transpose()`, `np.moveaxis()`, or `.mT` when it is applies. Maybe making sure that `.T` keeps working at least for 2-D, and think about what to do for 1-D (probably an error), although I am not sure about this unless there is a long term plan to consider allowing `.T` to mean the same as `.mT`. That sais, it is not correct to say there is any incompatibility! This is an opinionated recommendation at most and if it was more it would probably be a mistake there. - Sebastian > > For non-2-dimensional arrays, the replacement for `arr.T` can be > either: Array API compatible, namely `np.permute_dims(arr, > range(arr.ndim)[::-1])`, or shorter, NumPy specific: > `arr.transpose()`. > > The new discrepancy between `arr.T` and `arr.transpose()` is > justified, as `T` is defined by the Array API, where `transpose` > isn't and should retain the existing behavior. > > Please share your thoughts! > > Best regards, > Mateusz > ___ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: sebast...@sipsolutions.net > ___ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com