|Determines whether or not a matrix is positive semidefinite|
|Other toolboxes required||none|
|Related functions|| IsCP|
|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.
- PSD = IsPSD(X)
- PSD = IsPSD(X,TOL)
- [PSD,WIT] = IsPSD(X,TOL)
- 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.
- 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.
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.
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.
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 (firstname.lastname@example.org)
% package: QETLAB
% last updated: December 24, 2014
psd = 0;
wit = 0;
% set optional argument defaults: tol=eps^(3/4)
% Allow this function to be called within CVX optimization problems.
cvx_begin sdp quiet
X >= 0;
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).
% only check the Hermitian part of X
X = (X+X')/2;
% if the user requested the smallest eigenvector, compute it
if(nargout > 1)
eigs_id = 'SA';
eigs_id = 'SR';
[wit,eigval] = eigs(X,1,eigs_id);
psd = (eigval >= -tol);
% otherwise, use chol to determine positive semidefiniteness, which is both faster and more accurate
psd = (p == 0);