Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include "EIG.h"
00028 #include "fEIG.h"
00029
00030 #include "defun-dld.h"
00031 #include "error.h"
00032 #include "gripes.h"
00033 #include "oct-obj.h"
00034 #include "utils.h"
00035
00036 DEFUN_DLD (eig, args, nargout,
00037 "-*- texinfo -*-\n\
00038 @deftypefn {Loadable Function} {@var{lambda} =} eig (@var{A})\n\
00039 @deftypefnx {Loadable Function} {@var{lambda} =} eig (@var{A}, @var{B})\n\
00040 @deftypefnx {Loadable Function} {[@var{V}, @var{lambda}] =} eig (@var{A})\n\
00041 @deftypefnx {Loadable Function} {[@var{V}, @var{lambda}] =} eig (@var{A}, @var{B})\n\
00042 Compute the eigenvalues and eigenvectors of a matrix.\n\
00043 \n\
00044 Eigenvalues are computed in a several step process which begins with a\n\
00045 Hessenberg decomposition, followed by a Schur@tie{}decomposition, from which\n\
00046 the eigenvalues are apparent. The eigenvectors, when desired, are computed\n\
00047 by further manipulations of the Schur@tie{}decomposition.\n\
00048 \n\
00049 The eigenvalues returned by @code{eig} are not ordered.\n\
00050 @seealso{eigs, svd}\n\
00051 @end deftypefn")
00052 {
00053 octave_value_list retval;
00054
00055 int nargin = args.length ();
00056
00057 if (nargin > 2 || nargin == 0 || nargout > 2)
00058 {
00059 print_usage ();
00060 return retval;
00061 }
00062
00063 octave_value arg_a, arg_b;
00064
00065 octave_idx_type nr_a = 0, nr_b = 0;
00066 octave_idx_type nc_a = 0, nc_b = 0;
00067
00068 arg_a = args(0);
00069 nr_a = arg_a.rows ();
00070 nc_a = arg_a.columns ();
00071
00072 int arg_is_empty = empty_arg ("eig", nr_a, nc_a);
00073 if (arg_is_empty < 0)
00074 return retval;
00075 else if (arg_is_empty > 0)
00076 return octave_value_list (2, Matrix ());
00077
00078 if (!(arg_a.is_single_type () || arg_a.is_double_type ()))
00079 {
00080 gripe_wrong_type_arg ("eig", arg_a);
00081 return retval;
00082 }
00083
00084 if (nargin == 2)
00085 {
00086 arg_b = args(1);
00087 nr_b = arg_b.rows ();
00088 nc_b = arg_b.columns ();
00089
00090 arg_is_empty = empty_arg ("eig", nr_b, nc_b);
00091 if (arg_is_empty < 0)
00092 return retval;
00093 else if (arg_is_empty > 0)
00094 return octave_value_list (2, Matrix ());
00095
00096 if (!(arg_b.is_single_type() || arg_b.is_double_type ()))
00097 {
00098 gripe_wrong_type_arg ("eig", arg_b);
00099 return retval;
00100 }
00101 }
00102
00103 if (nr_a != nc_a)
00104 {
00105 gripe_square_matrix_required ("eig");
00106 return retval;
00107 }
00108
00109 if (nargin == 2 && nr_b != nc_b)
00110 {
00111 gripe_square_matrix_required ("eig");
00112 return retval;
00113 }
00114
00115 Matrix tmp_a, tmp_b;
00116 ComplexMatrix ctmp_a, ctmp_b;
00117 FloatMatrix ftmp_a, ftmp_b;
00118 FloatComplexMatrix fctmp_a, fctmp_b;
00119
00120 if (arg_a.is_single_type ())
00121 {
00122 FloatEIG result;
00123
00124 if (nargin == 1)
00125 {
00126 if (arg_a.is_real_type ())
00127 {
00128 ftmp_a = arg_a.float_matrix_value ();
00129
00130 if (error_state)
00131 return retval;
00132 else
00133 result = FloatEIG (ftmp_a, nargout > 1);
00134 }
00135 else
00136 {
00137 fctmp_a = arg_a.float_complex_matrix_value ();
00138
00139 if (error_state)
00140 return retval;
00141 else
00142 result = FloatEIG (fctmp_a, nargout > 1);
00143 }
00144 }
00145 else if (nargin == 2)
00146 {
00147 if (arg_a.is_real_type () && arg_b.is_real_type ())
00148 {
00149 ftmp_a = arg_a.float_matrix_value ();
00150 ftmp_b = arg_b.float_matrix_value ();
00151
00152 if (error_state)
00153 return retval;
00154 else
00155 result = FloatEIG (ftmp_a, ftmp_b, nargout > 1);
00156 }
00157 else
00158 {
00159 fctmp_a = arg_a.float_complex_matrix_value ();
00160 fctmp_b = arg_b.float_complex_matrix_value ();
00161
00162 if (error_state)
00163 return retval;
00164 else
00165 result = FloatEIG (fctmp_a, fctmp_b, nargout > 1);
00166 }
00167 }
00168
00169 if (! error_state)
00170 {
00171 if (nargout == 0 || nargout == 1)
00172 {
00173 retval(0) = result.eigenvalues ();
00174 }
00175 else
00176 {
00177
00178
00179 FloatComplexDiagMatrix d (result.eigenvalues ());
00180
00181 retval(1) = d;
00182 retval(0) = result.eigenvectors ();
00183 }
00184 }
00185 }
00186 else
00187 {
00188 EIG result;
00189
00190 if (nargin == 1)
00191 {
00192 if (arg_a.is_real_type ())
00193 {
00194 tmp_a = arg_a.matrix_value ();
00195
00196 if (error_state)
00197 return retval;
00198 else
00199 result = EIG (tmp_a, nargout > 1);
00200 }
00201 else
00202 {
00203 ctmp_a = arg_a.complex_matrix_value ();
00204
00205 if (error_state)
00206 return retval;
00207 else
00208 result = EIG (ctmp_a, nargout > 1);
00209 }
00210 }
00211 else if (nargin == 2)
00212 {
00213 if (arg_a.is_real_type () && arg_b.is_real_type ())
00214 {
00215 tmp_a = arg_a.matrix_value ();
00216 tmp_b = arg_b.matrix_value ();
00217
00218 if (error_state)
00219 return retval;
00220 else
00221 result = EIG (tmp_a, tmp_b, nargout > 1);
00222 }
00223 else
00224 {
00225 ctmp_a = arg_a.complex_matrix_value ();
00226 ctmp_b = arg_b.complex_matrix_value ();
00227
00228 if (error_state)
00229 return retval;
00230 else
00231 result = EIG (ctmp_a, ctmp_b, nargout > 1);
00232 }
00233 }
00234
00235 if (! error_state)
00236 {
00237 if (nargout == 0 || nargout == 1)
00238 {
00239 retval(0) = result.eigenvalues ();
00240 }
00241 else
00242 {
00243
00244
00245 ComplexDiagMatrix d (result.eigenvalues ());
00246
00247 retval(1) = d;
00248 retval(0) = result.eigenvectors ();
00249 }
00250 }
00251 }
00252
00253 return retval;
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336