Saint Louis University |
Computer Science 146
|
Dept. of Math & Computer Science |
Topic: Using Ranges to Describe Submatrices
Due:
11:59pm Tuesday, 21 March 2012
Please make sure you adhere to the policies on academic integrity.
MATLAB allows submatrices to be specified by using a syntax with colons to describe ranges on the respective rows and columns. As an example, If matrix A in MATLAB has contents:
5 15 3 8 44 15 20 2 14 3 1 22 40 12 7 30 24 35We can use a syntax
20 14 1 40 7 24The syntax
Our goal is to provide some similar functionality for our C++ matrices. However, there are several factors to consider. The first is that it is impossible for us to mimic the : syntax directly. That is a core syntax supported by MATLAB for matrices, but it is not a core syntax of C++ and we cannot make it so for our class. Instead, we decided to define a new class range so that a range instance describes an appropriate set of indices.
The second issue was how to appropriately use indices in describe a
range for C++. We have already noted the difference in conventions
between MATLAB's use of indices starting with 1, versus the C++
convention of indices starting with 0. When describing range, a
similar question of convention arises. MATLAB chooses to use a syntax
such as 4:7 to express the inclusive range of values
During lecture, we developed an implementation of a simple
range class. It supports three different versions of a
constructor,
For the current assignment, we will ignore several related variants that are legal in MATLAB and could eventually be incorporated in our C++ version.
Your task is to modify the matrix.h code to support the new functionality of allowing ranges to be used to generate a submatrix. In particular, you must provide an appropriate implementation for the matrix class method having signature
matrix operator()(range rows, range cols) constThis function is to produce and return a new matrix that is a copy of the appropriate submatrix as specified by the two ranges.
You are also expected to properly throw an out_of_range
exception anytime this function is called with a range that includes
any illegal index for the given matrix. For example, on the original
3-by-6 matrix given above, the evaluation of
We are continuing with the development of the same matrix.h code that you have been working with, but our version does not have your solutions to the most recent assignments. However, our version has the new definition for the range class within the matrix.h file, so you will need to either switch to our matrix.h file or port that range definition to your own most recent version of matrix.h
If working on turing, change directories into a desired subdirectory of your home folder if desired, and to execute the following command verbatim
cp -R /Public/goldwasser/146/asgn06 .This will cause a new folder named asgn06 to appear in your working directory including all files that you need for this assignment. Alternatively, these files can be downloaded individual from this website. The matrix definition is essentially the same one we have been using for past assignments. However, we have written a new driver for this assignment to make it easier for you to test your code. That driver is named test and can be built with the make command.
The driver test.cpp that we are providing has some code that should more easily allow you to run your matrix through a battery of tests involve ranges. In particular, starting with line 105 of that file, you will find some code that reads:
testcase tests[] = { { "range(3,5)", range(3,5), "range(2,3,10)", range(2,3,10) }, { "range(3,5)", range(3,5), "range(2,3,100)", range(2,3,100) }, };You should not concern yourself with the rest of the surrounding code (although I'm happy to explain it to those who are interested). What is important is that you can added additional lines using a similar format, where you specify the range of rows and range of columns. In particular, note that the first and second columns match except that the first is a string and the second is not. This describes a range of rows. Similarly, the third and fourth columns are used to describe a range of columns. The rest of the code is designed so that it will attempt to compute and output the appopriate submatrix for each line of this test set.
Your revised matrix.h file should be submitted electronically (details on the submission process).