Paul's Python CoursePPC

Project 10 of 12, for Riya

Linear Algebra Lab

A linear algebra lab for Riya in two parts: a month of wind from the Archive's vane as arrows, for her wind print and for Neil's headwind and crosswind on the runway; then a real photograph of a lighthouse as a matrix, mirrored, turned and blurred like her filters, with a rotation matrix to finish.

About 2 hours, in 21 steps.

Sign up free to open itLog in

Act 5 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • RiyaI'm making a print of January's wind, with an arrow for each day from the vane outside the Archive.
  • YahyaThe old summary script averages the bearings and says January came from the south, when the vane pointed north all month.
  • NeilAnd the flying field's runway points at forty degrees, so I'd like the headwind and the crosswind.
  • PaulBearings go round in a circle and plain numbers don't, which is why you turn them into arrows.
  • RiyaAfter that I want to do a photograph of a lighthouse as numbers, to see what my filters really do.

vane.csv has one row per day of January 2024: the date, the bearing the wind came from (degrees clockwise from north) and its speed in knots. load_vane and to_arrows are written: to_arrows gives one arrow per day, (east, north) in knots, pointing where the wind came from.

Write five functions that work on the arrows, with no loops:

  • to_bearings(arrows): the compass bearing of each arrow, from 0 up to 360.
  • prevailing(arrows): the average wind, (bearing, speed): the direction and length of the mean arrow.
  • steadiness(arrows): the mean arrow's length divided by the mean speed.
  • angle_between(a, b): the angle between two arrows in degrees, from 0 to 180.
  • along_and_across(arrows, heading): for a plane on that heading, one row per wind: the headwind (below 0 if the wind is behind) and the crosswind from the right (below 0 if it's from the left).

Then the second experiment, in lighthouse.py. lighthouse.csv is Riya's practice photograph, a real lighthouse in North Carolina, as a matrix: 64 rows of 48 brightnesses, 0 black to 255 white. load_image is written. Write seven more functions, each giving back a new array:

  • mirror(image): each row back to front. upside_down(image): the rows in reverse order.
  • quarter_turn(image): turned a quarter turn clockwise.
  • brighten(image, amount): every pixel amount brighter, kept between 0 and 255.
  • blur(image): each pixel the average of the 3 by 3 block around it, with the edge pixels repeated to fill the block.
  • rotation(degrees): the 2 by 2 matrix that turns an arrow degrees anticlockwise.
  • turn_points(points, degrees): points, one (x, y) per row, turned degrees anticlockwise about (0, 0), by multiplying by the rotation matrix with @.
What it prints
January 2024: 31 days
Average of the bearings: 197 degrees
Prevailing wind: from 350 degrees at 11.4 knots
Steadiness: 0.81
Biggest swing overnight: 120 degrees, 2024-01-28 to 2024-01-29
Runway 04: worst headwind 25.1 knots on 2024-01-26
Runway 04: worst crosswind 23.1 knots on 2024-01-15

Cape Lookout: 64 rows by 48 columns, brightness 0 to 229
Turned a quarter: 48 rows by 64 columns
Brightened by 60: average 211.2, against 154.5
Blurred: brightness 8 to 226
House turned 30 degrees and back: True
Roof, turned 90 degrees: [-12.  10.]

What you learn

What's in it

  1. Arrows, not bearings
  2. Two days of wind
  3. From a bearing to an arrow and back
  4. Due south
  5. The January vane
  6. How much lies along
  7. Dead ahead
  8. Many dot products at once
  9. The storm score
  10. How Paul would do it
  11. Fix Yahya's version
  12. Make the wind report
  13. A picture is a matrix
  14. A picture is a matrix
  15. A matrix that turns
  16. Half a turn
  17. Averaging the neighbours
  18. A bright corner
  19. Make the picture lab
  20. Ship it