Ejemplo n.º 1
0
    def _solve(self, b, tol):
        import numpy as np
        from scipy.linalg.blas import dasum, daxpy, ddot

        A = self._A
        M = self._M
        tol *= dasum(b)
        # Initialize.
        x = np.zeros(b.shape)
        r = b.copy()
        z = M(r)
        rz = ddot(r, z)
        p = z.copy()
        # Iterate.
        while True:
            Ap = A(p)
            alpha = rz / ddot(p, Ap)
            x = daxpy(p, x, a=alpha)
            r = daxpy(Ap, r, a=-alpha)
            if dasum(r) < tol:
                return x
            z = M(r)
            beta = ddot(r, z)
            beta, rz = beta / rz, beta
            p = daxpy(p, z, a=beta)
Ejemplo n.º 2
0
    def fit(self, X, Y):
        """
        Train a linear classifier using the perceptron learning algorithm.

        Note that this will only work if X is a sparse matrix, such as the
        output of a scikit-learn vectorizer.
        """
        self.find_classes(Y)

        # First determine which output class will be associated with positive
        # and negative scores, respectively.
        Ye = self.encode_outputs(Y)

        # Initialize the weight vector to all zeros.
        self.w = np.zeros(X.shape[1])

        X = X.toarray()

        # Iteration through sparse matrices can be a bit slow, so we first
        # prepare this list to speed up iteration.
        XY = list(zip(X, Ye))

        lam = 1 / len(Y)
        for i in range(1, self.n_iter):
            x, y = random.choice(XY)
            n = 1 / (lam * i)
            score = blas.ddot(x, self.w)
            self.w = (1 - n * lam) * self.w
            if score * y < 1:
                a = n * y
                blas.daxpy(x, self.w, a=a)
Ejemplo n.º 3
0
      def solve(self):
          
          x = self.rho/(self.rho+2)* self.K + 2/(self.rho+2) * self.D

          cost = ddot(self.D+x,self.D+x) #LA.norm(self.D+x)^2
                    
          return (x,cost)
Ejemplo n.º 4
0
 def P(x):
     x -= asarray(x * X * X.T)[0, :]
     if not normalized:
         x -= x.sum() / n
     else:
         x = daxpy(e, x, a=-ddot(x, e))
     return x
Ejemplo n.º 5
0
      def solve(self):
          
          x = self.rho/(self.rho-2)* self.K - 2/(self.rho-2) * self.D

          cost = ddot(self.D-x,self.D-x) #LA.norm(self.D-x)^2
          
          return (x,cost)
Ejemplo n.º 6
0
 def P(x):
     x -= asarray(x * X * X.T)[0, :]
     if not normalized:
         x -= x.sum() / n
     else:
         x = daxpy(e, x, a=-ddot(x, e))
     return x
Ejemplo n.º 7
0
      def solveX(self):

        x, c = self.optimizeX()
        c=self.gamma*self.delta*self.alpha*ddot(x, x) #self.problem.value    
                           
        #self.x.value = xRslt
        #self.xk = xRslt
        
        return (x, c) 
Ejemplo n.º 8
0
 def solveX(self):
     
   xRslt, costRslt =  self.optimizeX()
   costRslt=self.gamma*self.alpha*ddot(xRslt, xRslt)        
              
   #self.x.value = xRslt
   #self.xk = xRslt
   
   return (xRslt, costRslt) 
Ejemplo n.º 9
0
      def solveX(self):
          
          x = self.rho/(self.rho+2)* self.K + 2/(self.rho+2) * self.D

          cost = ddot(self.D+x,self.D+x) #LA.norm(self.D+x)^2
          
          self.x = x
          self.xk = x
                    
          return (x,cost)
