check if a square matrix is diagonal

*To*: mathgroup at smc.vnet.net*Subject*: [mg115286] check if a square matrix is diagonal*From*: benyaya <xiaochu at gmail.com>*Date*: Thu, 6 Jan 2011 02:06:17 -0500 (EST)

What I try to do is extract the diagonal, subtract if from the matrix, then compare the new matrix with 0. My code doesn't work out though, can anyone help? thanks a lot. checkIfDiagonalMatrix[m_] = Module[{d, mtemp}, d = Dimensions[m]; mtemp = DiagonalMatrix[Diagonal[m]] - m; If[mtemp == Table[Table[0, {i, 1, d}], {i, 1, d}], True, False] ]