Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slsymeig
Home > sltoolbox > core > slsymeig.m

slsymeig

PURPOSE ^

SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix

SYNOPSIS ^

function [evals, evecs] = slsymeig(A, k, ord)

DESCRIPTION ^

SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix

 $ Syntax $
   - evals = slsymeig(A)
   - [evals, evecs] = slsymeig(A)
   - ... = slsymeig(A, k)
   - ... = slsymeig(A, k, ord)

 $ Arguments $
   - A:        the target symmetrix matrix to be decomposed
   - k:        the number of eigenvalues to be solved
   - ord:      the order of the sorting ('ascend' | 'descend')
               default = 'descend';
   - evals:    the column vector of eigenvalues of A (in descending order)
   - evecs:    the matrix of eigenvectors of A

 $ Description $
   - evals = slsymeig(A) computes only the eigenvalues of symmetric 
     matrix A.

   - [evals, evecs] = slsymeig(A) computes both the eigenvalues and
     eigenvectors of matrix A. If A is a d x d matrix, then evals
     is a d x 1 column vector with the eigenvalues in descending order.
     evecs is a d x d matrix with each column vector corresponding to
     an eigenvalue in evals.

   - ... = slsymeig(A, k) the user can specify the number of eigenvalues
     to preserve, if not specified, all eigenvalues will be preserved.

   - ... = slsymeig(A, k, ord) the user can specify how to sort the
     eigenvalues. By default, they are sorted in descending order.

 $ History $
   - Created by Dahua Lin on Apr 21, 2006
   - Modified by Dahua Lin on Sep 9, 2006
       - The user now can specify the number of eigenvalues to preserve
         if necessary.  
       - An auto-selection scheme, which will use eigs when k is small
         in order to achieve higher efficiency.

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:
  • slsymgeig SLSYMGEIG Solve the generalized eigen decomposition for symmetric matrices
  • slkpca SLPCA Learns a Kernel PCA model from training samples
  • slgembed SLGEMBED Solves the general graph-based embedding
  • slkernelembed SLKERNELEMBED Finds an embedding space to preserve inner products
  • sllemap SLLEMAP Solves Laplacian Eigenmap Embedding
  • sllle_wg SLLLE_WG Solves the Locally Linear Embedding from weight graph
  • sllocalcoordalign SLLOCALCOORDALIGN Performs optimal local coordinate alignment
  • sllocaltanspace SLLOCALTANSPACE Solves the local tangent spaces
  • slgaussrnd SLGAUSSRND Generates random samples from Gaussian models
  • slinvcov SLINVCOV Compute the inverse of an covariance matrix
  • slwhiten_from_cov SLWHITEN_FROM_COV Compute the whitening transform from covariance matrix
  • slwhiten_from_samples SLWHITEN_FROM_SAMPLES Compute the whitening matrix from sample matrix
  • slcovpca SLCOVPCA Trains a PCA with the covariance matrix given
  • slfld SLFLD Performs Fisher Linear Discriminant Analysis
  • slnlda SLNLDA Performs Nullspace-based Linear Discriminant Analysis
  • slpca SLPCA Learns a PCA model from training samples
  • slrangespace SLRANGESPACE Determines the subspace of the range of X
  • sl2dpcaex SL2DPCAEX Learns Extended 2D PCA on a set of matrix samples
  • slpartitionpca SLPARTITIONPCA Performs Partition-based PCA and saves the models
  • sldrawellipse SLDRAWELLIPSE Draws an ellipse on current axis

SOURCE CODE ^

0001 function [evals, evecs] = slsymeig(A, k, ord)
0002 %SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
0003 %
0004 % $ Syntax $
0005 %   - evals = slsymeig(A)
0006 %   - [evals, evecs] = slsymeig(A)
0007 %   - ... = slsymeig(A, k)
0008 %   - ... = slsymeig(A, k, ord)
0009 %
0010 % $ Arguments $
0011 %   - A:        the target symmetrix matrix to be decomposed
0012 %   - k:        the number of eigenvalues to be solved
0013 %   - ord:      the order of the sorting ('ascend' | 'descend')
0014 %               default = 'descend';
0015 %   - evals:    the column vector of eigenvalues of A (in descending order)
0016 %   - evecs:    the matrix of eigenvectors of A
0017 %
0018 % $ Description $
0019 %   - evals = slsymeig(A) computes only the eigenvalues of symmetric
0020 %     matrix A.
0021 %
0022 %   - [evals, evecs] = slsymeig(A) computes both the eigenvalues and
0023 %     eigenvectors of matrix A. If A is a d x d matrix, then evals
0024 %     is a d x 1 column vector with the eigenvalues in descending order.
0025 %     evecs is a d x d matrix with each column vector corresponding to
0026 %     an eigenvalue in evals.
0027 %
0028 %   - ... = slsymeig(A, k) the user can specify the number of eigenvalues
0029 %     to preserve, if not specified, all eigenvalues will be preserved.
0030 %
0031 %   - ... = slsymeig(A, k, ord) the user can specify how to sort the
0032 %     eigenvalues. By default, they are sorted in descending order.
0033 %
0034 % $ History $
0035 %   - Created by Dahua Lin on Apr 21, 2006
0036 %   - Modified by Dahua Lin on Sep 9, 2006
0037 %       - The user now can specify the number of eigenvalues to preserve
0038 %         if necessary.
0039 %       - An auto-selection scheme, which will use eigs when k is small
0040 %         in order to achieve higher efficiency.
0041 %
0042 %
0043 
0044 %% parse and verify input arguments
0045 
0046 [d, d2] = size(A);
0047 if d2 ~= d
0048     error('sltoolbox:notsquaremat', 'The matrix A should be symmetric');
0049 end
0050 
0051 if nargin < 2 || isempty(k)
0052     k = d;
0053 else
0054     if ~isscalar(k) || k > d
0055         error('sltoolbox:invalidarg', 'k should be a scalar not larger than d');
0056     end
0057 end
0058 
0059 if nargin < 3 || isempty(ord)
0060     ord = 'descend';
0061 else
0062     if ~ismember(ord, {'descend', 'ascend'})
0063         error('sltoolbox:invalidarg', ...
0064             'The order of eigenvalues is invalid: %s', ord);
0065     end
0066 end
0067 
0068 
0069 %% compute
0070 
0071 % enforce symmetry
0072 A = 0.5 * (A + A');
0073 
0074 % decide scheme and compute
0075 if k > d / 3 && ~issparse(A)
0076     [evecs, evals] = eig(A);
0077 else
0078     switch ord
0079         case 'descend'
0080             sigma = 'LM';
0081         case 'ascend'
0082             sigma = 'SM';
0083     end
0084             
0085     opts = struct('disp', 0, 'issym', true);
0086     [evecs, evals] = eigs(A, k, sigma, opts);
0087 end
0088     
0089 evals = diag(evals);
0090 
0091 % enforce real
0092 if ~isreal(evecs)
0093     evecs = real(evecs);
0094 end
0095 
0096 if ~isreal(evals)
0097     evals = real(evals);
0098 end
0099 
0100 %% sort: make the eigenvalues in descending order
0101 
0102 [evals, si] = sort(evals, 1, ord);
0103 evecs = evecs(:, si);
0104 
0105 k0 = length(evals);
0106 if k < k0
0107     evals = evals(1:k);
0108     evecs = evecs(:, 1:k);
0109 end
0110 
0111 
0112

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com