Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment your task is to design, implement, and test the class hierarchy as described below. 1. Define and implement an abstract class called

image text in transcribed

In this assignment your task is to design, implement, and test the class hierarchy as described below. 1. Define and implement an abstract class called Structured Matrix. 2. Define and implement a concrete class called Symmetric Matrix derived from Structured Matrix. 3. Define and implement an abstract class called Triangular Matrix derived from Structured Matrix. 4. Define and implement a concrete class called Upper Triangular Matrix derived from Triangular Matrix. 5. Define and implement a concrete class called Lower Triangular Matrix derived from Triangular Matrix. 6. Explain (with the aid of an example) in your header file as comments, how you have constructed the element mapping functions for the concrete classes Upper Triangular Matrix, Lower Triangular Matrix, and Symmetric Matrix. Recall that the size of the array or vector that you use to store the matrix elements may not be larger than n(n+1) 2 for a nn matrix. 7. For each of the CONCRETE CLASSES you need to implement ONLY the following functions. (a) A constructor that takes two parameters: an integer parameter n and a double parameter value to create a n n matrix where each element is initialized with value. Default for value is 0.0. (b) Indexing operators with the following prototypes // indexing operator const double& operator()(int i, int j) const; //to work on const objects double& operator()(int i, int j) = 0; // to work with non-const objects (c) A function to display the matrix // display the matrix void print(); (d) a function that computes and returns the largest row sum, in absolute value, for a matrix. // return the largest row sum double infnorm(); The sum of the absolute values of entries of row i for a matrix M is calculated as image text in transcribed abs(M(i, j)) where abs() is the absolute value function of C++. Function infnorm() returns the largest of the sums over all rows of matrix M. For example, infnorm() called with matrix in Figure 1 as argument, will return 50. You need to include cstdlib to use the absolute value function. 8. For each of the abstract classes, you are to decide whether a member function be made virtual or pure virtual and explain your decision. 9. Write a test program (in testStrMatrix.cc) showing the use of each function that you have implemented. You may read test matrices (for n = 5 and n = 10) interactively from the user or from a file. 10. Write a function (in testStrMatrix.cc) called process str matrix with the following prototype. void process_str_matrix(STRMatrix *mtxPtr[]) The function should call the member functions infnorm and print to print to the screen the largest row sum and display the matrices pointed to by the pointer array mtxPtr. The pointer array argument should have been initialized with appropriate matrix objects to demonstrate polymorphic behaviour.

