Saint Louis University |
Computer Science 146
|
Dept. of Math & Computer Science |
Topic: Describing Submatrices
Due:
11:59pm Tuesday, 6 November 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
In fact, MATLAB is really more general as it allows you to describe
a set of relevant rows (respectively, columns) using any
one-dimensional matrix. That is, for the above matrix, the syntax
20 3 14 20 5 8 3 5 20 3 14 20In this case, the result's rows are taken respectively from the Msecond, first, and second rows of the original, and the columns from column 1, 4, 3, and 1 respectively. In fact the colon syntax is just a special case of this, as a syntax like 1:2:5 generates a new matrix
Our goal is to provide some similar functionality for our C++ matrices. However, there are several factors to consider.
To resolve the first question, we can define a new range
function that produces a row-matrix having contents that follow a
typical range pattern.
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
To describe a range with a non-unit stride, we can provide a
three-parameter version of this function with signature
For convenience, we technically allow a one-parameter form, range(start) that includes only that start value.
However, 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 one-dimensional matrices to be used to generate a submatrix. In particular, you must provide an appropriate implementation for the matrix class method having signature
matrix operator()(matrix rows, matrix cols) constThis function is to produce and return a new matrix that is a copy of the appropriate submatrix as specified by the two parameters.
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. There are two different segments during the execution of that test program:
testcase tests[] = { { "range(3)", range(3,3), "range(2,7)", range(2,7) }, { "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) }, };involve ranges. In particular, 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).