annotate scripts/linear-algebra/isdefinite.m @ 14138:72c96de7a403 stable

maint: update copyright notices for 2012
author John W. Eaton <jwe@octave.org>
date Mon, 02 Jan 2012 14:25:41 -0500
parents b2e9c4b0c4f8
children f3d52523cde1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 14067
diff changeset
1 ## Copyright (C) 2003-2012 Gabriele Pannocchia
4610
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
2 ##
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
4 ##
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
4610
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
9 ##
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
14 ##
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4610
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
18
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
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
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
22 ## Return 1 if @var{x} is symmetric positive definite within the
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
23 ## tolerance specified by @var{tol} or 0 if @var{x} is symmetric
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
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
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
28 ## @end deftypefn
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
29
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
30 ## Author: Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
31 ## Created: November 2003
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
32 ## Adapted-By: jwe
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
33
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
34 function retval = isdefinite (x, tol)
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
35
9870
5b733adba096 base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents: 9869
diff changeset
36 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
37 print_usage ();
4610
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
38 endif
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
39
9870
5b733adba096 base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents: 9869
diff changeset
40 if (! isfloat (x))
5b733adba096 base isdefinite on cholesky decomposition
Jaroslav Hajek <highegg@gmail.com>
parents: 9869
diff changeset
41 x = double (x);
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 (nargin == 1)
14067
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
45 tol = 100 * eps (class (x)) * norm (x, "fro");
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
46 endif
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
47
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
48 if (! ishermitian (x, tol))
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
49 error ("isdefinite: X must be a Hermitian matrix");
9870
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
02d2fcf835fc [project @ 2003-11-14 17:08:59 by jwe]
jwe
parents:
diff changeset
65 endfunction
12798
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
66
14067
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
67
12798
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
68 %!test
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
69 %! A = [-1 0; 0 -1];
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
70 %! assert (isdefinite (A), -1)
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
71
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
72 %!test
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
73 %! A = [1 0; 0 1];
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
74 %! assert (isdefinite (A), 1)
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
75
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
76 %!test
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
77 %! A = [2 -1 0; -1 2 -1; 0 -1 2];
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
78 %! assert (isdefinite (A), 1)
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
79
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
80 %!test
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
81 %! A = [1 0; 0 0];
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
82 %! assert (isdefinite (A), 0)
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
83
6fdf4927fefc codesprint: Write 6 tests for isdefinite
Sean Young <seannz@gmail.com>
parents: 11587
diff changeset
84 %!error isdefinite ()
14067
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
85 %!error isdefinite (1,2,3)
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
86 %!error <X must be a Hermitian matrix> isdefinite ([1 2; 3 4])
b2e9c4b0c4f8 isdefinite.m: Use any specified tolerance in deciding whether matrix is hermitian.
Lukas Reichlin <lukas.reichlin@gmail.com>
parents: 12798
diff changeset
87