v9.1.1
Menu

Solve Linear System

mediumFREELinear Algebra BasicsNumPy Linear AlgebraNumPy Foundations

Problem Statement

Solve a system of linear equations Ax = b.

Background

A linear system Ax = b can be solved using:

  • np.linalg.solve(A, b) - direct solution
  • np.linalg.inv(A) @ b - via inverse (less efficient)

Example: 2x + y = 5, x + 3y = 7 Matrix form: [[2,1],[1,3]] @ [x,y] = [5,7]

Your Task

Write a function solve_system(A, b) that solves the linear system.

Output Format

Return a dictionary with:

  • "solution": Solution vector x (rounded to 2 decimals, as list)
  • "verification": A @ x (should equal b, rounded to 2 decimals, as list)
  • "is_correct": True if A @ x ≈ b

Example

Input
A = [[2, 1], [1, 3]], b = [5, 7]
Output
{'solution': [1.6, 1.8], 'verification': [5.0, 7.0], 'is_correct': True}
Reasoning
  • Inputs: A is a 2×22 \times 2 coefficient matrix, one row per equation: row 1 is 2x+y2x + y, row 2 is x+3yx + 3y. b holds the right-hand sides, 55 and 77. The unknown vector x=(x1,x2)x = (x_1, x_2) is what makes both equations true at once.
  • Is there a unique answer? The determinant is 2⋅3−1⋅1=5≠02 \cdot 3 - 1 \cdot 1 = 5 \neq 0, so AA is invertible and exactly one solution exists.
  • Solving: by Cramer's rule, x1=5⋅3−1⋅75=85=1.6x_1 = \frac{5 \cdot 3 - 1 \cdot 7}{5} = \frac{8}{5} = 1.6 and x2=2⋅7−5⋅15=95=1.8x_2 = \frac{2 \cdot 7 - 5 \cdot 1}{5} = \frac{9}{5} = 1.8.
  • Verification: multiply AA by that xx: 2(1.6)+1(1.8)=5.02(1.6) + 1(1.8) = 5.0 and 1(1.6)+3(1.8)=7.01(1.6) + 3(1.8) = 7.0, which reproduces b, so is_correct is True.
  • Notice: the check compares AxAx with bb up to a tiny floating-point tolerance, not exact equality. Computer arithmetic rarely lands on 55 exactly, so an exact comparison could wrongly fail. Both lists are rounded to 2 decimal places and returned as plain lists.

Constraints

  • A is square and invertible
  • Round to 2 decimal places

More from NumPy Foundations

solution.py

Test results

Sign in to run your code against the test cases.