]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/cone/doubly_nonnegative.py
03d23b4ddcbd785b48fe9dd447876cefb586a067
2 The doubly-nonnegative cone in `S^{n}` is the set of all such matrices
5 a) are positive semidefinite
7 b) have only nonnegative entries
9 It is represented typically by either `\mathcal{D}^{n}` or
14 from sage
.all
import *
16 # Sage doesn't load ~/.sage/init.sage during testing (sage -t), so we
17 # have to explicitly mangle our sitedir here so that "mjo.cone"
19 from os
.path
import abspath
20 from site
import addsitedir
21 addsitedir(abspath('../../'))
22 from mjo
.cone
.symmetric_psd
import factor_psd
, is_symmetric_psd
26 def is_doubly_nonnegative(A
):
28 Determine whether or not the matrix ``A`` is doubly-nonnegative.
32 - ``A`` - The matrix in question
36 Either ``True`` if ``A`` is doubly-nonnegative, or ``False``
41 Every completely positive matrix is doubly-nonnegative::
43 sage: v = vector(map(abs, random_vector(ZZ, 10)))
44 sage: A = v.column() * v.row()
45 sage: is_doubly_nonnegative(A)
48 The following matrix is nonnegative but non positive semidefinite::
50 sage: A = matrix(ZZ, [[1, 2], [2, 1]])
51 sage: is_doubly_nonnegative(A)
56 if A
.base_ring() == SR
:
57 msg
= 'The matrix ``A`` cannot be the symbolic.'
58 raise ValueError.new(msg
)
60 # Check that all of the entries of ``A`` are nonnegative.
61 if not all([ a
>= 0 for a
in A
.list() ]):
64 # It's nonnegative, so all we need to do is check that it's
65 # symmetric positive-semidefinite.
66 return is_symmetric_psd(A
)
70 def has_admissible_extreme_rank(A
):
72 The extreme matrices of the doubly-nonnegative cone have some
73 restrictions on their ranks. This function checks to see whether or
74 not ``A`` could be extreme based on its rank.
78 - ``A`` - The matrix in question
82 ``False`` if the rank of ``A`` precludes it from being an extreme
83 matrix of the doubly-nonnegative cone, ``True`` otherwise.
87 Hamilton-Jester, Crista Lee; Li, Chi-Kwong. Extreme Vectors of
88 Doubly Nonnegative Matrices. Rocky Mountain Journal of Mathematics
89 26 (1996), no. 4, 1371--1383. doi:10.1216/rmjm/1181071993.
90 http://projecteuclid.org/euclid.rmjm/1181071993.
94 The zero matrix has rank zero, which is admissible::
96 sage: A = zero_matrix(QQ, 5, 5)
97 sage: has_admissible_extreme_rank(A)
101 if not A
.is_symmetric():
102 # This function is more or less internal, so blow up if passed
103 # something unexpected.
104 raise ValueError('The matrix ``A`` must be symmetric.')
107 n
= ZZ(A
.nrows()) # Columns would work, too, since ``A`` is symmetric.
110 # Zero is in the doubly-nonnegative cone.
113 # See Theorem 3.1 in the cited reference.
119 return r
<= max(1, n
-3)
122 return r
<= max(1, n
-2)
125 def E(matrix_space
, i
,j
):
127 Return the ``i``,``j``th element of the standard basis in
132 - ``matrix_space`` - The underlying matrix space of whose basis
133 the returned matrix is an element
135 - ``i`` - The row index of the single nonzero entry
137 - ``j`` - The column index of the single nonzero entry
141 A basis element of ``matrix_space``. It has a single \"1\" in the
142 ``i``,``j`` row,column and zeros elsewhere.
146 sage: M = MatrixSpace(ZZ, 2, 2)
160 Traceback (most recent call last):
162 IndexError: Index `i` is out of bounds.
164 Traceback (most recent call last):
166 IndexError: Index `j` is out of bounds.
169 # We need to check these ourselves, see below.
170 if i
>= matrix_space
.nrows():
171 raise IndexError('Index `i` is out of bounds.')
172 if j
>= matrix_space
.ncols():
173 raise IndexError('Index `j` is out of bounds.')
175 # The basis here is returned as a one-dimensional list, so we need
176 # to compute the offset into it based on ``i`` and ``j``. Since we
177 # compute the index ourselves, we need to do bounds-checking
178 # manually. Otherwise for e.g. a 2x2 matrix space, the index (0,2)
179 # would be computed as offset 3 into a four-element list and we
180 # would succeed incorrectly.
181 idx
= matrix_space
.ncols()*i
+ j
182 return matrix_space
.basis()[idx
]
186 def is_extreme_doubly_nonnegative(A
):
188 Returns ``True`` if the given matrix is an extreme matrix of the
189 doubly-nonnegative cone, and ``False`` otherwise.
193 The zero matrix is an extreme matrix::
195 sage: A = zero_matrix(QQ, 5, 5)
196 sage: is_extreme_doubly_nonnegative(A)
204 # Short circuit, we know the zero matrix is extreme.
207 if not is_admissible_extreme_rank(r
):
210 raise NotImplementedError()