Skip to content

Commit e436128

Browse files
authored
optimize vec_transform (#103)
1 parent ca09f0c commit e436128

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

pylinalg/vector.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,28 @@ def vec_transform(
105105
transformed vectors
106106
"""
107107

108-
matrix = np.asarray(matrix)
108+
matrix = np.asarray(matrix, dtype="f8")
109109

110110
if projection:
111-
vectors = vec_homogeneous(vectors, w=w, dtype=float)
112-
vectors @= matrix.T
113-
vectors[..., :-1] /= vectors[..., -1, None]
114-
vectors = vectors[..., :-1]
111+
vectors = vec_homogeneous(vectors, w=w, dtype="f8")
112+
if vectors.ndim == 1:
113+
vectors = matrix @ vectors
114+
vectors[:-1] /= vectors[-1]
115+
vectors = vectors[:-1]
116+
elif vectors.ndim == 2:
117+
vectors = (matrix @ vectors.T).T
118+
vectors = vectors[..., :-1] / vectors[..., -1, None]
119+
else:
120+
raise ValueError("vectors must be a 1D or 2D array")
115121
else:
116-
vectors = np.asarray(vectors, dtype=float, copy=True)
117-
vectors @= matrix[:-1, :-1].T
118-
vectors += matrix[:-1, -1]
122+
if vectors.ndim == 1:
123+
vectors = matrix[:-1, :-1] @ vectors + matrix[:-1, -1]
124+
elif vectors.ndim == 2:
125+
vectors = vec_homogeneous(vectors, w=w, dtype="f8")
126+
vectors = (matrix @ vectors.T).T
127+
vectors = vectors[..., :-1]
128+
else:
129+
raise ValueError("vectors must be a 1D or 2D array")
119130

120131
if out is None:
121132
out = vectors

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[project]
44
name = "pylinalg"
5-
version = "0.6.5"
5+
version = "0.6.6"
66
description = "Linear algebra utilities for Python"
77
readme = "README.md"
88
license = { file = "LICENSE" }

0 commit comments

Comments
 (0)