Skip to content

Commit dfdc9fa

Browse files
committed
feat: HasTranspose
1 parent 6fd91be commit dfdc9fa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/array_api_typing/_array.py

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

132132

133+
class HasTranspose(Protocol):
134+
"""Protocol for array classes that support the transpose operation."""
135+
136+
def T(self) -> Self: # noqa: N802
137+
"""Transpose of the array.
138+
139+
The array instance must be two-dimensional. If the array instance is not
140+
two-dimensional, an error should be raised.
141+
142+
Returns:
143+
Self: two-dimensional array whose first and last dimensions (axes)
144+
are permuted in reverse order relative to original array. The
145+
returned array must have the same data type as the original
146+
array.
147+
148+
Notes:
149+
Limiting the transpose to two-dimensional arrays (matrices) deviates
150+
from the NumPy et al practice of reversing all axes for arrays
151+
having more than two-dimensions. This is intentional, as reversing
152+
all axes was found to be problematic (e.g., conflicting with the
153+
mathematical definition of a transpose which is limited to matrices;
154+
not operating on batches of matrices; et cetera). In order to
155+
reverse all axes, one is recommended to use the functional
156+
`PermuteDims` interface found in this specification.
157+
158+
"""
159+
...
160+
161+
133162
# ============================================================================
134163

135164

@@ -583,6 +612,7 @@ class Array(
583612
HasNDim,
584613
HasShape,
585614
HasSize,
615+
HasTranspose,
586616
# ------ Methods -------
587617
HasArrayNamespace[NS_co],
588618
CanArrayPos,

0 commit comments

Comments
 (0)