Ejemplo n.º 10
0
    def g09_calculate_coupling(homo_monomer_a, lumo_monomer_a, homo_monomer_b,
                               lumo_monomer_b, nbf_monomer_a, f_d, s_d, cfrag):
        """

        :param homo_monomer_a:
        :param lumo_monomer_a:
        :param homo_monomer_b:
        :param lumo_monomer_b:
        :param nbf_monomer_a:
        :param f_d:
        :param s_d:
        :param cfrag:
        :return: "MO 1", "MO 2", "e 1, eV", "e 2, eV", "J_12, eV", "S_12",
                "e^eff_1, eV", "e^eff_2, eV", "J^eff_12, eV", "dE_12, eV"
        """
        irange = tuple(range(homo_monomer_a - 1, lumo_monomer_a))
        jrange = tuple(
            range(homo_monomer_b - 1 + nbf_monomer_a,
                  lumo_monomer_b + nbf_monomer_a))
        out = []
        for i in irange:
            for j in jrange:
                ctmp = cfrag[i, :] * f_d
                e1 = blas.ddot(cfrag[i, :], ctmp) * 27.2116
                ctmp = cfrag[j, :] * f_d
                e2 = blas.ddot(cfrag[j, :], ctmp) * 27.2116
                ctmp = cfrag[j, :] * f_d
                j12 = blas.ddot(cfrag[i, :], ctmp) * 27.2116
                ctmp = cfrag[j, :] * s_d
                s12 = blas.ddot(cfrag[i, :], ctmp)
                ee1 = 0.5 * (
                    (e1 + e2) - 2.0 * j12 * s12 +
                    (e1 - e2) * np.sqrt(1.0 - s12 * s12)) / (1.0 - s12 * s12)
                ee2 = 0.5 * (
                    (e1 + e2) - 2.0 * j12 * s12 -
                    (e1 - e2) * np.sqrt(1.0 - s12 * s12)) / (1.0 - s12 * s12)
                je12 = (j12 - 0.5 * (e1 + e2) * s12) / (1.0 - s12 * s12)
                de = np.sqrt((ee1 - ee2)**2 + (2 * je12)**2)
                out.append((i + 1, j + 1 - nbf_monomer_a, e1, e2, j12, s12,
                            ee1, ee2, je12, de))
        return out
Ejemplo n.º 11
0
 def _solve(self, b, tol):
     A = self._A
     M = self._M
     tol *= dasum(b)
     # Initialize.
     x = zeros(b.shape)
     r = b.copy()
     z = M(r)
     rz = ddot(r, z)
     p = z.copy()
     # Iterate.
     while True:
         Ap = A(p)
         alpha = rz / ddot(p, Ap)
         x = daxpy(p, x, a=alpha)
         r = daxpy(Ap, r, a=-alpha)
         if dasum(r) < tol:
             return x
         z = M(r)
         beta = ddot(r, z)
         beta, rz = beta / rz, beta
         p = daxpy(p, z, a=beta)
 def _solve(self, b, tol):
     A = self._A
     M = self._M
     tol *= dasum(b)
     # Initialize.
     x = zeros(b.shape)
     r = b.copy()
     z = M(r)
     rz = ddot(r, z)
     p = z.copy()
     # Iterate.
     while True:
         Ap = A(p)
         alpha = rz / ddot(p, Ap)
         x = daxpy(p, x, a=alpha)
         r = daxpy(Ap, r, a=-alpha)
         if dasum(r) < tol:
             return x
         z = M(r)
         beta = ddot(r, z)
         beta, rz = beta / rz, beta
         p = daxpy(p, z, a=beta)
Ejemplo n.º 13
0
      def solveX(self):
          
          if(self.rho.value == 2):
              self.rho.value += 1e-9
          
          x = (self.rho/(self.rho-2)* self.K - 2/(self.rho-2) * self.D).value
                     
          self.x.value = x
          self.xk = x

          cost = ddot(self.D-x,self.D-x) #LA.norm(self.D-x)^2
          
          return (x,cost)
Ejemplo n.º 14
0
def effectiveSampleSize(samples, monotSensitivity=0.01):
    length = float(len(samples))

    mean = sum(samples) / length
    shiftedSamples = np.array([x - mean for x in samples], float, order="F")
    gamma = [(blas.ddot(shiftedSamples, shiftedSamples, offx=i) / length)
             for i in range(len(samples))]
    monot = next(i for i, g in enumerate(gamma)
                 if g < monotSensitivity * gamma[0])
    gammaSum = sum(gamma[1:monot])
    v = (gamma[0] + 2 * gammaSum) / length
    ess = gamma[0] / float(v)
    return ess
