Exemplo n.º 1
0
    def test_power(self):
        x = Variable(3)
        y = Variable(3)
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = power(x.T*y, 0)
            self.assertTrue(s.is_constant())
            self.assertTrue(s.is_affine())
            self.assertTrue(s.is_quadratic())

        t = power(x-y, 1)
        self.assertFalse(t.is_constant())
        self.assertTrue(t.is_affine())
        self.assertTrue(t.is_quadratic())

        u = power(x+2*y, 2)
        self.assertFalse(u.is_constant())
        self.assertFalse(u.is_affine())
        self.assertTrue(u.is_quadratic())
        self.assertTrue(u.is_dcp())

        w = (x+2*y)**2
        self.assertFalse(w.is_constant())
        self.assertFalse(w.is_affine())
        self.assertTrue(w.is_quadratic())
        self.assertTrue(w.is_dcp())
Exemplo n.º 2
0
    def test_power(self):
        x = Variable(3)
        y = Variable(3)
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        s = power(x.T*y, 0)
        self.assertTrue(s.is_constant())
        self.assertTrue(s.is_affine())
        self.assertTrue(s.is_quadratic())

        t = power(x-y, 1)
        self.assertFalse(t.is_constant())
        self.assertTrue(t.is_affine())
        self.assertTrue(t.is_quadratic())

        u = power(x+2*y, 2)
        self.assertFalse(u.is_constant())
        self.assertFalse(u.is_affine())
        self.assertTrue(u.is_quadratic())
        self.assertTrue(u.is_dcp())

        w = (x+2*y)**2
        self.assertFalse(w.is_constant())
        self.assertFalse(w.is_affine())
        self.assertTrue(w.is_quadratic())
        self.assertTrue(w.is_dcp())
Exemplo n.º 3
0
    def test_power(self):
        x = Variable(3)
        y = Variable(3)
        self.assertFalse(x.is_constant())
        self.assertTrue(x.is_affine())
        self.assertTrue(x.is_quadratic())

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            s = power(x.T*y, 0)
            self.assertTrue(s.is_constant())
            self.assertTrue(s.is_affine())
            self.assertTrue(s.is_quadratic())

        t = power(x-y, 1)
        self.assertFalse(t.is_constant())
        self.assertTrue(t.is_affine())
        self.assertTrue(t.is_quadratic())

        u = power(x+2*y, 2)
        self.assertFalse(u.is_constant())
        self.assertFalse(u.is_affine())
        self.assertTrue(u.is_quadratic())
        self.assertTrue(u.is_dcp())

        w = (x+2*y)**2
        self.assertFalse(w.is_constant())
        self.assertFalse(w.is_affine())
        self.assertTrue(w.is_quadratic())
        self.assertTrue(w.is_dcp())
Exemplo n.º 4
0
def xexp_canon(expr, args):
    x = args[0]
    u = Variable(expr.shape, nonneg=True)
    t = Variable(expr.shape, nonneg=True)
    power_expr = power(x, 2)
    power_obj, constraints = power_canon(power_expr, power_expr.args)

    constraints += [ExpCone(u, x, t), u >= power_obj, x >= 0]
    return t, constraints
Exemplo n.º 5
0
    def test_non_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()

        s = max(vstack([x, y, z]))**2
        self.assertFalse(s.is_quadratic())

        t = max(vstack([x**2, power(y, 2), z]))
        self.assertFalse(t.is_quadratic())
Exemplo n.º 6
0
    def test_non_quadratic(self):
        x = Variable()
        y = Variable()
        z = Variable()
        with self.assertRaises(Exception) as cm:
            (x*y*z).is_quadratic()
        self.assertEqual(str(cm.exception), "Cannot multiply UNKNOWN and AFFINE.")

        s = max_entries(vstack(x, y, z))**2
        self.assertFalse(s.is_quadratic())

        t = max_entries(vstack(x**2, power(y, 2), z))
        self.assertFalse(t.is_quadratic())
Exemplo n.º 7
0
def inv_prod(value):
    """The reciprocal of a product of the entries of a vector ``x``.

    Parameters
    ----------
    x : Expression or numeric
        The expression whose reciprocal product is to be computed. Must have
        positive entries.

    Returns
    -------
    Expression
        .. math::
            \\left(\\prod_{i=1}^n x_i\\right)^{-1},

        where :math:`n` is the length of :math:`x`.
    """
    return power(inv_pos(geo_mean(value)), sum(value.shape))
Exemplo n.º 8
0
def huber_canon(expr, args):
    M = expr.M
    x = args[0]
    shape = expr.shape
    n = Variable(shape)
    s = Variable(shape)

    # n**2 + 2*M*|s|
    # TODO(akshayka): Make use of recursion inherent to canonicalization
    # process and just return a power / abs expressions for readability sake
    power_expr = power(n, 2)
    n2, constr_sq = power_canon(power_expr, power_expr.args)
    abs_expr = abs(s)
    abs_s, constr_abs = abs_canon(abs_expr, abs_expr.args)
    obj = n2 + 2 * M * abs_s

    constraints = constr_sq + constr_abs
    constraints.append(x == s + n)

    return obj, constraints
Exemplo n.º 9
0
def square(x):
    return power(x, 2)
Exemplo n.º 10
0
def square(x):
    """The square of an expression."""
    return power(x, 2)
Exemplo n.º 11
0
def inv_pos(x):
    return power(x, -1)
Exemplo n.º 12
0
def square(x):
    return power(x, 2)
Exemplo n.º 13
0
def inv_pos(x):
    """:math:`x^{-1}` for :math:`x > 0`.
    """
    return power(x, -1)
Exemplo n.º 14
0
def sqrt(x):
    """The square root of an expression."""
    return power(x, Fraction(1, 2))
Exemplo n.º 15
0
def sqrt(x):
    return power(x, Fraction(1, 2))