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_batch_svd(self): m = 9 n = 5 q = 7 k = 5 for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((q, m, n)) A = ctf.astensor(A, dtype=dt) [U, S, VT] = ctf.svd_batch(A, k) for i in range(q): self.assertTrue( allclose(A[i], ctf.dot(U[i], ctf.dot(ctf.diag(S[i]), VT[i])))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i]))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i], VT[i].T()))) m = 7 n = 8 q = 2 k = 3 for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((q, m, n)) A = ctf.astensor(A, dtype=dt) [U, S, VT] = ctf.svd_batch(A, k) for i in range(q): [nU, nS, nVT] = np.linalg.svd(A[i].to_nparray()) self.assertTrue( allclose( np.dot(nU[:, :k], np.dot(np.diag(nS[:k]), nVT[:k, :])), ctf.dot(U[i], ctf.dot(ctf.diag(S[i]), VT[i])))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i]))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i], VT[i].T())))
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_svd(self): m = 9 n = 5 k = 5 for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]: 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())))
def eaccsd_diag(eom, imds=None): if imds is None: imds = eom.make_imds() t1, t2 = imds.t1, imds.t2 nocc, nvir = t1.shape foo = imds.eris.foo fvv = imds.eris.fvv Hr1 = imds.Lvv.diagonal() Hr1 = tensor(Hr1) if eom.partition == 'mp': jab = fvv.diagonal()[None, :, None] jab = jab + fvv.diagonal()[None, None, :] jab = jab - foo.diagonal()[:, None, None] Hr2 = tensor(jab) else: jab = imds.Lvv.diagonal().reshape(1, nvir, 1) jab = jab + imds.Lvv.diagonal().reshape(1, 1, nvir) jab = jab - imds.Loo.diagonal().reshape(nocc, 1, 1) wab = ctf.einsum("abab->ab", imds.Wvvvv.array) wjb = ctf.einsum('jbjb->jb', imds.Wovov.array) wjb2 = ctf.einsum('jbbj->jb', imds.Wovvo.array) wja = ctf.einsum('jaja->ja', imds.Wovov.array) jab = jab + wab.reshape(1, nvir, nvir) jab = jab - wjb.reshape(nocc, 1, nvir) jab = jab + 2 * wjb2.reshape(nocc, 1, nvir) jab -= ctf.einsum('jb,ab->jab', wjb2, ctf.eye(nvir)) jab = jab - wja.reshape(nocc, nvir, 1) Hr2 = tensor(jab) Hr2 -= 2 * lib.einsum('ijab,ijab->jab', t2, imds.Woovv) Hr2 += lib.einsum('ijab,ijba->jab', t2, imds.Woovv) vector = eom.amplitudes_to_vector(Hr1, Hr2) return vector
def solve(A, b, factor, r, regParam): n = A.shape[0] L = ctf.cholesky(A + regParam * ctf.eye(n)) factor = ctf.solve_tri(L, b.reshape((n, 1)), True, True, False) factor = ctf.solve_tri(L, factor, True, True, True).reshape((n, )) return factor
def ipccsd_diag(eom, imds=None): if imds is None: imds = eom.make_imds() t1, t2 = imds.t1, imds.t2 nocc, nvir = t1.shape foo = imds.eris.foo fvv = imds.eris.fvv Hr1 = -imds.Loo.diagonal() Hr1 = tensor(Hr1) if eom.partition == 'mp': ijb = -foo.diagonal().reshape(nocc, 1, 1) ijb = ijb - foo.diagonal().reshape(1, nocc, 1) ijb = ijb + fvv.diagonal().reshape(1, 1, nvir) Hr2 = tensor(ijb) else: wij = ctf.einsum('ijij->ij', imds.Woooo.array) wjb = ctf.einsum('jbjb->jb', imds.Wovov.array) wjb2 = ctf.einsum('jbbj->jb', imds.Wovvo.array) wib = ctf.einsum('ibib->ib', imds.Wovov.array) ijb = imds.Lvv.diagonal().reshape(1, 1, nvir) ijb = ijb - imds.Loo.diagonal().reshape(nocc, 1, 1) ijb = ijb - imds.Loo.diagonal().reshape(1, nocc, 1) #print(ijb.shape, wjb.shape) ijb = ijb + wij.reshape(nocc, nocc, 1) ijb = ijb - wjb.reshape(1, nocc, nvir) ijb = ijb + 2 * wjb2.reshape(1, nocc, nvir) ijb = ijb - ctf.einsum('ij,jb->ijb', ctf.eye(nocc), wjb2) ijb = ijb - wib.reshape(nocc, 1, nvir) Hr2 = tensor(ijb) Hr2 -= 2. * lib.einsum('ijcb,jibc->ijb', t2, imds.Woovv) Hr2 += lib.einsum('ijcb,ijbc->ijb', t2, imds.Woovv) vector = eom.amplitudes_to_vector(Hr1, Hr2) return vector
def test_eigh(self): n = 11 for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((n, n)) A += A.conj().T() [D, X] = ctf.eigh(A) self.assertTrue(allclose(ctf.dot(A, X), X * D)) self.assertTrue(allclose(ctf.eye(n), ctf.dot(X.conj().T(), X)))
def test_svd_rand(self): m = 19 n = 15 k = 13 for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [U,S,VT]=ctf.svd_rand(A,k,5,1) self.assertTrue(allclose(ctf.eye(k),ctf.dot(U.T(),U))) self.assertTrue(allclose(ctf.eye(k),ctf.dot(VT,VT.T()))) [U2,S2,VT2]=ctf.svd(A,k) rs1 = ctf.vecnorm(A - ctf.dot(U*S,VT)) rs2 = ctf.vecnorm(A - ctf.dot(U2*S2,VT2)) rA = ctf.vecnorm(A) self.assertTrue(rs1 < rA) self.assertTrue(rs2 < rs1) self.assertTrue(numpy.abs(rs1 - rs2)<3.e-1)
def updateH(A, W, regParam, omega, m, n, k): '''Gets the new H matrix by using the formula, subbing H for W''' H = ctf.zeros((n, k)) identity = ctf.eye(k) for i in range(n): newW = getNewWCTF(omega[:, i], W, m, n, k) newMat = ctf.zeros((k, k)) newMat.i("xy") << newW.i("bx") * newW.i("by") newMat.i("xy") << regParam * identity.i("xy") inverseFirstPart = CTFinverse(newMat) temp = ctf.zeros((k)) temp.i("j") << inverseFirstPart.i("ja") * W.i("ba") * A[:, i].i("b") H[i] = temp return H
def updateW(A, H, regParam, omega, m, n, k): '''Gets the new W matrix by using the formula''' Wctf = ctf.zeros((m, k)) identity = ctf.eye(k) for i in range(m): newH = getNewHCTF(omega[i], H, m, n, k) newMat = ctf.zeros((k, k)) newMat.i( "xy") << newH.i("bx") * newH.i("by") + regParam * identity.i("xy") inverseFirstPart = CTFinverse(newMat) temp = ctf.zeros((k)) temp.i("j") << inverseFirstPart.i("ja") * H.i("ba") * A[i].i("b") Wctf[i] = temp return Wctf
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 test_solve_tri(self): n = 4 m = 7 for dt in [np.float32, np.float64]: B = ctf.random.random((n, m)) B = ctf.astensor(B, dtype=dt) L = ctf.random.random((n, n)) L = ctf.astensor(L, dtype=dt) L = ctf.tril(L) D = L.T() * L D.i("ii") << -1.0 * L.i("ii") * L.i("ii") self.assertTrue(abs(ctf.vecnorm(D)) <= 1.e-6) X = ctf.solve_tri(L, B) self.assertTrue(allclose(B, ctf.dot(L, X))) U = ctf.random.random((n, n)) + ctf.eye(n) U = ctf.astensor(U, dtype=dt) U = ctf.triu(U) D = U.T() * U D.i("ii") << -1.0 * U.i("ii") * U.i("ii") self.assertTrue(abs(ctf.vecnorm(D)) <= 1.e-6) X = ctf.solve_tri(U, B, False) self.assertTrue(allclose(B, ctf.dot(U, X))) U = ctf.random.random((m, m)) + np.eye(m) U = ctf.astensor(U, dtype=dt) U = ctf.triu(U) D = U.T() * U D.i("ii") << -1.0 * U.i("ii") * U.i("ii") self.assertTrue(abs(ctf.vecnorm(D)) <= 1.e-6) X = ctf.solve_tri(U, B, False, False) self.assertTrue(allclose(B, ctf.dot(X, U))) U = ctf.random.random((m, m)) U = ctf.astensor(U, dtype=dt) U = ctf.triu(U) D = U.T() * U D.i("ii") << -1.0 * U.i("ii") * U.i("ii") self.assertTrue(abs(ctf.vecnorm(D)) <= 1.e-6) X = ctf.solve_tri(U, B, False, False, True) self.assertTrue(allclose(B, ctf.dot(X, U.T())))
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 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 test_eye(self): a0 = ctf.identity(4) a1 = ctf.eye(4) self.assertTrue(ctf.all(a0 == a1)) a1 = ctf.eye(4, dtype=numpy.complex128) self.assertTrue(a1.dtype == numpy.complex128)
def test_eye(self): a0 = ctf.identity(4) a1 = ctf.eye(4) self.assertTrue(ctf.all(a0==a1)) a1 = ctf.eye(4, dtype=numpy.complex128) self.assertTrue(a1.dtype == numpy.complex128)
def eye(*args): return ctf.eye(*args)
def test_tsvd(self): lens = [4,5,6,3] for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]: A = ctf.tensor(lens,dtype=dt) A.fill_random() [U,S,VT]=A.i("ijkl").svd("ija","akl") A.i("ijkl") << -1.0*U.i("ija")*S.i("a")*VT.i("akl") self.assertTrue(ctf.vecnorm(A)/A.tot_size()<1.e-6) A = ctf.tensor(lens,dtype=dt) A.fill_random() [U,S,VT]=A.i("ijkl").svd("ika","ajl") A.i("ijkl") << -1.0*U.i("ika")*S.i("a")*VT.i("ajl") self.assertTrue(ctf.vecnorm(A)/A.tot_size()<1.e-6) A = ctf.tensor(lens,dtype=dt) A.fill_random() [U,S,VT]=A.i("ijkl").svd("ika","ajl") [U,S1,VT]=A.i("ijkl").svd("ika","ajl",4) [U,S2,VT]=A.i("ijkl").svd("ika","ajl",4,numpy.abs(S[3])*(1.-1.e-5)) self.assertTrue(allclose(S1,S2)) [U,S2,VT]=A.i("ijkl").svd("ika","ajl",4,numpy.abs(S[2])) self.assertTrue(not allclose(S1.shape,S2.shape)) [U,S2,VT]=A.i("ijkl").svd("ika","ajl",4,numpy.abs(S[5])) self.assertTrue(allclose(S1,S2)) [U,S,VT]=A.i("ijkl").svd("iakj","la") A.i("ijkl") << -1.0*U.i("iakj")*S.i("a")*VT.i("la") self.assertTrue(ctf.vecnorm(A)/A.tot_size()<1.e-6) A.fill_random() [U,S,VT]=A.i("ijkl").svd("alk","jai") A.i("ijkl") << -1.0*U.i("alk")*S.i("a")*VT.i("jai") self.assertTrue(ctf.vecnorm(A)/A.tot_size()<1.e-6) K = ctf.tensor((U.shape[0],U.shape[0]),dtype=dt) K.i("ab") << U.i("alk") * U.i("blk") self.assertTrue(allclose(K,ctf.eye(U.shape[0]))) 0.*K.i("ab") << VT.i("jai") * VT.i("jbi") self.assertTrue(allclose(K,ctf.eye(U.shape[0]))) A.fill_random() [U,S,VT]=A.i("ijkl").svd("alk","jai",4,0,True) nrm1 = ctf.vecnorm(A) A.i("ijkl") << -1.0*U.i("alk")*S.i("a")*VT.i("jai") self.assertTrue(ctf.vecnorm(A)<nrm1) K = ctf.tensor((U.shape[0],U.shape[0]),dtype=dt) K.i("ab") << U.i("alk") * U.i("blk") self.assertTrue(allclose(K,ctf.eye(U.shape[0]))) 0.*K.i("ab") << VT.i("jai") * VT.i("jbi") self.assertTrue(allclose(K,ctf.eye(U.shape[0]))) T = ctf.tensor((4,3,6,5,1,7),dtype=dt) [U,S,VT] = T.i("abcdef").svd("crd","aerfb") T.i("abcdef") << -1.0*U.i("crd")*S.i("r")*VT.i("aerfb") self.assertTrue(ctf.vecnorm(T)/T.tot_size()<1.e-6) K = ctf.tensor((S.shape[0],S.shape[0]),dtype=dt) K.i("rs") << U.i("crd")*U.i("csd") self.assertTrue(allclose(K,ctf.eye(S.shape[0]))) K = ctf.tensor((S.shape[0],S.shape[0]),dtype=dt) K.i("rs") << VT.i("aerfb")*VT.i("aesfb") self.assertTrue(allclose(K,ctf.eye(S.shape[0])))