Ejemplo n.º 15
0
      xsum += x[i]
      
  
  # the mean of all agents profiles (EVs + aggregator) 
  x_mean = xsum / N        
      
  for i in xrange(N):
      
      # ADMM iteration step (simplified for optimal exchage)            
      zi_old = np.copy(z[i])
      z[i] = x[i] - x_mean 
                          
      u[i] = u[i] + x_mean 
      
      # used for calculating convergence
      nxstack += ddot(x[i], x[i])# x[i]^2
      nystack += ddot(u[i], u[i])
      nzstack += ddot(z[i], z[i])
      
      zdiff = z[i] - zi_old
      nzdiffstack += ddot(zdiff, zdiff)
      
  # Temporary results for the convergence test
  nxstack = np.sqrt(nxstack)  # sqrt(sum ||x_i||_2^2) 
  nystack = rho * np.sqrt(nystack)  # sqrt(sum ||y_i||_2^2); rescaling y := rho * u
  nzstack = np.sqrt(nzstack) # dnrm2(-1 * zi)
  nzdiffstack = np.sqrt(nzdiffstack)
 
  # To calculate the real cost of EVs we need only the EVs profiles sum (=> - aggregator)
  x_sum = xsum - xAggr
  cost = costEVs + costAggr      
    #   generate random vector
    x_vec = np.random.normal(0, 1, N)
    x_et = Vector('x', x_vec)

    y_vec = np.random.normal(0, 1, N)
    y_et = Vector('y', y_vec)

    #   run dgemm in ET
    start = time.time()
    s = et.ddot(x_et, y_et)
    end = time.time()
    native_times.append(end - start)

    #   run dgemv in scipy
    start = time.time()
    s = blas.ddot(x_vec, y_vec)
    end = time.time()
    scipy_times.append(end - start)

    dims.append(N)
    N += 100

#   plot the results
fig, axs = plt.subplots()
axs.plot(dims, native_times, color='k', linestyle='--', label="ET")
axs.plot(dims, scipy_times, color='r', linestyle='--', label="Scipy")
axs.set_xlabel(r"dimension ($n$)")
axs.set_ylabel("log time (log(sec))")
axs.set_yscale("log")
plt.legend()
plt.title(r"DDOT: log time (log(sec)) vs. dimension ($n$)")
Ejemplo n.º 17
0
        # uk+1 = uk + Axk+1 + Bzk+1 − c
        u[i] = problem.solveU(x[i], z[i], u[i])

        # Axi + Bzi - c
        ri = problem.getPrimalResidual(x[i], z[i])
        # rho * A.T * B * (zi-zi_old)
        si = problem.getDualResidual(z[i], z_old[i])

        # (Axi, Bzi, c)
        eps_pri = problem.getPrimalFeasability()
        # A.T * ui
        eps_dual = problem.getDualFeasability()

        # used for calculating convergence
        r_norm += ddot(ri, ri)  # (ri)^2
        s_norm += ddot(si, si)  # (si)^2

        # used for calculating convergence
        nxstack += ddot(eps_pri[0], eps_pri[0])  # (Axi)^2
        nzstack += ddot(eps_pri[1], eps_pri[1])  # (Bzi)^2
        ncstack += ddot(eps_pri[2], eps_pri[2])  # c^2

        nystack += ddot(eps_dual, eps_dual)  # (A.T * ui)^2

    ####
    ##
    ## ADMM convergence criteria
    ##
    ## x ∈ Rn and z ∈ Rm, where A ∈ Rp×n, B ∈ Rp×m, and c ∈ Rp
    ##
Ejemplo n.º 18
0
 def solve(self):
     
   xRslt =  self.optimize()
   costRslt=self.gamma*self.alpha*ddot(xRslt, xRslt)
   
   return (xRslt, costRslt) 
Ejemplo n.º 19
0
      def solve(self):

        xRslt =  self.optimize()
        costRslt=self.gamma*self.delta*self.alpha*ddot(xRslt, xRslt) #self.problem.value    
        
        return (xRslt, costRslt) 
