VERB_code_2.3
dssmatrix-misc.hpp
1 //=============================================================================
3 inline void dssmatrix::clear()
4 {
5 #ifdef CPPL_VERBOSE
6  std::cerr << "# [MARK] dssmatrix::clear()"
7  << std::endl;
8 #endif//CPPL_VERBOSE
9 
10 #ifdef CPPL_DEBUG
11  std::cerr << "# [NOTE] dssmatrix::clear() "
12  << " An array at " << Array
13  << " is going to be cleared." << std::endl;
14 #endif//CPPL_DEBUG
15 
16  M =N =CAP =VOL=0;
17  delete [] Array; Array =NULL;
18  delete [] Indx; Indx =NULL;
19  delete [] Jndx; Jndx =NULL;
20 }
21 
22 //=============================================================================
24 inline void dssmatrix::zero()
25 {
26 #ifdef CPPL_VERBOSE
27  std::cerr << "# [MARK] dssmatrix::zero()"
28  << std::endl;
29 #endif//CPPL_VERBOSE
30 
31  VOL =0;
32 }
33 
34 //=============================================================================
36 inline void dssmatrix::chsign()
37 {
38 #ifdef CPPL_VERBOSE
39  std::cerr << "# [MARK] dssmatrix::chsign()"
40  << std::endl;
41 #endif//CPPL_VERBOSE
42 
43  for(long i=0; i<VOL; i++){ Array[i] =-Array[i]; }
44 }
45 
46 //=============================================================================
48 inline void dssmatrix::copy(const dssmatrix& mat)
49 {
50 #ifdef CPPL_VERBOSE
51  std::cerr << "# [MARK] dssmatrix::copy(const dssmatrix&)"
52  << std::endl;
53 #endif//CPPL_VERBOSE
54 
55 #ifdef CPPL_DEBUG
56  std::cerr << "# [NOTE] dssmatrix::copy(const dssmatrix&) "
57  << "A dssmatrix at " << Array << " is going to be deleted.";
58 #endif//CPPL_DEBUG
59 
60  resize(mat.M, mat.N, mat.CAP);
61  VOL =mat.VOL;
62  dcopy_(VOL, mat.Array, 1, Array, 1);
63  for(long i=0; i<VOL; i++){
64  Indx[i] =mat.Indx[i];
65  Jndx[i] =mat.Jndx[i];
66  }
67 
68 #ifdef CPPL_DEBUG
69  std::cerr << " Then, a COPY of a dssmatrix has been cleated at "
70  << Array << "." << std::endl;
71 #endif//CPPL_DEBUG
72 }
73 
74 //=============================================================================
77 inline void dssmatrix::shallow_copy(const _dssmatrix& mat)
78 {
79 #ifdef CPPL_VERBOSE
80  std::cerr << "# [MARK] dssmatrix::shallow_copy(const _dssmatrix&)"
81  << std::endl;
82 #endif//CPPL_VERBOSE
83 
84 #ifdef CPPL_DEBUG
85  std::cerr << "# [NOTE] dssmatrix:shallow_copy(const _dssmatrix&) "
86  << "A dssmatrix at " << Array << " is going to be deleted, "
87  << "and point at " << mat.Array << " instead." << std::endl;
88 #endif//CPPL_DEBUG
89 
90  delete [] Array;
91  delete [] Indx;
92  delete [] Jndx;
93  M =mat.M;
94  N =mat.N;
95  CAP =mat.CAP;
96  VOL =mat.VOL;
97  Array =mat.Array;
98  Indx =mat.Indx;
99  Jndx =mat.Jndx;
100 }
101 
102 //=============================================================================
104 inline void dssmatrix::resize(const long& _m, const long& _n, const long& _c)
105 {
106 #ifdef CPPL_VERBOSE
107  std::cerr << "# [MARK] dssmatrix::resize(const long&, const long&, const long&)"
108  << std::endl;
109 #endif//CPPL_VERBOSE
110 
111 #ifdef CPPL_DEBUG
112  if( _m<0 || _n<0 || _c<0 ){
113  std::cerr << "[ERROR] dssmatrix::resize"
114  << "(const long&, const long&, const long&)"
115  << std::endl
116  << "Matrix sizes and the length of arrays "
117  << "must be positive integers. " << std::endl
118  << "Your input was (" << _m << "," << _n << "," << _c << ")."
119  << std::endl;
120  exit(1);
121  }
122 #endif//CPPL_DEBUG
123 
124  delete [] Array;
125  M =_m;
126  N =_n;
127  CAP =_c;
128  VOL =0;
129  Array =new double[CAP];
130  Indx =new long[CAP];
131  Jndx =new long[CAP];
132 }
133 
134 //=============================================================================
136 inline void dssmatrix::expand(const long& dc)
137 {
138 #ifdef CPPL_VERBOSE
139  std::cerr << "# [MARK] dssmatrix::expand(const long&)"
140  << std::endl;
141 #endif//CPPL_VERBOSE
142 
143 #ifdef CPPL_DEBUG
144  if( dc<0 ){
145  std::cerr << "[ERROR] dssmatrix::expand(const long&)" << std::endl
146  << "The argument must be a positive integer. " << std::endl
147  << "Your input was (" << dc << ")." << std::endl;
148  exit(1);
149  }
150 #endif//CPPL_DEBUG
151 
152  CAP+=dc;
153  double* newArray(new double[CAP]);
154  long *newIndx(new long[CAP]), *newJndx(new long[CAP]);
155 
156  for(int c=0; c<VOL; c++){
157  newArray[c] =Array[c];
158  newIndx[c] =Indx[c];
159  newJndx[c] =Jndx[c];
160  }
161 
162  delete [] Array;
163  delete [] Indx;
164  delete [] Jndx;
165  Array =newArray;
166  Indx =newIndx;
167  Jndx =newJndx;
168 }
169 
170 //=============================================================================
172 inline bool dssmatrix::isListed(const long& i, const long& j)
173 {
174 #ifdef CPPL_VERBOSE
175  std::cerr << "# [MARK] dssmatrix::isListed(const long&, const long&)"
176  << std::endl;
177 #endif//CPPL_VERBOSE
178 
179 #ifdef CPPL_DEBUG
180  if( i<0 || j<0 || M<=i || N<=j ){
181  std::cerr << "[ERROR] dssmatrix::isListed(const long&, const long&)"
182  << std::endl
183  << "The required component is out of the matrix size."
184  << std::endl
185  << "Your input was (" << i << "," << j << ")." << std::endl;
186  exit(1);
187  }
188 #endif//CPPL_DEBUG
189 
190  for(int c=0; c<VOL; c++){
191  if(Indx[c]==i && Jndx[c]==j){ return 1; }
192  }
193 
194  return 0;
195 }
196 
197 
198 //=============================================================================
200 inline long dssmatrix::number(const long& i, const long& j)
201 {
202 #ifdef CPPL_VERBOSE
203  std::cerr << "# [MARK] dssmatrix::number(const long&, const long&)"
204  << std::endl;
205 #endif//CPPL_VERBOSE
206 
207 #ifdef CPPL_DEBUG
208  if( i<0 || j<0 || M<=i || N<=j ){
209  std::cerr << "[ERROR] dssmatrix::number(const long&, const long&)"
210  << std::endl
211  << "The required component is out of the matrix size."
212  << std::endl
213  << "Your input was (" << i << "," << j << ")." << std::endl;
214  exit(1);
215  }
216 #endif//CPPL_DEBUG
217 
218  for(long c=0; c<VOL; c++){
219  if(Indx[c]==i && Jndx[c]==j){ return c; }
220  }
221 
222  return -1;
223 }
224 
225 
226 //=============================================================================
228 inline void swap(dssmatrix& A, dssmatrix& B)
229 {
230 #ifdef CPPL_VERBOSE
231  std::cerr << "# [MARK] swap(dssmatrix&, dssmatrix&)"
232  << std::endl;
233 #endif//CPPL_VERBOSE
234 
235  long A_M(A.M), A_N(A.N), A_CAP(A.CAP), A_VOL(A.VOL);
236  double* A_Array(A.Array);
237  long *A_Indx(A.Indx), *A_Jndx(A.Jndx);
238  A.M=B.M; A.N=B.N; A.CAP=B.CAP; A.VOL=B.VOL;
239  A.Array=B.Array; A.Indx=B.Indx; A.Jndx=B.Jndx;
240  B.M=A_M; B.N=A_N; B.CAP=A_CAP; B.VOL=A_VOL;
241  B.Array=A_Array; B.Indx=A_Indx; B.Jndx=A_Jndx;
242 }
243 
244 //=============================================================================
246 inline _dssmatrix _(dssmatrix& mat)
247 {
248 #ifdef CPPL_VERBOSE
249  std::cerr << "# [MARK] _(dssmatrix&)"
250  << std::endl;
251 #endif//CPPL_VERBOSE
252 
253  _dssmatrix newmat;
254 
255  newmat.M =mat.M;
256  newmat.N =mat.N;
257  newmat.CAP =mat.CAP;
258  newmat.VOL =mat.VOL;
259  newmat.Array =mat.Array;
260  newmat.Indx =mat.Indx;
261  newmat.Jndx =mat.Jndx;
262 
263  mat.M =0;
264  mat.N =0;
265  mat.CAP =0;
266  mat.VOL =0;
267  mat.Array =NULL;
268  mat.Indx =NULL;
269  mat.Jndx =NULL;
270 
271  return newmat;
272 }
273 
274 
275 
279 
280 //=============================================================================
282 inline void dssmatrix::checkup()
283 {
284 #ifdef CPPL_VERBOSE
285  std::cerr << "# [MARK] dssmatrix::checkup()"
286  << std::endl;
287 #endif//CPPL_VERBOSE
288 
290  if(CAP<0){
291  std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
292  << "The cap is not valid." << std::endl
293  << "CAP was " << CAP << "." << std::endl;
294  exit(1);
295  }
296 
298  if(VOL<0 || VOL>CAP){
299  std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
300  << "The vol is not valid." << std::endl
301  << "VOL was " << VOL << "while CAP is " << CAP << "."
302  << std::endl;
303  exit(1);
304  }
305 
307  for(long c=0; c<VOL; c++){
309  if(Indx[c]<0 || Indx[c]>=M){
310  std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
311  << "The indx of the " << c
312  << "th element is out of the matrix size." << std::endl
313  << "Indx[" << c << "] was " << Indx[c] << "." << std::endl;
314  exit(1);
315  }
316 
318  if(Jndx[c]<0 || Jndx[c]>=N){
319  std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
320  << "The jndx of the " << c
321  << "th element is out of the matrix size." << std::endl
322  << "Jndx[" << c << "] was " << Jndx[c] << "." << std::endl;
323  exit(1);
324  }
325 
327  for(long C=c+1; C<VOL; C++){
328  if(Indx[c]==Indx[C] && Jndx[c]==Jndx[C]){
329  std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
330  << "The (" << Indx[c] << ", " << Jndx[c]
331  << ") component is double-listed at the "
332  << c << "th and the" << C << "the elements."<< std::endl;
333  exit(1);
334  }
335  }
336  }
337 
339  std::cerr << "# [NOTE] dssmatrix::checkup() "
340  << "This sparse matrix is fine." << std::endl;
341 }
void zero()
Definition: dssmatrix-misc.hpp:24
void copy(const dssmatrix &)
Definition: dssmatrix-misc.hpp:48
void checkup()
Definition: dssmatrix-misc.hpp:282
(DO NOT USE) Smart-temporary Real Double-precision Sparse Matrix Class
Definition: _dssmatrix.hpp:3
void chsign()
Definition: dssmatrix-misc.hpp:36
long M
matrix row size
Definition: _dssmatrix.hpp:7
long CAP
the length of data arrays
Definition: _dssmatrix.hpp:9
void shallow_copy(const _dssmatrix &)
Definition: dssmatrix-misc.hpp:77
bool isListed(const long &, const long &)
Definition: dssmatrix-misc.hpp:172
long * Indx
1D array to store the i-index of non-zero matrix components
Definition: _dssmatrix.hpp:12
double * Array
1D array to store non-zero matrix data
Definition: _dssmatrix.hpp:11
void clear()
Definition: dssmatrix-misc.hpp:3
long VOL
the number of non-zero components
Definition: _dssmatrix.hpp:10
long number(const long &, const long &)
Definition: dssmatrix-misc.hpp:200
long * Jndx
1D array to store the j-index of non-zero matrix components
Definition: _dssmatrix.hpp:13
long N
matrix column size
Definition: _dssmatrix.hpp:8
Real Double-precision Sparse Matrix Class.
Definition: dssmatrix.hpp:3
double * Array
1D Array to store vector data
Definition: _drovector.hpp:8
void resize(const long &, const long &, const long &)
Definition: dssmatrix-misc.hpp:104
void expand(const long &)
Definition: dssmatrix-misc.hpp:136