From 40b09c9d0b90a7b71145febd2532c2298a6315c9 Mon Sep 17 00:00:00 2001 From: "donald e. boyce" Date: Fri, 3 Apr 2026 11:30:59 -0400 Subject: [PATCH] fixes bug in unitVector --- hexrd/core/matrixutil.py | 24 ++++++++++++++++++------ tests/core/test_matrixutil.py | 9 +++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/hexrd/core/matrixutil.py b/hexrd/core/matrixutil.py index 8788a4d86..b79b73bbe 100644 --- a/hexrd/core/matrixutil.py +++ b/hexrd/core/matrixutil.py @@ -74,13 +74,25 @@ def rowNorm(a): def unitVector(a): + """ normalize array of column vectors (hstacked, axis = 0) + + Parameters + ---------- + a: numpy array + array to normalize; shape should be (m, n) or (m,) + + Returns + ------- + anrm: numpy array + array with shape (m, n) with each column normalized to magnitude 1.0 """ - normalize array of column vectors (hstacked, axis = 0) - """ - assert a.ndim in [ - 1, - 2, - ], "incorrect arg shape; must be 1-d or 2-d, yours is %d-d" % (a.ndim) + if a.ndim not in [1, 2]: + raise ValueError( + "incorrect arg shape; must be 1-d or 2-d, yours is %d-d" % (a.ndim) + ) + + if a.ndim == 1: + a = a.reshape(len(a), 1) ztol = constants.ten_epsf diff --git a/tests/core/test_matrixutil.py b/tests/core/test_matrixutil.py index 0d7a39b43..1302fbc01 100644 --- a/tests/core/test_matrixutil.py +++ b/tests/core/test_matrixutil.py @@ -58,6 +58,15 @@ def test_unitVector(random_vectors): res_z = matrixutil.unitVector(zeros) np.testing.assert_allclose(res_z, 0.0) + # Test on a single 1D vector. + x = np.array([1.0, 2.0, 3.0]) + u = matrixutil.unitVector(x) + assert u.shape == (3, 1) + + # Test dimensions of input. + x = np.zeros((2, 3, 4)) + with pytest.raises(ValueError): + u = matrixutil.unitVector(x) # --- Null Space ---