CS561 - Project

# CS561, Project 5

Due: See class webpage

## Solving linear equations with Jacobi

In this project, you are to write program that reads in a linear equation from an input file and solve the equation with the Jacobi method. The Jacobi method must be object-oriented; and uses a generic matrix-vector multiply and vector-vector addition operator. Unlike the matrix-vector multiply and vector-vector addition operators used the example in class (where I used a 3x3 matrix for simplicity), the matrix-vector multiply and vector-vector addition operator that you must implement must work for arbitrary size arrays.

You need get the following files and you must NOT make any changes to these files:

```   class Vector
{
public:
int   N
float *v;

Vector();
Vector(int n);
Vector(float w[], int n);
Vector operator+(Vector &w);
Vector operator=(Vector &w);
};
```
This class contains three constructor functions that are used to define vectors using the following declaration:
```  Vector v1();
Vector v1(n);
Vector v1(w, n);
```
The first form defines a vector of unknown n (pointer variable v is uninitialized). The second form defines an uninitialized vector of size n and in the thrid form, the variable v1 will have an array of n elements and the elements are initialized with the float array w. Basically, the constructor must allocate space for the vector and copy the elements from w to the allocated array.

The operator+ function adds 2 vectors together.

The operator= function is used to copy one vector to another vector. You need to define an assignment operator because you used pointers in Vector.

```   class Matrix
{
public:
virtual Vector operator*(Vector &v);
};
```
The Matrix type is used to make polymorphic function calls. You don't need to do anything with this class.

```   class DenseMatrix : public Matrix
{
public:
int    N;
float *A;
Vector output;

DenseMatrix(float *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 float 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 constructor must also allocate space for array in the Vector variable output vector (n floats). This variable is used in the operator* function to store the output vector and returned by the operator* function.

The operator* function multiplies a dense matrix and a vector together.

```   class SparseMatrix : public Matrix
{
public:
int nnz;
float *val;
int   *row;
int   *col;

SparseMatrix(float *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.

• A sample linear equation is given in the following file: click here
```3
10	1	2
1	10	3
2	3	10
5	6	7
```

The first number is the dimension of the matrix (3 equations). The next n*n numbers (float) are the coefficients of the matrix. Then last n is the RHS of the equation.

NOTE: the above equation input is the same equation that we solved in class - so you can compare your solution with a working one.

NOTE: this is just a sample input, your program must work for equations of arbitrary size !

## 2. What does your program have to do

Your program must be named main for ease of grading. (Make sure the target in your Makefile is "main").

Your program is invoked using the following syntax from the UNIX command prompt:

```  UNIX>> main < input-data
```
For example, to run the program on the sample data, you would issue this command:
```  UNIX>> main < pj5-data
```

The output of the program must go to the screen and you must print the solution vector of each Jacobi iteration.

The program must solve the same system using both the dense matrix and the sparse matrix representation. (You should see the same output twice if both methods are correctly implemented).

You will be writing member functions for classes that is written outside the class declaration. Consult the following webpage on how to do that: click here

## 4. Turn in

I have no ideas how many files you will create for this project, but it should not be more than 10 in total (if you have more, then email me at: cheung@mathcs.emory.edu to discuss how to turn in extra files)

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:

• Do not turn in any of the files that is give above (i.e., the files that you must not modify. I have them and I will use the original files to compile your project.

• Very important: turn in the Makefile file of your program as assignment `pj5':
```        /home/cs561000/turnin  Makefile pj5
```

• For each file that comprises your project (other than those that I gave you above and that you must not change), select a unique digit (must be a single digit between 0 and 9) and use the following command to turn in each source file:
```        /home/cs561000/turnin  OneOfYourFile  pj5-#
```
where "#" is a digit between 0 and 9.