Write a Python function that calculates the inverse of a 2x2 matrix. The inverse of a matrix A is another matrix A_inv such that A * A_inv = I (the identity matrix).\n\nFor a 2x2 matrix [[a, b], [c, d]], the inverse exists only if the determinant (ad - bc) is non-zero.\n\nReturn None if the matrix is not invertible (i.e., when the determinant equals zero).
Examples
Example 1:
Input:
matrix = [[4, 7], [2, 6]]Output:
[[0.6, -0.7], [-0.2, 0.4]]Explanation: For matrix [[a, b], [c, d]] = [[4, 7], [2, 6]]:
1. Calculate determinant: det = ad - bc = 4×6 - 7×2 = 24 - 14 = 10
2. Since det ≠ 0, the matrix is invertible
3. Apply formula: A⁻¹ = (1/det) × [[d, -b], [-c, a]] = (1/10) × [[6, -7], [-2, 4]] = [[0.6, -0.7], [-0.2, 0.4]]
Starter Code
def inverse_2x2(matrix: list[list[float]]) -> list[list[float]] | None:
"""
Calculate the inverse of a 2x2 matrix.
Args:
matrix: A 2x2 matrix represented as [[a, b], [c, d]]
Returns:
The inverse matrix as a 2x2 list, or None if the matrix is singular
(i.e., determinant equals zero)
"""
# Your code here
passPython3
ReadyLines: 1Characters: 0
Ready