How to use the cvxopt.base.spmatrix function in cvxopt

To help you get started, we’ve selected a few cvxopt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rwl / pylon / pylon / traits.py View on Github external
from cvxopt.base import spmatrix

        # FIXME: V, I, and J validation

        if tc not in ["d", "z"]:
            raise TraitError("tc should be 'd' or 'z'")

        if not isinstance(size, tuple):
            raise TraitError("size should be a tuple")
        for item in size:
            if not isinstance(item, int):
                raise TraitError("matrix dimensions must be integers")
            if item < 0:
                raise TraitError("matrix dimensions must be nonnegative")

        value = spmatrix(V, I, J, size, tc)

        self.size = size
        self.tc = tc

        super(SparseMatrix, self).__init__(value, **metadata)
github rwl / pylon / pylon / y.py View on Github external
def __call__(self, case):
        j = 0 + 1j
        buses = case.connected_buses
        n_buses = len(buses)
        branches = case.online_branches

        y = spmatrix([], [], [], size=(n_buses, n_buses), tc='z')

#        source_idxs = [e.source_bus for e in branches]
#        target_idxs = [e.target_bus for e in branches]
#
#        z = []
#        for e in branches:
#            if e.x != 0 and e.r != 0:
#                z.append(1/complex(e.r, e.x))
#            else:
#                z.append(Inf)
#
#        charge = [0.5*complex(0, e.b) for e in branches]
#
#        ts = [e.ratio*exp(j*e.phase_shift*pi/180) for e in branches]

        # TODO: Test speed increase with matrix algebra implementation
github cvxopt / cvxopt / src / python / cvxprog.py View on Github external
raise TypeError("'h' must be a 'd' matrix with one column")
    if not dims: dims = {'l': h.size[0], 'q': [], 's': []}

    # Dimension of the product cone of the linear inequalities. with 's' 
    # components unpacked.
    cdim = dims['l'] + sum(dims['q']) + sum([ k**2 for k in dims['s'] ])
    if h.size[0] != cdim:
        raise TypeError("'h' must be a 'd' matrix of size (%d,1)" %cdim)

    if G is None:
        if customx:
            def G(x, y, trans = 'N', alpha = 1.0, beta = 0.0):
                if trans == 'N': pass
                else: xscal(beta, y)
        else:
            G = spmatrix([], [], [], (0, x0.size[0]))
    if type(G) is matrix or type(G) is spmatrix:
        if G.typecode != 'd' or G.size != (cdim, x0.size[0]):
            raise TypeError("'G' must be a 'd' matrix with size (%d, %d)"\
                %(cdim, x0.size[0]))
        def fG(x, y, trans = 'N', alpha = 1.0, beta = 0.0):
            misc.sgemv(G, x, y, dims, trans = trans, alpha = alpha, 
                beta = beta)
    else:
        fG = G

    if A is None:
        if customy:
            def A(x, y, trans = 'N', alpha = 1.0, beta = 0.0):
                if trans == 'N': pass
                else: xscal(beta, y)
        else:
github rwl / pylon / pylon / routine / estimator.py View on Github external
Y = self.pf_routine.Y # Sparse admittance matrix.
        v = self.pf_routine.v # Vector of bus voltages.

        # Evaluate the Hessian.
        dSbus_dVm, dSbus_dVa = dSbus_dV(Y, v)
        dSbr_dVm, dSbr_dVa = dSbr_dV(branches, Ysource, Ytarget, v)

        H = spmatrix([
            dSf_dVa.real(),   dSf_dVm.real(),
            dSt_dVa.real(),   dSt_dVm.real(),
            dSbus_dVa.real(), dSbus_dVm.real(),
            spdiag(1.0, range(nb)), spmatrix(0.0, (nb,nb)),
            dSf_dVa.imag(),   dSf_dVm.imag(),
            dSt_dVa.imag(),   dSt_dVm.imag(),
            dSbus_dVa.imag(), dSbus_dVm.imag(),
            spmatrix(0.0, (nb,nb)), spdiag(1.0, range(nb))
        ])

        # True measurement.
        z = matrix([
            Sf.real(),
            St.real(),
            Sbus.real(),
            angle(V0),
            Sf.imag(),
            St.imag(),
            Sbus.imag(),
            abs(V0)
        ])

        # Create inverse of covariance matrix with all measurements.
        full_scale = 30
