Programming and Computation · Module 8 of 10
Numerical Python with Arrays
Once you handle whole datasets, plain lists get slow and clumsy. NumPy gives you the array: apply a formula to a thousand values at once, with no loop, and summarise them in one call. This is the workhorse of engineering computation.
Readiness check
This module handles whole datasets at once. Tick only what you can do closed-notes.
- Use lists and indexing from Module 7.
- Recall element-wise operations on a set of numbers.
- Recall a dot product or a weighted sum.
- Recall evenly spaced points along an axis.
- Recall importing a module from Module 6.
The core idea
A NumPy array holds many numbers and lets you compute on all of them at once. Arithmetic is element-wise, so a * 2 doubles every entry without a loop. Array methods like mean and sum summarise in one call, dot gives a weighted sum, and linspace builds evenly spaced values.
Lists are fine for a handful of values, but engineering data comes in hundreds or thousands, and NumPy is built for exactly that. You create an array with np.array([2, 4, 6, 8]), importing the library once as import numpy as np. The defining feature is vectorization: an operation applies to every element at once, so a * 2 is [4, 8, 12, 16] and a + b adds two arrays element by element, all without writing a loop. This is both faster and clearer than looping, because the code reads like the maths. Arrays carry summary methods, a.mean(), a.sum(), a.max(), that reduce a whole array to one number, so a mean is a single call. For linear-algebra style work, np.dot(a, b) forms the sum of products, the weighted sum or projection at the heart of many engineering formulas. And when you need a grid of points, say to evaluate a function along a beam, np.linspace(0, 10, 5) gives five evenly spaced values from 0 to 10. NumPy is the foundation every later numerical-methods, data, and simulation tool builds on, so getting comfortable with arrays now pays off through the rest of the roadmap.
The skills, taught in order
Five skills make array computation natural.
8.1 Creating arrays
import numpy as np, then np.array([2, 4, 6, 8]) makes an array from a list. np.zeros(n) and np.ones(n) make arrays of a given size to fill in.
8.2 Vectorized arithmetic
Operations apply element-wise: a * 2, a + b, a ** 2. No loop is needed, and the code reads like the mathematics it represents.
8.3 Array summaries
Reduce an array to one number with a.mean(), a.sum(), a.max(), and a.min(). These are the array versions of the built-ins from Module 7.
| Operation | Example | Result |
|---|---|---|
| Element-wise | np.array([2,4,6,8]) * 2 | [4, 8, 12, 16] |
| Mean | np.array([2,4,6,8]).mean() | 5.0 |
| Even spacing | np.linspace(0, 10, 5) | [0, 2.5, 5, 7.5, 10] |
A few array moves that replace many lines of loop code.
8.4 The dot product
np.dot(a, b) forms the sum of element-wise products, a weighted sum or projection: with [1,2,3] and [4,5,6] it is 4 + 10 + 18 = 32. This underlies much of engineering linear algebra.
8.5 Building grids with linspace
np.linspace(start, stop, n) returns n evenly spaced values including both ends. It is how you build the x-axis to evaluate a function along, ready for plotting (Module 9).
Engineering connection: evaluating a stress formula at 500 points along a part, or averaging a sensor array, is one vectorized line, which is why NumPy underlies every numerical-methods and simulation tool ahead.
Worked example 1: array arithmetic and mean
Double every element of an array and compute its mean and sum.
import numpy as np
a = np.array([2, 4, 6, 8])
print(a * 2)
print(a.mean(), a.sum())
Output:
[ 4 8 12 16]
5.0 20
- ProblemDouble an array and summarise it.
- Given / finda = [2, 4, 6, 8]. Find a*2, the mean, and the sum.
- ModelVectorized
a * 2; reduce withmeanandsum. - Solve
a * 2= [4, 8, 12, 16]; mean = 5.0; sum = 20. - CheckThe four values sum to 20 over 4 items, giving a mean of 5.0.
- ConclusionOne expression applies to every element, and one call summarises the whole array.
Worked example 2: dot product and linspace
Compute a weighted sum with the dot product, and build five evenly spaced points from 0 to 10.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))
print(np.linspace(0, 10, 5))
Output:
32
[ 0. 2.5 5. 7.5 10. ]
- ProblemFind the dot product of two arrays and make an evenly spaced grid.
- Given / finda = [1,2,3], b = [4,5,6]. Find the dot product and linspace(0,10,5).
- Model
np.dotsums the element-wise products;np.linspacespaces points. - Solvedot = 1×4 + 2×5 + 3×6 = 32; linspace gives [0, 2.5, 5, 7.5, 10].
- Check4 + 10 + 18 = 32, and five points across a span of 10 are 2.5 apart.
- ConclusionThe dot product is the vectorized weighted sum, and linspace builds the axis for evaluating functions.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Looping over an array | Slow, verbose code | "Can this be vectorized?" | Use an array expression like a * 2. |
| Mismatched shapes | A broadcasting or shape error | "Do the arrays line up?" | Combine arrays of compatible sizes. |
| List where array is needed | [1,2] * 2 repeats, not doubles | "Is this a list or an array?" | Make it an array first with np.array. |
| Confusing dot with element-wise | Wrong result for a weighted sum | "Sum of products, or element-wise?" | Use np.dot for the sum of products. |
Practice ladder
Make the array [3, 6, 9] and compute its mean.
Show answer
np.array([3, 6, 9]).mean() is 6.0.
Convert an array of forces in newtons to kilonewtons in one line.
Show answer
forces / 1000 divides every element, giving the kilonewton array with no loop.
Build 11 evenly spaced positions from 0 to 1 m and evaluate a stress that varies as position squared.
Show answer
x = np.linspace(0, 1, 11), then sigma = k * x ** 2 evaluates the whole array at once.
Take an array of sensor readings and report the mean, maximum, and standard deviation using array methods.
What good work looks like
data.mean(), data.max(), and data.std(), each printed with a label and units. A good answer uses array methods rather than manual loops.
Working with AI, and proving it yourself
Use AI as a tutor, not a black box
Portfolio task
Take a loop-based calculation from an earlier module and rewrite it as a single vectorized NumPy expression, checking the result matches.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. How do you import NumPy?
import numpy as np.
2. What does vectorized mean?
An operation applies to every element at once, with no loop.
3. What does np.dot compute?
The sum of element-wise products, a weighted sum.
4. What does linspace give?
Evenly spaced values including both endpoints.
5. Why use arrays over lists for data?
Vectorized, fast, and readable for whole-dataset math.
Textbook mapping
This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| NumPy arrays and vectorization | Fangohr, chapter on Numerical Python (numpy) |
| Array creation and operations | Fangohr, Numerical Python, array basics |
| Why arrays for computation | Fangohr, Chapter 1, computational modelling |
Chapter titles refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.