Esempio n. 1
0
 def __init__(self, lh_expr, rh_expr):
     lh_expr = DivExpression.cast_to_const(lh_expr)
     rh_expr = DivExpression.cast_to_const(rh_expr)
     if lh_expr.is_scalar() and not rh_expr.is_scalar():
         lh_expr = promote(lh_expr, rh_expr.shape)
     elif rh_expr.is_scalar() and not lh_expr.is_scalar():
         rh_expr = promote(rh_expr, lh_expr.shape)
     super(DivExpression, self).__init__(lh_expr, rh_expr)
Esempio n. 2
0
 def __init__(self, lh_expr, rh_expr):
     lh_expr = multiply.cast_to_const(lh_expr)
     rh_expr = multiply.cast_to_const(rh_expr)
     if lh_expr.is_scalar() and not rh_expr.is_scalar():
         lh_expr = promote(lh_expr, rh_expr.shape)
     elif rh_expr.is_scalar() and not lh_expr.is_scalar():
         rh_expr = promote(rh_expr, lh_expr.shape)
     super(multiply, self).__init__(lh_expr, rh_expr)
Esempio n. 3
0
def kl_div_canon(expr, args):
    shape = expr.shape
    x = promote(args[0], shape)
    y = promote(args[1], shape)
    t = Variable(shape)
    constraints = [ExpCone(t, x, y)]
    obj = y - x - t
    return obj, constraints
Esempio n. 4
0
def norm_inf_canon(expr, args):
    x = args[0]
    shape = expr.shape
    t = Variable(shape)

    promoted_t = promote(t, x.shape)
    return t, [x <= promoted_t, x + promoted_t >= 0]
Esempio n. 5
0
def lambda_max_canon(expr, args):
    A = args[0]
    n = A.shape[0]
    t = Variable()
    prom_t = promote(t, (n, ))
    # Constrain I*t - A to be PSD; note that this expression must be symmetric.
    tmp_expr = diag_vec(prom_t) - A
    constr = [tmp_expr == tmp_expr.T, PSD(tmp_expr)]
    return t, constr
Esempio n. 6
0
def lambda_max_canon(expr, args):
    A = args[0]
    n = A.shape[0]
    t = Variable()
    prom_t = promote(t, (n, ))
    # Constrain I*t - A to be PSD; note that this expression must be symmetric.
    tmp_expr = diag_vec(prom_t) - A
    constr = [PSD(tmp_expr)]
    if not A.is_symmetric():
        ut = upper_tri(A)
        lt = upper_tri(A.T)
        constr.append(ut == lt)
    return t, constr
Esempio n. 7
0
def add_canon(expr, args):
    if expr.is_scalar():
        return log_sum_exp(hstack(args)), []

    rows = []
    summands = [promote(s, expr.shape) if s.is_scalar() else s for s in args]
    if len(expr.shape) == 1:
        for i in range(expr.shape[0]):
            row = []
            row.append(
                log_sum_exp(hstack([summand[i] for summand in summands])))
            rows.append(row)
        return reshape(bmat(rows), expr.shape), []
    else:
        for i in range(expr.shape[0]):
            row = []
            for j in range(expr.shape[1]):
                row.append(
                    log_sum_exp(hstack([summand[i, j]
                                        for summand in summands])))
            rows.append(row)
        return reshape(bmat(rows), expr.shape), []