def multiply_add(self, A, AT): m = A.m if A != None else 0 # m = # of rows of A T = cs.cs_spalloc (m, m, m, True, True) # create triplet identity matrix for i in range(m): cs.cs_entry (T, i, i, 1) Eye = cs.cs_compress (T) # Eye = speye (m) C = cs.cs_multiply (A, AT) # C = A*A' D = cs.cs_add(C, Eye, 1, cs.cs_norm (C)) # D = C + Eye*norm (C,1) # print "D:" # cs.cs_print(D, False) # print D return D
def multiply_add(self, A, AT): m = A.m if A != None else 0 # m = # of rows of A T = cs.cs_spalloc(m, m, m, True, True) # create triplet identity matrix for i in range(m): cs.cs_entry(T, i, i, 1) Eye = cs.cs_compress(T) # Eye = speye (m) C = cs.cs_multiply(A, AT) # C = A*A' D = cs.cs_add(C, Eye, 1, cs.cs_norm(C)) # D = C + Eye*norm (C,1) # print "D:" # cs.cs_print(D, False) # print D return D
def test3(self, prob): """Cholesky update/downdate @param prob: problem @return: true if successful, false on error """ if prob is None or prob.sym == 0 or prob.A.n == 0: return False A = prob.A; C = prob.C; b = prob.b; x = prob.x; resid = prob.resid n = A.n if prob.sym == 0 or n == 0: return True self.rhs (x, b, n) # compute right-hand side stdout.write("\nchol then update/downdate ") self.print_order (1) y = cs.xalloc(n) t = self.tic() S = cs.cs_schol (1, C) # symbolic Chol, amd(A+A') stdout.write("\nsymbolic chol time %8.2f ms\n" % self.toc (t)) t = self.tic() N = cs.cs_chol (C, S) # numeric Cholesky stdout.write("numeric chol time %8.2f ms\n" % self.toc (t)) if S is None or N is None: return False t = self.tic() cs.cs_ipvec (S.pinv, b, y, n) # y = P*b cs.cs_lsolve (N.L, y) # y = L\y cs.cs_ltsolve (N.L, y) # y = L'\y cs.cs_pvec (S.pinv, y, x, n) # x = P'*y stdout.write("solve chol time %8.2f ms\n" % self.toc (t)) stdout.write("original: ") self.print_resid (True, C, x, b, resid, prob) # print residual k = n / 2 # construct W W = cs.cs_spalloc (n, 1, n, True, False) Lp = N.L.p; Li = N.L.i; Lx = N.L.x Wp = W.p; Wi = W.i; Wx = W.x Wp [0] = 0 p1 = Lp [k] Wp [1] = Lp [k+1] - p1 s = Lx[p1] while p1 < Lp [k+1]: p2 = p1 - Lp [k] ; Wi [p2] = Li [p1] ; Wx[p2] = s * random() p1+=1 t = self.tic() ok = cs.cs_updown (N.L, +1, W, S.parent) # update: L*L'+W*W' t1 = self.toc (t) stdout.write("update: time: %8.2f ms\n" % t1) if not ok: return False t = self.tic() cs.cs_ipvec (S.pinv, b, y, n) # y = P*b cs.cs_lsolve (N.L, y) # y = L\y cs.cs_ltsolve (N.L, y) # y = L'\y cs.cs_pvec (S.pinv, y, x, n) # x = P'*y t = self.toc (t) p = cs.cs_pinv (S.pinv, n) W2 = cs.cs_permute (W, p, None, True) # E = C + (P'W)*(P'W)' WT = cs.cs_transpose (W2, True) WW = cs.cs_multiply (W2, WT) WT = None W2 = None E = cs.cs_add (C, WW, 1, 1) WW = None if E is None or p is None: return False stdout.write("update: time: %8.2f ms(incl solve) " % (t1 + t)) self.print_resid (True, E, x, b, resid, prob) # print residual N = None # clear N t = self.tic() N = cs.cs_chol (E, S) # numeric Cholesky if N is None: return False cs.cs_ipvec (S.pinv, b, y, n) # y = P*b cs.cs_lsolve (N.L, y) # y = L\y cs.cs_ltsolve (N.L, y) # y = L'\y cs.cs_pvec (S.pinv, y, x, n) # x = P'*y t = self.toc (t) stdout.write("rechol: time: %8.2f ms(incl solve) " % t) self.print_resid (True, E, x, b, resid, prob) # print residual t = self.tic() ok = cs.cs_updown (N.L, -1, W, S.parent) # downdate: L*L'-W*W' t1 = self.toc (t) if not ok: return False stdout.write("downdate: time: %8.2f\n" % t1) t = self.tic() cs.cs_ipvec (S.pinv, b, y, n) # y = P*b cs.cs_lsolve (N.L, y) # y = L\y cs.cs_ltsolve (N.L, y) # y = L'\y cs.cs_pvec (S.pinv, y, x, n) # x = P'*y t = self.toc (t) stdout.printf("downdate: time: %8.2f ms(incl solve) " % (t1 + t)) self.print_resid (True, C, x, b, resid, prob) # print residual return True
def test3(self, prob): """Cholesky update/downdate @param prob: problem @return: true if successful, false on error """ if prob is None or prob.sym == 0 or prob.A.n == 0: return False A = prob.A C = prob.C b = prob.b x = prob.x resid = prob.resid n = A.n if prob.sym == 0 or n == 0: return True self.rhs(x, b, n) # compute right-hand side stdout.write("\nchol then update/downdate ") self.print_order(1) y = cs.xalloc(n) t = self.tic() S = cs.cs_schol(1, C) # symbolic Chol, amd(A+A') stdout.write("\nsymbolic chol time %8.2f ms\n" % self.toc(t)) t = self.tic() N = cs.cs_chol(C, S) # numeric Cholesky stdout.write("numeric chol time %8.2f ms\n" % self.toc(t)) if S is None or N is None: return False t = self.tic() cs.cs_ipvec(S.pinv, b, y, n) # y = P*b cs.cs_lsolve(N.L, y) # y = L\y cs.cs_ltsolve(N.L, y) # y = L'\y cs.cs_pvec(S.pinv, y, x, n) # x = P'*y stdout.write("solve chol time %8.2f ms\n" % self.toc(t)) stdout.write("original: ") self.print_resid(True, C, x, b, resid, prob) # print residual k = n / 2 # construct W W = cs.cs_spalloc(n, 1, n, True, False) Lp = N.L.p Li = N.L.i Lx = N.L.x Wp = W.p Wi = W.i Wx = W.x Wp[0] = 0 p1 = Lp[k] Wp[1] = Lp[k + 1] - p1 s = Lx[p1] while p1 < Lp[k + 1]: p2 = p1 - Lp[k] Wi[p2] = Li[p1] Wx[p2] = s * random() p1 += 1 t = self.tic() ok = cs.cs_updown(N.L, +1, W, S.parent) # update: L*L'+W*W' t1 = self.toc(t) stdout.write("update: time: %8.2f ms\n" % t1) if not ok: return False t = self.tic() cs.cs_ipvec(S.pinv, b, y, n) # y = P*b cs.cs_lsolve(N.L, y) # y = L\y cs.cs_ltsolve(N.L, y) # y = L'\y cs.cs_pvec(S.pinv, y, x, n) # x = P'*y t = self.toc(t) p = cs.cs_pinv(S.pinv, n) W2 = cs.cs_permute(W, p, None, True) # E = C + (P'W)*(P'W)' WT = cs.cs_transpose(W2, True) WW = cs.cs_multiply(W2, WT) WT = None W2 = None E = cs.cs_add(C, WW, 1, 1) WW = None if E is None or p is None: return False stdout.write("update: time: %8.2f ms(incl solve) " % (t1 + t)) self.print_resid(True, E, x, b, resid, prob) # print residual N = None # clear N t = self.tic() N = cs.cs_chol(E, S) # numeric Cholesky if N is None: return False cs.cs_ipvec(S.pinv, b, y, n) # y = P*b cs.cs_lsolve(N.L, y) # y = L\y cs.cs_ltsolve(N.L, y) # y = L'\y cs.cs_pvec(S.pinv, y, x, n) # x = P'*y t = self.toc(t) stdout.write("rechol: time: %8.2f ms(incl solve) " % t) self.print_resid(True, E, x, b, resid, prob) # print residual t = self.tic() ok = cs.cs_updown(N.L, -1, W, S.parent) # downdate: L*L'-W*W' t1 = self.toc(t) if not ok: return False stdout.write("downdate: time: %8.2f\n" % t1) t = self.tic() cs.cs_ipvec(S.pinv, b, y, n) # y = P*b cs.cs_lsolve(N.L, y) # y = L\y cs.cs_ltsolve(N.L, y) # y = L'\y cs.cs_pvec(S.pinv, y, x, n) # x = P'*y t = self.toc(t) stdout.printf("downdate: time: %8.2f ms(incl solve) " % (t1 + t)) self.print_resid(True, C, x, b, resid, prob) # print residual return True