X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fdunshire%2Fcones.py;h=2609b16eea729651c7d005c226d5f47ab4cd616c;hb=002b5370da24f083d2088c3482cf076615a13563;hp=18e6a1b9254f1675f56f1998396e13b0636153fc;hpb=6460b7fcac2c8c28e5cd038a66451076e75ea5e8;p=dunshire.git diff --git a/src/dunshire/cones.py b/src/dunshire/cones.py index 18e6a1b..2609b16 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. @@ -315,13 +316,24 @@ class IceCream(SymmetricCone): class SymmetricPSD(SymmetricCone): """ - The nonnegative orthant in ``n`` dimensions. + The cone of real symmetric positive-semidefinite matrices. + + This cone has a dimension ``n`` associated with it, but we let ``n`` + refer to the dimension of the domain of our matrices and not the + dimension of the (much larger) space in which the matrices + themselves live. In other words, our ``n`` is the ``n`` that appears + in the usual notation `S^{n}` for symmetric matrices. + + As a result, the cone ``SymmetricPSD(n)`` lives in a space of dimension + ``(n**2 + n)/2)``. EXAMPLES: >>> K = SymmetricPSD(3) >>> print(K) Cone of symmetric positive-semidefinite matrices on the real 3-space + >>> K.dimension() + 3 """ def __str__(self): @@ -333,6 +345,134 @@ 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. + + EXAMPLES: + + >>> K = SymmetricPSD(2) + >>> matrix([[1,0],[0,1]]) in K + True + + >>> K = SymmetricPSD(2) + >>> matrix([[0,0],[0,0]]) in K + True + + >>> K = SymmetricPSD(3) + >>> matrix([[2,-1,0],[-1,2,-1],[0,-1,2]]) in K + True + + >>> K = SymmetricPSD(5) + >>> A = matrix([[5,4,3,2,1], + ... [4,5,4,3,2], + ... [3,4,5,4,3], + ... [2,3,4,5,4], + ... [1,2,3,4,5]]) + >>> A in K + True + + >>> K = SymmetricPSD(5) + >>> A = matrix([[1,0,0,0,0], + ... [0,1,0,0,0], + ... [0,0,0,0,0], + ... [0,0,0,1,0], + ... [0,0,0,0,1]]) + >>> A 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]]) 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') + if not point.typecode == 'd': + point = matrix(point, (self.dimension(), self.dimension()), 'd') + 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]])) + True + + >>> K = SymmetricPSD(2) + >>> K.contains_strict(matrix([[0,0],[0,0]])) + False + + >>> K = SymmetricPSD(3) + >>> matrix([[2,-1,0],[-1,2,-1],[0,-1,2]]) in K + True + + >>> K = SymmetricPSD(5) + >>> A = matrix([[5,4,3,2,1], + ... [4,5,4,3,2], + ... [3,4,5,4,3], + ... [2,3,4,5,4], + ... [1,2,3,4,5]]) + >>> A in K + True + + >>> K = SymmetricPSD(5) + >>> A = matrix([[1,0,0,0,0], + ... [0,1,0,0,0], + ... [0,0,0,0,0], + ... [0,0,0,1,0], + ... [0,0,0,0,1]]) + >>> K.contains_strict(A) + 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]])) + 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') + if not point.typecode == 'd': + point = matrix(point, (self.dimension(), self.dimension()), 'd') + return all([e > 0 for e in eigenvalues(point)]) + + class CartesianProduct(SymmetricCone): """ A cartesian product of symmetric cones, which is itself a symmetric