Exemplo n.º 1
0
 def make_sym(self, A):
     """C = A + triu(A,1)'
     """
     AT = cs.cs_transpose (A, True)     # AT = A'
     cs.cs_fkeep (AT, Dropdiag(), None) # drop diagonal entries from AT
     C = cs.cs_add (A, AT, 1, 1)        # C = A+AT
     return C
Exemplo n.º 2
0
 def make_sym(self, A):
     """C = A + triu(A,1)'
     """
     AT = cs.cs_transpose(A, True)  # AT = A'
     cs.cs_fkeep(AT, Dropdiag(), None)  # drop diagonal entries from AT
     C = cs.cs_add(A, AT, 1, 1)  # C = A+AT
     return C
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    def transpose(self, A):
        AT = cs.cs_transpose (A, True) # AT = A'
#        print "AT:"
#        cs.cs_print (AT, False) # print AT
        return AT
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
 def transpose(self, A):
     AT = cs.cs_transpose(A, True)  # AT = A'
     #        print "AT:"
     #        cs.cs_print (AT, False) # print AT
     return AT