I will provide a test program that reads in a matrix and a vector from input and perform the matrix*vector operation on the inputs. The matrix and vector have arbitrary size which is given in the input file.
The following are the header files and you must NOT make any changes to these files:
class Vector { public: int N double *v; Vector() {}; // Default constructor Vector(double w[], int n); }; |
This class contain a default constructor (provided) and another constructor function that is used to define vectors using the following declaration:
double w[..]; int n; Vector v1(w, n);It defines a vector of size n and initializes the elements with the array w. The constructor must allocate space for the vector and copy the elements from w to the allocated array.
You must provide an implementation for the following function:
Vector::Vector(double w[], int n); // Constructor |
There will be 2 type of matrices: dense and sparse
class DenseMatrix { public: int N; double *A; DenseMatrix(double *M, int n); Vector operator*(Vector &v); }; |
This class also contains a constructor function like Vector. This contructor will be used to define matrices using the following declaration:
DenseMatrix M1(M, n);The variable M1 will have an array of n*n elements and the elements are initialized with the double array M (use the i*N+j method to access array elements).
The constructor must allocate space for the matrix and copy the elements from M to the allocated array.
The operator* function multiplies a dense matrix and a vector together.
You must provide an implementation for the following function:
DenseMatrix::DenseMatrix(double *M, int n); Vector DenseMatrix::operator*(Vector &v); |
class SparseMatrix { public: int N; // Dimension of matrix (need it to print) int nnz; double *val; int *row; int *col; SparseMatrix(double *m, int N); Vector operator*(Vector &v); }; |
This class represents a sparse matrix using the Coordinate-wise method. This contructor in this class is more involved, and it will be used to define matrices using the following declaration:
SparseMatrix M1(M, n);The input matrix M is an n x n matrix. Before you can allocate space for the 3 arrays (val, row and col), you need to determine the number of non-zero elements in M. Use the i*N+j method to access elements in M. After determining the number of non-zero elements, the constructor allocate space for val, row and col to store the non-zero elements of M.
The operator* function multiplies a sparse matrix and a vector together.
You must provide an implementation for the following function:
SparseMatrix::SparseMatrix(double *m, int n); Vector SparseMatrix::operator*(Vector &v); |
ostream & operator<<(ostream & cout, DenseMatrix m) (to print a DenseMatrix) ostream & operator<<(ostream & cout, SparseMatrix m) (to print a SparseMatrix) int main(int argc, char **argv) { int i, j, N; cin >> N; double *m = new double[N*N]; double *v = new double[N]; for (i = 0; i < N; i++) for (j = 0; j < N; j++) cin >> m[i*N+j]; for (i = 0; i < N; i++) cin >> v[i]; DenseMatrix D(m, N); SparseMatrix S(m, N); Vector v1(v, N); Vector v2; v2 = D * v1; cout << " D: " << endl << endl << D << endl; cout << " v1: " << endl << endl << v1 << endl; cout << " D*v1: " << endl << endl << v2 << endl; v2 = S * v1; cout << " S: " << endl << endl << S << endl; cout << " v1: " << endl << endl << v1 << endl; cout << " S*v1: " << endl << endl << v2 << endl; } |
The test program contains code to print the dense and sparse matrices; so you only need to focus on implementing the constructors and the * operators. (In fact, the dense matrix operations have been discussed in class, most of the work is on sparse matrices)
Do not make any changes to the test program...
The input matrix is:
1 2 1 3 1 0 0 1 3 |
The input vector is: (3, 6, 7)
NOTE: your program must work with any size input, here's another sample input file (with a different matrix size: click here
NOTE: although the size of the test matrices are small, I do it this way to keep complexity at a minimum. This project is to re-inforce sparse matrix techniques without having you use hge matrices. (The code is the same, and debugging is much easier). So please don't store ZERO entries in the sparse matrix !
Due to the use of Makefile, I don't need to know exactly how you organize your programs (and I should not care).
If you follow the instructions below carefully, I will be able to compile your program without any problems:
/home/cs561000/turnin Makefile pj4 |
/home/cs561000/turnin OneOfYourSourceFile pj4-# |
where "#" is a digit between 0 and 9.