Ejemplo n.º 20
0
        for i in xrange(cs):
            # uk+1 = uk + Axk+1 + Bzk+1 − c
            ui[i] = problem.solveU(xi[i], zi[i], ui[i])
            
            # Axi + Bzi - c
            ri = problem.getPrimalResidual(xi[i],zi[i])
            # rho * A.T * B * (zi-zi_old)
            si = problem.getDualResidual(zi[i], zi_old[i])

            # (Axi, Bzi, c)
            eps_pri = problem.getPrimalFeasability()
            # A.T * ui
            eps_dual = problem.getDualFeasability()
                        
            # used for calculating convergence
            send[0] += ddot(ri, ri)# (ri)^2
            send[1] += ddot(si, si)# (si)^2
            
            # used for calculating convergence
            send[2] += ddot(eps_pri[0], eps_pri[0])# (Axi)^2
            send[3] += ddot(eps_pri[1], eps_pri[1])# (Bzi)^2
            send[4] += ddot(eps_pri[2], eps_pri[2])# c^2
            
            send[5] += ddot(eps_dual, eps_dual)# (A.T * ui)^2
            
            if(i == 0 and RESOURCE_LIB):
               send[7] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss #memory usage
            
        # Reduces values on all processes to a single value onto all processes:
        # Here the operation computes the global sum over all processors of the contents of the vector
        # "send", and stores the result on every processor in "recv".
