← Home

Completing the Udacity Linear Algebra Course in Go

I’ve just finished working through the Udacity course over at [0]

The idea of the course is to not just learn the mechanics of linear algebra, but also to understand how to write your own linear algebra program code.

The course is taught in Python, but the instructor said to use any language, so I implemented everything in Go.

It took me a couple of months to complete the course in my spare time.

One of the problems I faced with using Go is that the quizzes at the end of each section expected outputs to be rounded to 3 decimal places. Weirdly, Go doesn’t have a math.Round function so I ended up making my own [1]

There were also a few bugs in the course materials and questions, but the discussion forums were helpful. Grapher on OSX was also helpful for visualising lines and planes, as was a quick chat with my colleague [2] to check over some algebra operations with me. It turned out I was doing it right after all. :)

The libraries I made as part of the course covered vector operations (e.g. scaling, multiplication, equality checks, are vectors parallel / orthogonal), lines (equations) and whether the lines are parallel or intersect etc., then moved onto systems of equations: Triangular Form, Reduced Row-Echelon Form (RREF) and Gaussian elimination.

import "github.com/a-h/linear"

func main() {
  // Angle between
  result, err := linear.NewVector(8.218, -9.341).Add(linear.NewVector(-1.129, 2.111))
  angle, err := linear.NewVector(3.183, -7.627).AngleBetween(linear.NewVector(-2.668, 5.319))
  
  // Intersection
  line1 := linear.NewEquation(linear.NewVector(7.204, 3.182), 8.68) // 7.204x + 3.182y = 8.68
  line2 := linear.NewEquation(linear.NewVector(8.172, 4.114), 9.883)
  intersection, intersects, equal, err := line1.IntersectionWith(line2)
}

I wrote unit tests for everything, so the library has 100% code coverage. The answers for each of the quizzes in the course are in the cmd subdirectory. (I’m not spoiling anything, the course includes the answers)

The code is available at [3]