Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

The Python Toolkit for Numerical Work

Set up the Python tools used throughout numerical methods: NumPy arrays, vectorized math, linspace, matplotlib plots and timing, with runnable examples.

You do not need a huge toolbox for numerical methods. Four things cover nearly everything in this course: plain Python for loops and functions, NumPy for arrays and fast math, Matplotlib for pictures, and SciPy for checking your answers against trusted routines. This lesson walks through the pieces you will use in every chapter.

Think of NumPy as the engine. A NumPy array stores numbers in one block of memory and applies an operation to all of them at once, which is both shorter to write and dozens of times faster than a Python loop.

Arrays and vectorized math

Create an array, then apply a function to the whole array without writing a loop. Such code is called vectorized.

Python Arrays and vectorized functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three functions create most grids you will need:

Function Meaning Example
np.linspace(a, b, n) n points including both ends linspace(0, 1, 5) gives 0, 0.25, ... 1
np.arange(a, b, h) steps of size h, end excluded arange(0, 1, 0.25) stops before 1
np.logspace(a, b, n) n points from 10^a to 10^b handy for step sizes

Prefer linspace for floating-point grids. With arange and a decimal step, rounding can add or drop the last point.

Matrices

A two-dimensional array is a matrix. You will solve linear systems and compute eigenvalues in later chapters, so get comfortable creating and slicing them.

Python Matrices, slicing and np.linalg

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that A * A multiplies element by element, while A @ A is the true matrix product. Mixing them up is the most common NumPy bug for beginners.

Functions as arguments

Numerical algorithms take a function as input: "find a root of f", "integrate f". In Python you pass functions like any other value, and lambda writes a short one inline.

Python
def endpoints(f, a, b):
    return f(a), f(b)

print(endpoints(lambda x: x**2 - 2, 0, 2))   # (-2, 2)

Write your methods so that f is a parameter. Then the same code solves any problem you hand it.

Plotting what you compute

A plot often reveals a mistake in seconds: a wrong sign, a missing root, a method that diverges. Matplotlib draws the curves. In the site's runner, figures appear below the code.

Python Plot a function and mark its root

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The curve crosses the x axis at one place. That visual check ("is there a sign change here?") is the first step of every root-finding method.

Timing and comparing

Sometimes you want to know which implementation is faster. Time a few repetitions with time.perf_counter.

Python Loop versus vectorized: a timing test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The exact times depend on the machine, but vectorized code wins by a wide margin. Note np.allclose, which compares floating-point arrays with a tolerance. Never test floats with ==; you will see why in Chapter 2.

Testing against SciPy

Throughout the course the pattern is: write the method, then check it with SciPy. A few routines you will meet again:

Task SciPy routine
Root of one equation scipy.optimize.brentq, newton
Linear system scipy.linalg.solve
Interpolation scipy.interpolate.interp1d, CubicSpline
Integration scipy.integrate.quad, solve_ivp
Minimization scipy.optimize.minimize

If you want a gentler introduction to these libraries, see NumPy and SciPy.

Everything in this course runs inside your browser with Python, NumPy and SciPy. You do not need to install anything.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a * b do when a and b are NumPy arrays of the same shape?

    The * operator is element-wise. Use @ (or np.dot) for the matrix product.
  2. 2. Why is linspace usually safer than arange for floating-point grids?

    arange with a decimal step can include or miss the end value because of rounding. linspace fixes the count.
  3. 3. How should you compare two floating-point arrays for near equality?

    Floating-point results differ by tiny rounding errors, so compare with a tolerance using np.allclose.
  4. 4. Why pass the function f as a parameter to a numerical method?

    Methods like bisection or trapezoid integration only need to evaluate f, so any f can be plugged in.

Summary

  • NumPy arrays store numbers compactly and support vectorized math, which is shorter and faster than loops.
  • Use linspace for grids, @ for matrix products and np.linalg.solve for systems.
  • Pass functions as arguments so methods stay general.
  • Plot early; a picture catches mistakes quickly.
  • Compare floats with np.allclose, never ==.

Next: Iteration, Convergence and Stopping Rules