Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 146
Object-Oriented Practicum

Michael Goldwasser

Fall 2011

Dept. of Math & Computer Science

Assignment 06

Using Ranges to Describe Submatrices

Overview

Topic: Using Ranges to Describe Submatrices
Due: 11:59pm Tuesday, 18 October 2011

Please make sure you adhere to the policies on academic integrity.


Introduction

MATLAB colon syntax

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 35
We can use a syntax A(2:3, 1:2:5) to produce the following result
20 14  1
40  7 24
The syntax 2:3 specifies rows going from 2 to 3 (inclusive). The syntax 1:2:5 specifies that columns start at 1, skiping with a stride of 2, going up to but not beyond 5, therefore columns 1, 3, and 5. Note that in the first case, there was an implicit stride of 1.

C++ variation

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 4, 5, 6, 7. There is a strong history in C++ of expressing ranges that are half-open intervals, whereby a range is described using parameters range(start,stop) such that the range begins with index start and goes up to but not including index stop. Thus we might have the syntax range(4,7) designate the set of indices 4, 5, 6 (but not 7). To describe a range with a non-unit stride, we can

Our range class

During lecture, we developed an implementation of a simple range class. It supports three different versions of a constructor, range(index), range(start, stop) and range(start, stride, stop). A complete definition for that class is given within the matrix.h file that we are providing for this assignment.

Unsupported variants:

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

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) const
This function is to produce and return a new matrix that is a copy of the appropriate submatrix as specified by the two ranges.

exceptions

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 A( range(2,5), range(1,3)) should be illegal because range(2,5) designates the set of indices 2, 3, and 4 for the choice of rows, but there is no such thing as row 3 or 4 in such a matrix.


Files we are providing

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.


Submitting your project

Your revised matrix.h file should be submitted electronically (details on the submission process).


Michael Goldwasser
CSCI 146, Fall 2011
Last modified: Sunday, 16 October 2011
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Schedule | Submit