From: Michael Orlitzky Date: Thu, 6 Oct 2016 18:53:15 +0000 (-0400) Subject: Add rough eigenvalue-based containment testing for SymmetricPSD. X-Git-Tag: 0.1.0~196 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=7577aaf1786d897fa1517bb8fd4ff367fd09113c;p=dunshire.git Add rough eigenvalue-based containment testing for SymmetricPSD. --- diff --git a/src/dunshire/cones.py b/src/dunshire/cones.py index 18e6a1b..4176f57 100644 --- a/src/dunshire/cones.py +++ b/src/dunshire/cones.py @@ -4,7 +4,7 @@ SymmetricCone) supported by CVXOPT. """ from cvxopt import matrix -from matrices import norm +from matrices import eigenvalues, norm class SymmetricCone: """ @@ -37,6 +37,7 @@ class SymmetricCone: self._dimension = dimension + def __contains__(self, point): """ Return whether or not ``point`` belongs to this cone. @@ -333,6 +334,87 @@ class SymmetricPSD(SymmetricCone): return tpl.format(self.dimension()) + def __contains__(self, point): + """ + Return whether or not ``point`` belongs to this cone. + + INPUT: + + An instance of the ``cvxopt.base.matrix`` class having + dimensions ``(n,n)`` where ``n`` is the dimension of this cone. + Its type code must be 'd'. + + EXAMPLES: + + >>> K = SymmetricPSD(2) + >>> matrix([[1,0],[0,1]], tc='d') in K + True + + >>> K = SymmetricPSD(2) + >>> matrix([[0,0],[0,0]], tc='d') in K + True + + >>> K = SymmetricPSD(2) + >>> [[1,2],[2,3]] in K + Traceback (most recent call last): + ... + TypeError: the given point is not a cvxopt.base.matrix + + >>> K = SymmetricPSD(3) + >>> matrix([[1,2],[3,4]], tc='d') in K + Traceback (most recent call last): + ... + TypeError: the given point has the wrong dimensions + + """ + if not isinstance(point, matrix): + raise TypeError('the given point is not a cvxopt.base.matrix') + if not point.size == (self.dimension(), self.dimension()): + raise TypeError('the given point has the wrong dimensions') + return all([e >= 0 for e in eigenvalues(point)]) + + + def contains_strict(self, point): + """ + Return whether or not ``point`` belongs to the interior + of this cone. + + INPUT: + + An instance of the ``cvxopt.base.matrix`` class having + dimensions ``(n,n)`` where ``n`` is the dimension of this cone. + Its type code must be 'd'. + + EXAMPLES: + + >>> K = SymmetricPSD(2) + >>> K.contains_strict(matrix([[1,0],[0,1]], tc='d')) + True + + >>> K = SymmetricPSD(2) + >>> K.contains_strict(matrix([[0,0],[0,0]], tc='d')) + False + + >>> K = SymmetricPSD(2) + >>> K.contains_strict([[1,2],[2,3]]) + Traceback (most recent call last): + ... + TypeError: the given point is not a cvxopt.base.matrix + + >>> K = SymmetricPSD(3) + >>> K.contains_strict(matrix([[1,2],[3,4]], tc='d')) + Traceback (most recent call last): + ... + TypeError: the given point has the wrong dimensions + + """ + if not isinstance(point, matrix): + raise TypeError('the given point is not a cvxopt.base.matrix') + if not point.size == (self.dimension(), self.dimension()): + raise TypeError('the given point has the wrong dimensions') + return all([e > 0 for e in eigenvalues(point)]) + + class CartesianProduct(SymmetricCone): """ A cartesian product of symmetric cones, which is itself a symmetric