I need to test if one variance matrix is diagonal. If not, I'll do Cholesky LDL decomposition. But I was wondering which would be most reliable and fastest way to test is matrix diagonal? I'm using Fortran.
First thing that come to my mind is to take sum of all elements of matrix, and substract diagonal elements from that sum. If the answer is 0, matrix is diagonal. Any better ideas?
In Fortran I'll write
!A is my matrix
k=0.0d0
do i in 1:n #n is the number of rows/colums
k = k + A(i,i)
end do
if(abs(sum(A)-k) < epsilon(k)*sum(A)) then
#do cholesky LDL, which I have to write myself, haven't found any subroutines for that in Lapack or anywhere else
end if