Menu

[r151]: / trunk / include / zgsmatrix- / zgsmatrix-misc.hpp  Maximize  Restore  History

Download this file

316 lines (278 with data), 9.2 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
//=============================================================================
/*! clear all the matrix data and set the sizes 0 */
inline void zgsmatrix::clear()
{CPPL_VERBOSE_REPORT;
m =0;
n =0;
data.clear();
rows.clear();
cols.clear();
}
//=============================================================================
/*! change the matrix into a zero matrix */
inline zgsmatrix& zgsmatrix::zero()
{CPPL_VERBOSE_REPORT;
data.resize(0);
for(long i=0; i<m; i++){ rows[i].resize(0); }
for(long j=0; j<n; j++){ cols[j].resize(0); }
return *this;
}
//=============================================================================
/*! change the matrix into an identity matrix */
inline zgsmatrix& zgsmatrix::identity()
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if(m!=n){
ERROR_REPORT;
std::cerr << "Only square matrix can be a identity matrix." << std::endl
<< "The matrix size was " << m << "x" << n << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
zero();
for(long i=0; i<m; i++){
put(i,i, comple(1.,0.));
}
return *this;
}
//=============================================================================
/*! change sign(+/-) of the matrix */
inline void zgsmatrix::chsign()
{CPPL_VERBOSE_REPORT;
for(std::vector<zcomponent>::iterator it=data.begin(); it!=data.end(); it++){
it->v =-it->v;
}
}
//=============================================================================
/*! make a deep copy of the matrix */
inline void zgsmatrix::copy(const zgsmatrix& mat)
{CPPL_VERBOSE_REPORT;
m =mat.m;
n =mat.n;
data =mat.data;
rows =mat.rows;
cols =mat.cols;
}
//=============================================================================
/*! make a shallow copy of the matrix\n
This function is not designed to be used in project codes. */
inline void zgsmatrix::shallow_copy(const _zgsmatrix& mat)
{CPPL_VERBOSE_REPORT;
data.clear();
rows.clear();
cols.clear();
m =mat.m;
n =mat.n;
data.swap(mat.data);
rows.swap(mat.rows);
cols.swap(mat.cols);
mat.nullify();
}
//=============================================================================
/*! resize the matrix */
inline zgsmatrix& zgsmatrix::resize(const long& _m, const long& _n, const long _c, const long _l)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( _m<0 || _n<0 || _c<0 ){
ERROR_REPORT;
std::cerr << "Matrix sizes and the length of arrays must be positive integers. " << std::endl
<< "Your input was (" << _m << "," << _n << "," << _c << "," << _l << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
m =_m;
n =_n;
data.resize(0);
data.reserve(_c);
rows.resize(m);
for(long i=0; i<m; i++){
rows[i].resize(0);
rows[i].reserve(_l);
}
cols.resize(n);
for(long i=0; i<n; i++){
cols[i].resize(0);
cols[i].reserve(_l);
}
return *this;
}
//=============================================================================
/*! stretch the matrix size */
inline void zgsmatrix::stretch(const long& dm, const long& dn)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( m+dm<0 || n+dn<0 ){
ERROR_REPORT;
std::cerr << "The new matrix size must be larger than zero. " << std::endl
<< "Your input was (" << dm << ", " << dn << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
//////// zero ////////
if(dm==0 && dn==0){ return; }
//////// non-zero ////////
m +=dm;
n +=dn;
//// for rows ////
if(dm<0){
//// delete components over the new size ////
for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
if( long(it->i)>=m ){ del(data.rend()-it-1); }
}
//// shrink rows ////
for(long i=0; i<-dm; i++){
rows.pop_back();
}
}
else{//dm>0
//// expand rows ////
for(long i=0; i<dm; i++){
rows.push_back( std::vector<uint32_t>(0) );
}
}
//// for cols ////
if(dn<0){
//// delete components over the new size ////
for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
if( long(it->j)>=n ){ del(data.rend()-it-1); }
}
for(long j=0; j<-dn; j++){
cols.pop_back();
}
}
else{//dn>0
//// expand cols ////
for(long j=0; j<dn; j++){
cols.push_back( std::vector<uint32_t>(0) );
}
}
}
//=============================================================================
/*! check if the component is listed */
inline bool zgsmatrix::isListed(const long& i, const long& j)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || m<=i || n<=j ){
ERROR_REPORT;
std::cerr << "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<uint32_t>::const_iterator p=rows[i].begin(); p!=rows[i].end(); p++){
if( long(data[*p].j)==j ){ return 1; }
}
return 0;
}
//=============================================================================
/*! return the element number of the component */
inline long zgsmatrix::number(const long& i, const long& j)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || m<=i || n<=j ){
ERROR_REPORT;
std::cerr << "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<uint32_t>::iterator p=rows[i].begin(); p!=rows[i].end(); p++){
if( long(data[*p].j)==j ){ return *p; }
}
return -1;
}
//=============================================================================
/*! erase components less than DBL_MIN */
inline void zgsmatrix::diet(const double eps)
{CPPL_VERBOSE_REPORT;
for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
if( fabs(it->v.real())<eps && fabs(it->v.imag())<eps ){ del(data.rend()-it-1); }
}
}
//=============================================================================
/*! health checkup */
inline void zgsmatrix::checkup()
{CPPL_VERBOSE_REPORT;
//////////////// data ////////////////
//////// check i,j ////////
for(std::vector<zcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
if( long(it->i)>=m || long(it->j)>=n ){
ERROR_REPORT;
std::cerr << "A component, (" << it->i << ", " << it->j << "), is out of matrix size." << std::endl;
exit(1);
}
}
//////// check double listing ////////
//////////////// rows ////////////////
//////////////// cols ////////////////
//////////////// NOTE ////////////////
std::cerr << "# [NOTE] zgsmatrix::checkup(): This sparse matrix is fine." << std::endl;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! get row of the matrix */
inline _zrovector zgsmatrix::row(const long& _m) const
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( _m<0 || _m>m ){
ERROR_REPORT;
std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
<< "Your input was " << _m << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
zrovector vec( zrovector(n).zero() );
for(std::vector<uint32_t>::const_iterator p=rows[_m].begin(); p!=rows[_m].end(); p++){
vec(data[*p].j) =data[*p].v;
}
return _(vec);
}
//=============================================================================
/*! get column of the matrix */
inline _zcovector zgsmatrix::col(const long& _n) const
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( _n<0 || _n>n ){
ERROR_REPORT;
std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
<< "Your input was " << _n << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
zcovector vec( zcovector(m).zero() );
for(std::vector<uint32_t>::const_iterator p=cols[_n].begin(); p!=cols[_n].end(); p++){
vec(data[*p].i) =data[*p].v;
}
return _(vec);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! swap two matrices */
inline void swap(zgsmatrix& A, zgsmatrix& B)
{CPPL_VERBOSE_REPORT;
std::swap(A.n,B.n);
std::swap(A.m,B.m);
std::swap(A.data,B.data);
std::swap(A.rows,B.rows);
std::swap(A.cols,B.cols);
}
//=============================================================================
/*! convert user object to smart-temporary object */
inline _zgsmatrix _(zgsmatrix& mat)
{CPPL_VERBOSE_REPORT;
_zgsmatrix newmat;
//////// shallow copy ////////
newmat.n =mat.n;
newmat.m =mat.m;
std::swap(newmat.data,mat.data);
std::swap(newmat.rows,mat.rows);
std::swap(newmat.cols,mat.cols);
//////// nullify ////////
mat.m =0;
mat.n =0;
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.