Ejemplo n.º 21
0
def lsqr(A, b, niter = 0, x = None, damp = 0, condlim = 0, atol = 0, btol = 0,
         Kr = None, Kl = None, debug = 0):

    """
    x, rho, eta, (info, i, msg, anorm, arnorm, acond) =
        lsqr(A, b, niter = 0, x = None, damp = 0, condlim = 0, atol = 0,
             btol = 0, K = None, debug = 0)

    compute the regularized iterative solution of Ax = b
    rho and eta contain the norms of the solution and residual

    Kr (Kl) can be a right (left) pre-conditioner K which has to
    implement the call K.precon(x, y) for y = Kx

    Reference: C. C. Paige & M. A. Saunders, "LSQR: an algorithm for 
    sparse linear equations and sparse least squares", ACM Trans. 
    Math. Software 8 (1982), 43-71.

    see also the Matlab implementation at:
        http://www.stanford.edu/group/SOL/software/lsqr/matlab/lsqr.m
    """

    Ax  = A.matvec
    Atx = A.matvec_transp
    m, n = A.shape
    if niter <= 0:
        niter = min(m, n)
    info = 0

    msg = [
        'The iteration limit has been reached',
        'Ax - b is small enough, given atol, btol',
        'The least-squares solution is good enough, given atol',
        'The estimate of cond(Abar) has exceeded condlim',
        'Ax - b is small enough for this machine',
        'The least-squares solution is good enough for this machine',
        'Cond(Abar) seems to be too large for this machine',
    ]

    # Prepare for LSQR iteration
    if nxTypecode(b) != nxFloat or len(b) != m:
        raise TypeError("Vector b has wrong type or length")
    bnorm = norm2(b)
    if bnorm == 0:
        raise ValueError("Vector b must be nonzero")

    # solution vector
    if x is None:
        x = N.zeros(n, nxFloat)
    else:
        if nxTypecode(x) != nxFloat or len(x) != n:
            raise TypeError("Vector x has wrong type or length")
        x[:] = 0

    # auxiliary vectors
    p = N.zeros(m, nxFloat)
    r = N.zeros(n, nxFloat)

    # the Preconditioner needs one extra storage vector
    if Kr is not None:
        rv = N.zeros(x.shape, nxTypecode(x))

    if Kl is not None:
        lv = N.zeros(b.shape, nxTypecode(b))

    # Initialize the bi-diagonalization
    if Kl is not None:      # use the left pre-conditioner
        u = N.zeros(b.shape, nxTypecode(b))
        Kl.precon(b, u)
        bnorm = norm2(u)
        u /= bnorm
    else:
        u = b/bnorm
    beta = bnorm
    Atx(u, r)
    if Kr is not None:      # use the right pre-conditioner
        Kr.precon(r, rv)
        tmpv = r
        r = rv
        rv = tmpv
    alpha = norm2(r)
    v = r/alpha
    w = copy.copy(v)
    rho_bar = alpha
    phi_bar = beta

    # initialize variables for the computation of norms
    eta = []
    rho = []
    res2 = 0
    c2 = -1
    s2 = 0
    z = 0
    xnorm = 0
    bbnorm = 0
    ddnorm = 0
    xxnorm = 0
    rnorm = beta
    arnorm = alpha*beta
    dampsq = damp*damp
    ctol = 0
    if condlim > 0:
        ctol = 1/condlim

    if debug:
        s = "%3s%10s%10s%10s%10s%10s%10s%10s\n" % ('nit', 'alpha', 'beta',
            'rrho', 'phi', 'theta', 'rho_bar', 'phi_bar')
        s += '-' * (len(s)-1)
        print s

    i = 1
    while i < niter and info == 0:
        i += 1
        alpha_old = alpha
        beta_old = beta

        # Continue the bi-diagonalization
        if Kr is not None:      # use the right pre-conditioner
            Kr.precon(v, rv)
            tmpv = v
            v = rv
            rv = tmpv
        Ax(v, p)
        u *= alpha
        p -= u                  # p = A v - alpha u
        beta = norm2(p)
        u = N.divide(p, beta, u)

        if Kl is not None:      # use the left pre-conditioner
            Kl.precon(u, lv)
            tmpv = u
            u = lv
            lv = tmpv
        Atx(u, r)
        v *= beta
        r -= v                  # r = A^T u - beta v
        alpha = norm2(r)
        v = N.divide(r, alpha, v)

        # Use a plane rotation to eliminate the damping parameter.
        # This alters the diagonal (rho_bar) of the lower-bidiagonal matrix.

        rho_bar1 = pythag(rho_bar, damp)
        c1       = rho_bar / rho_bar1
        s1       = damp / rho_bar1
        psi      = s1 * phi_bar
        phi_bar  = c1 * phi_bar

        # Use a plane rotation to eliminate the subdiagonal element (beta)
        # of the lower-bidiagonal matrix, giving an upper-bidiagonal matrix.

        rrho     = pythag(rho_bar1, beta)
        c1       = rho_bar1 / rrho
        s1       = beta / rrho
        theta    =  s1 * alpha
        rho_bar  = -c1 * alpha
        phi      =  c1 * phi_bar
        phi_bar  =  s1 * phi_bar

        if debug:
            print "%3d%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f%10.4f" % \
                (i, alpha, beta, rrho, phi, theta, rho_bar, phi_bar)

        # Update x and w
        w /= rrho
        r = N.multiply(w, phi, r)
        x += r                      # x = x + (phi/rrho) * w
        ddnorm += ddot(w, w)
        w *= -theta
        w += v                      # w = v - (theta/rrho) * w

        # Use a plane rotation on the right to eliminate the
        # super-diagonal element (theta) of the upper-bidiagonal matrix.
        # Then use the result to estimate  norm(x).

        delta = s2*rrho
        gamma_bar = -c2*rrho
        rhs = phi - delta*z
        z_bar = rhs/gamma_bar
        xnorm = N.sqrt(xxnorm + z_bar*z_bar)
        gamma = pythag(gamma_bar, theta)
        c2 = gamma_bar/gamma
        s2 = theta/gamma
        z = rhs/gamma
        xxnorm += z*z

        eta.append(xnorm)

        # Compute norms for convergence criteria (see TOMS 583)
        bbnorm += alpha_old*alpha_old + beta*beta
        anorm = N.sqrt(bbnorm)
        acond = anorm * N.sqrt(ddnorm)
        arnorm = alpha * abs(s1*phi)
        res1 = phi_bar * phi_bar
        res2 += psi * psi
        rnorm = N.sqrt(res1 + res2)

        rho.append(rnorm)

        # Distinguish between
        #    r1norm = ||b - Ax|| and
        #    r2norm = rnorm in current code
        #           = sqrt(r1norm^2 + damp^2*||x||^2).
        #    Estimate r1norm from
        #    r1norm = sqrt(r2norm^2 - damp^2*||x||^2).
        # Although there is cancellation, it might be accurate enough.

        r1sq   = rnorm*rnorm - dampsq*xxnorm
        r1norm = N.sqrt(abs(r1sq))
        if r1sq < 0: r1norm = -r1norm
        r2norm = rnorm

        # Now use these norms to estimate certain other quantities,
        # some of which will be small near a solution.

        test1 = rnorm/bnorm
        test2 = arnorm/(anorm*rnorm)
        test3 = 1/acond
        t1    = test1/(1 + anorm*xnorm/bnorm)
        rtol  = btol + atol*anorm*xnorm/bnorm

        # The following tests guard against extremely small values of
        # atol, btol  or  ctol.  (The user may have set any or all of
        # the parameters  atol, btol, conlim  to 0.)
        # The effect is equivalent to the normal tests using
        # atol = eps,  btol = eps,  condlim = 1/eps.

        if 1 + test3  <= 1: info = 6
        if 1 + test2  <= 1: info = 5
        if 1 + t1     <= 1: info = 4

        # Allow for tolerances set by the user.

        if test3 <= ctol: info = 3
        if test2 <= atol: info = 2
        if test1 <= rtol: info = 1

    if Kr is not None:
        Kr.precon(x, rv)
        x[:] = rv

    return x, rho, eta, (info, i, msg[info], anorm, arnorm, acond)
