5.4.8 · D3Scientific Computing (Python)

Worked examples — SciPy — overview of submodules

3,614 words16 min readBack to topic

The scenario matrix

Before examples, we list every case class a SciPy question can throw at you. Each row is a "cell". Every cell is covered by at least one worked example below.

Let us build the vocabulary once so no symbol is unexplained.


C1 — Clean positive input

The curve never dips below the axis on , so all the area counts as positive. That is the "clean" cell. Next we break that assumption.


C2 — Sign flip: the curve goes negative

The figure below is the whole argument in one picture — read it before the steps. The lavender curve is . Everything shaded coral sits below the horizontal axis (counts as negative area); everything shaded mint sits above it (counts as positive area). Notice the two shaded blobs are the same size — that equal-and-opposite pairing is exactly why the answer will be zero.

Figure — SciPy — overview of submodules
Figure C2 — The coral blob on (negative area) and the mint blob on (positive area) are mirror images of equal size, so the signed total is .


C3 — Multiple valleys: the optimizer can land in the wrong one

First, one word we lean on all through this example.

Now study the figure — it is the punchline. There are two valleys; a ball placed on the left rolls into the deep one, a ball placed on the right rolls into the shallow one.

Figure — SciPy — overview of submodules
Figure C3 — Two valleys of . Mint dot = deep global min (, ); butter dot = shallow local min (, ). The coral arrows show a ball dropped at x0=-2 rolling into the LEFT valley and one dropped at x0=2 rolling into the RIGHT valley.


C4 — Degenerate input: a system with no unique solution


C5 — Limiting case: an integral out to infinity

Before the example, one symbol we are about to use as a wall.


C6 — Real-world word problem


C7 — Exam twist: the tuple gotcha


C8 — A system with all sign combinations


C9 — Statistics: the probability tail


C10 — Root finding: where does cross zero?


Recall Which submodule for each example?

Ex 1,2,5,6 use scipy.integrate ::: area / distance / improper integrals. Ex 3 uses scipy.optimize.minimize ::: find valley bottoms (local, needs x0). Ex 4,8 use scipy.linalg.solve ::: systems (Ex 4 is singular → error). Ex 9 uses scipy.stats.norm.sf ::: right-tail probability. Ex 10 uses scipy.optimize.brentq ::: root of inside a sign-change bracket.