I have a question about the MATLAB function eigs.
Recall that if A and B are both square, symmetric, positive definite matrices of the same size, then the set of generalized eigenvalues of the pencil (A,B) are all positive (and hence, real). When computing some of the pencil's extremal eigenvalues (let's say the smallest k), one can call either
eigs(A,B,k,'SA'); %(SA := smallest algebraic)
or
eigs(A,B,k,'SM'); %(SM := smallest magnitude)
and expect to get the same results. When I run my code, I notice that using the SM option is faster than the SA option, and sometimes significantly so. Can anyone offer an explanation as to why this is the case? I have a written a modest benchmark program that clocks the performance of eigs for both options (in this setting, k = 1; we are computing the smallest eigenvalue of the pencil (A,B)). Below is the code that I ran. Note that one can increase the value of the parameter num (but still keep it less than n) to exaggerate the results of experiment.
clc;close all;clear all;symm = @(X) (X+X')/2;sqn = 100; n = sqn^2;A = gallery('poisson',sqn);num = 2; density = num/n; rc = 0.1; B = sprandsym(n, density, rc, 1); A = symm(A); B = symm(B);fprintf('Running eigs script.\n\n');if norm(A - A',1)==0 fprintf('A is symmetric ');else fprintf('A is not symmetric ');endif eigs(A,1,'SA')>0 fprintf('and positive definite.\n');else fprintf('but not positive definite.\n');endif norm(B - B',1)==0 fprintf('B is symmetric ');else fprintf('B is not symmetric ');endif eigs(B,1,'SA')>0 fprintf('and positive definite.\n\n');else fprintf('but not positive definite.\n\n');end k = 1;tic; e1 = eigs(A,B,k,'SA'); t1 = toc;tic; e2 = eigs(A,B,k,'SM'); t2 = toc;fprintf('The smallest (algebraic) eigenvalue of the matrix pencil (A,B) is %f. Elapsed time: %f.\n',e1,t1);fprintf('The smallest (magnitude) eigenvalue of the matrix pencil (A,B) is %f. Elapsed time: %f.\n',e2,t2);
Best Answer