Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/isdefinite.m @ 15144:9cc337ced51a
build: Update OCTAVE_UMFPACK_SEPARATE_SPLIT macro to look for SuiteSparse header file.
* acinclude.m4: Update OCTAVE_UMFPACK_SEPARATE_SPLIT macro to look for
SuiteSparse header file.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 10 Aug 2012 12:56:15 -0700 |
parents | f3d52523cde1 |
children | d63878346099 |
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 | 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 (! 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 | 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]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 %! assert (isdefinite (A), -1); |
12798
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]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %! assert (isdefinite (A), 1); |
12798
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]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %! assert (isdefinite (A), 1); |
12798
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]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
82 %! assert (isdefinite (A), 0); |
12798
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 |