Skip to content

Commit 58a1209

Browse files
committed
Improve speed of transpose
- Add shortcuts for simple, easy cases
1 parent c120210 commit 58a1209

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

coloraide/algebra.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,9 +3187,19 @@ def transpose(array: ArrayLike | float) -> float | Array:
31873187
"""
31883188

31893189
s = shape(array)[::-1] # type: Shape
3190-
if not s:
3191-
return array # type: ignore[return-value]
3190+
l = len(s)
31923191

3192+
# Number
3193+
if l == 0:
3194+
return array # type: ignore[return-value]
3195+
# Vector
3196+
if l == 1:
3197+
return list(array) # type: ignore[return-value, arg-type]
3198+
# 2 x 2 matrix
3199+
if l == 2:
3200+
return list(list(z) for z in zip(*array)) # type: ignore[misc]
3201+
3202+
# N x M matrix
31933203
if s and s[0] == 0:
31943204
s = s[1:] + (0,)
31953205
total = prod(s[:-1])
@@ -3201,7 +3211,7 @@ def transpose(array: ArrayLike | float) -> float | Array:
32013211

32023212
# Calculate data sizes
32033213
dims = len(s)
3204-
length = s[-1]
3214+
length = s[-1] # type: ignore[misc]
32053215

32063216
# Initialize indexes so we can properly write our data
32073217
idx = [0] * dims

0 commit comments

Comments
 (0)