050_Assignment_1_Matrix

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

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

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 -

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

View Full Document
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: const Matrix operator+ (const Matrix& one, const Matrix& two) { /* ... 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- arly define operator- to subtract two matrices and operator* to multiply a matrix and a scalar (this multiplies every element in the matrix by the scalar). With these operators in tow, we can write expres- sions like the following: /* Create four matrices – A, B, C, and D – of the same size. */ Matrix A = /* ... */, B = /* ... */, C = /* ... */, D = /* ... */; /* Create a new matrix from them. */
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern