Skip to content

Commit

Permalink
[Behavior change] Remove zero division error for linear_transform
Browse files Browse the repository at this point in the history
  • Loading branch information
waltsims committed Jun 5, 2024
1 parent e75a7d7 commit 54c460c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
11 changes: 6 additions & 5 deletions kwave/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,12 @@ def compute_linear_transform(pos1, pos2, offset=None):

magnitude = np.linalg.norm(beam_vec)

# TODO: it seems matlab behaviour is to return nans when positions are the same.
# we should open an issue and change our behaviour once matlab is fixed.
# if magnitude == 0:
# # "pos1 and pos2 are the same"
# return np.eye(3), 0 if offset is None else offset
# matlab behaviour is to return nans when positions are the same.
# we choose to return the identity matrix and the offset in this case.
# TODO: we should open an issue and change our behaviour once matlab is fixed.
if magnitude == 0:
# "pos1 and pos2 are the same"
return np.eye(3), 0 if offset is None else offset

# Normalise to give unit beam vector
beam_vec = beam_vec / magnitude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ def test_compute_linear_transform():
if len(params) == 2:
pos1, pos2 = params
pos1, pos2 = pos1.astype(float), pos2.astype(float)

rot_mat, offset_pos = compute_linear_transform(pos1, pos2)

else:
pos1, pos2, offset = params
pos1, pos2, offset = pos1.astype(float), pos2.astype(float), float(offset)
rot_mat, offset_pos = compute_linear_transform(pos1, pos2, offset)

assert np.allclose(rot_mat, reader.expected_value_of("rotMat"), equal_nan=True)
assert np.allclose(offset_pos, reader.expected_value_of("offsetPos"), equal_nan=True)
if not np.any(np.isnan(reader.expected_value_of("rotMat"))):
assert np.allclose(rot_mat, reader.expected_value_of("rotMat"))
assert np.allclose(offset_pos, reader.expected_value_of("offsetPos"))
reader.increment()

logging.log(logging.INFO, "compute_linear_transform(..) works as expected!")


if __name__ == "__main__":
test_compute_linear_transform()

0 comments on commit 54c460c

Please sign in to comment.