Loading content...
In numerical linear algebra, solving systems of linear equations is one of the most essential computational tasks. A powerful classical technique for finding the exact solution to a square system Ax = b leverages the determinant properties of matrices—known as Cramer's Method or the determinant substitution technique.
Given a square coefficient matrix A of dimensions n × n and a constants vector b of length n, the goal is to find the solution vector x such that the matrix equation Ax = b is satisfied.
The Determinant Substitution Approach:
For each unknown xᵢ in the solution vector, we construct a modified matrix Aᵢ by replacing the i-th column of the original matrix A with the constants vector b. The solution for each variable is then computed as:
$$x_i = \frac{\det(A_i)}{\det(A)}$$
where:
Singular Matrix Condition:
If the determinant of the coefficient matrix A equals zero (det(A) = 0), the matrix is said to be singular or non-invertible. In this case, the system either has:
When the matrix is singular, there is no unique solution, and the function should return -1 to indicate this condition.
Your Task:
Implement a function that solves a system of linear equations using the determinant-based substitution method. Your solution should:
A = [[2, -1, 3], [4, 2, 1], [-6, 1, -2]]
b = [5, 10, -3][0.1667, 3.3333, 2.6667]We solve the 3×3 system of linear equations: 2x₁ - x₂ + 3x₃ = 5 4x₁ + 2x₂ + x₃ = 10 -6x₁ + x₂ - 2x₃ = -3
Step 1: Compute det(A) Using cofactor expansion or the rule of Sarrus for 3×3 matrices, we calculate det(A) = 18
Step 2: Compute det(A₁) by replacing column 1 with b: A₁ = [[5, -1, 3], [10, 2, 1], [-3, 1, -2]] det(A₁) = 3
Step 3: Compute det(A₂) by replacing column 2 with b: A₂ = [[2, 5, 3], [4, 10, 1], [-6, -3, -2]] det(A₂) = 60
Step 4: Compute det(A₃) by replacing column 3 with b: A₃ = [[2, -1, 5], [4, 2, 10], [-6, 1, -3]] det(A₃) = 48
Step 5: Calculate solutions x₁ = det(A₁)/det(A) = 3/18 = 0.1667 x₂ = det(A₂)/det(A) = 60/18 = 3.3333 x₃ = det(A₃)/det(A) = 48/18 = 2.6667
The solution vector is [0.1667, 3.3333, 2.6667].
A = [[1, 2], [3, 4]]
b = [5, 11][1.0, 2.0]We solve the 2×2 system: x₁ + 2x₂ = 5 3x₁ + 4x₂ = 11
Step 1: Compute det(A) det(A) = (1)(4) - (2)(3) = 4 - 6 = -2
Since det(A) ≠ 0, a unique solution exists.
Step 2: Compute det(A₁) by replacing column 1 with b: A₁ = [[5, 2], [11, 4]] det(A₁) = (5)(4) - (2)(11) = 20 - 22 = -2
Step 3: Compute det(A₂) by replacing column 2 with b: A₂ = [[1, 5], [3, 11]] det(A₂) = (1)(11) - (5)(3) = 11 - 15 = -4
Step 4: Calculate solutions x₁ = det(A₁)/det(A) = -2/-2 = 1.0 x₂ = det(A₂)/det(A) = -4/-2 = 2.0
The solution vector is [1.0, 2.0].
A = [[1, 2], [2, 4]]
b = [3, 6]-1We attempt to solve the 2×2 system: x₁ + 2x₂ = 3 2x₁ + 4x₂ = 6
Step 1: Compute det(A) det(A) = (1)(4) - (2)(2) = 4 - 4 = 0
Since det(A) = 0, the coefficient matrix is singular (non-invertible).
Examining the equations: the second equation is exactly twice the first equation (2x₁ + 4x₂ = 6 is equivalent to x₁ + 2x₂ = 3). This means the rows are linearly dependent.
The system has infinitely many solutions along the line x₁ = 3 - 2x₂, but no unique solution exists.
Therefore, we return -1 to indicate the absence of a unique solution.
Constraints