# eig on several matrices in a vectorized manner

5 messages
Open this post in threaded view
|

## eig on several matrices in a vectorized manner

 Greetings, this is my first post in this mailing-list. I used Matlab for many years but since 2018 I switched to Octave, and plan to keep using it in my research. I have to calculate eigenvalues of multiple matrices which have all the same size (2x2,4x4,8x8, etc) and are Hermitian, repeatedly. Running this on a loop seems to me to be inefficient. I have tried to do a small benchmark test in which I compare looping, changing the size of the matrices and the number of matrices; against generating a supermatrix by using blkdiag, and taking the eigenvalues all at once. The blkdiag approach is only faster when the size of the matrices are 2x2, but for larger systems it becomes quite slower than looping. I imagined this has to do with the use of memory by the large block supermatrices. So I tried to convert the individual matrices to sparse before making the block supermatrix. This has not improved the timings for the block approach. Perhaps I'm doing something wrong, but in any case, is there some way of broadcasting the eig function. I imagine I can write the Cholesky algorithm so that it acts on the elements of multiple matrices at the same time, and taylor that to my problem. But if there is some solution already implemented I think it would be better. Thank you very much! Best regards, Nicolás Neuman -- Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: eig on several matrices in a vectorized manner

 Hi, It would be useful to have a simple code to test your results. Also, how many matrices do you need to process? for a few hundrerds, looping is not the source of cost, it is just the cost of eig itself. I do not see much point on vectorizing eig (maybe I can be convinced otehrwise) so I think the options for speed up here are: 1. paralelization 2. sending the loop a level bellow e.g. cellfun (do not expect huge improvements) Alternatively, (and risky) if your matrix set is homogenous in size (ie.e. all matrice have the same size), try to reduce the set into a basis of matrices and try exploit Horn's conjecture[1]. [1]: https://terrytao.wordpress.com/tag/schur-horn-inequalities/On Fri, Oct 19, 2018 at 5:02 PM niconeuman <[hidden email]> wrote: > > Greetings, > this is my first post in this mailing-list. I used Matlab for many years but > since 2018 I switched to Octave, and plan to keep using it in my research. > I have to calculate eigenvalues of multiple matrices which have all the same > size (2x2,4x4,8x8, etc) and are Hermitian, repeatedly. > Running this on a loop seems to me to be inefficient. I have tried to do a > small benchmark test in which I compare looping, changing the size of the > matrices and the number of matrices; against generating a supermatrix by > using blkdiag, and taking the eigenvalues all at once. > The blkdiag approach is only faster when the size of the matrices are 2x2, > but for larger systems it becomes quite slower than looping. I imagined this > has to do with the use of memory by the large block supermatrices. So I > tried to convert the individual matrices to sparse before making the block > supermatrix. This has not improved the timings for the block approach. > Perhaps I'm doing something wrong, but in any case, is there some way of > broadcasting the eig function. > I imagine I can write the Cholesky algorithm so that it acts on the elements > of multiple matrices at the same time, and taylor that to my problem. But if > there is some solution already implemented I think it would be better. > Thank you very much! > Best regards, > Nicolás Neuman > > > > -- > Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html> >
Open this post in threaded view
|

## Re: eig on several matrices in a vectorized manner

 Hi, thank you for the prompt response!The problem I want to apply this for is simulation of magnetic resonance spectra. Basically the matrices are functions of a magnetic field B, and I have to find the values B0 for which energy (eigenvalue) differences in the energy matrix match a certain value (microwave frequency). Finding of the values B0 is done adaptively, by dividing the interval [Bmin,Bmax] in halfs, and finding by approximation the solutions. My algorithm is probably very far from efficient at this point, but I estimate I need to diagonalize tens of matrices per cycle of the adaptive scheme.It is highly possible that the actual diagonalization is not the time consuming part, but then the short loops I run to find the eigenvalue differences are what is slowing everything down. I should rethink the whole thing.Anyway, below I send the code for the small benchmark I made. Perhaps there is a much faster way of doing this.Thank you very much!Best,Nicolas%beginclear all, close allmore offdisp(['Size of each matrix: ',' Number of matrices: ',' Loop to block ratio: ']); for pMatSize = [2,4,8,16,32,64,128]    for nMat = 2:20        SizeMatrices = pMatSize;        Nmatrices = nMat;        eigA = zeros(SizeMatrices,Nmatrices);        timeLoop = zeros(Nmatrices,1);        augA = cell(Nmatrices,1);        for k = 1:Nmatrices                    tmpA = rand(SizeMatrices,SizeMatrices);            augA{k} = tmpA'*tmpA;        end                tic        for k = 1:Nmatrices                eigA(:,k) = eig(augA{k});           end        timeLoop = toc;                        augAmat = blkdiag(augA{:});        tic        eigaugA = eig(augAmat);        timeBlock = toc;                disp([num2str(SizeMatrices) '        ' num2str(Nmatrices) '        ' num2str(timeLoop/timeBlock)]);     endend%endOn Sun, Oct 21, 2018 at 11:06 PM Juan Pablo Carbajal <[hidden email]> wrote:Hi, It would be useful to have a simple code to test your results. Also, how many matrices do you need to process? for a few hundrerds, looping is not the source of cost, it is just the cost of eig itself. I do not see much point on vectorizing eig (maybe I can be convinced otehrwise) so I think the options for speed up here are: 1. paralelization 2. sending the loop a level bellow e.g. cellfun (do not expect huge improvements) Alternatively, (and risky) if your matrix set is homogenous in size (ie.e. all matrice have the same size), try to reduce the set into a basis of matrices and try exploit Horn's conjecture[1]. [1]: https://terrytao.wordpress.com/tag/schur-horn-inequalities/ On Fri, Oct 19, 2018 at 5:02 PM niconeuman <[hidden email]> wrote: > > Greetings, > this is my first post in this mailing-list. I used Matlab for many years but > since 2018 I switched to Octave, and plan to keep using it in my research. > I have to calculate eigenvalues of multiple matrices which have all the same > size (2x2,4x4,8x8, etc) and are Hermitian, repeatedly. > Running this on a loop seems to me to be inefficient. I have tried to do a > small benchmark test in which I compare looping, changing the size of the > matrices and the number of matrices; against generating a supermatrix by > using blkdiag, and taking the eigenvalues all at once. > The blkdiag approach is only faster when the size of the matrices are 2x2, > but for larger systems it becomes quite slower than looping. I imagined this > has to do with the use of memory by the large block supermatrices. So I > tried to convert the individual matrices to sparse before making the block > supermatrix. This has not improved the timings for the block approach. > Perhaps I'm doing something wrong, but in any case, is there some way of > broadcasting the eig function. > I imagine I can write the Cholesky algorithm so that it acts on the elements > of multiple matrices at the same time, and taylor that to my problem. But if > there is some solution already implemented I think it would be better. > Thank you very much! > Best regards, > Nicolás Neuman > > > > -- > Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html > > -- Dr. Nicolas NeumanInstitut für Chemie und Biochemie, Anorganische ChemieFreie Universität Berlin,Fabeckstraße 34–36, 14195, Berlin (Germany)Instituto de Desarrollo Tecnológico para la Industria Química - INTECUNL-CONICETCCT-CONICET Santa FeS3000ZAA Santa Fe - Santa Fe - ArgentinaJefe de Trabajos PrácticosDepartamento de FísicaFac. de Bioquímica y Ciencias BiológicasUniversidad Nacional del LitoralCiudad Universitaria - Paraje El PozoS3000ZAA Santa Fe - Santa Fe - Argentina