050_Assignment_1_Matrix

050_Assignment_1_Matrix - CS106L Spring 2009 Handout #05...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106L Spring 2009 Handout #05 November 17, 2009 Assignment 1: Matrix _________________________________________________________________________________________________________ Due December 4, 11:59 PM Introduction A matrix is a two-dimensional array of numbers. For example, here is a 4 x 5 matrix: A = 3 1 4 2 7 1 5 9 1 8 2 6 5 2 8 3 5 8 1 8 Notice that a 4 x 5 matrix has four rows and five columns. Given a matrix A , we use the notation A ij to denote the element in row i , column j of that matrix. Conven- tions vary on whether matrix elements are zero- or one-indexed, but since we're C++ programmers we'll assume that the entries are zero-indexed. This means that if A is the above matrix, A 00 = 3, A 12 = 9, and A 21 = 6 . Matrices are used in all fields of computer science and applied mathematics, including graphics, machine learning, program analysis, and physics simulations. Matrices are useful for a variety of reasons, one of which is that they provide a compact syntax for manipulating large quantities of numbers. For instance, we can add two matrices of the same dimensions by summing their components. Thus if A = 3 1 4 2 7 1 5 9 1 8 2 6 5 2 8 3 5 8 1 8 ; B = 9 7 9 4 5 3 2 3 9 0 8 4 6 4 5 2 6 4 2 3 Then A B = 12 8 13 6 12 4 7 12 10 8 10 10 11 6 13 5 11 12 3 11 Suppose that we want to design a C++ class that encapsulates a matrix, which we'll call Matrix . One simple method for implementing Matrix would be to back the Matrix with a two-dimensional array. If we use the grid class from Chapter 25 of the course reader, we could implement Matrix as follows: (You might want to read over the interface for grid before proceeding) - 1 -
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
class Matrix { public: /* Constructor creates a matrix of a specific size that's all zeros. */ Matrix(size_t rows, size_t cols) : elems(rows, cols) {} /* Read and write values in the matrix. */ double getAt(size_t row, size_t col) const { return elems[row][col]; } void setAt(size_t row, size_t col, double value) { elems[row][col] = value; } /* Query the size of the matrix. */ size_t numRows() const { return elems.numRows(); } size_t numCols() const { return elems.numCols(); } private: /* Layer the matrix on top of a grid. */ grid<double> elems; } ; Now that we have a basic implementation of the Matrix , let's see how we might implement matrix addi- tion. We'll define a version of operator+ that takes in two matrices and returns their sum. One imple- mentation is as follows: { /* . .. check that dimensions agree . .. */ Matrix result(one.numRows(), one.numCols(); for(size_t row = 0; row < one.numRows(); ++row) for(size_t col = 0; col < one.numCols(); ++col) result.setAt(row, col, one.getAt(row, col) + two.getAt(row, col) ); return result; } This function constructs a new matrix of the same dimensions as the original matrices, then sets each ele- ment in the resulting matrix to be the sum of the elements from the two input matrices. We could simil-
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/12/2010 for the course CS 106l at Stanford.

Page1 / 19

050_Assignment_1_Matrix - CS106L Spring 2009 Handout #05...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online