From 4e7b7d5ceec9af17b81daf8fff9792834fc8e4b0 Mon Sep 17 00:00:00 2001 From: Rosalba Date: Mon, 15 Jul 2024 09:52:57 -0400 Subject: [PATCH] Vector typing w/o breaking changes --- automol/util/vector.py | 47 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/automol/util/vector.py b/automol/util/vector.py index 02133476..ee49eb87 100644 --- a/automol/util/vector.py +++ b/automol/util/vector.py @@ -28,12 +28,12 @@ def require_vector_like(obj: Sequence | Vector): assert is_vector_like(obj), f"{obj} is not vector-like" -def unit_norm(xyz: Sequence | Vector) -> float: +def unit_norm(xyz: Sequence | Vector) -> Vector: """Normalize a vector (xyz) to 1.0. - Returns 0. for null vectors + Does nothing to null vectors - :param xyz: Vector + :param xyz:3D Vector :return: Numbers in the vector """ require_vector_like(xyz) @@ -44,7 +44,7 @@ def unit_norm(xyz: Sequence | Vector) -> float: return uxyz -def unit_direction(xyz1: Sequence | Vector, xyz2: Sequence | Vector) -> float: +def unit_direction(xyz1: Sequence | Vector, xyz2: Sequence | Vector) -> Vector: """Calculate a unit direction vector from `xyz1` to `xyz2`. :param xyz1: 3D vector @@ -93,12 +93,13 @@ def are_parallel( def orthogonalize( - xyz1: Sequence | Vector, xyz2: Sequence | Vector, normalize=False -) -> float: + xyz1: Sequence | Vector, xyz2: Sequence | Vector, normalize: bool = False +) -> Vector: """Orthogonalize `xyz2` against `xyz1`. :param xyz1: 3D vector :param xyz2: 3D vector - :return: Normal unit vector. + :param normalize: Whether to normalize the result + :return: The orthoganol component. """ require_vector_like(xyz1) require_vector_like(xyz2) @@ -130,11 +131,11 @@ def flip_if_left_handed( zdir = numpy.cross(xvec, yvec) proj = numpy.dot(zvec, zdir) if proj < 0.0: - zvec = numpy.negative(zvec) + return zvec return tuple(map(float, zvec)) -def best_unit_perpendicular(xyzs: list[Vector]): +def best_unit_perpendicular(xyzs: Sequence[Sequence | Vector]) -> Vector: """Find a vector that is perpendicular to a series of points as much as possible. For 1 point, this is an arbitrary vector. @@ -158,11 +159,13 @@ def best_unit_perpendicular(xyzs: list[Vector]): _, _, vmat = numpy.linalg.svd(numpy.subtract(xyzs, xyz0)) nvec = vmat[2, :] - nvec = tuple(map(float, nvec)) return nvec -def arbitrary_unit_perpendicular(xyz, orig_xyz=(0.0, 0.0, 0.0)): +def arbitrary_unit_perpendicular( + xyz: Sequence | Vector, + orig_xyz: Sequence | Vector = (0.0, 0.0, 0.0) +) -> Vector: """Determine an arbitrary perpendicular vector.""" require_vector_like(xyz) require_vector_like(orig_xyz) @@ -190,7 +193,7 @@ def unit_perpendicular( """ require_vector_like(xyz1) require_vector_like(xyz2) - require_vector_like(value_if_parallel) + require_vector_like(orig_xyz) xyz1 = numpy.subtract(xyz1, orig_xyz) xyz2 = numpy.subtract(xyz2, orig_xyz) xyz3 = numpy.cross(xyz1, xyz2) @@ -239,7 +242,7 @@ def from_internals( xyz2: Sequence | Vector = (0.0, 0.0, 1.0), dih: float = 0.0, xyz3: Sequence | Vector = (0.0, 1.0, 0.0), -) -> tuple[float]: +) -> Vector: """Determine the position of a point (xyz4) in Cartesian coordinates whose position is related to three other points (xyz1, xyz2, xyz3) via a set of internal coordinates. @@ -253,16 +256,15 @@ def from_internals( """ require_vector_like(xyz1) require_vector_like(xyz2) + require_vector_like(xyz3) local_xyz = _local_position(dist=dist, ang=ang, dih=dih) local_basis = _local_axes(xyz1=xyz1, xyz2=xyz2, xyz3=xyz3) - xyz4 = tuple(xyz1 + numpy.dot(local_xyz, local_basis)) + xyz4 = xyz1 + numpy.dot(local_xyz, local_basis) return xyz4 -def _local_position( - dist: float = 0.0, ang: float = 0.0, dih: float = 0.0 -) -> tuple[float]: +def _local_position(dist: float = 0.0, ang: float = 0.0, dih: float = 0.0) -> Vector: """Determine the xyz coordinates of a point in the local axis frame defined by a set of internal coordinates. @@ -275,14 +277,14 @@ def _local_position( y_comp = dist * numpy.sin(ang) * numpy.cos(dih) z_comp = dist * numpy.cos(ang) - return (x_comp, y_comp, z_comp) + return numpy.array([x_comp, y_comp, z_comp]) def _local_axes( xyz1: Sequence | Vector = (0.0, 0.0, 0.0), xyz2: Sequence | Vector = (0.0, 0.0, 1.0), xyz3: Sequence | Vector = (0.0, 1.0, 0.0), -) -> tuple[float]: +) -> Vector: """Determine the local axes for defining bond, angle, dihedral from the Cartesian coordinates of three support atoms. @@ -327,9 +329,7 @@ def angle( :param xyz1: 3D vector to point 1 :param xyz2: 3D vector to point 2 - :param xyz3: 3D vector to point 3 - :Note: there is no xyz3 - :param orig_xyz: Origin of coordinate system `xyz1` and `xyz2` are in + :param orig_xyz: Origin to measure the vector angle from :return: Angle between three atoms """ require_vector_like(xyz1) @@ -466,11 +466,12 @@ def rotate_(xyz: Vector) -> Vector: return rotate_ + # I/O def string( vec: Sequence | Vector, num_per_row: int | None = None, - val_format="{0:>8.3f}", + val_format: str = "{0:>8.3f}", ) -> str: """Write a vector to a string.