1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
445
|
27 #include "EIG.h" |
1
|
28 |
1352
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "gripes.h" |
|
32 #include "help.h" |
1740
|
33 #include "oct-obj.h" |
1
|
34 #include "user-prefs.h" |
636
|
35 #include "utils.h" |
1
|
36 |
1957
|
37 DEFUN_DLD_BUILTIN (eig, args, nargout, |
519
|
38 "eig (X) or [V, D] = eig (X): compute eigenvalues and eigenvectors of X") |
1
|
39 { |
2086
|
40 octave_value_list retval; |
1
|
41 |
712
|
42 int nargin = args.length (); |
|
43 |
|
44 if (nargin != 1 || nargout > 2) |
519
|
45 { |
|
46 print_usage ("eig"); |
|
47 return retval; |
|
48 } |
|
49 |
2086
|
50 octave_value arg = args(0); |
1
|
51 |
636
|
52 int nr = arg.rows (); |
|
53 int nc = arg.columns (); |
1
|
54 |
718
|
55 int arg_is_empty = empty_arg ("eig", nr, nc); |
|
56 if (arg_is_empty < 0) |
636
|
57 return retval; |
718
|
58 else if (arg_is_empty > 0) |
2086
|
59 return octave_value_list (2, Matrix ()); |
1
|
60 |
636
|
61 if (nr != nc) |
1
|
62 { |
|
63 gripe_square_matrix_required ("eig"); |
|
64 return retval; |
|
65 } |
|
66 |
|
67 Matrix tmp; |
|
68 ComplexMatrix ctmp; |
|
69 EIG result; |
636
|
70 |
|
71 if (arg.is_real_type ()) |
620
|
72 { |
1
|
73 tmp = arg.matrix_value (); |
636
|
74 |
|
75 if (error_state) |
|
76 return retval; |
|
77 else |
|
78 result = EIG (tmp); |
620
|
79 } |
636
|
80 else if (arg.is_complex_type ()) |
620
|
81 { |
1
|
82 ctmp = arg.complex_matrix_value (); |
636
|
83 |
|
84 if (error_state) |
|
85 return retval; |
|
86 else |
|
87 result = EIG (ctmp); |
620
|
88 } |
|
89 else |
|
90 { |
|
91 gripe_wrong_type_arg ("eig", tmp); |
|
92 return retval; |
1
|
93 } |
|
94 |
497
|
95 if (nargout == 0 || nargout == 1) |
1
|
96 { |
516
|
97 retval(0) = result.eigenvalues (), 1; |
1
|
98 } |
|
99 else |
|
100 { |
1357
|
101 // Blame it on Matlab. |
1
|
102 |
|
103 ComplexDiagMatrix d (result.eigenvalues ()); |
|
104 |
620
|
105 retval(1) = d; |
516
|
106 retval(0) = result.eigenvectors (); |
1
|
107 } |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
|
112 /* |
|
113 ;;; Local Variables: *** |
|
114 ;;; mode: C++ *** |
|
115 ;;; End: *** |
|
116 */ |