Mercurial > octave
annotate src/DLD-FUNCTIONS/eig.cc @ 7578:91f8446ce4ae
handle possible error from EIG
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 11 Mar 2008 10:49:33 -0400 |
parents | a1dbe9d80eee |
children | 82be108cc558 72830070a17b |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007 |
4 John W. Eaton | |
2928 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "EIG.h" | |
29 | |
30 #include "defun-dld.h" | |
31 #include "error.h" | |
32 #include "gripes.h" | |
33 #include "oct-obj.h" | |
34 #include "utils.h" | |
35 | |
36 DEFUN_DLD (eig, args, nargout, | |
3548 | 37 "-*- texinfo -*-\n\ |
3372 | 38 @deftypefn {Loadable Function} {@var{lambda} =} eig (@var{a})\n\ |
39 @deftypefnx {Loadable Function} {[@var{v}, @var{lambda}] =} eig (@var{a})\n\ | |
40 The eigenvalues (and eigenvectors) of a matrix are computed in a several\n\ | |
41 step process which begins with a Hessenberg decomposition, followed by a\n\ | |
42 Schur decomposition, from which the eigenvalues are apparent. The\n\ | |
43 eigenvectors, when desired, are computed by further manipulations of the\n\ | |
44 Schur decomposition.\n\ | |
5829 | 45 \n\ |
46 The eigenvalues returned by @code{eig} are not ordered.\n\ | |
3372 | 47 @end deftypefn") |
2928 | 48 { |
49 octave_value_list retval; | |
50 | |
51 int nargin = args.length (); | |
52 | |
53 if (nargin != 1 || nargout > 2) | |
54 { | |
5823 | 55 print_usage (); |
2928 | 56 return retval; |
57 } | |
58 | |
59 octave_value arg = args(0); | |
60 | |
5275 | 61 octave_idx_type nr = arg.rows (); |
62 octave_idx_type nc = arg.columns (); | |
2928 | 63 |
64 int arg_is_empty = empty_arg ("eig", nr, nc); | |
65 if (arg_is_empty < 0) | |
66 return retval; | |
67 else if (arg_is_empty > 0) | |
68 return octave_value_list (2, Matrix ()); | |
69 | |
70 if (nr != nc) | |
71 { | |
72 gripe_square_matrix_required ("eig"); | |
73 return retval; | |
74 } | |
75 | |
76 Matrix tmp; | |
77 ComplexMatrix ctmp; | |
78 EIG result; | |
79 | |
80 if (arg.is_real_type ()) | |
81 { | |
82 tmp = arg.matrix_value (); | |
83 | |
84 if (error_state) | |
85 return retval; | |
86 else | |
4725 | 87 result = EIG (tmp, nargout > 1); |
2928 | 88 } |
89 else if (arg.is_complex_type ()) | |
90 { | |
91 ctmp = arg.complex_matrix_value (); | |
92 | |
93 if (error_state) | |
94 return retval; | |
95 else | |
4725 | 96 result = EIG (ctmp, nargout > 1); |
2928 | 97 } |
98 else | |
99 { | |
100 gripe_wrong_type_arg ("eig", tmp); | |
101 return retval; | |
102 } | |
103 | |
7578
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 if (! error_state) |
2928 | 105 { |
7578
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
106 if (nargout == 0 || nargout == 1) |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 { |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
108 retval(0) = result.eigenvalues (); |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
109 } |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 else |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
111 { |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
112 // Blame it on Matlab. |
2928 | 113 |
7578
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
114 ComplexDiagMatrix d (result.eigenvalues ()); |
2928 | 115 |
7578
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
116 retval(1) = d; |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
117 retval(0) = result.eigenvectors (); |
91f8446ce4ae
handle possible error from EIG
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
118 } |
2928 | 119 } |
120 | |
121 return retval; | |
122 } | |
123 | |
124 /* | |
125 ;;; Local Variables: *** | |
126 ;;; mode: C++ *** | |
127 ;;; End: *** | |
128 */ |