Skip to content

Commit 9702eff

Browse files
committed
✨ HasTranspose
1 parent 05ee34c commit 9702eff

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,35 @@ def size(self) -> int | None:
151151
...
152152

153153

154+
class HasTranspose(Protocol):
155+
"""Protocol for array classes that support the transpose operation."""
156+
157+
def T(self) -> Self: # noqa: N802
158+
"""Transpose of the array.
159+
160+
The array instance must be two-dimensional. If the array instance is not
161+
two-dimensional, an error should be raised.
162+
163+
Returns:
164+
Self: two-dimensional array whose first and last dimensions (axes)
165+
are permuted in reverse order relative to original array. The
166+
returned array must have the same data type as the original
167+
array.
168+
169+
Notes:
170+
Limiting the transpose to two-dimensional arrays (matrices) deviates
171+
from the NumPy et al practice of reversing all axes for arrays
172+
having more than two-dimensions. This is intentional, as reversing
173+
all axes was found to be problematic (e.g., conflicting with the
174+
mathematical definition of a transpose which is limited to matrices;
175+
not operating on batches of matrices; et cetera). In order to
176+
reverse all axes, one is recommended to use the functional
177+
`PermuteDims` interface found in this specification.
178+
179+
"""
180+
...
181+
182+
154183
class Array(
155184
# ------ Attributes -------
156185
HasDType[DTypeT_co],
@@ -159,6 +188,7 @@ class Array(
159188
HasNDim,
160189
HasShape,
161190
HasSize,
191+
HasTranspose,
162192
# ------- Methods ---------
163193
HasArrayNamespace[NamespaceT_co],
164194
# -------------------------

tests/integration/test_numpy2p0.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ _: tuple[int | None, ...] = x_b.shape
8686
_: int | None = x_f32.size
8787
_: int | None = x_i32.size
8888
_: int | None = x_b.size
89+
90+
# Check Attribute `.T`
91+
_: xpt.Array[np.dtype[F32]] = x_f32.T
92+
_: xpt.Array[np.dtype[I32]] = x_i32.T
93+
_: xpt.Array[np.dtype[B]] = x_b.T

0 commit comments

Comments
 (0)