followed by elements of row n-1. For example, the elements of matrix M of Figure 1 stored in row-major order in an array or vector v would appear as below: In practical applications, two-dimensional arrays or matrices that arise may have special "struc- ture" and it usually pays (in storage needed and time to perform matrix operations) to take advantage of this information. This assignment is concerned with defining and implementing a class hierarchy for structured matrices. The elements of a matrix M is references by giving the row and column indexes. For example, matrix M shown in Figure 1 has 4 rows and 4 columns, The rows and columns can be indexed 0,1,2,3. Then, each element can be uniquely identified by givng its row index and column index. For example, elent t ow index 2 and column index 1, written as M(2,1), is the integer 8. A square matrix is a matrix with same number of rows and columns. For a symmetric matrix M, we only need to store elements that are on or below the main diagonal. The remaining elelnents are known by symmetry condition M (i, j) = M(j,i). Thus, elements of matrix in Figure 1 can be stored in an array or vector v of size 10 instead of 16 if we do not exploit the symmetry property) as shown below: using the row-major layout A square matrix M with ros and n columns is called symTmetric if the transpose of M written MT, is same as M. In other words, the elemens of matrix M satisfy the property: M(i,j) = MGA) for every pair of indices (i, j), i = 0, 1, . . . ,n-1 and J 0, 1, . . . , n-1. The matrix displayed in Figr1 is symmetric. 13 Figure 3: Symmetric matrix M stored in row-major order For triangular matrices, we must not explicitly store matrix elements that are known to be 1 5 8 13 2 8 21 0 3 13 0 34 zero. Thus, for matrices in Figure 2 only the elements marked X are explictly stored. In other for the triangular matrices shown in Figure 2 you will use a dynamic array or a vector of size and use the row-major order upper-triangular matri and lower-triangular matri are related by "IS-A* relationships. A words, we can use an array or vector of size 10 to store only the elements that are marked X In general, to store the elements of a n x n symmetric matrix or a n n triangular matrix. Notice that the objects of type structured matrix, symmetric matriz, triangular matrir, Figure 1: A symmetric matrix A triangular matrir is a square matrix where the nonzero elements are confined in one half of the matrix and the other half contains only zero. We can define two types of triangular matrices, namely, lower triangular and upper triangular. In Figure 2. X denotes an element which can be a zero or a nonzero. The matrix on the left is lower triangular and the one on the right is upper triangular suitable inheritance hierarchy representing these relationships can be set up as 1. a symimetric matrir "IS-A" kind of structured matrir, order. In the row major 2. a triangular matriT "IS-A" kind of structured matrir, order the elements of a square matrix M that has n rows and n columns are stored in an array or vector of length n2 row-by-row: elements of row 0 followed by elements of row 1 3. a lower-triangular matrir "IS-A" kind of triangular matriz, 4. an upper-triangular matriz "IS-A kind of triangular matriz. X 0 0 0 X X0 0 Note that to the clients a matrix is still a two-dimensional array implying that an element has a row index and a column index. However, in the implementation, elements are stored in an array or a vector. Therefore, you need to find a mapping for indices from two-dimensional arrays to one-dimensional arrays or vectors in order to identify the matrix elements correctly. More specifically, consider the element at row index 2 and column index 1. A client references the element by M(2,1). Using the row-major order as shown in Figure 3, the element at M(2,1) is mapped to index 4 of array u which corresponds to array element4 0 0 0 X Figure 2: (a) A lower triangular matrix. (b) An upper triangular matrix followed by elements of row n-1. For example, the elements of matrix M of Figure 1 stored in row-major order in an array or vector v would appear as below: In practical applications, two-dimensional arrays or matrices that arise may have special "struc- ture" and it usually pays (in storage needed and time to perform matrix operations) to take advantage of this information. This assignment is concerned with defining and implementing a class hierarchy for structured matrices. The elements of a matrix M is references by giving the row and column indexes. For example, matrix M shown in Figure 1 has 4 rows and 4 columns, The rows and columns can be indexed 0,1,2,3. Then, each element can be uniquely identified by givng its row index and column index. For example, elent t ow index 2 and column index 1, written as M(2,1), is the integer 8. A square matrix is a matrix with same number of rows and columns. For a symmetric matrix M, we only need to store elements that are on or below the main diagonal. The remaining elelnents are known by symmetry condition M (i, j) = M(j,i). Thus, elements of matrix in Figure 1 can be stored in an array or vector v of size 10 instead of 16 if we do not exploit the symmetry property) as shown below: using the row-major layout A square matrix M with ros and n columns is called symTmetric if the transpose of M written MT, is same as M. In other words, the elemens of matrix M satisfy the property: M(i,j) = MGA) for every pair of indices (i, j), i = 0, 1, . . . ,n-1 and J 0, 1, . . . , n-1. The matrix displayed in Figr1 is symmetric. 13 Figure 3: Symmetric matrix M stored in row-major order For triangular matrices, we must not explicitly store matrix elements that are known to be 1 5 8 13 2 8 21 0 3 13 0 34 zero. Thus, for matrices in Figure 2 only the elements marked X are explictly stored. In other for the triangular matrices shown in Figure 2 you will use a dynamic array or a vector of size and use the row-major order upper-triangular matri and lower-triangular matri are related by "IS-A* relationships. A words, we can use an array or vector of size 10 to store only the elements that are marked X In general, to store the elements of a n x n symmetric matrix or a n n triangular matrix. Notice that the objects of type structured matrix, symmetric matriz, triangular matrir, Figure 1: A symmetric matrix A triangular matrir is a square matrix where the nonzero elements are confined in one half of the matrix and the other half contains only zero. We can define two types of triangular matrices, namely, lower triangular and upper triangular. In Figure 2. X denotes an element which can be a zero or a nonzero. The matrix on the left is lower triangular and the one on the right is upper triangular suitable inheritance hierarchy representing these relationships can be set up as 1. a symimetric matrir "IS-A" kind of structured matrir, order. In the row major 2. a triangular matriT "IS-A" kind of structured matrir, order the elements of a square matrix M that has n rows and n columns are stored in an array or vector of length n2 row-by-row: elements of row 0 followed by elements of row 1 3. a lower-triangular matrir "IS-A" kind of triangular matriz, 4. an upper-triangular matriz "IS-A kind of triangular matriz. X 0 0 0 X X0 0 Note that to the clients a matrix is still a two-dimensional array implying that an element has a row index and a column index. However, in the implementation, elements are stored in an array or a vector. Therefore, you need to find a mapping for indices from two-dimensional arrays to one-dimensional arrays or vectors in order to identify the matrix elements correctly. More specifically, consider the element at row index 2 and column index 1. A client references the element by M(2,1). Using the row-major order as shown in Figure 3, the element at M(2,1) is mapped to index 4 of array u which corresponds to array element4 0 0 0 X Figure 2: (a) A lower triangular matrix. (b) An upper triangular matrix

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions