Menu

[r78]: / trunk / include / dssmatrix- / dssmatrix-misc.hpp  Maximize  Restore  History

Download this file

447 lines (379 with data), 13.8 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
 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//=============================================================================
/*! clear all the matrix data and set the sizes 0 */
inline void dssmatrix::clear()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::clear()" << std::endl;
std::cerr << "# [NOTE] dssmatrix::clear()" << " An array at " << Array << " is going to be cleared." << std::endl;
#endif//CPPL_VERBOSE
N =CAP =VOL=0;
delete [] Array; Array=NULL;
delete [] Indx; Indx=NULL;
delete [] Jndx; Jndx=NULL;
//delete [] Line; Line=NULL;
Line.clear();
}
//=============================================================================
/*! change the matrix into a zero matrix */
inline dssmatrix& dssmatrix::zero()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::zero()" << std::endl;
#endif//CPPL_VERBOSE
VOL =0;
for(long i=0; i<N; i++){ Line[i].resize(0); }
return *this;
}
//=============================================================================
/*! change sign(+/-) of the matrix */
inline void dssmatrix::chsign()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::chsign()" << std::endl;
#endif//CPPL_VERBOSE
for(long i=0; i<VOL; i++){ Array[i] =-Array[i]; }
}
//=============================================================================
/*! make a deep copy of the matrix */
inline void dssmatrix::copy(const dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::copy(const dssmatrix&)" << std::endl;
std::cerr << "# [NOTE] dssmatrix::copy(const dssmatrix&)" << " A dssmatrix at " << Array << " is going to be deleted." << std::endl;
#endif//CPPL_VERBOSE
resize(mat.N, mat.CAP);
VOL =mat.VOL;
dcopy_(VOL, mat.Array, 1, Array, 1);
for(long i=0; i<VOL; i++){
Indx[i] =mat.Indx[i];
Jndx[i] =mat.Jndx[i];
}
//for(long i=0; i<N; i++){ Line[i] =mat.Line[i]; }
Line =mat.Line;
#ifdef CPPL_VERBOSE
std::cerr << " Then, a COPY of a dssmatrix has been cleated at " << Array << "." << std::endl;
#endif//CPPL_VERBOSE
}
//=============================================================================
/*! make a shallow copy of the matrix\n
This function is not designed to be used in project codes. */
inline void dssmatrix::shallow_copy(const _dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::shallow_copy(const _dssmatrix&)" << std::endl;
std::cerr << "# [NOTE] dssmatrix:shallow_copy(const _dssmatrix&) " << "A dssmatrix at " << Array << " is going to be deleted, " << "and point at " << mat.Array << " instead." << std::endl;
#endif//CPPL_VERBOSE
delete [] Array;
delete [] Indx;
delete [] Jndx;
//delete [] Line;
N =mat.N;
CAP =mat.CAP;
VOL =mat.VOL;
Array=mat.Array;
Indx =mat.Indx;
Jndx =mat.Jndx;
Line =mat.Line;
mat.nullify();
}
//=============================================================================
/*! resize the matrix */
inline dssmatrix& dssmatrix::resize(const long& _n, const long _c, const long _l)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::resize(const long&, const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _c<0 || _l<0 ){
std::cerr << "[ERROR] dssmatrix::resize(const long&, const long&, const long&)" << std::endl
<< "Matrix sizes, the length of arrays, and line size must be positive integers. " << std::endl
<< "Your input was (" << _n << "," << _c << "," << _l << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
N =_n;
CAP =_c;
VOL =0;
delete [] Array;
Array =new double[CAP];
delete [] Indx;
Indx =new long[CAP];
delete [] Jndx;
Jndx =new long[CAP];
//delete [] Line;
//Line =new std::vector< std::pair<long,long> >[N];
Line.resize(N);
for(long i=0; i<N; i++){ Line[i].reserve(_l); }
return *this;
}
//=============================================================================
/*! stretch the matrix size */
inline void dssmatrix::stretch(const long& dn)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::stretch(const long&)" << std::endl;
#endif//CPPL_VERBOSE
if(dn==0){ return; }
#ifdef CPPL_DEBUG
if( n+dn<0 ){
std::cerr << "[ERROR] dssmatrix::stretch(const long&)" << std::endl
<< "The new matrix size must be larger than zero." << std::endl
<< "Your input was (" << dn << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
N +=dn;
if(dn<0){
//// delete components over the new size ////
for(long c=VOL-1; c>=0; c--){
if(Indx[c]>=N || Jndx[c]>=N){ fdel(c); }
}
for(long i=0; i<-dn; i++){ Line.pop_back(); }
}
else{//dn>0
for(long i=0; i<dn; i++){ Line.push_back( std::vector< std::pair<long,long> >() ); }
}
}
//=============================================================================
/*! expand the matrix capacity */
inline void dssmatrix::expand(const long& dc)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::expand(const long&)" << std::endl;
#endif//CPPL_VERBOSE
if(dc==0){ return; }
#ifdef CPPL_DEBUG
if( dc<0 ){
std::cerr << "[ERROR] dssmatrix::expand(const long&)" << std::endl
<< "The argument must be a positive integer. " << std::endl
<< "Your input was (" << dc << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
CAP+=dc;
double* newArray(new double[CAP]);
long *newIndx(new long[CAP]), *newJndx(new long[CAP]);
for(int c=0; c<VOL; c++){
newArray[c] =Array[c];
newIndx[c] =Indx[c];
newJndx[c] =Jndx[c];
}
delete [] Array;
delete [] Indx;
delete [] Jndx;
Array =newArray;
Indx =newIndx;
Jndx =newJndx;
}
//=============================================================================
/*! check if the component is listed */
inline bool dssmatrix::isListed(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::isListed(const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::isListed(const long&, const long&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return 1; }
}
return 0;
}
//=============================================================================
/*! return the element number of the component */
inline long dssmatrix::number(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::number(const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::number(const long&, const long&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return p->second; }
}
return -1;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! get row of the matrix */
inline _drovector dssmatrix::row(const long& _m) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::row(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _m<0 || _m>M ){
std::cerr << "[ERROR] dssmatrix::row(const long&)" << std::endl
<< "Input row number must be between 0 and " << M << "." << std::endl
<< "Your input was " << _m << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
drovector v( drovector(N).zero() );
for(std::vector< std::pair<long,long> >::const_iterator p=line[_m].begin(); p!=line[_m].end(); p++){
v(p->first)=Array[p->second];
}
return _(v);
}
//=============================================================================
/*! get column of the matrix */
inline _dcovector dssmatrix::col(const long& _n) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::col(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _n>N ){
std::cerr << "[ERROR] dssmatrix::col(const long&)" << std::endl
<< "Input row number must be between 0 and " << N << "." << std::endl
<< "Your input was " << _n << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
dcovector v( dcovector(M).zero() );
for(std::vector< std::pair<long,long> >::const_iterator p=line[_n].begin(); p!=line[_n].end(); p++){
v(p->first)=Array[p->second];
}
return _(v);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! erase components less than DBL_MIN */
inline void dssmatrix::diet(const double eps)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::diet()" << std::endl;
#endif//CPPL_VERBOSE
for(long c=VOL-1; c>=0; c--){
if( fabs(Array[c])<eps ){ fdel(c); }
}
}
//=============================================================================
/*! health checkup */
inline void dssmatrix::checkup()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::checkup()" << std::endl;
#endif//CPPL_VERBOSE
//////// write ////////
for(long c=0; c<VOL; c++){
std::cerr << "Array[" << c << "] = (" << Indx[c] << "," << Jndx[c] << ") = " << Array[c] << std::endl;
}
std::cerr << std::endl;
for(long i=0; i<N; i++){
std::cerr << "Line[" << i << "] =" << std::flush;
for(unsigned long k=0; k<Line[i].size(); k++){
std::cerr << " <" << Line[i][k].first << "," << Line[i][k].second << ">" << std::flush;
}
std::cerr << std::endl;
}
std::cerr << std::endl;
//////// CAP ////////
if(CAP<0){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The cap is not valid." << std::endl
<< "CAP was " << CAP << "." << std::endl;
exit(1);
}
//////// VOL ////////
if(VOL<0 || VOL>CAP){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The vol is not valid." << std::endl
<< "VOL was " << VOL << "while CAP is " << CAP << "."
<< std::endl;
exit(1);
}
//////// Elements ////////
for(long c=0; c<VOL; c++){
//// m bound ////
if(Indx[c]<0 || Indx[c]>=N){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The indx of the " << c
<< "th element is out of the matrix size." << std::endl
<< "Indx[" << c << "] was " << Indx[c] << "." << std::endl;
exit(1);
}
//// n bound ////
if(Jndx[c]<0 || Jndx[c]>=N){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The jndx of the " << c
<< "th element is out of the matrix size." << std::endl
<< "Jndx[" << c << "] was " << Jndx[c] << "." << std::endl;
exit(1);
}
//// double-listed ////
for(long C=c+1; C<VOL; C++){
if(Indx[c]==Indx[C] && Jndx[c]==Jndx[C]){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The (" << Indx[c] << ", " << Jndx[c]
<< ") component is double-listed at the "
<< c << "th and the" << C << "the elements."<< std::endl;
exit(1);
}
}
}
//////// ijc consistence ////////
//////// NOTE ////////
std::cerr << "# [NOTE] dssmatrix::checkup() "
<< "This symmetric sparse matrix is fine." << std::endl;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! swap two matrices */
inline void swap(dssmatrix& A, dssmatrix& B)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] swap(dssmatrix&, dssmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
long A_N(A.N), A_CAP(A.CAP), A_VOL(A.VOL);
double* A_Array(A.Array);
long *A_Indx(A.Indx), *A_Jndx(A.Jndx);
//std::vector< std::pair<long,long> >* A_Line(A.Line);
std::vector< std::vector< std::pair<long,long> > > A_Line(A.Line);
A.N=B.N; A.CAP=B.CAP; A.VOL=B.VOL; A.Array=B.Array; A.Indx=B.Indx; A.Jndx=B.Jndx; A.Line=B.Line;
B.N=A_N; B.CAP=A_CAP; B.VOL=A_VOL; B.Array=A_Array; B.Indx=A_Indx; B.Jndx=A_Jndx; B.Line=A_Line;
}
//=============================================================================
/*! convert user object to smart-temporary object */
inline _dssmatrix _(dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] _(dssmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
_dssmatrix newmat;
//////// shallow copy ////////
newmat.N =mat.N;
newmat.CAP =mat.CAP;
newmat.VOL =mat.VOL;
newmat.Array =mat.Array;
newmat.Indx =mat.Indx;
newmat.Jndx =mat.Jndx;
newmat.Line =mat.Line;
//////// nullify ////////
mat.N =0;
mat.CAP =0;
mat.VOL =0;
mat.Array =NULL;
mat.Indx =NULL;
mat.Jndx =NULL;
mat.Line.clear();
return newmat;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.