Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize #101

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pylinalg/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
36 changes: 22 additions & 14 deletions pylinalg/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,18 +100,19 @@ 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]

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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down