本文整理汇总了Python中cvxpy.expressions.constants.Constant类的典型用法代码示例。如果您正苦于以下问题:Python Constant类的具体用法?Python Constant怎么用?Python Constant使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Constant类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_expression
def test_add_expression(self):
# Vectors
c = Constant([2,2])
exp = self.x + c
self.assertEqual(exp.curvature, u.Curvature.AFFINE)
self.assertEqual(exp.sign, u.Sign.UNKNOWN)
self.assertEqual(exp.canonicalize()[0].size, (2,1))
self.assertEqual(exp.canonicalize()[1], [])
self.assertEqual(exp.name(), self.x.name() + " + " + c.name())
self.assertEqual(exp.size, (2,1))
z = Variable(2, name='z')
exp = exp + z + self.x
with self.assertRaises(Exception) as cm:
(self.x + self.y)
self.assertEqual(str(cm.exception), "Incompatible dimensions.")
# Matrices
exp = self.A + self.B
self.assertEqual(exp.curvature, u.Curvature.AFFINE)
self.assertEqual(exp.size, (2,2))
with self.assertRaises(Exception) as cm:
(self.A + self.C)
self.assertEqual(str(cm.exception), "Incompatible dimensions.")
示例2: test_constant_copy
def test_constant_copy(self):
"""Test the copy function for Constants.
"""
x = Constant(2)
y = x.copy()
self.assertEqual(y.size, (1, 1))
self.assertEqual(y.value, 2)
示例3: test_constants
def test_constants(self):
c = Constant(2)
self.assertEqual(c.name(), str(2))
c = Constant(2)
self.assertEqual(c.value, 2)
self.assertEqual(c.size, (1,1))
self.assertEqual(c.curvature, u.Curvature.CONSTANT_KEY)
self.assertEqual(c.sign, u.Sign.POSITIVE_KEY)
self.assertEqual(Constant(-2).sign, u.Sign.NEGATIVE_KEY)
self.assertEqual(Constant(0).sign, u.Sign.ZERO_KEY)
self.assertEqual(c.canonical_form[0].size, (1,1))
self.assertEqual(c.canonical_form[1], [])
# coeffs = c.coefficients()
# self.assertEqual(coeffs.keys(), [s.CONSTANT])
# self.assertEqual(coeffs[s.CONSTANT], [2])
# Test the sign.
c = Constant([[2], [2]])
self.assertEqual(c.size, (1, 2))
self.assertEqual(c.sign, u.Sign.UNKNOWN_KEY)
# Test sign of a complex expression.
c = Constant([1, 2])
A = Constant([[1,1],[1,1]])
exp = c.T*A*c
self.assertEqual(exp.sign, u.Sign.UNKNOWN_KEY)
self.assertEqual((c.T*c).sign, u.Sign.UNKNOWN_KEY)
exp = c.T.T
self.assertEqual(exp.sign, u.Sign.UNKNOWN_KEY)
exp = c.T*self.A
self.assertEqual(exp.sign, u.Sign.UNKNOWN_KEY)
示例4: setUp
def setUp(self):
self.a = Variable()
self.x = Variable(2, name='x')
self.y = Variable(2, name='y')
self.A = Variable(2,2)
self.c = Constant(3)
self.C = Constant([[1, 2], [1, 2]])
示例5: test_constants
def test_constants(self):
c = Constant(2)
self.assertEqual(c.name(), str(2))
c = Constant(2, name="c")
self.assertEqual(c.name(), "c")
self.assertEqual(c.value, 2)
self.assertEqual(c.size, (1,1))
self.assertEqual(c.curvature, u.Curvature.CONSTANT)
self.assertEqual(c.sign, u.Sign.POSITIVE)
self.assertEqual(Constant(-2).sign, u.Sign.NEGATIVE)
self.assertEqual(Constant(0).sign, u.Sign.ZERO)
self.assertEqual(c.canonicalize()[0].size, (1,1))
self.assertEqual(c.canonicalize()[1], [])
coeffs = c.coefficients(self.intf)
self.assertEqual(coeffs.keys(), [Constant])
self.assertEqual(coeffs[Constant], 2)
# Test the sign.
c = Constant([[2],[2]])
self.assertEqual(c.size, (1,2))
self.assertEqual(c.sign.neg_mat.value.shape, (1,2))
# Test sign of a complex expression.
c = Constant([1, 2])
A = Constant([[1,1],[1,1]])
exp = c.T*A*c
self.assertEqual(exp.sign, u.Sign.POSITIVE)
self.assertEqual((c.T*c).sign, u.Sign.POSITIVE)
exp = c.T.T
self.assertEqual(exp.sign.pos_mat.value.ndim, 2)
exp = c.T*self.A
self.assertEqual(exp.sign.pos_mat.value.ndim, 2)
示例6: test_matmul_expression
def test_matmul_expression(self):
"""Test matmul function, corresponding to .__matmul__( operator.
"""
if PY35:
# Vectors
c = Constant([[2], [2]])
exp = c.__matmul__(self.x)
self.assertEqual(exp.curvature, s.AFFINE)
self.assertEqual(exp.sign, s.UNKNOWN)
self.assertEqual(exp.canonical_form[0].size, (1, 1))
self.assertEqual(exp.canonical_form[1], [])
# self.assertEqual(exp.name(), c.name() + " .__matmul__( " + self.x.name())
self.assertEqual(exp.size, (1, 1))
with self.assertRaises(Exception) as cm:
self.x.__matmul__(2)
self.assertEqual(str(cm.exception),
"Scalar operands are not allowed, use '*' instead")
with self.assertRaises(Exception) as cm:
(self.x.__matmul__(np.array([2, 2, 3])))
self.assertEqual(str(cm.exception), "Incompatible dimensions (2, 1) (3, 1)")
# Matrices
with self.assertRaises(Exception) as cm:
Constant([[2, 1], [2, 2]]) .__matmul__(self.C)
self.assertEqual(str(cm.exception), "Incompatible dimensions (2, 2) (3, 2)")
# Affine times affine is okay
with warnings.catch_warnings():
warnings.simplefilter("ignore")
q = self.A .__matmul__(self.B)
self.assertTrue(q.is_quadratic())
# Nonaffine times nonconstant raises error
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with self.assertRaises(Exception) as cm:
(self.A.__matmul__(self.B).__matmul__(self.A))
self.assertEqual(str(cm.exception), "Cannot multiply UNKNOWN and AFFINE.")
# Constant expressions
T = Constant([[1, 2, 3], [3, 5, 5]])
exp = (T + T) .__matmul__(self.B)
self.assertEqual(exp.curvature, s.AFFINE)
self.assertEqual(exp.size, (3, 2))
# Expression that would break sign multiplication without promotion.
c = Constant([[2], [2], [-2]])
exp = [[1], [2]] + c.__matmul__(self.C)
self.assertEqual(exp.sign, s.UNKNOWN)
else:
pass
示例7: linearize
def linearize(expr):
"""Returns the tangent approximation to the expression.
Gives an elementwise lower (upper) bound for convex (concave)
expressions. No guarantees for non-DCP expressions.
Returns None if cannot be linearized.
Args:
expr: An expression.
Returns:
An affine expression or None.
"""
expr = Constant.cast_to_const(expr)
if expr.is_affine():
return expr
else:
tangent = expr.value
if tangent is None:
raise ValueError(
"Cannot linearize non-affine expression with missing variable values."
)
grad_map = expr.grad
for var in expr.variables():
if grad_map[var] is None:
return None
elif var.is_matrix():
flattened = Constant(grad_map[var]).T*vec(var - var.value)
tangent = tangent + reshape(flattened, *expr.size)
else:
tangent = tangent + Constant(grad_map[var]).T*(var - var.value)
return tangent
示例8: test_mul_expression
def test_mul_expression(self):
# Vectors
c = Constant([[2],[2]])
exp = c*self.x
self.assertEqual(exp.curvature, u.Curvature.AFFINE)
self.assertEqual((c[0]*self.x).sign, u.Sign.UNKNOWN)
self.assertEqual(exp.canonicalize()[0].size, (1,1))
self.assertEqual(exp.canonicalize()[1], [])
self.assertEqual(exp.name(), c.name() + " * " + self.x.name())
self.assertEqual(exp.size, (1,1))
with self.assertRaises(Exception) as cm:
([2,2,3]*self.x)
const_name = Constant([2,2,3]).name()
self.assertEqual(str(cm.exception),
"Incompatible dimensions.")
# Matrices
with self.assertRaises(Exception) as cm:
Constant([[2, 1],[2, 2]]) * self.C
self.assertEqual(str(cm.exception), "Incompatible dimensions.")
with self.assertRaises(Exception) as cm:
(self.A * self.B)
self.assertEqual(str(cm.exception), "Cannot multiply two non-constants.")
# Constant expressions
T = Constant([[1,2,3],[3,5,5]])
exp = (T + T) * self.B
self.assertEqual(exp.curvature, u.Curvature.AFFINE)
self.assertEqual(exp.size, (3,2))
# Expression that would break sign multiplication without promotion.
c = Constant([[2],[2],[-2]])
exp = [[1],[2]] + c*self.C
self.assertEqual(exp.sign.pos_mat.value.shape, (1,2))
示例9: test_constants
def test_constants(self):
c = Constant(2)
self.assertEqual(c.name(), str(2))
c = Constant(2, name="c")
self.assertEqual(c.name(), "c")
self.assertEqual(c.value, 2)
self.assertEqual(c.size, (1,1))
self.assertEqual(c.curvature, u.Curvature.CONSTANT)
self.assertEqual(c.sign, u.Sign.POSITIVE)
self.assertEqual(Constant(-2).sign, u.Sign.NEGATIVE)
self.assertEqual(Constant(0).sign, u.Sign.ZERO)
self.assertEqual(c.canonicalize()[0].size, (1,1))
self.assertEqual(c.canonicalize()[1], [])
coeffs = c.coefficients(self.intf)
self.assertEqual(coeffs.keys(), [s.CONSTANT])
self.assertEqual(coeffs[s.CONSTANT], 2)
# Test the sign.
c = Constant([[2],[2]])
self.assertEqual(c.size, (1,2))
self.assertEqual(c.sign.neg_mat.value.shape, (1,2))
示例10: test_constants
def test_constants(self):
c = Constant(2)
self.assertEqual(c.name(), str(2))
c = Constant(2)
self.assertEqual(c.value, 2)
self.assertEqual(c.size, (1, 1))
self.assertEqual(c.curvature, s.CONSTANT)
self.assertEqual(c.sign, s.POSITIVE)
self.assertEqual(Constant(-2).sign, s.NEGATIVE)
self.assertEqual(Constant(0).sign, s.ZERO)
self.assertEqual(c.canonical_form[0].size, (1, 1))
self.assertEqual(c.canonical_form[1], [])
# coeffs = c.coefficients()
# self.assertEqual(coeffs.keys(), [s.CONSTANT])
# self.assertEqual(coeffs[s.CONSTANT], [2])
# Test the sign.
c = Constant([[2], [2]])
self.assertEqual(c.size, (1, 2))
self.assertEqual(c.sign, s.POSITIVE)
self.assertEqual((-c).sign, s.NEGATIVE)
self.assertEqual((0*c).sign, s.ZERO)
c = Constant([[2], [-2]])
self.assertEqual(c.sign, s.UNKNOWN)
# Test sign of a complex expression.
c = Constant([1, 2])
A = Constant([[1, 1], [1, 1]])
exp = c.T*A*c
self.assertEqual(exp.sign, s.POSITIVE)
self.assertEqual((c.T*c).sign, s.POSITIVE)
exp = c.T.T
self.assertEqual(exp.sign, s.POSITIVE)
exp = c.T*self.A
self.assertEqual(exp.sign, s.UNKNOWN)
# Test repr.
self.assertEqual(repr(c), "Constant(CONSTANT, POSITIVE, (2, 1))")
示例11: test_coefficients
class test_coefficients(unittest.TestCase):
""" Unit tests for the expressions.affine module. """
def setUp(self):
self.a = Variable()
self.x = Variable(2, name='x')
self.y = Variable(2, name='y')
self.A = Variable(2,2)
self.c = Constant(3)
self.C = Constant([[1, 2], [1, 2]])
def test_leaf_coeffs(self):
"""Test the coefficients for Variables and Constants.
"""
# Scalars
coeffs = self.a.coefficients()
self.assertItemsEqual(coeffs.keys(), [self.a])
blocks = coeffs[self.a]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0], 1)
# Vectors
coeffs = self.x.coefficients()
self.assertItemsEqual(coeffs.keys(), [self.x])
blocks = coeffs[self.x]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].size, (2,2))
# Matrices
coeffs = self.A.coefficients()
self.assertItemsEqual(coeffs.keys(), [self.A])
blocks = coeffs[self.A]
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].size, (2,4))
# Constants
coeffs = self.c.coefficients()
self.assertItemsEqual(coeffs.keys(), [s.CONSTANT])
blocks = coeffs[s.CONSTANT]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0], 3)
coeffs = self.C.coefficients()
self.assertItemsEqual(coeffs.keys(), [s.CONSTANT])
blocks = coeffs[s.CONSTANT]
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].size, (2,1))
self.assertEqual(blocks[0][0,0], 1)
def test_add(self):
"""Test adding coefficients.
"""
coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
blocks = coeffs[self.x]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].size, (2,2))
coeffs = cu.add(coeffs, coeffs)
self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
blocks = coeffs[self.x]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].size, (2,2))
self.assertEqual(blocks[0][0,0], 2)
coeffs = cu.add(coeffs, self.C.coefficients())
self.assertItemsEqual(coeffs.keys(), [self.x, self.y, s.CONSTANT])
blocks = coeffs[s.CONSTANT]
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].size, (2,1))
self.assertEqual(blocks[0][0,0], 1)
def test_neg(self):
"""Test negating coefficients.
"""
coeffs = cu.neg(self.a.coefficients())
self.assertItemsEqual(coeffs.keys(), [self.a])
blocks = coeffs[self.a]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0], -1)
coeffs = cu.neg(self.A.coefficients())
self.assertItemsEqual(coeffs.keys(), [self.A])
blocks = coeffs[self.A]
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].size, (2,4))
self.assertEqual(blocks[0][0,0], -1)
def test_sub(self):
"""Test subtracting coefficients.
"""
coeffs = cu.sub(self.x.coefficients(), self.y.coefficients())
self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
blocks = coeffs[self.y]
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].size, (2,2))
self.assertEqual(blocks[0][0,0], -1)
#.........这里部分代码省略.........