Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/isdefinite.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | a8ce6bdecce5 |
children | 0d9640d755b1 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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} | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9870
diff
changeset
|
25 ## is omitted, use a tolerance of |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9870
diff
changeset
|
26 ## @code{100 * eps * norm (@var{x}, "fro")} |
5642 | 27 ## @seealso{issymmetric} |
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)) |
5b733adba096
base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents:
9869
diff
changeset
|
41 error ("isdefinite: x must be a hermitian matrix"); |
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 |