Skip to content

Commit 97f85d2

Browse files
authored
Add case when matrix is ill-conditioned for generic procrustes (#210)
1 parent 58d4924 commit 97f85d2

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

procrustes/generic.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ def generic(
120120
weight,
121121
)
122122
# compute the generic solution
123-
a_inv = pinv(np.dot(new_a.T, new_a))
123+
try:
124+
a_inv = pinv(np.dot(new_a.T, new_a))
125+
# add little bit of random noise when the matrix is ill conditioned
126+
except np.linalg.LinAlgError:
127+
# conver new_a to float if it is not
128+
new_a = new_a.astype(float)
129+
new_a += 2e-14 * np.random.random_sample((new_a.shape[0], new_a.shape[1])) - 1e-14
130+
a_inv = pinv(np.dot(new_a.T, new_a))
131+
# a_inv = pinv(np.dot(new_a.T, new_a))
124132

125133
array_x = np.linalg.multi_dot([a_inv, new_a.T, new_b])
126134
# compute one-sided error

0 commit comments

Comments
 (0)