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

Saint Louis University

Computer Science 146
Object-Oriented Practicum

Michael Goldwasser

Fall 2012

Dept. of Math & Computer Science

Assignment 06

Describing Submatrices

Overview

Topic: Describing Submatrices
Due: 11:59pm Tuesday, 6 November 2012

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


Submatrices in MATLAB

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.

General MATLAB submatrix

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 A([2 1 2], [1 4 3 1]) produces the result following result

20  3 14 20
 5  8  3  5
20  3 14 20
In 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 [1 3 5] that can subsequently be used to describe a range of columns, as in our first example.


C++ variation

Our goal is to provide some similar functionality for our C++ matrices. However, there are several factors to consider.

A range function

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 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 provide a three-parameter version of this function with signature range(start, stride, stop). For example, range(1, 2, 5) could return a matrix with contents 1 3 (note well that the stop value is not included).

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.

Describing Submatricies (your task!)

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

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. There are two different segments during the execution of that test program:


Submitting your project

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


Michael Goldwasser
CSCI 146, Fall 2012
Last modified: Wednesday, 31 October 2012
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Questionnaire | Schedule | Submit