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\ |
|
45 @end deftypefn") |
2928
|
46 { |
|
47 octave_value_list retval; |
|
48 |
|
49 int nargin = args.length (); |
|
50 |
|
51 if (nargin != 1 || nargout > 2) |
|
52 { |
5823
|
53 print_usage (); |
2928
|
54 return retval; |
|
55 } |
|
56 |
|
57 octave_value arg = args(0); |
|
58 |
5275
|
59 octave_idx_type nr = arg.rows (); |
|
60 octave_idx_type nc = arg.columns (); |
2928
|
61 |
|
62 int arg_is_empty = empty_arg ("eig", nr, nc); |
|
63 if (arg_is_empty < 0) |
|
64 return retval; |
|
65 else if (arg_is_empty > 0) |
|
66 return octave_value_list (2, Matrix ()); |
|
67 |
|
68 if (nr != nc) |
|
69 { |
|
70 gripe_square_matrix_required ("eig"); |
|
71 return retval; |
|
72 } |
|
73 |
|
74 Matrix tmp; |
|
75 ComplexMatrix ctmp; |
|
76 EIG result; |
|
77 |
|
78 if (arg.is_real_type ()) |
|
79 { |
|
80 tmp = arg.matrix_value (); |
|
81 |
|
82 if (error_state) |
|
83 return retval; |
|
84 else |
4725
|
85 result = EIG (tmp, nargout > 1); |
2928
|
86 } |
|
87 else if (arg.is_complex_type ()) |
|
88 { |
|
89 ctmp = arg.complex_matrix_value (); |
|
90 |
|
91 if (error_state) |
|
92 return retval; |
|
93 else |
4725
|
94 result = EIG (ctmp, nargout > 1); |
2928
|
95 } |
|
96 else |
|
97 { |
|
98 gripe_wrong_type_arg ("eig", tmp); |
|
99 return retval; |
|
100 } |
|
101 |
|
102 if (nargout == 0 || nargout == 1) |
|
103 { |
4661
|
104 retval(0) = result.eigenvalues (); |
2928
|
105 } |
|
106 else |
|
107 { |
|
108 // Blame it on Matlab. |
|
109 |
|
110 ComplexDiagMatrix d (result.eigenvalues ()); |
|
111 |
|
112 retval(1) = d; |
|
113 retval(0) = result.eigenvectors (); |
|
114 } |
|
115 |
|
116 return retval; |
|
117 } |
|
118 |
|
119 /* |
|
120 ;;; Local Variables: *** |
|
121 ;;; mode: C++ *** |
|
122 ;;; End: *** |
|
123 */ |