Write helper function to test if a matrix is diagonal.
ramhiser opened this issue · comments
John Ramey commented
John Ramey commented
Here is an example, where we test if the Frobenius norm of the difference between the matrix consisting of the diagonal elements of x
and x
itself is less than some tolerance.
is_diagonal <- function(x, tol = 1e-8) {
norm(diag(diag(x)) - x, "F") < tol
}