A76 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A7 · Lesson 6 · Lab: vectors and matrices in NumPy

Vectors, matrices and two-ports in code

Everything so far fits on paper: dot products, rotations, cascaded two-ports, areas. Real problems have hundreds of entries, and NumPy is where engineers do this work. In this lab you'll write the first five lessons as code: the angle between vectors, rotation matrices that compose, a ladder of two-ports whose gains follow the Fibonacci numbers, and the area a matrix gives the unit square. Along the way you'll meet the one NumPy habit that catches everyone: * multiplies entry by entry, and @ is matrix multiplication.

  • The arrows are 𝐯=(3,1) and 𝐰, whose angle and length you set.
  • The readouts give 𝐯⋅𝐰 and the angle between them, computed the way your code will compute them.
  • Turn 𝐰 until the dot product is zero. The angle reads 90°.

Predict

A and B are 2 × 2 NumPy arrays. What does A * B compute?

Name it

Arrays

  • np.array([3, 4]) is a vector and np.array([[1, 2], [3, 4]]) a matrix; A.shape gives its size.
  • A @ B is the matrix product, A @ v a matrix times a vector, and v @ w a dot product.
  • A * B, A + B and A ** 2 act entry by entry.
  • A.T is the transpose, np.eye(n) the identity, np.linalg.norm(v) the length and np.linalg.det(A) the determinant.

Comparing floating-point results

Computed matrices are rarely exact: cos⁡(π/3) comes out as 0.500 000 000 000 000 1. Compare with np.allclose(A, B), which allows a relative error of about 10−5 plus a tiny absolute one, never with ==.

Angles and areas

  • cos⁡θ=𝐯⋅𝐰|𝐯||𝐰|, then math.acos. Rounding can push the ratio slightly past 1 for parallel vectors, so clip it to [−1,1] in careful code.
  • The shoelace formula gives the area of a polygon with corners (xi,yi) in order: 12|∑(xiyi+1−xi+1yi)|, wrapping round at the end. Applied to the image of the unit square, it should equal |det⁡M|.

Worked example

Compute the angle between (3,4) and (4,3) in NumPy.

Show the solution
  1. Compute it in NumPy.
import math
import numpy as np

v, w = np.array([3, 4]), np.array([4, 3])
c = v @ w / (np.linalg.norm(v) * np.linalg.norm(w))
print(math.degrees(math.acos(c)))   # 16.26
  1. Check by hand. The dot product is 24 and both lengths are 5, so cos⁡θ=0.96 and θ=16.26°, as in lesson 1.

Answer the angle is 16.26°.

Worked example

A ladder repeats a section of 1 kΩ in series followed by 1 mS in shunt. What is the open-circuit gain of one, two, three and four sections?

Show the solution
  1. One section's matrix. [1100001][100.0011]=[210000.0011]
  2. Multiply copies. Multiplying n copies and taking 1/𝒜 gives gains of 12, 15, 113 and 134.
  3. The pattern. The denominators 2, 5, 13 and 34 are every other Fibonacci number, and each is about 2.6 times the last.
  4. A long ladder. It attenuates by a fixed ratio per section: 1/2.618=0.382

Answer the gains are 12, 15, 113 and 134 for one, two, three and four sections.

Check

A = np.array([[a, 2], [3, b]]) with a = 2, b = 1. What is (A @ A)[0, 1]?

Check

One section has 1 kΩ in series and 0.5 mS in shunt. What is its open-circuit gain?

Check

Write angle(v, w) returning the angle between two NumPy vectors in degrees. Set theta = angle(np.array([3, 4]), np.array([4, 3])).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write rot(t), the 2 × 2 matrix that rotates by t radians. Set R60 = rot(math.pi / 6) @ rot(math.pi / 6) and check it against rot(math.pi / 3).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write series(Z) and shunt(Y) returning transmission matrices, and ladder_gain(n) returning the open-circuit gain of n sections of 1000 Ω in series then 0.001 S in shunt. Set gain = ladder_gain(n) for n = 3.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write shoelace(P) returning the area of a polygon whose corners are the columns of a 2 × k array. Map the unit square's corners with M=[a10.5b] (a = 2, b = 1) and set area = shoelace(M @ square).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why should you compare computed matrices with np.allclose rather than ==?

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.