Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-308

Diagonal Matrix Check

Problem

Check whether a square matrix is diagonal.

Return True when every off-diagonal entry has absolute value below 1e-9. Diagonal entries themselves can be anything, including zero.

Hint

Only entries where row index differs from column index need checking.

Test cases

case 1
in: [[[1,0],[0,2]]]
expected: true
case 2
in: [[[1,0],[0,1]]]
expected: true
case 3
in: [[[1,1],[0,1]]]
expected: false
case 4
in: [[[0,0],[0,0]]]
expected: true
case 5
in: [[[1,1e-10,0],[0,2,0],[0,0,3]]]
expected: true
Starter codePython
def diagonal_matrix_check(A):
    # Your code here
    pass

5 test cases · run your code against them in the editor