github rwl / pylon / pylon / routine / estimator.py View on Github external
# Save some values from the load flow solution.
        plf_source = [branch.p_source for branch in branches]
        qlf_source = [branch.q_source for branch in branches]
        plf_target = [branch.p_target for branch in branches]
        qlf_target = [branch.q_target for branch in branches]

        # Begin state estimation.
        Y = self.pf_routine.Y # Sparse admittance matrix.
        v = self.pf_routine.v # Vector of bus voltages.

        # Evaluate the Hessian.
        dSbus_dVm, dSbus_dVa = dSbus_dV(Y, v)
        dSbr_dVm, dSbr_dVa = dSbr_dV(branches, Ysource, Ytarget, v)

        H = spmatrix([
            dSf_dVa.real(),   dSf_dVm.real(),
            dSt_dVa.real(),   dSt_dVm.real(),
            dSbus_dVa.real(), dSbus_dVm.real(),
            spdiag(1.0, range(nb)), spmatrix(0.0, (nb,nb)),
            dSf_dVa.imag(),   dSf_dVm.imag(),
            dSt_dVa.imag(),   dSt_dVm.imag(),
            dSbus_dVa.imag(), dSbus_dVm.imag(),
            spmatrix(0.0, (nb,nb)), spdiag(1.0, range(nb))
        ])

        # True measurement.
        z = matrix([
            Sf.real(),
            St.real(),
            Sbus.real(),
            angle(V0),
github cvxopt / cvxopt / src / python / mosek.py View on Github external
(type(h) is not matrix and type(h) is not spmatrix) or 
        h.typecode != 'd' ]: 
        raise TypeError, "'hq' must be a list of %d dense or sparse "\
            "'d' matrices" %len(mq)
    a = [ k for k in xrange(len(mq)) if hq[k].size != (mq[k], 1) ]
    if a:
        k = a[0]
        raise TypeError, "'hq[%d]' has size (%d,%d).  Expected size "\
            "is (%d,1)." %(k, hq[k].size[0], hq[k].size[1], mq[k]) 

    N = ml + sum(mq)
    h = matrix(0.0, (N,1))
    if type(Gl) is matrix or [ Gk for Gk in Gq if type(Gk) is matrix ]:
        G = matrix(0.0, (N, n))
    else:
        G = spmatrix([], [], [], (N, n), 'd')
    h[:ml] = hl
    G[:ml,:] = Gl
    ind = ml
    for k in xrange(len(mq)):
        h[ind : ind + mq[k]] = hq[k]
        G[ind : ind + mq[k], :] = Gq[k]
        ind += mq[k]

    bkc = n*[ msk.boundkey.fx ] 
    blc = array(-c)
    buc = array(-c)

    bkx = ml*[ msk.boundkey.lo ] + sum(mq)*[ msk.boundkey.fr ]
    blx = ml*[ 0.0 ] + sum(mq)*[ -inf ]
    bux = N*[ +inf ]
github cvxopt / cvxopt / src / python / mosek.py View on Github external
if type(c) is not matrix or c.typecode != 'd' or c.size[1] != 1: 
        raise TypeError, "'c' must be a dense column matrix"
    n = c.size[0]
    if n < 1: raise ValueError, "number of variables must be at least 1"

    if (type(G) is not matrix and type(G) is not spmatrix) or \
        G.typecode != 'd' or G.size[1] != n:
        raise TypeError, "'G' must be a dense or sparse 'd' matrix "\
            "with %d columns" %n 
    m = G.size[0]
    if m is 0: raise ValueError, "m cannot be 0"

    if type(h) is not matrix or h.typecode != 'd' or h.size != (m,1):
        raise TypeError, "'h' must be a 'd' matrix of size (%d,1)" %m

    if A is None:  A = spmatrix([], [], [], (0,n), 'd')
    if (type(A) is not matrix and type(A) is not spmatrix) or \
        A.typecode != 'd' or A.size[1] != n:
        raise TypeError, "'A' must be a dense or sparse 'd' matrix "\
            "with %d columns" %n
    p = A.size[0]
    if b is None: b = matrix(0.0, (0,1))
    if type(b) is not matrix or b.typecode != 'd' or b.size != (p,1): 
        raise TypeError, "'b' must be a dense matrix of size (%d,1)" %p
 
    c   = array(c)        

    if I is None: I = set(range(n))

    if type(I) is not set: 
        raise TypeError, "invalid argument for integer index set"
github rwl / pylon / pylon / routine / spf.py View on Github external
slack_idx = [buses.index(v) for v in buses if v.type == "Slack"]
        for i in slack_idx:
            idx = a_idx[i]
            self._set_gy(idx)
            if not self.q_limiting:
                idx = v_idx[i]
                self._set_gy(idx)
            else:
                raise NotImplementedError, "See @SWclass/Gycall.m"

        # More Jacobian matrices
        n = dae.n
        m = dae.m
        dae.fx = spmatrix([], [], [], (n, n))
        dae.fy = spmatrix([], [], [], (n, m))
        dae.gx = spmatrix([], [], [], (m, n))
github cvxopt / cvxopt / src / python / mosek.py View on Github external
>>> mosek.options = {iparam.log:0} 
    
    see chapter 14 of the MOSEK Python API manual.                    
    """
    if (type(P) is not matrix and type(P) is not spmatrix) or \
        P.typecode != 'd' or P.size[0] != P.size[1]:
        raise TypeError, "'P' must be a square dense or sparse 'd' matrix "
    n = P.size[0]

    if n < 1: raise ValueError, "number of variables must be at least 1"

    if type(q) is not matrix or q.typecode != 'd' or q.size != (n,1):
        raise TypeError, "'q' must be a 'd' matrix of size (%d,1)" %n

    if G is None: G = spmatrix([], [], [], (0,n), 'd')
    if (type(G) is not matrix and type(G) is not spmatrix) or \
        G.typecode != 'd' or G.size[1] != n:
        raise TypeError, "'G' must be a dense or sparse 'd' matrix "\
            "with %d columns" %n 

    m = G.size[0]
    if h is None: h = matrix(0.0, (0,1))
    if type(h) is not matrix or h.typecode != 'd' or h.size != (m,1):
        raise TypeError, "'h' must be a 'd' matrix of size (%d,1)" %m

    if A is None:  A = spmatrix([], [], [], (0,n), 'd')
    if (type(A) is not matrix and type(A) is not spmatrix) or \
        A.typecode != 'd' or A.size[1] != n:
        raise TypeError, "'A' must be a dense or sparse 'd' matrix "\
            "with %d columns" %n
    p = A.size[0]
github pymc-devs / pymc3 / pymc / sandbox / NormalSubmodel.py View on Github external
        @deterministic
        def changeable_tau_slice(tau = self.tau):
            out = cvx.base.spmatrix([],[],[], (self.changeable_len, self.changeable_len))
            N = len(self.changeable_slices_from)
            for i in xrange(N):
                sfi = self.changeable_slices_from[i]
                sti = self.changeable_slices_to[i]
                for j in xrange(i, N):
                    sfj = self.changeable_slices_from[j]
                    stj = self.changeable_slices_to[j]
                    out[sti, stj] = tau[sfi,sfj]
            return out
            # return slice_by_stochastics(tau, self.changeable_stochastic_list,