本文整理汇总了Python中cvxpy.expressions.constants.Constant.__matmul__方法的典型用法代码示例。如果您正苦于以下问题:Python Constant.__matmul__方法的具体用法?Python Constant.__matmul__怎么用?Python Constant.__matmul__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy.expressions.constants.Constant
的用法示例。
在下文中一共展示了Constant.__matmul__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matmul_expression
# 需要导入模块: from cvxpy.expressions.constants import Constant [as 别名]
# 或者: from cvxpy.expressions.constants.Constant import __matmul__ [as 别名]
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