Skip to content

Commit a53c176

Browse files
committed
✨ HasTranspose
Signed-off-by: nstarman <[email protected]>
1 parent 7f1eb4e commit a53c176

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/array_api_typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"HasNDim",
1010
"HasShape",
1111
"HasSize",
12+
"HasTranspose",
1213
"__version__",
1314
"__version_tuple__",
1415
)
@@ -22,5 +23,6 @@
2223
HasNDim,
2324
HasShape,
2425
HasSize,
26+
HasTranspose,
2527
)
2628
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"HasNDim",
88
"HasShape",
99
"HasSize",
10+
"HasTranspose",
1011
)
1112

1213
from types import ModuleType
@@ -158,6 +159,36 @@ def size(self) -> int | None:
158159
...
159160

160161

162+
class HasTranspose(Protocol):
163+
"""Protocol for array classes that support the transpose operation."""
164+
165+
@property
166+
def T(self) -> Self: # noqa: N802
167+
"""Transpose of the array.
168+
169+
The array instance must be two-dimensional. If the array instance is not
170+
two-dimensional, an error should be raised.
171+
172+
Returns:
173+
Self: two-dimensional array whose first and last dimensions (axes)
174+
are permuted in reverse order relative to original array. The
175+
returned array must have the same data type as the original
176+
array.
177+
178+
Notes:
179+
Limiting the transpose to two-dimensional arrays (matrices) deviates
180+
from the NumPy et al practice of reversing all axes for arrays
181+
having more than two-dimensions. This is intentional, as reversing
182+
all axes was found to be problematic (e.g., conflicting with the
183+
mathematical definition of a transpose which is limited to matrices;
184+
not operating on batches of matrices; et cetera). In order to
185+
reverse all axes, one is recommended to use the functional
186+
`PermuteDims` interface found in this specification.
187+
188+
"""
189+
...
190+
191+
161192
class Array(
162193
# ------ Attributes -------
163194
HasDType[DTypeT_co],

tests/integration/test_numpy1p0.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ _: xpt.HasShape = nparr
7474
_: xpt.HasShape = nparr_i32
7575
_: xpt.HasShape = nparr_f32
7676

77+
# =========================================================
78+
# `xpt.HasTranspose`
79+
80+
_: xpt.HasTranspose = nparr
81+
_: xpt.HasTranspose = nparr_i32
82+
_: xpt.HasTranspose = nparr_f32
83+
7784
# =========================================================
7885
# `xpt.Array`
7986

tests/integration/test_numpy2p0.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ _: xpt.HasSize = nparr_i32
8585
_: xpt.HasSize = nparr_f32
8686
_: xpt.HasSize = nparr_b
8787

88+
# =========================================================
89+
# `xpt.HasTranspose`
90+
91+
_: xpt.HasTranspose = nparr
92+
_: xpt.HasTranspose = nparr_i32
93+
_: xpt.HasTranspose = nparr_f32
94+
_: xpt.HasTranspose = nparr_b
95+
8896
# =========================================================
8997
# `xpt.Array`
9098

0 commit comments

Comments
 (0)