Пример #1
0
def _align_args(x, y):
    if not isinstance(x, Expression):
        x = Expression(x)
    if not isinstance(y, Expression):
        y = Expression(y)
    x = x.ravel()
    y = y.ravel()
    if x.size != y.size:
        raise RuntimeError('Illegal arguments to sum_relent.')
    return x, y
Пример #2
0
def weighted_sum_exp(c, x):
    """
    Return a coniclifts Expression of size 1, representing the signomial

        sum([ci * e^xi for (ci, xi) in (c, x)])

    :param c: a numpy ndarray of nonnegative numbers.
    :param x: a coniclifts Expression of the same size as c.
    """
    if not isinstance(x, Expression):
        x = Expression(x)
    if not isinstance(c, np.ndarray):
        c = np.array(c)
    if np.any(c < 0):
        raise RuntimeError(
            'Epigraphs of non-constant signomials with negative terms are not supported.'
        )
    if x.size != c.size:
        raise RuntimeError('Incompatible arguments.')
    x = x.ravel()
    c = c.ravel()
    kvs = []
    for i in range(x.size):
        if c[i] != 0:
            kvs.append((Exponential(x[i]), c[i]))
    d = dict(kvs)
    se = ScalarExpression(d, 0, verify=False)
    expr = se.as_expr()
    return expr
Пример #3
0
def relent(x, y, elementwise=False):
    if not isinstance(x, Expression):
        x = Expression(x)
    if not isinstance(y, Expression):
        y = Expression(y)
    if x.size != y.size:
        raise RuntimeError('Incompatible arguments to relent.')
    if elementwise:
        expr = np.empty(shape=x.shape, dtype=object)
        for tup in array_index_iterator(expr.shape):
            expr[tup] = ScalarExpression({RelEnt(x[tup], y[tup]): 1}, 0)
        return expr.view(Expression)
    else:
        x = x.ravel()
        y = y.ravel()
        d = dict((RelEnt(x[i], y[i]), 1) for i in range(x.size))
        return ScalarExpression(d, 0).as_expr()
Пример #4
0
 def __init__(self, y, K):
     self.id = PrimalProductCone._CONSTRAINT_ID_
     PrimalProductCone._CONSTRAINT_ID_ += 1
     if not isinstance(y, Expression):
         y = Expression(y)
     K_size = sum([co.len for co in K])
     if not y.size == K_size:
         raise RuntimeError('Incompatible dimensions for y (' + str(y.size) + ') and K (' + str(K_size) + ')')
     if np.any([co.type == 'P' for co in K]):
         raise RuntimeError('This function does not currently support the PSD cone.')
     self.y = y.ravel()
     self.K = K
     self.K_size = K_size
     pass
Пример #5
0
 def __init__(self, y, K):
     # y must belong to K^dagger
     self.id = DualProductCone._CONSTRAINT_ID_
     DualProductCone._CONSTRAINT_ID_ += 1
     if not isinstance(y, Expression):
         y = Expression(y)
     K_size = sum([co.len for co in K])
     if not y.size == K_size:
         raise RuntimeError(
             'Incompatible dimensions for y (%s) and K (%s)' %
             (str(y.size), str(K_size)))
     if np.any([co.type == 'P' for co in K]):
         raise RuntimeError(
             'This function does not currently support the PSD cone.')
     self.y = y.ravel()
     self.K = K
     self.K_size = K_size
     pass
Пример #6
0
def vector2norm(x):
    if not isinstance(x, Expression):
        x = Expression(x)
    x = x.ravel()
    d = {Vector2Norm(x): 1}
    return ScalarExpression(d, 0).as_expr()