From 6b7d837f84989ccd3a236d4edbaf5d30c8e0291c Mon Sep 17 00:00:00 2001 From: Michael Schneeberger Date: Sat, 10 Feb 2024 15:30:39 +0100 Subject: integrate changes from polymatrix library --- sumofsquares/cvxopt.py | 5 ++++ sumofsquares/mixins/sosexpropmixin.py | 47 ++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/sumofsquares/cvxopt.py b/sumofsquares/cvxopt.py index a703104..f27212d 100644 --- a/sumofsquares/cvxopt.py +++ b/sumofsquares/cvxopt.py @@ -107,6 +107,9 @@ def solve_cone( if print_info: print(f'number of variables: {G.shape[1]}') print(f'{dim_l=}, {dim_q=}, {dim_s=}') + n_constr_q = sum(dim_q) + n_constr_s = sum(i**2 for i in dim_s) + print(f'{n_constr_q=}, {n_constr_s=}') if print_matrix: print(f'cost={q}') @@ -388,6 +391,7 @@ def solve_sos_problem2( state: polymatrix.ExpressionState, subs: tuple[ParamSOSExpr] = None, x0: dict[ParamSOSExpr, np.ndarray] = None, + print_info = False, ): if x0 is None: x0 = {} @@ -419,6 +423,7 @@ def solve_sos_problem2( variables=tuple(param_expr.param for param_expr in free_param_expr), cost=sum(cost), s_inequality=inequality, + print_info=print_info, ) return state, result diff --git a/sumofsquares/mixins/sosexpropmixin.py b/sumofsquares/mixins/sosexpropmixin.py index 9d6dacd..ce75bbc 100644 --- a/sumofsquares/mixins/sosexpropmixin.py +++ b/sumofsquares/mixins/sosexpropmixin.py @@ -2,8 +2,7 @@ import dataclasses import typing import polymatrix -import polymatrix.expression.init.initfromsympyexpr -from polymatrix.expression.expression import Expression, ExpressionImpl +import polymatrix.expression.from_ from sumofsquares.mixins.sosexprmixin import SOSExprMixin from sumofsquares.sosexprbase.init.initsosexprbase import init_sos_expr_base @@ -18,7 +17,7 @@ class SOSExprOPMixin(SOSExprMixin): ) -> 'SOSExprOPMixin': if not isinstance(left, SOSExprOPMixin): - left = polymatrix.expression.init.initfromsympyexpr.init_from_expr_or_none(left) + left = polymatrix.expression.from_.from_expr_or_none(left) if left is None: return NotImplemented @@ -35,7 +34,7 @@ class SOSExprOPMixin(SOSExprMixin): ) elif not isinstance(right, SOSExprOPMixin): - right = polymatrix.expression.init.initfromsympyexpr.init_from_expr_or_none(right) + right = polymatrix.expression.from_.from_expr_or_none(right) if right is None: return NotImplemented @@ -53,6 +52,8 @@ class SOSExprOPMixin(SOSExprMixin): assert left.variables == right.variables, f'{left.variables=}, {right.variables=}' + # print(left.dependence + right.dependence) + underlying=init_sos_expr_base( expr=op(left.expr, right.expr), variables=left.variables, @@ -76,31 +77,31 @@ class SOSExprOPMixin(SOSExprMixin): ) def __add__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__add__, self, other) + return self._binary(polymatrix.Expression.__add__, self, other) def __matmul__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__matmul__, self, other) + return self._binary(polymatrix.Expression.__matmul__, self, other) def __mul__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__mul__, self, other) + return self._binary(polymatrix.Expression.__mul__, self, other) def __neg__(self) -> 'SOSExprOPMixin': - return self._unary(Expression.__neg__, self) + return self._unary(polymatrix.Expression.__neg__, self) def __radd__(self, other): - return self._binary(Expression.__add__, other, self) + return self._binary(polymatrix.Expression.__add__, other, self) def __rmatmul__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__matmul__, other, self) + return self._binary(polymatrix.Expression.__matmul__, other, self) def __rmul__(self, other): - return self._binary(Expression.__mul__, other, self) + return self._binary(polymatrix.Expression.__mul__, other, self) def __rsub__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__sub__, other, self) + return self._binary(polymatrix.Expression.__sub__, other, self) def __sub__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': - return self._binary(Expression.__sub__, self, other) + return self._binary(polymatrix.Expression.__sub__, self, other) def __getitem__(self, key: tuple[int, int]): return dataclasses.replace( @@ -113,7 +114,7 @@ class SOSExprOPMixin(SOSExprMixin): ) def cache(self) -> 'SOSExprOPMixin': - return self._unary(Expression.cache, self) + return self._unary(polymatrix.Expression.cache, self) def diff( self, @@ -130,10 +131,26 @@ class SOSExprOPMixin(SOSExprMixin): dependence=self.dependence, ), ) + + def divergence( + self, + variables: polymatrix.Expression | None = None, + ) -> 'SOSExprOPMixin': + if variables is None: + variables = self.variables + + return dataclasses.replace( + self, + underlying=init_sos_expr_base( + expr=self.expr.divergence(variables), + variables=self.variables, + dependence=self.dependence, + ), + ) @property def T(self): - return self._unary(Expression.transpose, self) + return self._unary(polymatrix.Expression.transpose, self) def substitute(self, substitutions, variables): return dataclasses.replace( -- cgit v1.2.1