def test_astensor(self): # astensor converts python object to ctf tensor a0 = ctf.astensor((1,2,3)) a0 = ctf.astensor([1,2.,3]) a0 = ctf.astensor([(1,2), (3,4)]) a0 = ctf.astensor(numpy.arange(3)) a0 = ctf.astensor([numpy.array((1,2)), numpy.array((3,4))+1j]) a1 = ctf.astensor(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.asarray(a1) # self.assertTrue(ctf.asarray(a0).__class__ == ctf.astensor(a0).__class__) a0 = ctf.astensor([1,2.,3], dtype='D') self.assertTrue(a0.dtype == numpy.complex128) with self.assertRaises(TypeError): ctf.astensor([1j,2j], dtype='d') a0 = numpy.arange(4.).reshape(2,2) a1 = ctf.to_nparray(ctf.from_nparray(a0)) self.assertTrue(ctf.all(a0==a1)) try: a1 = ctf.from_nparray(a1).to_nparray() self.assertTrue(ctf.all(a0==a1)) except AttributeError: pass a0 = ctf.from_nparray(numpy.arange(3)) a1 = ctf.from_nparray(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.arange(6).reshape(2,3) a1 = ctf.array(a0) self.assertTrue(ctf.all(a0==a1)) self.assertTrue(ctf.all(a1==a0)) a1 = ctf.array(a0, copy=False) self.assertTrue(ctf.all(a1==0))
def test_svd(self): m = 9 n = 5 k = 5 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [U,S,VT]=ctf.svd(A,k) [U1,S1,VT1]=la.svd(ctf.to_nparray(A),full_matrices=False) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T()))) 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) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(VT, ctf.conj(VT.T())))) 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) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(VT, ctf.conj(VT.T()))))
def allclose(a, b): return abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() < 1e-14
def allclose(a, b): if abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() > 1e-4: print(ctf.to_nparray(a)) print(ctf.to_nparray(b)) return abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() <= 1e-4
def allclose(a, b): return abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() <= 1e-5
# eris.foo = eris.fock[:,:nocc,:nocc] * 0 # eris.fvv = eris.fock[:,nocc:,nocc:] * 0 # eris.fov = eris.fock[:,:nocc,nocc:] eris = mycc.ao2mo() # print(abs(ctf.to_nparray(eris1.oooo) - eris.oooo).max()) # print(abs(ctf.to_nparray(eris1.ooov) - eris.ooov).max()) # print(abs(ctf.to_nparray(eris1.oovv) - eris.oovv).max()) # print(abs(ctf.to_nparray(eris1.ovov) - eris.ovov).max()) # print(abs(ctf.to_nparray(eris1.voov) - eris.voov).max()) # print(abs(ctf.to_nparray(eris1.vovv) - eris.vovv).max()) # print(abs(ctf.to_nparray(eris1.vvvv) - eris.vvvv).max()) emp2, t1, t2 = mycc.init_amps(eris) print(emp2 - -0.131341714277) print( lib.finger(ctf.to_nparray(t2)) - (-0.0576606747244 + 0.0336759095279j)) nkpts = kpts.shape[0] numpy.random.seed(1) no = mycc.nocc nv = mycc.nmo - no t2 = eris.oovv t1 = (numpy.random.random((nkpts, no, nv)) - .5 + numpy.random.random( (nkpts, no, nv)) * .1j) t1 = ctf.astensor(t1) t2 = ctf.astensor(t2) t1b, t2b = mycc.update_amps(t1, t2, eris) print( lib.finger(ctf.to_nparray(t1b)) - (0.17663716146287139 - 0.16562727702376817j))
def allclose(a, b): #if abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() > 1e-3: # print(ctf.to_nparray(a)) # print(ctf.to_nparray(b)) return (ctf.to_nparray(a).shape == ctf.to_nparray(b).shape ) and abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() <= 1e-3