Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
A matrix inverse is not made by replacing each nonzero entry x with 1/x. For A = [[4, 7], [2, 6]], that guess would be [[1/4, 1/7], [1/2, 1/6]]. The entries may all be nonzero, but that says nothing about whether the matrix is an inverse. The real test is multiplication: a proposed inverse A^-1 must make A * A^-1 equal the identity matrix.
A Entry-wise reciprocal guess
[[4, 7], [[1/4, 1/7],
[2, 6]] [1/2, 1/6]]Check the first row and first column of the product. Their row-column product is 4 * 1/4 + 7 * 1/2 = 1 + 7/2 = 9/2, but the top-left entry of the identity matrix must be 1. One failed product is enough to reject the candidate. The inverse must be chosen so every row-column product gives the matching identity entry: 1 on the main diagonal and 0 elsewhere.
Which criterion correctly identifies the inverse of A?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For a 2 x 2 matrix [[a, b], [c, d]], the determinant is det = ad - bc. When this value is nonzero, the inverse formula is A^-1 = (1 / det) * [[d, -b], [-c, a]]. For A = [[4, 7], [2, 6]], det(A) = 4 * 6 - 7 * 2 = 24 - 14 = 10.
The determinant controls whether the formula can be used. Here, 1 / det means 1 / 10, so each entry in the numerator matrix can be scaled. If the determinant were zero, division by zero would be impossible, and the matrix would have no inverse. A zero determinant is therefore a gate: reject the matrix before attempting the division.
Apply the formula directly to A. First swap the diagonal entries 4 and 6. Then keep the off-diagonal entries in their positions and change their signs, turning 7 into -7 and 2 into -2. The numerator becomes [[6, -7], [-2, 4]]. Finally divide every entry by the determinant 10.
A Swap and sign changes Divide by 10
[[4, 7], [[6, -7], [[6/10, -7/10],
[2, 6]] [-2, 4]] [-2/10, 4/10]]
[[3/5, -7/10],
[-1/5, 2/5]]The result is A^-1 = [[3/5, -7/10], [-1/5, 2/5]]. Verify it by multiplying A^-1 by A. The top-left product is 3/5 * 4 + (-7/10) * 2 = 12/5 - 14/10 = 1. The top-right product is 3/5 * 7 + (-7/10) * 6 = 21/5 - 42/10 = 0. The other row-column products similarly give 0 and 1, so the product is the identity matrix.
After the sign change and division by det(A) = 10, what is the top-right entry of A^-1?
A^-1 = [[3/5, ?], [-1/5, 2/5]]Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A program should calculate the determinant before it constructs the inverse. For A, compute det = 4 * 6 - 7 * 2, reject the input if det == 0, and then store the inverse entries as double values. If both operands of 7 / 10 are integers, integer division produces 0 before the result is assigned anywhere. Writing 7.0 / det or storing the matrix values as double makes the result -0.7 instead.
double a = 4, b = 7;
double c = 2, d = 6;
double det = a * d - b * c;
if (det == 0) {
// No inverse exists.
} else {
double inv00 = d / det;
double inv01 = -b / det;
double inv10 = -c / det;
double inv11 = a / det;
}If you multiply the computed inverse by A to check the identity product, compare each result with a small tolerance instead of demanding exact equality. Floating-point values such as 0.6 and -0.7 are approximations, so a product that should be 0 can be stored as a tiny value such as 0.0000000001. The swap-and-sign shortcut belongs specifically to 2 x 2 matrices. It is not a general rule for larger matrices, where a different inverse method is required.