-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrix.cpp
260 lines (218 loc) · 4.52 KB
/
SparseMatrix.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/******************************************************************************
* FILE NAME SparseMatrix.cpp
* PURPOSE sparse matrix implementation
*
* SPEC
* NOTES NONE
******************************************************************************/
// ****************************************************************************
// Includes
// ****************************************************************************
#include <iostream>
#include "SparseMatrix.h"
void sparse_matrix::print()
{
sparse_matrix *mA = this;
sparse_matrix::iterator itA(*mA);
for (itA.First1(); !itA.IsDone1(); itA.Next1())
{
printf("\n");
for (itA.First2(); !itA.IsDone2(); itA.Next2())
{
printf("%5.1f(%d %d) ", *itA, itA.first_index(), itA.second_index());
}
}
printf("\n");
}
/**
* detailed description
*
* @memo <memo>
* @param nSize1
* @param nSize2
* @param nWidth
* @author Denis
* @see nothing
*/
void sparse_matrix::resize(int nSize1, int nSize2, int nWidth)
{
int nI, nK;
delete_sm();
m_mVal = new_matrix<double>(nSize1, nWidth);
m_mInd = new_matrix<int>(nSize1, nWidth);
m_nSize1 = nSize1;
m_nSize2 = nSize2;
m_nWidth = nWidth;
for (nI = 0; nI < nSize1; nI++)
{
for (nK = 0; nK < nWidth; nK++)
{
m_mVal[nI][nK] = 0.0;
m_mInd[nI][nK] = not_index;
}
}
}
/**
* detailed description
*
* @memo <memo>
* @author Denis
* @see nothing
*/
void sparse_matrix::delete_sm()
{
if (NULL != m_mVal && NULL != m_mInd)
{
delete_matrix(m_mVal);
delete_matrix(m_mInd);
}
}
/**
* detailed description
*
* @memo <memo>
* @return <return>
* @param
* @author Denis
* @see nothing
*/
double sparse_matrix::operator()(int nI, int nJ) const
{
int nK, nSecondIdx;
for (nK = 0; nK < m_nWidth; nK++)
{
nSecondIdx = m_mInd[nI][nK];
if ( nSecondIdx == nJ )
{
return m_mVal[nI][nK];
}
}
return 0;
}
/**
* detailed description
*
* @memo <memo>
* @return <return>
* @param
* @author Denis
* @see nothing
*/
double& sparse_matrix::add2(int nI, int nJ)
{
int nK, nSecondIdx;
for (nK = 0; nK < m_nWidth; nK++)
{
nSecondIdx = m_mInd[nI][nK];
if ( nSecondIdx == not_index || nSecondIdx == nJ )
{
m_mInd[nI][nK] = nJ;
return m_mVal[nI][nK];
}
}
// TODO: throw exception
std::cout << "incorrect width of the matrix" << std::endl;
return m_mVal[0][0];
}
/**
* detailed description
*
* @memo <memo>
* @return <return>
* @param nRow
* @author Denis
* @see nothing
*/
void sparse_matrix::del_row(int nRow)
{
int nK;
for (nK = 0; nK < m_nWidth; nK++)
{
m_mInd[nRow][nK] = not_index;
m_mVal[nRow][nK] = 0;
}
}
/**
* detailed description
*
* @memo <memo>
* @return <return>
* @param vIn
* @param vOut
* @author Denis
* @see nothing
*/
void sparse_matrix::vector_multiply(const vector& vIn, vector& vOut) const
{
int nI, nJ, nIdx;
vOut = 0.0;
for (nI = 0; nI < m_nSize1; nI++)
{
for (nJ = 0; nJ < m_nWidth; nJ++)
{
nIdx = m_mInd[nI][nJ];
if (not_index == nIdx)
{
break;
}
vOut[nI] += m_mVal[nI][nJ] * vIn[nIdx];
}
}
return;
}
/**
* detailed description
*
* @memo dot product
* @return none
* @param vVec
* @author Denis
* @see nothing
*/
double vector::operator*( const vector& vVec ) const
{
double rDotProduct;
size_t nI;
rDotProduct = 0.0;
for (nI = 0; nI < size(); ++nI)
{
rDotProduct += (*this)[nI] * vVec[nI];
}
return rDotProduct;
} // end of operator*
/**
* detailed description
*
* @memo dot product
* @return none
* @param vVec
* @author Denis
* @see nothing
*/
vector& vector::operator*=( double rScale )
{
size_t nI;
for (nI = 0; nI < this->size(); ++nI)
{
(*this)[nI] *= rScale;
}
return *this;
} // end of operator*=
/**
* detailed description
*
* @memo this += vVec * rScale
* @return none
* @param vVec - vector
* @param rScale - scale
* @author Denis
* @see nothing
*/
void vector::add_scale_vector( const vector& vVec, double rScale )
{
size_t nI;
for (nI = 0; nI < this->size(); ++nI)
{
(*this)[nI] += vVec[nI] * rScale;
}
} // end of add_scale_vector