bhylu.com

Advertisements

SciPy Linear Systems

Solve a small calibration system without forming an inverse.

How it works

A linear system relates a coefficient matrix, unknown values and a right-hand side. scipy.linalg.solve uses a suitable factorisation rather than asking you to form an inverse first. After solving, check the residual A @ x - b and consider whether the system is ill-conditioned. A small residual is useful evidence but does not by itself guarantee small error in a poorly conditioned problem.

Run the code

The complete source is checked locally and in the existing SciPy browser editor. Select Try in playground to preload it, then choose Run. Preloading does not execute the program. The browser starts an isolated runtime and does not read your personal PC files.

The reviewed local environment is Python 3.12.14, NumPy 2.5.4 and SciPy 1.18.0. The browser uses Pyodide 0.27.7, NumPy 2.0.2 and SciPy 1.14.1. Use a separate virtual environment for local work, then run python linear-systems.py. Apache, MySQL and PHP are not required. Environment setup.

Download the complete source

Example

linear-systems.py

import numpy as np
import scipy

from scipy import linalg

A = np.array([[3., 1.], [1., 2.]])
b = np.array([9., 8.])
x = linalg.solve(A, b)
np.testing.assert_allclose(x, [2., 3.], rtol=0, atol=1e-12)
assert np.linalg.norm(A @ x - b) < 1e-12
print("Solution:", [round(float(v), 6) for v in x])

Expected result

Solution: [2.0, 3.0]

The hand-check is direct: three times two plus three equals nine, and two plus twice three equals eight. This independent solution is stronger evidence than printing the solver success alone. Check shape and finite inputs before generalising the example to received data.

For precise contracts and version differences use the official SciPy documentation. Synthetic fixtures do not establish real instrument accuracy or population conclusions.