7
|
1 // f-eig.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
445
|
28 #include "EIG.h" |
1
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "user-prefs.h" |
|
32 #include "gripes.h" |
|
33 #include "error.h" |
519
|
34 #include "defun-dld.h" |
1
|
35 |
519
|
36 DEFUN_DLD ("eig", Feig, Seig, 2, 1, |
|
37 "eig (X) or [V, D] = eig (X): compute eigenvalues and eigenvectors of X") |
1
|
38 { |
497
|
39 Octave_object retval; |
1
|
40 |
519
|
41 int nargin = args.length (); |
|
42 |
|
43 if (nargin != 2 || nargout > 2) |
|
44 { |
|
45 print_usage ("eig"); |
|
46 return retval; |
|
47 } |
|
48 |
497
|
49 tree_constant arg = args(1).make_numeric (); |
1
|
50 |
|
51 int a_nr = arg.rows (); |
|
52 int a_nc = arg.columns (); |
|
53 |
|
54 if (a_nr == 0 || a_nc == 0) |
|
55 { |
|
56 int flag = user_pref.propagate_empty_matrices; |
|
57 if (flag != 0) |
|
58 { |
|
59 if (flag < 0) |
|
60 gripe_empty_arg ("eig", 0); |
|
61 Matrix m; |
497
|
62 retval.resize (2); |
516
|
63 retval(0) = m; |
|
64 retval(1) = m; |
1
|
65 } |
|
66 else |
|
67 gripe_empty_arg ("eig", 1); |
|
68 |
|
69 return retval; |
|
70 } |
|
71 |
|
72 if (a_nr != a_nc) |
|
73 { |
|
74 gripe_square_matrix_required ("eig"); |
|
75 return retval; |
|
76 } |
|
77 |
|
78 Matrix tmp; |
|
79 ComplexMatrix ctmp; |
|
80 EIG result; |
|
81 switch (arg.const_type ()) |
|
82 { |
|
83 case tree_constant_rep::scalar_constant: |
|
84 tmp.resize (1, 1); |
|
85 tmp.elem (0, 0) = arg.double_value (); |
|
86 result = EIG (tmp); |
|
87 break; |
|
88 case tree_constant_rep::matrix_constant: |
|
89 tmp = arg.matrix_value (); |
|
90 result = EIG (tmp); |
|
91 break; |
|
92 case tree_constant_rep::complex_scalar_constant: |
|
93 ctmp.resize (1, 1); |
|
94 ctmp.elem (0, 0) = arg.complex_value (); |
|
95 result = EIG (ctmp); |
|
96 break; |
|
97 case tree_constant_rep::complex_matrix_constant: |
|
98 ctmp = arg.complex_matrix_value (); |
|
99 result = EIG (ctmp); |
|
100 break; |
|
101 default: |
|
102 panic_impossible (); |
|
103 break; |
|
104 } |
|
105 |
497
|
106 if (nargout == 0 || nargout == 1) |
1
|
107 { |
497
|
108 retval.resize (1); |
516
|
109 retval(0) = result.eigenvalues (), 1; |
1
|
110 } |
|
111 else |
|
112 { |
|
113 // Blame it on Matlab. |
|
114 |
|
115 ComplexDiagMatrix d (result.eigenvalues ()); |
|
116 |
497
|
117 retval.resize (2); |
516
|
118 retval(0) = result.eigenvectors (); |
|
119 retval(1) = d; |
1
|
120 } |
|
121 |
|
122 return retval; |
|
123 } |
|
124 |
|
125 /* |
|
126 ;;; Local Variables: *** |
|
127 ;;; mode: C++ *** |
|
128 ;;; page-delimiter: "^/\\*" *** |
|
129 ;;; End: *** |
|
130 */ |