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 run_bench(num_iter, s, k): wrld = ctf.comm() M = ctf.random.random((s,s)) X = ctf.random.random((k,s)) [U,S,VT] = ctf.svd(M) S = np.arange(0,s)+1 M = ctf.dot(U*S,U.T()) te = ctf.timer_epoch("BENCHMARK: SPD SOLVE") te.begin() times = [] for i in range(num_iter): t0 = time.time() X = ctf.solve_spd(M,X) times.append(time.time()-t0) te.end() if ctf.comm().rank() == 0: print("ctf.solve_spd average time:",np.sum(times)/num_iter,"sec") print("ctf.solve_spd iteration timings:",times) te = ctf.timer_epoch("BENCHMARK: Manual Cholesky+TRSM SPD SOLVE") te.begin() times = [] for i in range(num_iter): t0 = time.time() L = ctf.cholesky(M) X = ctf.solve_tri(M,X,from_left=False) times.append(time.time()-t0) te.end() if ctf.comm().rank() == 0: print("ctf.cholesky+solve_tri average time:",np.sum(times)/num_iter,"sec") print("ctf.cholesky+solve_tri iteration timings:",times)
def test_solve_tri(self): n = 4 m = 7 for dt in [numpy.float32, numpy.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)) 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)) 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 solve_tri(A, B, lower=True, from_left=False, transp_L=False): return ctf.solve_tri(A, B, lower, from_left, transp_L)