-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsparse_matrix.cpp
More file actions
94 lines (81 loc) · 2.31 KB
/
Copy pathsparse_matrix.cpp
File metadata and controls
94 lines (81 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Class of sparse matrix. The matrix is specified
// with the following inputs: (limited to square matrix)
// dimension of matrix : dim
// nubmer of nonzero elements : nnZero
// array of row indecies of nonzero elements : indx[nnZero]
// array of column indecies of nonzero elements : indx[nnZero]
// nonzero matrix elements mtx[indx,jndx] : mtx[nnZero]
// ===========================================================
// Author : A. Ahmadi
// Date : May 27 2016
//============================================================
#include <iostream>
using namespace std;
class SprsMtx
{
public:
SprsMtx(int sdim, int snnz); // constructor
~SprsMtx(); // destructor
// need a method to set the value of indx,jndx and mtx
// return the value
void init(int *in_vec, int *jn_vec, double *mtx, int snnz);
// then define operator overloading for sparse mtrx-mtrx multiplication
void sget() const;
private:
int dim, nnZero;
int *indx, *jndx;
double *mtx_elmt;
};
SprsMtx::SprsMtx(int sdim, int snnz){
if (snnz<=(sdim*sdim)){
dim = sdim;
nnZero = snnz;
indx = new int [nnZero];
jndx = new int [nnZero];
mtx_elmt = new double [nnZero];
}else{
cout << "\nnumber of non-zero elemetns exceeded"
<< " number matrix elements\n";
}
}
SprsMtx::~SprsMtx(){
delete[] indx;
delete[] jndx;
delete[] mtx_elmt;
indx = 0; jndx = 0; mtx_elmt = 0;
}
void SprsMtx::init(int *in_vec, int *jn_vec, double *mtx, int snnz){
if (snnz == nnZero){
for(int i = 0; i < nnZero; i++){
indx[i] = in_vec[i];
jndx[i] = jn_vec[i];
mtx_elmt[i] = mtx[i];
}//endfor
}
else{
cout << "Error : non-zero elements does not match "
<< " the vectors' dimension \n";
}//endelse
}
void SprsMtx:: sget() const {
for(int i = 0; i < nnZero; i++){
cout << indx[i] << "\t";}
cout << endl;
for(int i = 0; i < nnZero; i++){
cout << jndx[i] << "\t";}
cout << endl;
for(int i = 0; i < nnZero; i++){
cout << mtx_elmt[i] << "\t";}
cout << endl;
}
int main(){
SprsMtx mtx1(10,5);
// example of indx and jndx
int indx[5] = {1,2,3,4,5};
int jndx[5] = {1,2,3,4,5};
double mtx[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
mtx1.init(indx, jndx, mtx,5);
mtx1.sget();
return 0;
cout << "Just small change\n";
}