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