Skip to content

Commit 53ddb98

Browse files
committed
✨ HasTranspose
Signed-off-by: nstarman <[email protected]>
1 parent ac4e177 commit 53ddb98

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ def size(self) -> int | None:
152152
...
153153

154154

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

tests/integration/test_numpy1p0.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ _: tuple[int | None, ...] = x_i32.shape
7575
# Check Attribute `.size`
7676
_: int | None = x_f32.size
7777
_: int | None = x_i32.size
78+
79+
# Check Attribute `.T`
80+
_: xpt.Array[dtype[Any]] = x_f32.T
81+
_: xpt.Array[dtype[Any]] = x_i32.T

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)