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