Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/isdefinite.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | 6fdf4927fefc |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2003-2011 Gabriele Pannocchia |
4610 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4610 | 9 ## |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4610 | 18 |
19 ## -*- texinfo -*- | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9870
diff
changeset
|
20 ## @deftypefn {Function File} {} isdefinite (@var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9870
diff
changeset
|
21 ## @deftypefnx {Function File} {} isdefinite (@var{x}, @var{tol}) |
4610 | 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} | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## is omitted, use a tolerance of |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9870
diff
changeset
|
26 ## @code{100 * eps * norm (@var{x}, "fro")} |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
27 ## @seealso{issymmetric, ishermitian} |
4610 | 28 ## @end deftypefn |
29 | |
30 ## Author: Gabriele Pannocchia <g.pannocchia@ing.unipi.it> | |
31 ## Created: November 2003 | |
32 ## Adapted-By: jwe | |
33 | |
34 function retval = isdefinite (x, tol) | |
35 | |
9870
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
36 if (nargin < 1 || nargin > 2) |
6046 | 37 print_usage (); |
4610 | 38 endif |
39 | |
9870
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
40 if (! ishermitian (x)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11431
diff
changeset
|
41 error ("isdefinite: X must be a hermitian matrix"); |
9870
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
42 endif |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
43 |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
44 if (! isfloat (x)) |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
45 x = double (x); |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
46 endif |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
47 |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
48 if (nargin == 1) |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
49 tol = 100 * eps(class (x)) * norm (x, "fro"); |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
50 endif |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
51 |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
52 e = tol * eye (rows (x)); |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
53 [r, p] = chol (x - e); |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
54 if (p == 0) |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
55 retval = 1; |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
56 else |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
57 [r, p] = chol (x + e); |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
58 if (p == 0) |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
59 retval = 0; |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
60 else |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
61 retval = -1; |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
62 endif |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
63 endif |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
64 |
4610 | 65 endfunction |