IsPSD
IsPSD | |
Determines whether or not a matrix is positive semidefinite | |
Other toolboxes required | none |
---|---|
Related functions | IsCP IsPPT IsTotallyPositive |
Function category | Basic operation |
IsPSD is a function that determines whether or not a given matrix is positive semidefinite. The input matrix can be either full or sparse and, if requested, a vector that proves that the given matrix is not positive semidefinite can be provided as output.
Contents
Syntax
- PSD = IsPSD(X)
- PSD = IsPSD(X,TOL)
- [PSD,WIT] = IsPSD(X,TOL)
Argument descriptions
Input arguments
- X: A square matrix.
- TOL (optional, default eps^(3/4)): The numerical tolerance used when determining positive semidefiniteness. The matrix will be determined to be positive semidefinite if its minimal eigenvalue is computed to be at least -TOL.
Output arguments
- PSD: A flag (either 1 or 0) indicating that X is or is not positive semidefinite.
- WIT (optional): An eigenvector corresponding to the minimal eigenvalue of X. When PSD = 0, this serves as a witness that verifies that X is not positive semidefinite, since WIT'*X*WIT < 0.
Examples
Simple example with low tolerance
When X is very simple, positive semidefiniteness can be be determined exactly. The following example has the TOL = 0 (not recommended in general!) to highlight the fact that the script really is checking for positive semidefiniteness, not positive definiteness.
Furthermore, if we make one of the eigenvalues even slightly negative in this case, it is detected as not positive semidefinite:
Note that in general you can not expect this kind of accuracy.
Notes
Do not request the WIT output argument unless you need it. If WIT is not requested, positive semidefiniteness is determined by attempting a Cholesky decomposition of X, which is both faster and more accurate than computing its minimum eigenvalue/eigenvector pair.
Source code
Click on "expand" to the right to view the MATLAB source code for this function.
%% ISPSD Determines whether or not a matrix is positive semidefinite
% This function has one required argument:
% X: a square matrix
%
% PSD = IsPSD(X) is either 1 or 0, indicating that X is or is not
% positive semidefinite (within reasonable numerical error).
%
% This function has one optional input argument:
% TOL (default eps^(3/4))
%
% [PSD,WIT] = IsPSD(X,TOL) determines whether or not X is positive
% semidefinite within the tolerance specified by TOL. WIT is the
% eigenvector corresponding to the minimal eigenvalue of X, and thus can
% act as a witness that proves X is not positive semidefinite (i.e.,
% WIT'*X*WIT < 0).
%
% URL: http://www.qetlab.com/IsPSD
% requires: opt_args.m
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: December 24, 2014
function [psd,wit] = IsPSD(X,varargin)
if(size(X,1) ~= size(X,2))
psd = 0;
wit = 0;
return
end
% set optional argument defaults: tol=eps^(3/4)
[tol] = opt_args({ eps^(3/4) },varargin{:});
% Allow this function to be called within CVX optimization problems.
if(isa(X,'cvx'))
cvx_begin sdp quiet
subject to
X >= 0;
cvx_end
psd = 1-min(cvx_optval,1); % CVX-safe way to map (0,Inf) to (1,0)
% If the function is just being called on a non-CVX variable, just check
% positive semidefiniteness normally (which is much faster).
else
% only check the Hermitian part of X
X = (X+X')/2;
% if the user requested the smallest eigenvector, compute it
if(nargout > 1)
if(isreal(X))
eigs_id = 'SA';
else
eigs_id = 'SR';
end
[wit,eigval] = eigs(X,1,eigs_id);
psd = (eigval >= -tol);
% otherwise, use chol to determine positive semidefiniteness, which is both faster and more accurate
else
[~,p] = chol(X+(tol+eps)*speye(length(X)));
psd = (p == 0);
end
end