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