CPPLapack
 All Classes Files Functions Variables Friends Pages
zgsmatrix-misc.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! clear all the matrix data and set the sizes 0 */
3 inline void zgsmatrix::clear()
4 {CPPL_VERBOSE_REPORT;
5  m =0;
6  n =0;
7  data.clear();
8  rows.clear();
9  cols.clear();
10 }
11 
12 //=============================================================================
13 /*! change the matrix into a zero matrix */
15 {CPPL_VERBOSE_REPORT;
16  data.resize(0);
17  for(CPPL_INT i=0; i<m; i++){ rows[i].resize(0); }
18  for(CPPL_INT j=0; j<n; j++){ cols[j].resize(0); }
19  return *this;
20 }
21 
22 //=============================================================================
23 /*! change the matrix into an identity matrix */
25 {CPPL_VERBOSE_REPORT;
26 #ifdef CPPL_DEBUG
27  if(m!=n){
28  ERROR_REPORT;
29  std::cerr << "Only square matrix can be a identity matrix." << std::endl
30  << "The matrix size was " << m << "x" << n << "." << std::endl;
31  exit(1);
32  }
33 #endif//CPPL_DEBUG
34 
35  zero();
36  for(CPPL_INT i=0; i<m; i++){
37  put(i,i, comple(1.,0.));
38  }
39  return *this;
40 }
41 
42 //=============================================================================
43 /*! change sign(+/-) of the matrix */
44 inline void zgsmatrix::chsign()
45 {CPPL_VERBOSE_REPORT;
46  const std::vector<zcomponent>::iterator data_end =data.end();
47  for(std::vector<zcomponent>::iterator it=data.begin(); it!=data_end; it++){
48  it->v =-it->v;
49  }
50 }
51 
52 //=============================================================================
53 /*! make a deep copy of the matrix */
54 inline void zgsmatrix::copy(const zgsmatrix& mat)
55 {CPPL_VERBOSE_REPORT;
56  m =mat.m;
57  n =mat.n;
58  data =mat.data;
59  rows =mat.rows;
60  cols =mat.cols;
61 }
62 
63 //=============================================================================
64 /*! make a shallow copy of the matrix\n
65  This function is not designed to be used in project codes. */
66 inline void zgsmatrix::shallow_copy(const _zgsmatrix& mat)
67 {CPPL_VERBOSE_REPORT;
68  data.clear();
69  rows.clear();
70  cols.clear();
71 
72  m =mat.m;
73  n =mat.n;
74  data.swap(mat.data);
75  rows.swap(mat.rows);
76  cols.swap(mat.cols);
77 
78  mat.nullify();
79 }
80 
81 //=============================================================================
82 /*! resize the matrix */
83 inline zgsmatrix& zgsmatrix::resize(const CPPL_INT& _m, const CPPL_INT& _n, const CPPL_INT _c, const CPPL_INT _l)
84 {CPPL_VERBOSE_REPORT;
85 #ifdef CPPL_DEBUG
86  if( _m<0 || _n<0 || _c<0 ){
87  ERROR_REPORT;
88  std::cerr << "Matrix sizes and the length of arrays must be positive integers. " << std::endl
89  << "Your input was (" << _m << "," << _n << "," << _c << "," << _l << ")." << std::endl;
90  exit(1);
91  }
92 #endif//CPPL_DEBUG
93 
94  m =_m;
95  n =_n;
96  data.resize(0);
97  data.reserve(_c);
98  rows.resize(m);
99  for(CPPL_INT i=0; i<m; i++){
100  rows[i].resize(0);
101  rows[i].reserve(_l);
102  }
103  cols.resize(n);
104  for(CPPL_INT i=0; i<n; i++){
105  cols[i].resize(0);
106  cols[i].reserve(_l);
107  }
108 
109  return *this;
110 }
111 
112 //=============================================================================
113 /*! stretch the matrix size */
114 inline void zgsmatrix::stretch(const CPPL_INT& dm, const CPPL_INT& dn)
115 {CPPL_VERBOSE_REPORT;
116 #ifdef CPPL_DEBUG
117  if( m+dm<0 || n+dn<0 ){
118  ERROR_REPORT;
119  std::cerr << "The new matrix size must be larger than zero. " << std::endl
120  << "Your input was (" << dm << ", " << dn << ")." << std::endl;
121  exit(1);
122  }
123 #endif//CPPL_DEBUG
124 
125  //////// zero ////////
126  if(dm==0 && dn==0){ return; }
127 
128  //////// non-zero ////////
129  m +=dm;
130  n +=dn;
131 
132  //// for rows ////
133  if(dm<0){
134  //// delete components over the new size ////
135  const std::vector<zcomponent>::reverse_iterator data_rend =data.rend();
136  for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data_rend; it++){
137  if( it->i>=m ){ del( CPPL_INT(data_rend-it-1) ); }
138  }
139  //// shrink rows ////
140  for(CPPL_INT i=0; i<-dm; i++){
141  rows.pop_back();
142  }
143  }
144  else{//dm>0
145  //// expand rows ////
146  for(CPPL_INT i=0; i<dm; i++){
147  rows.push_back( std::vector<CPPL_INT>(0) );
148  }
149  }
150 
151  //// for cols ////
152  if(dn<0){
153  //// delete components over the new size ////
154  const std::vector<zcomponent>::reverse_iterator data_rend =data.rend();
155  for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data_rend; it++){
156  if( it->j>=n ){ del( CPPL_INT(data_rend-it-1) ); }
157  }
158  for(CPPL_INT j=0; j<-dn; j++){
159  cols.pop_back();
160  }
161  }
162  else{//dn>0
163  //// expand cols ////
164  for(CPPL_INT j=0; j<dn; j++){
165  cols.push_back( std::vector<CPPL_INT>(0) );
166  }
167  }
168 }
169 
170 //=============================================================================
171 /*! check if the component is listed */
172 inline bool zgsmatrix::isListed(const CPPL_INT& i, const CPPL_INT& j)
173 {CPPL_VERBOSE_REPORT;
174 #ifdef CPPL_DEBUG
175  if( i<0 || j<0 || m<=i || n<=j ){
176  ERROR_REPORT;
177  std::cerr << "The required component is out of the matrix size." << std::endl
178  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << "." << std::endl;
179  exit(1);
180  }
181 #endif//CPPL_DEBUG
182 
183  const std::vector<CPPL_INT>::const_iterator rows_i_end =rows[i].end();
184  for(std::vector<CPPL_INT>::const_iterator p=rows[i].begin(); p!=rows_i_end; p++){
185  if( data[*p].j==j ){ return 1; }
186  }
187 
188  return 0;
189 }
190 
191 //=============================================================================
192 /*! return the element number of the component */
193 inline CPPL_INT zgsmatrix::number(const CPPL_INT& i, const CPPL_INT& j)
194 {CPPL_VERBOSE_REPORT;
195 #ifdef CPPL_DEBUG
196  if( i<0 || j<0 || m<=i || n<=j ){
197  ERROR_REPORT;
198  std::cerr << "The required component is out of the matrix size." << std::endl
199  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << "." << std::endl;
200  exit(1);
201  }
202 #endif//CPPL_DEBUG
203 
204  const std::vector<CPPL_INT>::iterator rows_i_end =rows[i].end();
205  for(std::vector<CPPL_INT>::iterator p=rows[i].begin(); p!=rows_i_end; p++){
206  if( data[*p].j==j ){ return *p; }
207  }
208 
209  return -1;
210 }
211 
212 //=============================================================================
213 /*! erase components less than DBL_MIN */
214 inline void zgsmatrix::diet(const double eps)
215 {CPPL_VERBOSE_REPORT;
216  const std::vector<zcomponent>::reverse_iterator data_rend =data.rend();
217  for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data_rend; it++){
218  if( fabs(it->v.real())<eps && fabs(it->v.imag())<eps ){ del( CPPL_INT(data_rend-it-1) ); }
219  }
220 }
221 
222 //=============================================================================
223 /*! health checkup */
224 inline void zgsmatrix::checkup()
225 {CPPL_VERBOSE_REPORT;
226  //////////////// data ////////////////
227  //////// check i,j ////////
228  const std::vector<zcomponent>::const_iterator data_end =data.end();
229  for(std::vector<zcomponent>::const_iterator it=data.begin(); it!=data_end; it++){
230  if( it->i>=m || it->j>=n ){
231  ERROR_REPORT;
232  std::cerr << "A component, (" << it->i << ", " << it->j << "), is out of matrix size." << std::endl;
233  exit(1);
234  }
235  }
236  //////// check double listing ////////
237 
238  //////////////// rows ////////////////
239  //////////////// cols ////////////////
240 
241  //////////////// NOTE ////////////////
242  std::cerr << "# [NOTE] zgsmatrix::checkup(): This sparse matrix is fine." << std::endl;
243 }
244 
245 ///////////////////////////////////////////////////////////////////////////////
246 ///////////////////////////////////////////////////////////////////////////////
247 ///////////////////////////////////////////////////////////////////////////////
248 
249 //=============================================================================
250 /*! get row of the matrix */
251 inline _zrovector zgsmatrix::row(const CPPL_INT& _m) const
252 {CPPL_VERBOSE_REPORT;
253 #ifdef CPPL_DEBUG
254  if( _m<0 || _m>m ){
255  ERROR_REPORT;
256  std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
257  << "Your input was " << _m << "." << std::endl;
258  exit(1);
259  }
260 #endif//CPPL_DEBUG
261 
262  zrovector vec( zrovector(n).zero() );
263 
264  const std::vector<CPPL_INT>::const_iterator rows__m_end =rows[_m].end();
265  for(std::vector<CPPL_INT>::const_iterator p=rows[_m].begin(); p!=rows__m_end; p++){
266  vec(data[*p].j) =data[*p].v;
267  }
268 
269  return _(vec);
270 }
271 
272 //=============================================================================
273 /*! get column of the matrix */
274 inline _zcovector zgsmatrix::col(const CPPL_INT& _n) const
275 {CPPL_VERBOSE_REPORT;
276 #ifdef CPPL_DEBUG
277  if( _n<0 || _n>n ){
278  ERROR_REPORT;
279  std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
280  << "Your input was " << _n << "." << std::endl;
281  exit(1);
282  }
283 #endif//CPPL_DEBUG
284 
285  zcovector vec( zcovector(m).zero() );
286 
287  const std::vector<CPPL_INT>::const_iterator cols__n_end =cols[_n].end();
288  for(std::vector<CPPL_INT>::const_iterator p=cols[_n].begin(); p!=cols__n_end; p++){
289  vec(data[*p].i) =data[*p].v;
290  }
291 
292  return _(vec);
293 }
294 
295 ///////////////////////////////////////////////////////////////////////////////
296 ///////////////////////////////////////////////////////////////////////////////
297 ///////////////////////////////////////////////////////////////////////////////
298 
299 //=============================================================================
300 /*! swap two matrices */
301 inline void swap(zgsmatrix& A, zgsmatrix& B)
302 {CPPL_VERBOSE_REPORT;
303  std::swap(A.n,B.n);
304  std::swap(A.m,B.m);
305  std::swap(A.data,B.data);
306  std::swap(A.rows,B.rows);
307  std::swap(A.cols,B.cols);
308 }
309 
310 //=============================================================================
311 /*! convert user object to smart-temporary object */
312 inline _zgsmatrix _(zgsmatrix& mat)
313 {CPPL_VERBOSE_REPORT;
314  _zgsmatrix newmat;
315 
316  //////// shallow copy ////////
317  newmat.n =mat.n;
318  newmat.m =mat.m;
319  std::swap(newmat.data,mat.data);
320  std::swap(newmat.rows,mat.rows);
321  std::swap(newmat.cols,mat.cols);
322 
323  //////// nullify ////////
324  mat.m =0;
325  mat.n =0;
326 
327  return newmat;
328 }
void clear()
std::vector< std::vector< CPPL_INT > > rows
array of vector to store the entry information of component for each row
Definition: _zgsmatrix.hpp:12
std::vector< std::vector< CPPL_INT > > cols
array of vector to store the entry information of component for each column
Definition: _zgsmatrix.hpp:13
Complex Double-precision General Sparse Matrix Class.
Definition: zgsmatrix.hpp:3
friend _zgsmatrix _(zgsmatrix &)
zgsmatrix & del(const CPPL_INT, const CPPL_INT)
bool isListed(const CPPL_INT &, const CPPL_INT &)
_zcovector col(const CPPL_INT &) const
zgsmatrix & resize(const CPPL_INT &, const CPPL_INT &, const CPPL_INT=0, const CPPL_INT=0)
_dgematrix i(const _dgbmatrix &mat)
CPPL_INT n
matrix column size
Definition: _zgsmatrix.hpp:10
void stretch(const CPPL_INT &, const CPPL_INT &)
(DO NOT USE) Smart-temporary Real Double-precision General Sparse Matrix Class
Definition: _zgsmatrix.hpp:3
zgsmatrix & put(const CPPL_INT &, const CPPL_INT &, const comple &)
void copy(const zgsmatrix &)
void shallow_copy(const _zgsmatrix &)
_zrovector row(const CPPL_INT &) const
std::vector< std::vector< CPPL_INT > > rows
array of vector to store the entry information of component for each row
Definition: zgsmatrix.hpp:12
std::vector< zcomponent > data
matrix data
Definition: zgsmatrix.hpp:11
Complex Double-precision Row Vector Class.
Definition: zrovector.hpp:3
(DO NOT USE) Smart-temporary Complex Double-precision Row Vector Class
Definition: _zrovector.hpp:3
zgsmatrix & identity()
void swap(zgsmatrix &A, zgsmatrix &B)
void diet(const double=DBL_MIN)
CPPL_INT m
matrix row size
Definition: _zgsmatrix.hpp:9
CPPL_INT number(const CPPL_INT &, const CPPL_INT &)
std::vector< std::vector< CPPL_INT > > cols
array of vector to store the entry information of component for each column
Definition: zgsmatrix.hpp:13
CPPL_INT m
matrix row size
Definition: zgsmatrix.hpp:9
zgsmatrix & zero()
void nullify() const
Complex Double-precision Column Vector Class.
Definition: zcovector.hpp:3
(DO NOT USE) Smart-temporary Complex Double-precision Column Vector Class
Definition: _zcovector.hpp:3
CPPL_INT n
matrix column size
Definition: zgsmatrix.hpp:10
_zgsmatrix _(zgsmatrix &mat)
std::vector< zcomponent > data
matrix data
Definition: _zgsmatrix.hpp:11