use of svds with function handles
1 view (last 30 days)
I want to do singular value decomposition of a matrix A, which is very big but simply structured. Specifically, it is a rank-2 matrix plus a diagonal matrix. I just need the first few singular values. So I want to call "svds", and I do not want to construct A explicitly as a matrix, as that is too costy in memory. The idea is to write a function to perform the multiplication x---> A*x
This is the nested function for the multiplication:
function y = fun_A_times_x(x)
y = sum(phi2.*x)*kphi1 + sum(phi1.*x)*kphi2...
+ sum(kphi1.*x)*phi2 + sum(kphi2.*x)*phi1...
+ diagonal.*x - E*sum(phi1.*x)*phi2 - E* sum(phi2.*x)*phi1;
How should I write the svds part?
myfun = @fun_A_times_x ;
[UU,SS,VV] = svds(@(x) myfun, [dim dim], 2);
This does not work.
I still do not understand the mechanism of the function handle. Can anyone help me out? My matlab is R2017a.
Steven Lord on 5 Nov 2021
When in doubt, check the documentation page to see if there's an example you can adapt. In this case there is, "Largest Singular Values Using Function Handle". You can also look at the description of the Afun input argument on that page to determine what behavior your value for that input is expected to satisfy.
Your function needs to accept two input arguments: a vector and a flag that is either 'transp' or 'notransp'. The flag value svds passes into your function determines what it needs to compute and return to svds.
In the code you posted, once you modify fun_A_times_x to accept two inputs and return the appropriate result based on the flag value if fun_A_times_x requires only the two input arguments with which svds will call it you can just pass a function handle to it as the first input argument.
[UU,SS,VV] = svds(@fun_A_times_x, [dim dim], 2);
In the example in the documentation B, C, and n are being passed in as additional parameters using the anonymous function approach described on this documentation page. Since you mentioned fun_A_times_x is a nested function it seems you're using the nested function approach also described on that documentation page and so you won't need those additional parameters. Just modify your fun_A_times_x so it works with the two-input syntax and it should work.