Ejemplo n.º 22
0
    # To calculate the real cost of EVs we need only the EVs profiles sum (=> - aggregator)
    x_sum = x_mean - xAggr
 
 # now, it is really the mean
 x_mean /=  N
 
 for i in xrange(n):
     # ADMM iteration step (simplified for optimal exchage)
 
     zi_old = np.copy(zi[i])
     zi[i] = xi[i] - x_mean            
     
     ui[i] = ui[i] + x_mean 
     
     # used for calculating convergence
     send[0] += ddot(xi[i], xi[i])# xi[i]^2
     send[1] += ddot(ui[i], ui[i])
     send[2] += ddot(zi[i], zi[i])
     
     zdiff = zi[i] - zi_old
     send[3] += ddot(zdiff, zdiff)
     
     if(RESOURCE_LIB and i == 0):
        send[5] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss #memory usage
     
 # similarly, sums all local "send" into "recv"
 comm.Allreduce(send, recv, op=MPI.SUM)
 
  # Temporary results for the convergence test
 nxstack = np.sqrt(recv[0])  # sqrt(sum ||x_i||_2^2) 
 nystack = rho * np.sqrt(recv[1])  # sqrt(sum ||y_i||_2^2) ; rescaling y := rho * u
Ejemplo n.º 23
0
      xsum += x[i]
      
     
  # the mean of all agents profiles (EVs + aggregator) 
  x_mean = xsum / N        
              
  for i in xrange(N):
      
      # ADMM iteration step (simplified for optimal exchage)            
      zi_old = np.copy(z[i])
      z[i] = x[i] - x_mean 
                          
      u[i] = u[i] + x_mean 
      
      # used for calculating convergence
      nxstack += ddot(x[i], x[i])# x[i]^2
      nystack += ddot(u[i], u[i])
      nzstack += ddot(z[i], z[i])
      
      zdiff = z[i] - zi_old
      nzdiffstack += ddot(zdiff, zdiff)
      
      
  # Temporary results for the convergence test
  nxstack = np.sqrt(nxstack)  # sqrt(sum ||x_i||_2^2) 
  nystack = rho * np.sqrt(nystack)  # sqrt(sum ||y_i||_2^2); rescaling y := rho * u
  nzstack = np.sqrt(nzstack) # dnrm2(-1 * zi)
  nzdiffstack = np.sqrt(nzdiffstack)
 
  # To calculate the real cost of EVs we need only the EVs profiles sum (=> - aggregator)
  x_sum = xsum - xAggr
