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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
|
104 if (nargout == 0 || nargout == 1) |
|
105 { |
4661
|
106 retval(0) = result.eigenvalues (); |
2928
|
107 } |
|
108 else |
|
109 { |
|
110 // Blame it on Matlab. |
|
111 |
|
112 ComplexDiagMatrix d (result.eigenvalues ()); |
|
113 |
|
114 retval(1) = d; |
|
115 retval(0) = result.eigenvectors (); |
|
116 } |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
|
121 /* |
|
122 ;;; Local Variables: *** |
|
123 ;;; mode: C++ *** |
|
124 ;;; End: *** |
|
125 */ |