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