The docs for cones.py have all been switched (roughly) to use the
numpy style. In the process, I noticed that cartesian product
containment was a lot weirder than it needs to be. The "point" that
the __contains__() and contains_strict() methods take is now a tuple
corresponding to the factors of the cartesian product.
"""
Class definitions for all of the symmetric cones (and their superclass,
"""
Class definitions for all of the symmetric cones (and their superclass,
-SymmetricCone) supported by CVXOPT.
+class:`SymmetricCone`) supported by CVXOPT.
"""
from cvxopt import matrix
"""
from cvxopt import matrix
3. The cone of symmetric positive-semidefinite matrices.
This class is intended to encompass them all.
3. The cone of symmetric positive-semidefinite matrices.
This class is intended to encompass them all.
- """
- def __init__(self, dimension):
- """
- A generic constructor for symmetric cones.
- When constructing a single symmetric cone (i.e. not a cartesian
- product of them), the only information that we need is its
- dimension. We take that dimension as a parameter, and store it
- for later.
+ When constructing a single symmetric cone (i.e. not a
+ :class:`CartesianProduct` of them), the only information that we
+ need is its dimension. We take that dimension as a parameter, and
+ store it for later.
+ Parameters
+ ----------
- - ``dimension`` -- the dimension of this cone.
+ dimension : int
+ The dimension of this cone.
+ """
+ def __init__(self, dimension):
+ """
+ A generic constructor for symmetric cones.
"""
if dimension <= 0:
raise ValueError('cones must have dimension greater than zero')
"""
if dimension <= 0:
raise ValueError('cones must have dimension greater than zero')
"""
Return whether or not ``point`` belongs to this cone.
"""
Return whether or not ``point`` belongs to this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ The point to test for membership in this cone.
+
+ Raises
+ ------
+
+ NotImplementedError
+ Always, this method must be implemented in subclasses.
+
+ Examples
+ --------
>>> K = SymmetricCone(5)
>>> matrix([1,2]) in K
>>> K = SymmetricCone(5)
>>> matrix([1,2]) in K
"""
raise NotImplementedError
"""
raise NotImplementedError
def contains_strict(self, point):
"""
Return whether or not ``point`` belongs to the interior
of this cone.
def contains_strict(self, point):
"""
Return whether or not ``point`` belongs to the interior
of this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ The point to test for strict membership in this cone.
+
+ Raises
+ ------
+
+ NotImplementedError
+ Always, this method must be implemented in subclasses.
+
+ Examples
+ --------
>>> K = SymmetricCone(5)
>>> K.contains_strict(matrix([1,2]))
>>> K = SymmetricCone(5)
>>> K.contains_strict(matrix([1,2]))
"""
raise NotImplementedError
"""
raise NotImplementedError
def dimension(self):
"""
Return the dimension of this symmetric cone.
def dimension(self):
"""
Return the dimension of this symmetric cone.
any special computation in ``__init__()`` and record the result
in ``self._dimension``.
any special computation in ``__init__()`` and record the result
in ``self._dimension``.
+ Returns
+ -------
+
+ int
+ The stored dimension (from when this cone was constructed)
+ of this cone.
+
+ Examples
+ --------
>>> K = SymmetricCone(5)
>>> K.dimension()
>>> K = SymmetricCone(5)
>>> K.dimension()
class NonnegativeOrthant(SymmetricCone):
"""
class NonnegativeOrthant(SymmetricCone):
"""
- The nonnegative orthant in ``n`` dimensions.
+ The nonnegative orthant in the given number of dimensions.
>>> K = NonnegativeOrthant(3)
>>> print(K)
>>> K = NonnegativeOrthant(3)
>>> print(K)
tpl = 'Nonnegative orthant in the real {:d}-space'
return tpl.format(self.dimension())
tpl = 'Nonnegative orthant in the real {:d}-space'
return tpl.format(self.dimension())
def __contains__(self, point):
"""
Return whether or not ``point`` belongs to this cone.
def __contains__(self, point):
"""
Return whether or not ``point`` belongs to this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,1)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
+
+ bool
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ ``True`` if ``point`` belongs to this cone, ``False`` otherwise.
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = NonnegativeOrthant(3)
>>> matrix([1,2,3]) in K
>>> K = NonnegativeOrthant(3)
>>> matrix([1,2,3]) in K
Return whether or not ``point`` belongs to the interior of this
cone.
Return whether or not ``point`` belongs to the interior of this
cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,1)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
+
+ bool
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ ``True`` if ``point`` belongs to the interior of this cone,
+ ``False`` otherwise.
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = NonnegativeOrthant(3)
>>> K.contains_strict(matrix([1,2,3]))
>>> K = NonnegativeOrthant(3)
>>> K.contains_strict(matrix([1,2,3]))
class IceCream(SymmetricCone):
"""
class IceCream(SymmetricCone):
"""
- The nonnegative orthant in ``n`` dimensions.
+ The Lorentz "ice cream" cone in the given number of dimensions.
>>> K = IceCream(3)
>>> print(K)
>>> K = IceCream(3)
>>> print(K)
"""
Return whether or not ``point`` belongs to this cone.
"""
Return whether or not ``point`` belongs to this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,1)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
+
+ bool
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ ``True`` if ``point`` belongs to this cone, ``False`` otherwise.
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = IceCream(3)
>>> matrix([1,0.5,0.5]) in K
>>> K = IceCream(3)
>>> matrix([1,0.5,0.5]) in K
Return whether or not ``point`` belongs to the interior
of this cone.
Return whether or not ``point`` belongs to the interior
of this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,1)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
+
+ bool
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ ``True`` if ``point`` belongs to the interior of this cone,
+ ``False`` otherwise.
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = IceCream(3)
>>> K.contains_strict(matrix([1,0.5,0.5]))
>>> K = IceCream(3)
>>> K.contains_strict(matrix([1,0.5,0.5]))
As a result, the cone ``SymmetricPSD(n)`` lives in a space of dimension
``(n**2 + n)/2)``.
As a result, the cone ``SymmetricPSD(n)`` lives in a space of dimension
``(n**2 + n)/2)``.
>>> K = SymmetricPSD(3)
>>> print(K)
>>> K = SymmetricPSD(3)
>>> print(K)
"""
Return whether or not ``point`` belongs to this cone.
"""
Return whether or not ``point`` belongs to this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,n)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,n)`` where ``n`` is the dimension of this cone.
+ ``True`` if ``point`` belongs to this cone, ``False`` otherwise.
+
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = SymmetricPSD(2)
>>> matrix([[1,0],[0,1]]) in K
>>> K = SymmetricPSD(2)
>>> matrix([[1,0],[0,1]]) in K
Return whether or not ``point`` belongs to the interior
of this cone.
Return whether or not ``point`` belongs to the interior
of this cone.
+ Parameters
+ ----------
+
+ point : matrix
+ A :class:`cvxopt.base.matrix` having dimensions ``(n,n)``
+ where ``n`` is the :meth:`dimension` of this cone.
+
+ Returns
+ -------
- 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'.
+ ``True`` if ``point`` belongs to the interior of this cone,
+ ``False`` otherwise.
+
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = SymmetricPSD(2)
>>> K.contains_strict(matrix([[1,0],[0,1]]))
>>> K = SymmetricPSD(2)
>>> K.contains_strict(matrix([[1,0],[0,1]]))
A cartesian product of symmetric cones, which is itself a symmetric
cone.
A cartesian product of symmetric cones, which is itself a symmetric
cone.
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
>>> print(K)
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
>>> print(K)
super().__init__(my_dimension)
self._factors = factors
super().__init__(my_dimension)
self._factors = factors
def __str__(self):
"""
Output a human-readable description of myself.
def __str__(self):
"""
Output a human-readable description of myself.
format_args += list(self.factors())
return tpl.format(*format_args)
format_args += list(self.factors())
return tpl.format(*format_args)
def __contains__(self, point):
"""
Return whether or not ``point`` belongs to this cone.
def __contains__(self, point):
"""
Return whether or not ``point`` belongs to this cone.
+ The ``point`` is expected to be a tuple of points which will be
+ tested for membership in this cone's factors. If each point in
+ the tuple belongs to its corresponding factor, then the whole
+ point belongs to this cone. Otherwise, it doesn't.
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ Parameters
+ ----------
+ point : tuple of matrix
+ A tuple of :class:`cvxopt.base.matrix` corresponding to the
+ :meth:`factors` of this cartesian product.
+
+ Returns
+ -------
+
+ bool
+
+ ``True`` if ``point`` belongs to this cone, ``False`` otherwise.
+
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a tuple of :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If any element of ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> matrix([1,2,3,1,0.5,0.5]) in K
+ >>> (matrix([1,2,3]), matrix([1,0.5,0.5])) in K
True
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
True
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> matrix([0,0,0,1,0,1]) in K
+ >>> (matrix([0,0,0]), matrix([1,0,1])) in K
True
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
True
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> matrix([1,1,1,1,1,1]) in K
+ >>> (matrix([1,1,1]), matrix([1,1,1])) in K
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> matrix([1,-1,1,1,0,1]) in K
+ >>> (matrix([1,-1,1]), matrix([1,0,1])) in K
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
+ >>> [[1,2,3],[4,5,6]] in K
Traceback (most recent call last):
...
TypeError: the given point is not a cvxopt.base.matrix
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
Traceback (most recent call last):
...
TypeError: the given point is not a cvxopt.base.matrix
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
+ >>> (matrix([1,2]), matrix([3,4,5,6])) in K
Traceback (most recent call last):
...
TypeError: the given point has the wrong dimensions
"""
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(), 1):
- raise TypeError('the given point has the wrong dimensions')
-
- for factor in self.factors():
- # Split off the components of ``point`` corresponding to
- # ``factor``.
- factor_part = point[0:factor.dimension()]
- if not factor_part in factor:
- return False
- point = point[factor.dimension():]
-
- return True
+ return all([p in f for (p,f) in zip(point, self.factors())])
def contains_strict(self, point):
def contains_strict(self, point):
Return whether or not ``point`` belongs to the interior
of this cone.
Return whether or not ``point`` belongs to the interior
of this cone.
+ The ``point`` is expected to be a tuple of points which will be
+ tested for membership in this cone's factors. If each point in
+ the tuple belongs to the interior of its corresponding factor,
+ then the whole point belongs to the interior of this
+ cone. Otherwise, it doesn't.
- An instance of the ``cvxopt.base.matrix`` class having
- dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
+ Parameters
+ ----------
+ point : tuple of matrix
+ A tuple of :class:`cvxopt.base.matrix` corresponding to the
+ :meth:`factors` of this cartesian product.
- >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([1,2,3,1,0.5,0.5]))
- True
+ Returns
+ -------
+
+ bool
+
+ ``True`` if ``point`` belongs to the interior of this cone,
+ ``False`` otherwise.
+
+ Raises
+ ------
+
+ TypeError
+ If ``point`` is not a tuple of :class:`cvxopt.base.matrix`.
+
+ TypeError
+ If any element of ``point`` has the wrong dimensions.
+
+ Examples
+ --------
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([1,2,3,1,0,1]))
- False
+ >>> K.contains_strict((matrix([1,2,3]), matrix([1,0.5,0.5])))
+ True
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([0,1,1,1,0.5,0.5]))
+ >>> K.contains_strict((matrix([0,0,0]), matrix([1,0,1])))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([1,1,1,1,1,1]))
+ >>> K.contains_strict((matrix([1,1,1]), matrix([1,1,1])))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([1,-1,1,1,0,1]))
+ >>> K.contains_strict((matrix([1,-1,1]), matrix([1,0,1])))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
False
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict([1,2,3,4,5,6])
+ >>> K.contains_strict([[1,2,3],[4,5,6]])
Traceback (most recent call last):
...
TypeError: the given point is not a cvxopt.base.matrix
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
Traceback (most recent call last):
...
TypeError: the given point is not a cvxopt.base.matrix
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
- >>> K.contains_strict(matrix([1,2]))
+ >>> K.contains_strict((matrix([1,2]), matrix([3,4,5,6])))
Traceback (most recent call last):
...
TypeError: the given point has the wrong dimensions
"""
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(), 1):
- raise TypeError('the given point has the wrong dimensions')
-
- for factor in self.factors():
- # Split off the components of ``point`` corresponding to
- # ``factor``.
- factor_part = point[0:factor.dimension()]
- if not factor.contains_strict(factor_part):
- return False
- point = point[factor.dimension():]
+ return all([f.contains_strict(p)
+ for (p,f) in zip(point, self.factors())])
Return a tuple containing the factors (in order) of this
cartesian product.
Return a tuple containing the factors (in order) of this
cartesian product.
+ Returns
+ -------
+
+ tuple of :class:`SymmetricCone`.
+ The factors of this cartesian product.
+
+ Examples
+ --------
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
>>> len(K.factors())
>>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
>>> len(K.factors())
def cvxopt_dims(self):
"""
Return a dictionary of dimensions corresponding to the factors
def cvxopt_dims(self):
"""
Return a dictionary of dimensions corresponding to the factors
http://cvxopt.org/userguide/coneprog.html#linear-cone-programs
http://cvxopt.org/userguide/coneprog.html#linear-cone-programs
+ Returns
+ -------
+
+ dict
+ A dimension dictionary suitable to feed to CVXOPT.
+
+ Examples
+ --------
>>> K = CartesianProduct(NonnegativeOrthant(3),
... IceCream(2),
>>> K = CartesianProduct(NonnegativeOrthant(3),
... IceCream(2),