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