1 ## Copyright (C) 2003-2012 Gabriele Pannocchia
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} isdefinite (@var{x})
21 ## @deftypefnx {Function File} {} isdefinite (@var{x}, @var{tol})
22 ## Return 1 if @var{x} is symmetric positive definite within the
23 ## tolerance specified by @var{tol} or 0 if @var{x} is symmetric
24 ## positive semidefinite. Otherwise, return -1. If @var{tol}
25 ## is omitted, use a tolerance of
26 ## @code{100 * eps * norm (@var{x}, "fro")}
27 ## @seealso{issymmetric, ishermitian}
30 ## Author: Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
31 ## Created: November 2003
34 function retval = isdefinite (x, tol)
36 if (nargin < 1 || nargin > 2)
45 tol = 100 * eps (class (x)) * norm (x, "fro");
48 if (! ishermitian (x, tol))
49 error ("isdefinite: X must be a Hermitian matrix");
52 e = tol * eye (rows (x));
53 [r, p] = chol (x - e);
57 [r, p] = chol (x + e);
70 %! assert (isdefinite (A), -1)
74 %! assert (isdefinite (A), 1)
77 %! A = [2 -1 0; -1 2 -1; 0 -1 2];
78 %! assert (isdefinite (A), 1)
82 %! assert (isdefinite (A), 0)
85 %!error isdefinite (1,2,3)
86 %!error <X must be a Hermitian matrix> isdefinite ([1 2; 3 4])