//
// CoordMatrix.h define the matrix stored in a coordinate format
//
#ifndef COORDMATRIX_H
#define COORDMATRIX_H
#include<vector>
#include <assert.h>
using namespace std;
class CoordMatrix
{
private:
// val strore the values in matrix;
vector<double> val;
// row is row index of value;
vector<int> row;
// col is colume index of value ;
vector<int> col;
// nonZeor is the total number of nonzero value in the matrix;
int nonZero;
// numRow is the dimension of the matrix generall we use square matrix;
int numRow;
public:
// default constructor
CoordMatrix():nonZero(0),numRow(0) {val.resize(0); row.resize(0); col.resize(0); row.resize(0); col.resize(0); val.resize(0);};
// constructor from a two D array
CoordMatrix(double **aVal)
{ int R=sizeof aVal /sizeof aVal[0];
int C=column;
for (int i=0; i<R; i++) {
for(int j=0; j<C; j++){
if( *(aVal[i]+j) != 0.0 ) {row.push_back(i); col.push_back(j); val.push_back( *(aVal[i]+j) ); }
}
}
nonZero=row.size();
numRow=R;
}
}
Dear friends:
I wrote a spare Matrix class and the Matrix was stored in a coornidate format. Only the row index, colume index and the value of sparse matrix was stored. I want to constructe a CoordMatrix from a 2d array, but the code is wrong , i do not how to transfer a 2d of arbitrary size to the constructor. Could you please give me some advice.
regards