From 225c6c8e976f47baf10bd90ce6f906f215bac91d Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 18 Feb 2025 17:15:20 +0100 Subject: [PATCH 1/2] optimize a few functions and fix astype usage --- pylinalg/misc.py | 3 ++- pylinalg/vector.py | 36 ++++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/pylinalg/misc.py b/pylinalg/misc.py index 01ef15b..92dadc8 100644 --- a/pylinalg/misc.py +++ b/pylinalg/misc.py @@ -131,7 +131,8 @@ def quat_to_axis_angle(quaternion, /, *, out=None, dtype=None) -> np.ndarray: quaternion = np.asarray(quaternion) if out is None: - quaternion = quaternion.astype(dtype, copy=False) + if dtype is not None: + quaternion = quaternion.astype(dtype, copy=False) out = ( quaternion[..., :3] / np.sqrt(1 - quaternion[..., 3] ** 2), 2 * np.arccos(quaternion[..., 3]), diff --git a/pylinalg/vector.py b/pylinalg/vector.py index a024b0a..805d236 100644 --- a/pylinalg/vector.py +++ b/pylinalg/vector.py @@ -61,11 +61,12 @@ def vec_homogeneous(vectors, /, *, w=1, out=None, dtype=None) -> np.ndarray: ndarray, [..., 4] The list of vectors with appended homogeneous value. """ - vectors = np.asarray(vectors) - shape = list(vectors.shape) - shape[-1] += 1 + if out is None: - out = np.empty_like(vectors, shape=shape, dtype=dtype) + vectors = np.asarray(vectors) + shape = list(vectors.shape) + shape[-1] += 1 + out = np.empty(shape, dtype=dtype) out[..., -1] = w out[..., :-1] = vectors return out @@ -99,10 +100,9 @@ def vec_transform(vectors, matrix, /, *, w=1, out=None, dtype=None) -> np.ndarra transformed vectors """ - vectors = np.asarray(vectors, dtype=float) - matrix = np.asarray(matrix, dtype=float) + matrix = np.asarray(matrix) - vectors = vec_homogeneous(vectors, w=w) + vectors = vec_homogeneous(vectors, w=w, dtype=float) result = matrix @ vectors[..., None] result /= result[..., -1, :][..., None, :] result = result[..., :-1, 0] @@ -110,7 +110,9 @@ def vec_transform(vectors, matrix, /, *, w=1, out=None, dtype=None) -> np.ndarra if out is not None: out[:] = result else: - out = result.astype(dtype, copy=False) + out = result + if dtype is not None: + out = out.astype(dtype, copy=False) return out @@ -302,7 +304,9 @@ def vec_dist(vector_a, vector_b, /, *, out=None, dtype=None) -> np.ndarray: shape = vector_a.shape[:-1] if out is None: - out = np.linalg.norm(vector_a - vector_b, axis=-1).astype(dtype, copy=False) + out = np.linalg.norm(vector_a - vector_b, axis=-1) + if dtype is not None: + out = out.astype(dtype, copy=False) elif len(shape) >= 0: out[:] = np.linalg.norm(vector_a - vector_b, axis=-1) else: @@ -353,7 +357,9 @@ def vec_angle(vector_a, vector_b, /, *, out=None, dtype=None) -> np.ndarray: ) if out is None: - out = np.arccos(the_cos).astype(dtype, copy=False) + out = np.arccos(the_cos) + if dtype is not None: + out = out.astype(dtype, copy=False) elif len(shape) >= 0: out[:] = np.arccos(the_cos) else: @@ -387,12 +393,14 @@ def mat_decompose_translation( """ - homogeneous_matrix = np.asarray(homogeneous_matrix, dtype=float) + homogeneous_matrix = np.asarray(homogeneous_matrix) if out is None: - out = np.empty((*homogeneous_matrix.shape[:-2], 3), dtype=dtype) - - out[:] = homogeneous_matrix[..., :-1, -1] + out = homogeneous_matrix[..., :-1, -1] + if dtype is not None: + out = out.astype(dtype, copy=False) + else: + out[:] = homogeneous_matrix[..., :-1, -1] return out From 890227bba954cbf25b47bcaf2ce35253ceadb03f Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 18 Feb 2025 17:15:40 +0100 Subject: [PATCH 2/2] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c0420c4..27c0c8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [project] name = "pylinalg" -version = "0.6.3" +version = "0.6.4" description = "Linear algebra utilities for Python" readme = "README.md" license = { file = "LICENSE" }