# eig() function

3 messages
Open this post in threaded view
|

## eig() function

 Dr. Eaton, While working with the eig() function in octave (all version that I am awared of), I noticed that the computed eigenvalues and their corresponding eigenvectors are sorted in an ascending order.  This also happens with MatLab.  I am wondering if it is possible to add a routine in the eig() function to return the eigenvalues and the corresponding eigenvectors in a descending order, for the next release of octave.  To make this function compatible with MatLab's eig function, make the descending order feature as part of the OPT switch, i.e. when OPT=d, the returned eigenvalues and their corresponding eigenvectors are sorted in a descending order. I know that sorting can be done after one has computed the eigenvalues and their corresponding eigenvectors.  As a matter of fact, I usually sort the eigenvalues and their respective eigenvectors in a descending order with the following macros:         function [v,d] = eigsort (x)                 length = is_square (x);                 if !(length)                         error ('Input argument is not a square matrix!\n');                 end;                 [v,d] = eig (x);                 [dd,ix] = sort (-diag (d));                 for i=1:length                         d(i,i) = - dd(i);                 end;                 v = v(:,ix)         endfunction If sorting the eigenvalues and their corresponding eigenvectors in the eig() function is not complicated and can speed up the process, wouldn't it be better to add the sorting algorithm as an additional OPT feature in the eig function instead of the above macros? Thank you very much for your understanding.     --     [hidden email]
Open this post in threaded view
|

## Re: eig() function

 >         function [v,d] = eigsort (x) >                 length  = is_square (x); >                 if !(length) >                         error ('Input argument is not a square matrix!\n'); >                 end; >   >                 [v,d]   = eig (x); >                 [dd,ix] = sort (-diag (d)); >                 for i=1:length >                         d(i,i)  = - dd(i); >                 end; >                 v       = v(:,ix) >         endfunction >   > If sorting the eigenvalues and their corresponding eigenvectors in the > eig() function is not complicated and can speed up the process, wouldn't > it be better to add the sorting algorithm as an additional OPT feature in > the eig function instead of the above macros? Try using the flipud() and fliplr() functions on your v,d matrices above...         function [v,d] = eigd (x)            [v,d]   = eig (x);            v = fliplr(v);            d = flipud(fliplr(d));  % or use:  d = diag(flipud(diag(d)))                                    % ...whichever is faster         end My personal vote is to not load any of the built-in linear algebra package subroutines. The initial eigsort() function above will also sort the eigenvalues in decending order *including* sign, meaning that my suggestion above is only the same as the initial eigsort() routine if all of the eigenvalues are positive reals....furthermore, I think the sort() routine disregards the imaginary portion of the eigenvalue, whereas the sort in the eig() function is based on the abs() of the eigenvalues.  There would be a convention to define here if we were to modify eig(). M.
Open this post in threaded view
|

## Re: eig() function

 On  9-Oct-1997, Marvin Vis <[hidden email]> wrote: | > If sorting the eigenvalues and their corresponding eigenvectors in the | > eig() function is not complicated and can speed up the process, wouldn't | > it be better to add the sorting algorithm as an additional OPT feature in | > the eig function instead of the above macros? | | Try using the flipud() and fliplr() functions on your v,d matrices above... | |         function [v,d] = eigd (x) |   [v,d]   = eig (x); |   v = fliplr(v); |   d = flipud(fliplr(d));  % or use:  d = diag(flipud(diag(d))) |   % ...whichever is faster | end | | My personal vote is to not load any of the built-in linear algebra package | subroutines. I agree, and since it is possible to do what you want by defining a separate function, I'm not likely to add such a feature to eig myself. But if someone else contributes the code, I'd probably agree to install it. jwe