Creating a Matrix

First things first. You need to be able to describe a new matrix. Write a test like the following, which shows that a matrix is composed of four rows of four floating point numbers each, for a total of sixteen numbers. It should also show how to refer to the elements of the matrix.

 Scenario​: Constructing and inspecting a 4x4 matrix
 Given​ the following 4x4 matrix M:
  | 1 | 2 | 3 | 4 |
  | 5.5 | 6.5 | 7.5 | 8.5 |
  | 9 | 10 | 11 | 12 |
  | 13.5 | 14.5 | 15.5 | 16.5 |
 Then​ M[0,0] = 1
 And​ M[0,3] = 4
 And​ M[1,0] = 5.5
 And​ M[1,2] = 7.5
 And​ M[2,2] = 11
 And​ M[3,0] = 13.5
 And​ M[3,2] = 15.5

The first thing to notice is when talking about the individual elements of the matrix, we specify the element’s row first, and then its column. For example, element M23 is the one at row 2, column 3. Also note in this book, row and column indices will be zero-based, so row 2 is actually the third row.

Later, in Inverting Matrices, you’ll need to be able to instantiate both 2x2 and 3x3 matrices in addition to 4x4 matrices, so take a moment to make sure you can create matrices of those sizes as well. Add the following tests to show that your code supports those dimensions:

 Scenario​: A 2x2 matrix ought to be representable
 Given​ the following 2x2 matrix M:
  | -3 | 5 |
  | 1 | -2 |
 Then​ M[0,0] = -3
 And​ M[0,1] = 5
 And​ M[1,0] = 1
 And​ M[1,1] = -2
 
 Scenario​: A 3x3 matrix ought to be representable
 Given​ the following 3x3 matrix M:
  | -3 | 5 | 0 |
  | 1 | -2 | -7 |
  | 0 | 1 | 1 |
 Then​ M[0,0] = -3
 And​ M[1,1] = -2
 And​ M[2,2] = 1
images/aside-icons/tip.png

Keep your matrix implementation as simple as possible. Prefer native types wherever you can, and avoid complicated abstractions. Your matrices will be doing a lot of work!

Another critical part of your matrix implementation is matrix comparison. You’ll be comparing matrices a lot, especially in this chapter and the next, so it’s important to get it right. The following two tests are not exhaustive but ought to point you in the right direction. For example, you’ll want to make sure that very similar numbers are handled correctly when comparing matrices, as described in Comparing Floating Point Numbers.

 Scenario​: Matrix equality with identical matrices
 Given​ the following matrix A:
  | 1 | 2 | 3 | 4 |
  | 5 | 6 | 7 | 8 |
  | 9 | 8 | 7 | 6 |
  | 5 | 4 | 3 | 2 |
 And​ the following matrix B:
  | 1 | 2 | 3 | 4 |
  | 5 | 6 | 7 | 8 |
  | 9 | 8 | 7 | 6 |
  | 5 | 4 | 3 | 2 |
 Then​ A = B
 
 Scenario​: Matrix equality with different matrices
 Given​ the following matrix A:
  | 1 | 2 | 3 | 4 |
  | 5 | 6 | 7 | 8 |
  | 9 | 8 | 7 | 6 |
  | 5 | 4 | 3 | 2 |
 And​ the following matrix B:
  | 2 | 3 | 4 | 5 |
  | 6 | 7 | 8 | 9 |
  | 8 | 7 | 6 | 5 |
  | 4 | 3 | 2 | 1 |
 Then​ A != B

Once you’ve got the basic matrix data structure working, linear algebra is your oyster. We’re going to do some wild things with matrices, but we’ll start small; let’s talk about multiplying them together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.43