From eb54e62cc4bb4966ce7e6f57dc2f1f4bc8beeca0 Mon Sep 17 00:00:00 2001 From: dexhunter Date: Mon, 9 Nov 2026 10:30:11 +0000 Subject: [PATCH] perf(markov): vectorize PCCA+ inner simplex search -- 78x faster _pcca_connected_isa looped over every state in Python to find the most outlying state and, for each later representative, to project each row and compute its norm. Use whole-array row norms, one np.outer rank-one update and a shared masked argmax helper instead. The same states are selected in the same order, so chi and the rotation matrix are unchanged. Benchmark (6000 states, 10 eigenvectors, plus smaller calls): 228.81 ms -> 2.92 ms. Co-Authored-By: Aiden --- .../markov/tools/analysis/dense/_pcca.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/deeptime/markov/tools/analysis/dense/_pcca.py b/src/deeptime/markov/tools/analysis/dense/_pcca.py index 5c5c2b7d..361d4f86 100644 --- a/src/deeptime/markov/tools/analysis/dense/_pcca.py +++ b/src/deeptime/markov/tools/analysis/dense/_pcca.py @@ -61,37 +61,39 @@ def _pcca_connected_isa(eigenvectors, n_clusters): "Probably the eigenvectors are incorrectly sorted. Cannot do PCCA." # local copy of the eigenvectors - c = eigenvectors[:, list(range(n_clusters))] + c = eigenvectors[:, :n_clusters] ortho_sys = np.copy(c) - max_dist = 0.0 # representative states ind = np.zeros(n_clusters, dtype=np.int32) + chosen = np.zeros(n, dtype=bool) + ones = np.ones(n_clusters) + + def farthest(points): + # first index of the largest positive row norm among non-representatives, 0 if there is none + dist = np.sqrt(np.square(points) @ ones) + return np.argmax(np.where((dist > 0) & ~chosen, dist, 0)) # select the first representative as the most outlying point - for (i, row) in enumerate(c): - if np.linalg.norm(row, 2) > max_dist: - max_dist = np.linalg.norm(row, 2) - ind[0] = i + ind[0] = farthest(c) # translate coordinates to make the first representative the origin ortho_sys -= c[ind[0], None] # select the other m-1 representatives using a Gram-Schmidt orthogonalization for k in range(1, n_clusters): - max_dist = 0.0 - temp = np.copy(ortho_sys[ind[k - 1]]) + chosen[ind[k - 1]] = True # select next farthest point that is not yet a representative - for (i, row) in enumerate(ortho_sys): - row -= np.dot(np.dot(temp, np.transpose(row)), temp) - distt = np.linalg.norm(row, 2) - if distt > max_dist and i not in ind[0:k]: - max_dist = distt - ind[k] = i + ind[k] = farthest(ortho_sys) ortho_sys /= np.linalg.norm(ortho_sys[ind[k]], 2) + # project out the new representative; the first one is the origin and needs no projection + if k + 1 < n_clusters: + temp = np.copy(ortho_sys[ind[k]]) + ortho_sys -= np.outer(ortho_sys @ temp, temp) + # print "Final selection ", ind # obtain transformation matrix of eigenvectors to membership matrix