]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/cone/doubly_nonnegative.py
92184d15c4133e33f04e877352787378ae3739df
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 from mjo
.cone
.symmetric_psd
import factor_psd
, is_symmetric_psd
, random_psd
17 from mjo
.matrix_vector
import isomorphism
20 def is_doubly_nonnegative(A
):
22 Determine whether or not the matrix ``A`` is doubly-nonnegative.
26 - ``A`` - The matrix in question
30 Either ``True`` if ``A`` is doubly-nonnegative, or ``False``
35 sage: from mjo.cone.doubly_nonnegative import is_doubly_nonnegative
39 Every completely positive matrix is doubly-nonnegative::
41 sage: v = vector(map(abs, random_vector(ZZ, 10)))
42 sage: A = v.column() * v.row()
43 sage: is_doubly_nonnegative(A)
46 The following matrix is nonnegative but non positive semidefinite::
48 sage: A = matrix(ZZ, [[1, 2], [2, 1]])
49 sage: is_doubly_nonnegative(A)
54 if A
.base_ring() == SR
:
55 msg
= 'The matrix ``A`` cannot be the symbolic.'
56 raise ValueError.new(msg
)
58 # Check that all of the entries of ``A`` are nonnegative.
59 if not all( a
>= 0 for a
in A
.list() ):
62 # It's nonnegative, so all we need to do is check that it's
63 # symmetric positive-semidefinite.
64 return is_symmetric_psd(A
)
68 def is_admissible_extreme_rank(r
, n
):
70 The extreme matrices of the doubly-nonnegative cone have some
71 restrictions on their ranks. This function checks to see whether the
72 rank ``r`` would be an admissible rank for an ``n``-by-``n`` matrix.
76 - ``r`` - The rank of the matrix.
78 - ``n`` - The dimension of the vector space on which the matrix acts.
82 Either ``True`` if a rank ``r`` matrix could be an extreme vector of
83 the doubly-nonnegative cone in `$\mathbb{R}^{n}$`, or ``False``
88 sage: from mjo.cone.doubly_nonnegative import is_admissible_extreme_rank
92 For dimension 5, only ranks zero, one, and three are admissible::
94 sage: is_admissible_extreme_rank(0,5)
96 sage: is_admissible_extreme_rank(1,5)
98 sage: is_admissible_extreme_rank(2,5)
100 sage: is_admissible_extreme_rank(3,5)
102 sage: is_admissible_extreme_rank(4,5)
104 sage: is_admissible_extreme_rank(5,5)
107 When given an impossible rank, we just return false::
109 sage: is_admissible_extreme_rank(100,5)
114 # Zero is in the doubly-nonnegative cone.
118 # Impossible, just return False
121 # See Theorem 3.1 in the cited reference.
127 return r
<= max(1, n
-3)
130 return r
<= max(1, n
-2)
133 def has_admissible_extreme_rank(A
):
135 The extreme matrices of the doubly-nonnegative cone have some
136 restrictions on their ranks. This function checks to see whether or
137 not ``A`` could be extreme based on its rank.
141 - ``A`` - The matrix in question
145 ``False`` if the rank of ``A`` precludes it from being an extreme
146 matrix of the doubly-nonnegative cone, ``True`` otherwise.
150 Hamilton-Jester, Crista Lee; Li, Chi-Kwong. Extreme Vectors of
151 Doubly Nonnegative Matrices. Rocky Mountain Journal of Mathematics
152 26 (1996), no. 4, 1371--1383. doi:10.1216/rmjm/1181071993.
153 http://projecteuclid.org/euclid.rmjm/1181071993.
157 sage: from mjo.cone.doubly_nonnegative import has_admissible_extreme_rank
161 The zero matrix has rank zero, which is admissible::
163 sage: A = zero_matrix(QQ, 5, 5)
164 sage: has_admissible_extreme_rank(A)
167 Likewise, rank one is admissible for dimension 5::
169 sage: v = vector(QQ, [1,2,3,4,5])
170 sage: A = v.column()*v.row()
171 sage: has_admissible_extreme_rank(A)
174 But rank 2 is never admissible::
176 sage: v1 = vector(QQ, [1,0,0,0,0])
177 sage: v2 = vector(QQ, [0,1,0,0,0])
178 sage: A = v1.column()*v1.row() + v2.column()*v2.row()
179 sage: has_admissible_extreme_rank(A)
182 In dimension 5, three is the only other admissible rank::
184 sage: v1 = vector(QQ, [1,0,0,0,0])
185 sage: v2 = vector(QQ, [0,1,0,0,0])
186 sage: v3 = vector(QQ, [0,0,1,0,0])
187 sage: A = v1.column()*v1.row()
188 sage: A += v2.column()*v2.row()
189 sage: A += v3.column()*v3.row()
190 sage: has_admissible_extreme_rank(A)
194 if not A
.is_symmetric():
195 # This function is more or less internal, so blow up if passed
196 # something unexpected.
197 raise ValueError('The matrix ``A`` must be symmetric.')
200 n
= ZZ(A
.nrows()) # Columns would work, too, since ``A`` is symmetric.
202 return is_admissible_extreme_rank(r
,n
)
205 def stdE(matrix_space
, i
,j
):
207 Return the ``i``,``j``th element of the standard basis in
212 - ``matrix_space`` - The underlying matrix space of whose basis
213 the returned matrix is an element
215 - ``i`` - The row index of the single nonzero entry
217 - ``j`` - The column index of the single nonzero entry
221 A basis element of ``matrix_space``. It has a single \"1\" in the
222 ``i``,``j`` row,column and zeros elsewhere.
226 sage: from mjo.cone.doubly_nonnegative import stdE
230 sage: M = MatrixSpace(ZZ, 2, 2)
244 Traceback (most recent call last):
246 IndexError: Index `i` is out of bounds.
248 Traceback (most recent call last):
250 IndexError: Index `j` is out of bounds.
253 # We need to check these ourselves, see below.
254 if i
>= matrix_space
.nrows():
255 raise IndexError('Index `i` is out of bounds.')
256 if j
>= matrix_space
.ncols():
257 raise IndexError('Index `j` is out of bounds.')
259 # The basis here is returned as a one-dimensional list, so we need
260 # to compute the offset into it based on ``i`` and ``j``. Since we
261 # compute the index ourselves, we need to do bounds-checking
262 # manually. Otherwise for e.g. a 2x2 matrix space, the index (0,2)
263 # would be computed as offset 3 into a four-element list and we
264 # would succeed incorrectly.
265 idx
= matrix_space
.ncols()*i
+ j
266 return list(matrix_space
.basis())[idx
]
270 def is_extreme_doubly_nonnegative(A
):
272 Returns ``True`` if the given matrix is an extreme matrix of the
273 doubly-nonnegative cone, and ``False`` otherwise.
277 1. Hamilton-Jester, Crista Lee; Li, Chi-Kwong. Extreme Vectors of
278 Doubly Nonnegative Matrices. Rocky Mountain Journal of Mathematics
279 26 (1996), no. 4, 1371--1383. doi:10.1216/rmjm/1181071993.
280 http://projecteuclid.org/euclid.rmjm/1181071993.
282 2. Berman, Abraham and Shaked-Monderer, Naomi. Completely Positive
283 Matrices. World Scientific, 2003.
287 sage: from mjo.cone.doubly_nonnegative import is_extreme_doubly_nonnegative
291 The zero matrix is an extreme matrix::
293 sage: A = zero_matrix(QQ, 5, 5)
294 sage: is_extreme_doubly_nonnegative(A)
297 Any extreme vector of the completely positive cone is an extreme
298 vector of the doubly-nonnegative cone::
300 sage: v = vector([1,2,3,4,5,6])
301 sage: A = v.column() * v.row()
302 sage: A = A.change_ring(QQ)
303 sage: is_extreme_doubly_nonnegative(A)
306 We should be able to generate the extreme completely positive
309 sage: v = vector(map(abs, random_vector(ZZ, 4)))
310 sage: A = v.column() * v.row()
311 sage: A = A.change_ring(QQ)
312 sage: is_extreme_doubly_nonnegative(A)
314 sage: v = vector(map(abs, random_vector(ZZ, 10)))
315 sage: A = v.column() * v.row()
316 sage: A = A.change_ring(QQ)
317 sage: is_extreme_doubly_nonnegative(A)
320 The following matrix is completely positive but has rank 3, so by a
321 remark in reference #1 it is not extreme::
323 sage: A = matrix(QQ, [[1,2,1],[2,6,3],[1,3,5]])
324 sage: is_extreme_doubly_nonnegative(A)
327 The following matrix is completely positive (diagonal) with rank 2,
328 so it is also not extreme::
330 sage: A = matrix(QQ, [[1,0,0],[2,0,0],[0,0,0]])
331 sage: is_extreme_doubly_nonnegative(A)
336 if not A
.base_ring().is_exact() and not A
.base_ring() is SR
:
337 msg
= 'The base ring of ``A`` must be either exact or symbolic.'
338 raise ValueError(msg
)
340 if not A
.base_ring().is_field():
341 raise ValueError('The base ring of ``A`` must be a field.')
343 if not A
.base_ring() is SR
:
344 # Change the base field of ``A`` so that we are sure we can take
345 # roots. The symbolic ring has no algebraic_closure method.
346 A
= A
.change_ring(A
.base_ring().algebraic_closure())
348 # Step 1 (see reference #1)
352 # Short circuit, we know the zero matrix is extreme.
355 if not is_symmetric_psd(A
):
358 # Step 1.5, appeal to Theorem 3.1 in reference #1 to short
360 if not has_admissible_extreme_rank(A
):
368 # Begin with an empty spanning set, and add a new matrix to it
369 # whenever we come across an index pair `$(i,j)$` with
372 for j
in xrange(A
.ncols()):
376 S
= X
.transpose() * (stdE(M
,i
,j
) + stdE(M
,j
,i
)) * X
377 spanning_set
.append(S
)
379 # The spanning set that we have at this point is of matrices. We
380 # only care about the dimension of the spanned space, and Sage
381 # can't compute the dimension of a set of matrices anyway, so we
382 # convert them all to vectors and just ask for the dimension of the
383 # resulting vector space.
384 (phi
, phi_inverse
) = isomorphism(A
.matrix_space())
385 vectors
= map(phi
,spanning_set
)
387 V
= span(vectors
, A
.base_ring())
390 # Needed to safely divide by two here (we don't want integer
391 # division). We ensured that the base ring of ``A`` is a field
393 two
= A
.base_ring()(2)
394 return d
== (k
*(k
+ 1)/two
- 1)
397 def random_doubly_nonnegative(V
, accept_zero
=True, rank
=None):
399 Generate a random doubly nonnegative matrix over the vector
400 space ``V``. That is, the returned matrix will be a linear
401 transformation on ``V``, with the same base ring as ``V``.
403 We take a very loose interpretation of "random," here. Otherwise we
404 would never (for example) choose a matrix on the boundary of the
409 - ``V`` - The vector space on which the returned matrix will act.
411 - ``accept_zero`` - Do you want to accept the zero matrix (which
412 is doubly nonnegative)? Default to ``True``.
414 - ``rank`` - Require the returned matrix to have the given rank
419 A random doubly nonnegative matrix, i.e. a linear transformation
420 from ``V`` to itself.
424 sage: from mjo.cone.doubly_nonnegative import (is_doubly_nonnegative,
425 ....: random_doubly_nonnegative)
429 Well, it doesn't crash at least::
431 sage: V = VectorSpace(QQ, 2)
432 sage: A = random_doubly_nonnegative(V)
433 sage: A.matrix_space()
434 Full MatrixSpace of 2 by 2 dense matrices over Rational Field
435 sage: is_doubly_nonnegative(A)
438 A matrix with the desired rank is returned::
440 sage: V = VectorSpace(QQ, 5)
441 sage: A = random_doubly_nonnegative(V,False,1)
444 sage: A = random_doubly_nonnegative(V,False,2)
447 sage: A = random_doubly_nonnegative(V,False,3)
450 sage: A = random_doubly_nonnegative(V,False,4)
453 sage: A = random_doubly_nonnegative(V,False,5)
459 # Generate random symmetric positive-semidefinite matrices until
460 # one of them is nonnegative, then return that.
461 A
= random_psd(V
, accept_zero
, rank
)
463 while not all( x
>= 0 for x
in A
.list() ):
464 A
= random_psd(V
, accept_zero
, rank
)
470 def random_extreme_doubly_nonnegative(V
, accept_zero
=True, rank
=None):
472 Generate a random extreme doubly nonnegative matrix over the
473 vector space ``V``. That is, the returned matrix will be a linear
474 transformation on ``V``, with the same base ring as ``V``.
476 We take a very loose interpretation of "random," here. Otherwise we
477 would never (for example) choose a matrix on the boundary of the
482 - ``V`` - The vector space on which the returned matrix will act.
484 - ``accept_zero`` - Do you want to accept the zero matrix
485 (which is extreme)? Defaults to ``True``.
487 - ``rank`` - Require the returned matrix to have the given rank
488 (optional). WARNING: certain ranks are not possible
489 in any given dimension! If an impossible rank is
490 requested, a ValueError will be raised.
494 A random extreme doubly nonnegative matrix, i.e. a linear
495 transformation from ``V`` to itself.
499 sage: from mjo.cone.doubly_nonnegative import (is_extreme_doubly_nonnegative,
500 ....: random_extreme_doubly_nonnegative)
504 Well, it doesn't crash at least::
506 sage: V = VectorSpace(QQ, 2)
507 sage: A = random_extreme_doubly_nonnegative(V)
508 sage: A.matrix_space()
509 Full MatrixSpace of 2 by 2 dense matrices over Rational Field
510 sage: is_extreme_doubly_nonnegative(A)
513 Rank 2 is never allowed, so we expect an error::
515 sage: V = VectorSpace(QQ, 5)
516 sage: A = random_extreme_doubly_nonnegative(V, False, 2)
517 Traceback (most recent call last):
519 ValueError: Rank 2 not possible in dimension 5.
521 Rank 4 is not allowed in dimension 5::
523 sage: V = VectorSpace(QQ, 5)
524 sage: A = random_extreme_doubly_nonnegative(V, False, 4)
525 Traceback (most recent call last):
527 ValueError: Rank 4 not possible in dimension 5.
531 if not is_admissible_extreme_rank(rank
, V
.dimension()):
532 msg
= 'Rank %d not possible in dimension %d.'
533 raise ValueError(msg
% (rank
, V
.dimension()))
535 # Generate random doubly-nonnegative matrices until
536 # one of them is extreme, then return that.
537 A
= random_doubly_nonnegative(V
, accept_zero
, rank
)
539 while not is_extreme_doubly_nonnegative(A
):
540 A
= random_doubly_nonnegative(V
, accept_zero
, rank
)