Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-010

2x2 Matrix Inverse

Problem

Compute the inverse of a 2x2 matrix. If the determinant is 0, return None.

inv(A) = (1/det) * [[A[1][1], -A[0][1]], [-A[1][0], A[0][0]]]

Hint

Swap the diagonal elements, negate the off-diagonal, divide by det.

Test cases

case 1
in: [[[4,7],[2,6]]]
expected: [[0.6,-0.7],[-0.2,0.4]]
case 2
in: [[[1,2],[3,4]]]
expected: [[-2,1],[1.5,-0.5]]
case 3
in: [[[1,0],[0,1]]]
expected: [[1,0],[0,1]]
case 4
in: [[[1,2],[2,4]]]
expected: null
Starter codePython
def inverse_2x2(A):
    # Your code here
    pass

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