def test_qr(self): m = 8 n = 4 for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
def test_qr(self): m = 8 n = 4 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
def test_qr(self): for (m, n) in [(8, 4), (4, 7), (3, 3)]: for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((m, n)) A = ctf.astensor(A, dtype=dt) [Q, R] = ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q, R))) if (m >= n): self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) else: self.assertTrue(allclose(ctf.eye(m), ctf.dot(Q, Q.T()))) A = ctf.tensor((m, n), dtype=np.complex64) rA = ctf.tensor((m, n), dtype=np.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m, n), dtype=np.float32) iA.fill_random() A.imag(iA) [Q, R] = ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q, R))) if (m >= n): self.assertTrue( allclose(ctf.eye(n, dtype=np.complex64), ctf.dot(ctf.conj(Q.T()), Q))) else: self.assertTrue( allclose(ctf.eye(m, dtype=np.complex64), ctf.dot(Q, ctf.conj(Q.T())))) A = ctf.tensor((m, n), dtype=np.complex128) rA = ctf.tensor((m, n), dtype=np.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m, n), dtype=np.float64) iA.fill_random() A.imag(iA) [Q, R] = ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q, R))) if (m >= n): self.assertTrue( allclose(ctf.eye(n, dtype=np.complex128), ctf.dot(ctf.conj(Q.T()), Q))) else: self.assertTrue( allclose(ctf.eye(m, dtype=np.complex128), ctf.dot(Q, ctf.conj(Q.T()))))
def rsvd(a, rank, niter=2, oversamp=5): m, n = a.shape r = min(rank + oversamp, m, n) # find subspace q = ctf.random.random((n, r)) * 2 - 1. for i in range(niter): q = a.transpose() @ (a @ q) q, _ = ctf.qr(q) q = a @ q q, _ = ctf.qr(q) # svd a_sub = q.transpose() @ a u_sub, s, vh = ctf.svd(a_sub) u = q @ u_sub if rank < r: u, s, vh = u[:, :rank], s[:rank], vh[:rank, :] return u, s, vh
def davidson(mult_by_A, N, neig, x0=None, Adiag=None, verbose=4): """Diagonalize a matrix via non-symmetric Davidson algorithm. mult_by_A() is a function which takes a vector of length N and returns a vector of length N. neig is the number of eigenvalues requested """ if rank == 0: log = lib.logger.Logger(sys.stdout, verbose) else: log = lib.logger.Logger(sys.stdout, 0) cput1 = (time.clock(), time.time()) Mmin = min(neig, N) Mmax = min(N, 2000) tol = 1e-6 def mult(arg): return mult_by_A(arg) if Adiag is None: Adiag = np.zeros(N, np.complex) for i in range(N): test = np.zeros(N, np.complex) test[i] = 1.0 Adiag[i] = mult(test)[i] else: Adiag = Adiag.to_nparray() idx = Adiag.argsort() lamda_k_old = 0 lamda_k = 0 target = 0 conv = False if x0 is not None: assert x0.shape == (Mmin, N) b = x0.copy() Ab = tuple([mult(b[m, :]) for m in range(Mmin)]) Ab = ctf.vstack(Ab).transpose() evals = np.zeros(neig, dtype=np.complex) evecs = [] for istep, M in enumerate(range(Mmin, Mmax + 1)): if M == Mmin: b = ctf.zeros((N, M)) if rank == 0: ind = [i * M + m for m, i in zip(range(M), idx)] fill = np.ones(len(ind)) b.write(ind, fill) else: b.write([], []) Ab = tuple([mult(b[:, m]) for m in range(M)]) Ab = ctf.vstack(Ab).transpose() else: Ab = ctf.hstack((Ab, mult(b[:, M - 1]).reshape(N, -1))) Atilde = ctf.dot(b.conj().transpose(), Ab) Atilde = Atilde.to_nparray() lamda, alpha = diagonalize_asymm(Atilde) lamda_k_old, lamda_k = lamda_k, lamda[target] alpha_k = ctf.astensor(alpha[:, target]) if M == Mmax: break q = ctf.dot(Ab - lamda_k * b, alpha_k) qnorm = ctf.norm(q) log.info( 'davidson istep = %d root = %d E = %.15g dE = %.9g residual = %.6g', istep, target, lamda_k.real, (lamda_k - lamda_k_old).real, qnorm) cput1 = log.timer('davidson iter', *cput1) if ctf.norm(q) < tol: evecs.append(ctf.dot(b, alpha_k)) evals[target] = lamda_k if target == neig - 1: conv = True break else: target += 1 eps = 1e-10 xi = q / (lamda_k - Adiag + eps) bxi, R = ctf.qr(ctf.hstack((b, xi.reshape(N, -1)))) nlast = bxi.shape[-1] - 1 b = ctf.hstack( (b, bxi[:, nlast].reshape(N, -1)) ) #can not replace nlast with -1, (inconsistent between numpy and ctf) evecs = ctf.vstack(tuple(evecs)) return conv, evals, evecs
def qr(A): return ctf.qr(q)