def system_function(x, Y, pv_inds, slack_inds, s):
     nb = Y.shape[0]
     vX = x[:2*nb]
     pF = powerflow(vX,Y)
     C = PowerNetwork.get_C(pv_inds, slack_inds, nb)
     pF += C.T.dot(x[2*nb:])
     out = np.hstack([C.dot(x[:2*nb]), pF]) - s
     return out
    def lamed(self):
        """lamed, the Phoenician name for lambda. (As the word lambda is 
        reserved in Python.)"""
        if self._lamed is None:
            eH = powerflow(self.ev, self.extY)
            resid = self.eb - eH
            rresid = resid[self.reindr2eindr]

            out = spsl.lsqr(self.C, rresid, atol=1e-14, btol=1e-14)
            # assert out[3] < 1e-8
            self._lamed = np.matrix(out[0]).T
        return self._lamed
 def lamed(self):
     """lamed, the Phoenician name for lambda. (As the word lambda is 
     reserved in Python.)"""
     if self._lamed is None:
         eH = powerflow(self.ev, self.extY)
         resid = self.eb - eH
         rresid = resid[self.reindr2eindr]
         
         out = spsl.lsqr(self.C, rresid, atol=1e-14, btol=1e-14)
         # assert out[3] < 1e-8
         self._lamed = np.matrix(out[0]).T
     return self._lamed
    def dHdv(self):
        """Compute the reordered extended Jacobian matrix."""
        if self._dHdv is None:
            en = self.en
            extY = self.extY
            e2r = self.eindr2reindr
            vM = self.ev[en:]
            vTheta = self.ev[:en]
            vC = vM * np.exp(1j * vTheta)
            vA = np.exp(1j * vTheta)
            s = powerflow(self.ev, extY)
            s = s[:en] + 1j * s[en:]
            ij = []
            data = []

            def add_element(i, j, val):
                ij.append(i)
                ij.append(j)
                data.append(val)

            for i, j in zip(*extY.nonzero()):
                val = np.imag(vC[i] * np.conj(extY[i, j] * vC[j]))
                add_element(e2r[i], e2r[j], val)

                val = np.real(vC[i] * np.conj(extY[i, j] * vA[j]))
                add_element(e2r[i], e2r[en + j], val)

                val = np.real(-vC[i] * np.conj(extY[i, j] * vC[j]))
                add_element(e2r[en + i], e2r[j], val)

                val = np.imag(vC[i] * np.conj(extY[i, j] * vA[j]))
                add_element(e2r[en + i], e2r[en + j], val)

            extYvC = extY.dot(vC)
            for i in xrange(en):
                if s[i] == 0 and extYvC[i] == 0:
                    continue
                add_element(e2r[i], e2r[i], -np.imag(s[i]))
                add_element(e2r[i], e2r[en + i],
                            np.real(vA[i] * np.conj(extYvC[i])))
                add_element(e2r[en + i], e2r[i], np.real(s[i]))
                add_element(e2r[en + i], e2r[en + i],
                            np.imag(vA[i] * np.conj(extYvC[i])))

            ij = np.reshape(ij, (2, len(ij) / 2), order='F')
            self._dHdv = sps.csr_matrix((data, ij), shape=(2 * en, 2 * en))
        return self._dHdv
 def dHdv(self):
     """Compute the reordered extended Jacobian matrix."""
     if self._dHdv is None:
         en = self.en
         extY = self.extY
         e2r = self.eindr2reindr
         vM = self.ev[en:]
         vTheta = self.ev[:en]
         vC = vM * np.exp(1j * vTheta)
         vA = np.exp(1j*vTheta)
         s = powerflow(self.ev, extY)
         s = s[:en] + 1j * s[en:]
         ij = []
         data = []
         def add_element(i,j,val):
             ij.append(i)
             ij.append(j)
             data.append(val)
         for i,j in zip(*extY.nonzero()):
             val = np.imag(vC[i] * np.conj(extY[i,j] * vC[j]))
             add_element(e2r[i], e2r[j], val)
             
             val = np.real(vC[i] * np.conj(extY[i,j] * vA[j]))
             add_element(e2r[i], e2r[en+j], val)
             
             val = np.real(-vC[i] * np.conj(extY[i,j] * vC[j]))
             add_element(e2r[en+i], e2r[j], val)
             
             val = np.imag(vC[i] * np.conj(extY[i,j] * vA[j]))
             add_element(e2r[en+i], e2r[en+j], val)
             
         extYvC = extY.dot(vC)
         for i in xrange(en):
             if s[i] == 0 and extYvC[i] == 0:
                 continue
             add_element(e2r[   i], e2r[   i], -np.imag(s[i]))
             add_element(e2r[   i], e2r[en+i],  
                         np.real(vA[i] * np.conj(extYvC[i])))
             add_element(e2r[en+i], e2r[   i],  np.real(s[i]))
             add_element(e2r[en+i], e2r[en+i],  
                         np.imag(vA[i] * np.conj(extYvC[i])))
             
         ij = np.reshape(ij, (2, len(ij)/2), order='F')
         self._dHdv = sps.csr_matrix((data, ij), shape=(2*en, 2*en))
     return self._dHdv