def L2_inner_ddot(f,g,dx):
    return blas.ddot(f,g)*dx/np.pi
        send[4] += ci #costEVs
     
     # sum all profiles
     xsum += xi[i]            
     
     
 # Reduces values on all processes to a single value onto all processes.  
 # Here the operation computes the global sum over all processors of the contents of 
 # the vector "xsum", and stores the result on every processor in "x_mean".
 comm.Allreduce(xsum, x_mean, op=MPI.SUM)     
 
 # at this point x_mean represents the sum of all agents profiles (EVs + aggregator)        
 if rank == 0:
    # To calculate the real cost of EVs we need only the EVs profiles sum (=> - aggregator)
    x_sum = x_mean - xAggr
    cost = ddot(D + x_sum, D + x_sum) #+ delta*costEVs; real cost of the EVs        
 
 # now, it is really the mean
 x_mean /=  N
 
 for i in xrange(n):
     # ADMM iteration step (simplified for optimal exchage)
 
     zi_old = np.copy(zi[i])
     zi[i] = xi[i] - x_mean            
     
     ui[i] = ui[i] + x_mean 
     
     # used for calculating convergence
     send[0] += ddot(xi[i], xi[i])# xi[i]^2
     send[1] += ddot(ui[i], ui[i])
Ejemplo n.º 26
0
def cgls(A, b, niter = 0, x = None, condlim = 0, atol = 0, btol = 0,
         Kl = None, Ki = None, Kr = None, debug = 0):
    """
    preconditioners:
        Kl      ... left preconditioner:  Kl At (A x - b) = 0
        Kr      ... right preconditioner: At (A Kr x - b) = 0
        Kl & Kr ... split preconditioner: Kl At (A Kr x - b) = 0
        Ki      ... inner preconditioner: At Ki (A x - b) = 0
    """

    swap = lambda x,y: (y,x)
    Ax  = A.matvec
    Atx = A.matvec_transp
    m, n = A.shape
    if niter <= 0:
        niter = min(m, n)
    info = 0

    if debug:
        fmt = "%4d " + "%16.6e" * 4

    # Prepare for CGLS iteration
    if nxTypecode(b) != nxFloat or len(b) != m:
        raise TypeError("Vector b has wrong type or length")
    bnorm = norm2(b)
    if bnorm == 0:
        raise ValueError("Vector b must be nonzero")

    # solution vector, we use x0 = 0
    if x is None:
        x = N.zeros(n, nxFloat)
    else:
        if nxTypecode(x) != nxFloat or len(x) != n:
            raise TypeError("Vector x has wrong type or length")
        x[:] = 0

    # auxiliary vectors
    s = N.zeros(n, nxFloat)
    q = N.zeros(m, nxFloat)

    # the Preconditioners need one extra storage vector
    if Kl is not None or Kr is not None:
        vn = N.zeros(x.shape, nxTypecode(x))

    if Ki is not None:
        vm = N.zeros(b.shape, nxTypecode(b))

    # initialize CG iteration
    if Ki is not None:
        r = N.zeros(b.shape, nxFloat)
        Ki.precon(b, r)
    else:
        r = copy.copy(b) # since: x0 = 0  and thus also: Kr x0 = 0
    bnorm = norm2(r)
    Atx(r, s)
    if Kl is not None:
        p = N.zeros(s.shape, nxFloat)
        Kl.precon(s, p)
    else:
        p = copy.copy(s)
    gamma = ddot(s, p)

    # continue CG iteration
    i = 1
    while i < niter and info == 0:
        i += 1
        if Kr is not None:
            Kr.precon(p, vn)
            p, vn = swap(p, vn)
        Ax(p, q)
        if Ki is not None:
            Ki.precon(q, vm)
            q, vm = swap(q, vm)
            alpha = gamma/ddot(q, vm)
        else:
            alpha = gamma/ddot(q, q)
        x += N.multiply(p, alpha, s)    # temporary use of s
        r -= N.multiply(q, alpha, q)
        Atx(r, s)
        if Kl is not None:
            Kl.precon(s, vn)
            s, vn = swap(s, vn)
            gamma_new = ddot(s, vn)
        else:
            gamma_new = ddot(s, s)
        beta = gamma_new/gamma
        if debug:
            print fmt % (i, alpha, beta, gamma, gamma_new)
        gamma = gamma_new
        p *= beta
        p += s

        if norm2(r)/bnorm < 1e-8:
            info = 1
        if gamma < 1e-8:
            info = 3

    return x, r, (info, i)