Check to see if a matrix is positive-definite, positive-semidefinite, indefinite, negative-semidefinite, or negative-definite.
An optional tolerance, tol, can be provided. Any eigenvalues between
-tol and tol will be treated as 0. By default, the tolerance is the
machine precision of the maximum eigenvalue.
s = msign(P);
s = msign(P, tol);P | Square matrix to test |
|---|---|
tol | Tolerance to use for comparison (optional) |
s | "Sign" of matrix: |
|---|
When we generate a random matrix, we expect a positive-definite matrix. Let's test:
X = randcov(3);
msign(X) % Expect 2 to indicate PDans =
2If we multiply a 2D covariance matrix by a 3-by-2 matrix on each side, we'll end up with a 3-by-3 positive-semidefinite matrix.
F = [1 0; 0 1; -1 0];
P = [1 0; 0 1];
msign(F * P * F.') % Expect 1ans =
1Let's manually construct an indefinite matrix and see that we get the right thing.
A = randn(3);
P = A * diag([1 2 -3]) * A.';
msign(P)ans =
0P = [0 0; 0 -1];
msign(P)ans =
-1We'll use exactly 0 as the tolerance to test a badly-scaled matrix for negative-definiteness (2).
P = [-1e12 0; 0 -1e-6];
msign(P, 0)ans =
-2Note that the default tolerance, for numerical reasons, would say that this matrix only appears to be negaitve-semidefinite (-1).
msign(P)ans =
-1*kf v1.0.3