当前位置: 首页>>代码示例>>Python>>正文


Python Constant.__matmul__方法代码示例

本文整理汇总了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
开发者ID:heath9,项目名称:cvxpy,代码行数:54,代码来源:test_expressions.py


注:本文中的cvxpy.expressions.constants.Constant.__matmul__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。