def test_hstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9, )) a1 = ctf.astensor(numpy.ones((2, 4))) a2 = ctf.astensor(numpy.ones((2, 5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) a1 = ctf.astensor(numpy.ones((2, 4))) a2 = ctf.astensor(numpy.ones((2, 5)) + 0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) a1 = numpy.ones((2, 4)) a2 = ctf.astensor(numpy.ones((2, 5)) + 0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) na2 = numpy.ones((2, 5)) + 0j self.assertTrue( ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1, na2)))) a1 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5, )) a1 = ctf.astensor(numpy.ones((2, 4, 2))) a2 = ctf.astensor(numpy.ones((2, 5, 2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9, 2))
def test_hstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9,)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) a1 = numpy.ones((2,4)) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) na2 = numpy.ones((2,5))+0j self.assertTrue(ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1,na2)))) a1 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5,)) a1 = ctf.astensor(numpy.ones((2,4,2))) a2 = ctf.astensor(numpy.ones((2,5,2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9,2))
def amplitudes_to_vector(self, t1, t2, out=None): vector = ctf.hstack((t1.array.ravel(), t2.array.ravel())) return vector
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 hstack(self, tensors): return self.tensor(ctf.hstack(tuple(tsr.unwrap() for tsr in tensors)))
def amplitudes_to_vector(eom, r1, r2): vector = ctf.hstack((r1.array.ravel(), r2.array.ravel())) return vector