clear all
delete result.dat
diary result.dat
diary on

n      = input('Mesh number = ');
mtx_A  = mtx_generate_2DLaplacian(n);

s     = RandStream.create('mt19937ar','seed',5489);
RandStream.setDefaultStream(s);

rhs_f     = randn(n^2,1);
kk        = 1;
stop_flag = 0;
while ( kk <= 100 && stop_flag == 0 )
    vec_u = randn(n^2,1);
    vec_v = randn(n^2,1);
    A_inv_u = mtx_A \ vec_u;
    if ( abs(vec_v'*A_inv_u+1) > 1.0e-10 )
        stop_flag = 1;
    else
        kk        = kk + 1;
    end
end

tic;
sol_d      = ( mtx_A + vec_u * vec_v' ) \ rhs_f; 
toc_direct = toc;

fprintf('  CPUTime of full inverse     = %14.4e \n', toc_direct); 

tic;
A_inv_u = mtx_A \ vec_u;
A_inv_b = mtx_A \ rhs_f;
tmp     = vec_v' * A_inv_b / (1 + vec_v' * A_inv_u);
sol_s   = A_inv_b - tmp * A_inv_u;  
toc_SM  = toc;

fprintf('  CPUTime of Sherman-Morrison = %14.4e \n', toc_SM); 

diary off