X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fcone%2Fsymmetric_psd.py;h=89eba53c9687823a0882304a49d9ddefc1d009db;hb=7af2b9d146a6bf2fb8acc3c342983de577b417ce;hp=40fd90c0e3b1d20cf8e522e8504ef8f55804b959;hpb=b4b48ce895557cc5708b090568cfa33e7d09817f;p=sage.d.git diff --git a/mjo/cone/symmetric_psd.py b/mjo/cone/symmetric_psd.py index 40fd90c..89eba53 100644 --- a/mjo/cone/symmetric_psd.py +++ b/mjo/cone/symmetric_psd.py @@ -1,4 +1,4 @@ -""" +r""" The positive semidefinite cone `$S^{n}_{+}$` is the cone consisting of all symmetric positive-semidefinite matrices (as a subset of `$\mathbb{R}^{n \times n}$` @@ -6,74 +6,6 @@ all symmetric positive-semidefinite matrices (as a subset of from sage.all import * -def is_symmetric_psd(A): - """ - Determine whether or not the matrix ``A`` is symmetric - positive-semidefinite. - - INPUT: - - - ``A`` - The matrix in question - - OUTPUT: - - Either ``True`` if ``A`` is symmetric positive-semidefinite, or - ``False`` otherwise. - - SETUP:: - - sage: from mjo.cone.symmetric_psd import is_symmetric_psd - - EXAMPLES: - - Every completely positive matrix is symmetric - positive-semidefinite:: - - sage: v = vector(map(abs, random_vector(ZZ, 10))) - sage: A = v.column() * v.row() - sage: is_symmetric_psd(A) - True - - The following matrix is symmetric but not positive semidefinite:: - - sage: A = matrix(ZZ, [[1, 2], [2, 1]]) - sage: is_symmetric_psd(A) - False - - This matrix isn't even symmetric:: - - sage: A = matrix(ZZ, [[1, 2], [3, 4]]) - sage: is_symmetric_psd(A) - False - - The trivial matrix in a trivial space is trivially symmetric and - positive-semidefinite:: - - sage: A = matrix(QQ, 0,0) - sage: is_symmetric_psd(A) - True - - """ - - if A.base_ring() == SR: - msg = 'The matrix ``A`` cannot be symbolic.' - raise ValueError.new(msg) - - # First make sure that ``A`` is symmetric. - if not A.is_symmetric(): - return False - - # If ``A`` is symmetric, we only need to check that it is positive - # semidefinite. For that we can consult its minimum eigenvalue, - # which should be zero or greater. Since ``A`` is symmetric, its - # eigenvalues are guaranteed to be real. - if A.is_zero(): - # A is trivial... so trivially positive-semudefinite. - return True - else: - return min(A.eigenvalues()) >= 0 - - def unit_eigenvectors(A): """ Return the unit eigenvectors of a symmetric positive-definite matrix. @@ -253,22 +185,23 @@ def random_symmetric_psd(V, accept_zero=True, rank=None): SETUP:: - sage: from mjo.cone.symmetric_psd import (is_symmetric_psd, - ....: random_symmetric_psd) + sage: from mjo.cone.symmetric_psd import random_symmetric_psd EXAMPLES: Well, it doesn't crash at least:: + sage: set_random_seed() sage: V = VectorSpace(QQ, 2) sage: A = random_symmetric_psd(V) sage: A.matrix_space() Full MatrixSpace of 2 by 2 dense matrices over Rational Field - sage: is_symmetric_psd(A) + sage: A.is_positive_semidefinite() True A matrix with the desired rank is returned:: + sage: set_random_seed() sage: V = VectorSpace(QQ, 5) sage: A = random_symmetric_psd(V,False,1) sage: A.rank() @@ -288,6 +221,7 @@ def random_symmetric_psd(V, accept_zero=True, rank=None): If the user asks for a rank that's too high, we fail:: + sage: set_random_seed() sage: V = VectorSpace(QQ, 2) sage: A = random_symmetric_psd(V,False,3) Traceback (most recent call last):