Skip to content

Commit f162fcb

Browse files
authored
Optimize (#101)
* optimize a few functions and fix astype usage * bump version
1 parent ce18542 commit f162fcb

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

pylinalg/misc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def quat_to_axis_angle(quaternion, /, *, out=None, dtype=None) -> np.ndarray:
131131
quaternion = np.asarray(quaternion)
132132

133133
if out is None:
134-
quaternion = quaternion.astype(dtype, copy=False)
134+
if dtype is not None:
135+
quaternion = quaternion.astype(dtype, copy=False)
135136
out = (
136137
quaternion[..., :3] / np.sqrt(1 - quaternion[..., 3] ** 2),
137138
2 * np.arccos(quaternion[..., 3]),

pylinalg/vector.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ def vec_homogeneous(vectors, /, *, w=1, out=None, dtype=None) -> np.ndarray:
6161
ndarray, [..., 4]
6262
The list of vectors with appended homogeneous value.
6363
"""
64-
vectors = np.asarray(vectors)
65-
shape = list(vectors.shape)
66-
shape[-1] += 1
64+
6765
if out is None:
68-
out = np.empty_like(vectors, shape=shape, dtype=dtype)
66+
vectors = np.asarray(vectors)
67+
shape = list(vectors.shape)
68+
shape[-1] += 1
69+
out = np.empty(shape, dtype=dtype)
6970
out[..., -1] = w
7071
out[..., :-1] = vectors
7172
return out
@@ -99,18 +100,19 @@ def vec_transform(vectors, matrix, /, *, w=1, out=None, dtype=None) -> np.ndarra
99100
transformed vectors
100101
"""
101102

102-
vectors = np.asarray(vectors, dtype=float)
103-
matrix = np.asarray(matrix, dtype=float)
103+
matrix = np.asarray(matrix)
104104

105-
vectors = vec_homogeneous(vectors, w=w)
105+
vectors = vec_homogeneous(vectors, w=w, dtype=float)
106106
result = matrix @ vectors[..., None]
107107
result /= result[..., -1, :][..., None, :]
108108
result = result[..., :-1, 0]
109109

110110
if out is not None:
111111
out[:] = result
112112
else:
113-
out = result.astype(dtype, copy=False)
113+
out = result
114+
if dtype is not None:
115+
out = out.astype(dtype, copy=False)
114116

115117
return out
116118

@@ -302,7 +304,9 @@ def vec_dist(vector_a, vector_b, /, *, out=None, dtype=None) -> np.ndarray:
302304

303305
shape = vector_a.shape[:-1]
304306
if out is None:
305-
out = np.linalg.norm(vector_a - vector_b, axis=-1).astype(dtype, copy=False)
307+
out = np.linalg.norm(vector_a - vector_b, axis=-1)
308+
if dtype is not None:
309+
out = out.astype(dtype, copy=False)
306310
elif len(shape) >= 0:
307311
out[:] = np.linalg.norm(vector_a - vector_b, axis=-1)
308312
else:
@@ -353,7 +357,9 @@ def vec_angle(vector_a, vector_b, /, *, out=None, dtype=None) -> np.ndarray:
353357
)
354358

355359
if out is None:
356-
out = np.arccos(the_cos).astype(dtype, copy=False)
360+
out = np.arccos(the_cos)
361+
if dtype is not None:
362+
out = out.astype(dtype, copy=False)
357363
elif len(shape) >= 0:
358364
out[:] = np.arccos(the_cos)
359365
else:
@@ -387,12 +393,14 @@ def mat_decompose_translation(
387393
388394
"""
389395

390-
homogeneous_matrix = np.asarray(homogeneous_matrix, dtype=float)
396+
homogeneous_matrix = np.asarray(homogeneous_matrix)
391397

392398
if out is None:
393-
out = np.empty((*homogeneous_matrix.shape[:-2], 3), dtype=dtype)
394-
395-
out[:] = homogeneous_matrix[..., :-1, -1]
399+
out = homogeneous_matrix[..., :-1, -1]
400+
if dtype is not None:
401+
out = out.astype(dtype, copy=False)
402+
else:
403+
out[:] = homogeneous_matrix[..., :-1, -1]
396404

397405
return out
398406

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

0 commit comments

Comments
 (0)