def approxUpdateEig(self, subW, ABBA, omega, Q): """ Update the eigenvalue decomposition of ABBA """ # --- remove rows/columns --- if self.n > ABBA.shape[0]: omega, Q = EigenUpdater.eigenRemove(omega, Q, ABBA.shape[0], min(self.k2, ABBA.shape[0])) # --- update existing nodes --- currentN = min(self.n, ABBA.shape[0]) deltaDegrees = numpy.array( subW.sum(0)).ravel()[0:currentN] - self.degrees[:currentN] inds = numpy.arange(currentN)[deltaDegrees != 0] if len(inds) > 0: Y1 = ABBA[:currentN, inds] - self.ABBALast[:currentN, inds] Y1 = numpy.array(Y1.todense()) Y1[inds, :] = Y1[inds, :] / 2 Y2 = numpy.zeros((currentN, inds.shape[0])) Y2[(inds, numpy.arange(inds.shape[0]))] = 1 omega, Q = EigenUpdater.eigenAdd2(omega, Q, Y1, Y2, min(self.k2, currentN)) # --- add rows/columns --- if self.n < ABBA.shape[0]: AB = numpy.array(ABBA[0:self.n, self.n:].todense()) BB = numpy.array(ABBA[self.n:, self.n:].todense()) omega, Q = EigenUpdater.lazyEigenConcatAsUpdate( omega, Q, AB, BB, min(self.k2, ABBA.shape[0])) return omega, Q
def approxUpdateEig(self, subW, ABBA, omega, Q): """ Update the eigenvalue decomposition of ABBA """ # --- remove rows/columns --- if self.n > ABBA.shape[0]: omega, Q = EigenUpdater.eigenRemove(omega, Q, ABBA.shape[0], min(self.k2, ABBA.shape[0])) # --- update existing nodes --- currentN = min(self.n, ABBA.shape[0]) deltaDegrees = numpy.array(subW.sum(0)).ravel()[0:currentN]- self.degrees[:currentN] inds = numpy.arange(currentN)[deltaDegrees!=0] if len(inds) > 0: Y1 = ABBA[:currentN, inds] - self.ABBALast[:currentN, inds] Y1 = numpy.array(Y1.todense()) Y1[inds, :] = Y1[inds, :]/2 Y2 = numpy.zeros((currentN, inds.shape[0])) Y2[(inds, numpy.arange(inds.shape[0]))] = 1 omega, Q = EigenUpdater.eigenAdd2(omega, Q, Y1, Y2, min(self.k2, currentN)) # --- add rows/columns --- if self.n < ABBA.shape[0]: AB = numpy.array(ABBA[0:self.n, self.n:].todense()) BB = numpy.array(ABBA[self.n:, self.n:].todense()) omega, Q = EigenUpdater.lazyEigenConcatAsUpdate(omega, Q, AB, BB, min(self.k2, ABBA.shape[0])) return omega, Q
def testEigenAdd2(self): tol = 10**-6 for i in range(10): m = numpy.random.randint(5, 10) n = numpy.random.randint(5, 10) p = numpy.random.randint(5, 10) A = numpy.random.randn(m, n) Y1 = numpy.random.randn(n, p) Y2 = numpy.random.randn(n, p) AA = A.conj().T.dot(A) Y1Y2 = Y1.dot(Y2.conj().T) lastError = 100 omega, Q = numpy.linalg.eigh(AA) self.assertTrue( numpy.linalg.norm(AA - (Q * omega).dot(Q.conj().T)) < tol) C = AA + Y1Y2 + Y1Y2.conj().T for k in range(1, 9): pi, V, D, DUD = EigenUpdater.eigenAdd2(omega, Q, Y1, Y2, k, debug=True) # V is "orthogonal" self.assertTrue( numpy.linalg.norm(V.conj().T.dot(V) - numpy.eye(V.shape[1])) < tol) # The approximation converges to the exact decomposition C_k = (V * pi).dot(V.conj().T) error = numpy.linalg.norm(C - C_k) if Util.rank(C) == k: self.assertTrue(error <= tol) lastError = error # DomegaD corresponds to AA_k omega_k, Q_k = Util.indEig( omega, Q, numpy.flipud(numpy.argsort(omega))[0:k]) DomegakD = (D * numpy.c_[omega_k[numpy.newaxis, :], numpy.zeros( (1, max(D.shape[1] - k, 0)))]).dot( D.conj().T) self.assertTrue( numpy.linalg.norm((Q_k * omega_k).dot(Q_k.conj().T) - DomegakD) < tol) # DUD is exactly decomposed self.assertTrue( numpy.linalg.norm(Y1Y2 + Y1Y2.conj().T - D.dot(DUD).dot(D.conj().T)) < tol)
def eigenUpdate(L1, L2, omega, Q, k): """ Find the eigen-update between two matrices L1 (with eigenvalues omega, and eigenvectors Q), and L2. """ deltaL = L2 - L1 deltaL.prune() inds = numpy.unique(deltaL.nonzero()[0]) if len(inds) > 0: Y1 = deltaL[:, inds] Y1 = numpy.array(Y1.todense()) Y1[inds, :] = Y1[inds, :]/2 logging.debug("rank(deltaL)=" + str(Y1.shape[1])) Y2 = numpy.zeros((L1.shape[0], inds.shape[0])) Y2[(inds, numpy.arange(inds.shape[0]))] = 1 omega, Q = EigenUpdater.eigenAdd2(omega, Q, Y1, Y2, min(k, L1.shape[0])) return omega, Q
def testEigenAdd2(self): tol = 10**-6 for i in range(10): m = numpy.random.randint(5, 10) n = numpy.random.randint(5, 10) p = numpy.random.randint(5, 10) A = numpy.random.randn(m, n) Y1 = numpy.random.randn(n, p) Y2 = numpy.random.randn(n, p) AA = A.conj().T.dot(A) Y1Y2 = Y1.dot(Y2.conj().T) lastError = 100 omega, Q = numpy.linalg.eigh(AA) self.assertTrue(numpy.linalg.norm(AA-(Q*omega).dot(Q.conj().T)) < tol ) C = AA + Y1Y2 + Y1Y2.conj().T for k in range(1,9): pi, V, D, DUD = EigenUpdater.eigenAdd2(omega, Q, Y1, Y2, k, debug = True) # V is "orthogonal" self.assertTrue(numpy.linalg.norm(V.conj().T.dot(V) - numpy.eye(V.shape[1])) < tol ) # The approximation converges to the exact decomposition C_k = (V*pi).dot(V.conj().T) error = numpy.linalg.norm(C-C_k) if Util.rank(C)==k: self.assertTrue(error <= tol) lastError = error # DomegaD corresponds to AA_k omega_k, Q_k = Util.indEig(omega, Q, numpy.flipud(numpy.argsort(omega))[0:k]) DomegakD = (D*numpy.c_[omega_k[numpy.newaxis,:],numpy.zeros((1,max(D.shape[1]-k,0)))]).dot(D.conj().T) self.assertTrue(numpy.linalg.norm((Q_k*omega_k).dot(Q_k.conj().T)-DomegakD) < tol ) # DUD is exactly decomposed self.assertTrue(numpy.linalg.norm(Y1Y2 + Y1Y2.conj().T - D.dot(DUD).dot(D.conj().T)) < tol )