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

Vector bug squashing #536

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 24 additions & 23 deletions automol/util/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand All @@ -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.

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

Expand Down
Loading