7
|
1 // f-eig.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
445
|
28 #include "EIG.h" |
1
|
29 |
1352
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "help.h" |
1740
|
34 #include "oct-obj.h" |
1
|
35 #include "user-prefs.h" |
636
|
36 #include "utils.h" |
1
|
37 |
1957
|
38 DEFUN_DLD_BUILTIN (eig, args, nargout, |
519
|
39 "eig (X) or [V, D] = eig (X): compute eigenvalues and eigenvectors of X") |
1
|
40 { |
497
|
41 Octave_object retval; |
1
|
42 |
712
|
43 int nargin = args.length (); |
|
44 |
|
45 if (nargin != 1 || nargout > 2) |
519
|
46 { |
|
47 print_usage ("eig"); |
|
48 return retval; |
|
49 } |
|
50 |
712
|
51 tree_constant arg = args(0); |
1
|
52 |
636
|
53 int nr = arg.rows (); |
|
54 int nc = arg.columns (); |
1
|
55 |
718
|
56 int arg_is_empty = empty_arg ("eig", nr, nc); |
|
57 if (arg_is_empty < 0) |
636
|
58 return retval; |
718
|
59 else if (arg_is_empty > 0) |
|
60 return Octave_object (2, Matrix ()); |
1
|
61 |
636
|
62 if (nr != nc) |
1
|
63 { |
|
64 gripe_square_matrix_required ("eig"); |
|
65 return retval; |
|
66 } |
|
67 |
|
68 Matrix tmp; |
|
69 ComplexMatrix ctmp; |
|
70 EIG result; |
636
|
71 |
|
72 if (arg.is_real_type ()) |
620
|
73 { |
1
|
74 tmp = arg.matrix_value (); |
636
|
75 |
|
76 if (error_state) |
|
77 return retval; |
|
78 else |
|
79 result = EIG (tmp); |
620
|
80 } |
636
|
81 else if (arg.is_complex_type ()) |
620
|
82 { |
1
|
83 ctmp = arg.complex_matrix_value (); |
636
|
84 |
|
85 if (error_state) |
|
86 return retval; |
|
87 else |
|
88 result = EIG (ctmp); |
620
|
89 } |
|
90 else |
|
91 { |
|
92 gripe_wrong_type_arg ("eig", tmp); |
|
93 return retval; |
1
|
94 } |
|
95 |
497
|
96 if (nargout == 0 || nargout == 1) |
1
|
97 { |
516
|
98 retval(0) = result.eigenvalues (), 1; |
1
|
99 } |
|
100 else |
|
101 { |
1357
|
102 // Blame it on Matlab. |
1
|
103 |
|
104 ComplexDiagMatrix d (result.eigenvalues ()); |
|
105 |
620
|
106 retval(1) = d; |
516
|
107 retval(0) = result.eigenvectors (); |
1
|
108 } |
|
109 |
|
110 return retval; |
|
111 } |
|
112 |
|
113 /* |
|
114 ;;; Local Variables: *** |
|
115 ;;; mode: C++ *** |
|
116 ;;; page-delimiter: "^/\\*" *** |
|
117 ;;; End: *** |
|
118 */ |