X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fcone%2Fdoubly_nonnegative.py;h=03d23b4ddcbd785b48fe9dd447876cefb586a067;hb=bc4804ef6cbf4b5f12fb654193e133f719dcc623;hp=b43c974b2076a5dbda14ba43f679fd8caee30327;hpb=231c13764614dd43deb161a2f29f98aa2ccbd1e0;p=sage.d.git diff --git a/mjo/cone/doubly_nonnegative.py b/mjo/cone/doubly_nonnegative.py index b43c974..03d23b4 100644 --- a/mjo/cone/doubly_nonnegative.py +++ b/mjo/cone/doubly_nonnegative.py @@ -99,10 +99,12 @@ def has_admissible_extreme_rank(A): """ if not A.is_symmetric(): + # This function is more or less internal, so blow up if passed + # something unexpected. raise ValueError('The matrix ``A`` must be symmetric.') r = rank(A) - n = A.nrows() # Columns would work, too, since ``A`` is symmetric. + n = ZZ(A.nrows()) # Columns would work, too, since ``A`` is symmetric. if r == 0: # Zero is in the doubly-nonnegative cone. @@ -120,6 +122,67 @@ def has_admissible_extreme_rank(A): return r <= max(1, n-2) +def E(matrix_space, i,j): + """ + Return the ``i``,``j``th element of the standard basis in + ``matrix_space``. + + INPUT: + + - ``matrix_space`` - The underlying matrix space of whose basis + the returned matrix is an element + + - ``i`` - The row index of the single nonzero entry + + - ``j`` - The column index of the single nonzero entry + + OUTPUT: + + A basis element of ``matrix_space``. It has a single \"1\" in the + ``i``,``j`` row,column and zeros elsewhere. + + EXAMPLES:: + + sage: M = MatrixSpace(ZZ, 2, 2) + sage: E(M,0,0) + [1 0] + [0 0] + sage: E(M,0,1) + [0 1] + [0 0] + sage: E(M,1,0) + [0 0] + [1 0] + sage: E(M,1,1) + [0 0] + [0 1] + sage: E(M,2,1) + Traceback (most recent call last): + ... + IndexError: Index `i` is out of bounds. + sage: E(M,1,2) + Traceback (most recent call last): + ... + IndexError: Index `j` is out of bounds. + + """ + # We need to check these ourselves, see below. + if i >= matrix_space.nrows(): + raise IndexError('Index `i` is out of bounds.') + if j >= matrix_space.ncols(): + raise IndexError('Index `j` is out of bounds.') + + # The basis here is returned as a one-dimensional list, so we need + # to compute the offset into it based on ``i`` and ``j``. Since we + # compute the index ourselves, we need to do bounds-checking + # manually. Otherwise for e.g. a 2x2 matrix space, the index (0,2) + # would be computed as offset 3 into a four-element list and we + # would succeed incorrectly. + idx = matrix_space.ncols()*i + j + return matrix_space.basis()[idx] + + + def is_extreme_doubly_nonnegative(A): """ Returns ``True`` if the given matrix is an extreme matrix of the