CPPLapack
 All Classes Files Functions Variables Friends Pages
zhematrix_small-functions.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! convert zhematrix_small to zgematrix_small */
3 template<CPPL_INT n>
5 {CPPL_VERBOSE_REPORT;
7  for(CPPL_INT i=0; i<n; i++){
8  for(CPPL_INT j=0; j<=i; j++){ newmat(i,j) =(*this)(i,j); }
9  for(CPPL_INT j=i+1; j<n; j++){ newmat(i,j) =(*this)(j,i); }
10  }
11  return newmat;
12 }
13 
14 //=============================================================================
15 /*! convert zhematrix_small to zhematrix */
16 template<CPPL_INT n>
18 {CPPL_VERBOSE_REPORT;
19  zhematrix newmat(n);
20  for(CPPL_INT i=0; i<n; i++){
21  for(CPPL_INT j=0; j<=i; j++){
22  newmat(i,j) =(*this)(i,j);
23  }
24  }
25  return newmat;
26 }
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 //=============================================================================
36 /*! operator() */
37 template<CPPL_INT n>
38 inline comple& zhematrix_small<n>::operator()(const CPPL_INT& i, const CPPL_INT& j)
39 {CPPL_VERBOSE_REPORT;
40 #ifdef CPPL_DEBUG
41  if(i<j){
42  ERROR_REPORT;
43  std::cerr << "i must be greater than or equal to j since dsymatrix_small is L strage. " << std::endl;
44  std::cerr << "Your input was (" << i << ", " << j << ")." << std::endl;
45  exit(1);
46  }
47 #endif//CPPL_DEBUG
48 
49  //const CPPL_INT I(max(i,j)), J(min(i,j)); return array[(I*(I+1))/2 +J];
50  return array[(i*(i+1))/2 +j]; //l storage
51 }
52 
53 //=============================================================================
54 /*! operator() for const */
55 template<CPPL_INT n>
56 inline comple zhematrix_small<n>::operator()(const CPPL_INT& i, const CPPL_INT& j) const
57 {CPPL_VERBOSE_REPORT;
58 #ifdef CPPL_DEBUG
59  if(i<j){
60  ERROR_REPORT;
61  std::cerr << "i must be greater than or equal to j since dsymatrix_small is L strage. " << std::endl;
62  std::cerr << "Your input was (" << i << ", " << j << ")." << std::endl;
63  exit(1);
64  }
65 #endif//CPPL_DEBUG
66 
67  //const CPPL_INT I(max(i,j)), J(min(i,j)); return array[(I*(I+1))/2 +J];
68  return array[(i*(i+1))/2 +j];
69 }
70 
71 //=============================================================================
72 /*! set */
73 template<CPPL_INT n>
74 inline zhematrix_small<n>& zhematrix_small<n>::set(const CPPL_INT& i, const CPPL_INT& j, const comple& v)
75 {CPPL_VERBOSE_REPORT;
76  (*this)(i,j)=v;
77  return *this;
78 }
79 
80 //=============================================================================
81 /*! operator<< */
82 template<CPPL_INT n>
83 inline std::ostream& operator<<(std::ostream& s, const zhematrix_small<n>& A)
84 {CPPL_VERBOSE_REPORT;
85  s << std::setiosflags(std::ios::showpos);
86  for(CPPL_INT i=0; i<n; i++){
87  for(CPPL_INT j=0; j<=i; j++){ s << " " << A(i,j) << " "<< std::flush; }
88  for(CPPL_INT j=i+1; j<n; j++){ s << "{" << A(j,i) << "}" << std::flush; }
89  s << std::endl;
90  }
91  return s;
92 }
93 
94 //=============================================================================
95 /*! write to file */
96 template<CPPL_INT n>
97 inline void zhematrix_small<n>::write(const char* filename) const
98 {CPPL_VERBOSE_REPORT;
99  std::ofstream ofs(filename, std::ios::trunc);
100  ofs.setf(std::cout.flags());
101  ofs.precision(std::cout.precision());
102  ofs.width(std::cout.width());
103  ofs.fill(std::cout.fill());
104  ofs << "#zhematrix" << " " << n << std::endl;
105  for(CPPL_INT i=0; i<n; i++){
106  for(CPPL_INT j=0; j<=i; j++){
107  ofs << (*this)(i,j) << " ";
108  }
109  ofs << std::endl;
110  }
111  ofs.close();
112 }
113 
114 //=============================================================================
115 /*! read from file */
116 template<CPPL_INT n>
117 inline void zhematrix_small<n>::read(const char* filename)
118 {CPPL_VERBOSE_REPORT;
119  std::ifstream s(filename);
120  if(!s){
121  ERROR_REPORT;
122  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
123  exit(1);
124  }
125 
126  std::string id;
127  s >> id;
128  if( id != "zhematrix" && id != "#zhematrix" ){
129  ERROR_REPORT;
130  std::cerr << "The type name of the file \"" << filename << "\" is not zhematrix." << std::endl
131  << "Its type name was " << id << " ." << std::endl;
132  exit(1);
133  }
134 
135  CPPL_INT _n;
136  s >> _n;
137  if(n!=_n){
138  ERROR_REPORT;
139  std::cerr << "Matrix size is invalid." << std::endl;
140  exit(1);
141  }
142 
143  for(CPPL_INT i=0; i<n; i++){
144  for(CPPL_INT j=0; j<=i; j++ ){
145  s >> operator()(i,j);
146  }
147  }
148  if(s.eof()){
149  ERROR_REPORT;
150  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
151  << "Most likely, there is a lack of data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
152  exit(1);
153  }
154 
155  s.close();
156 }
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////////
160 ///////////////////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////////////////
163 ///////////////////////////////////////////////////////////////////////////////
164 
165 //=============================================================================
166 /*! zero */
167 template<CPPL_INT n>
169 {CPPL_VERBOSE_REPORT;
170  for(CPPL_INT i=0; i<n; i++){
171  for(CPPL_INT j=0; j<=i; j++){
172  (*this)(i,j) =0.;
173  }
174  }
175  return *this;
176 }
177 
178 //=============================================================================
179 /*! identity */
180 template<CPPL_INT n>
182 {CPPL_VERBOSE_REPORT;
183  zero();
184  for(CPPL_INT k=0; k<n; k++){
185  (*this)(k,k) =1.;
186  }
187  return *this;
188 }
189 
190 //=============================================================================
191 /*! return its trace */
192 template<CPPL_INT n>
193 inline comple trace(const zhematrix_small<n>& A)
194 {CPPL_VERBOSE_REPORT;
195  comple trace =comple(0.,0.);
196  for(CPPL_INT i=0; i<n; i++){
197  trace +=A(i,i);
198  }
199  return trace;
200 }
201 
202 //=============================================================================
203 /*! find index of the maximum component */
204 template<CPPL_INT n>
205 inline void idamax(CPPL_INT& I, CPPL_INT& J, const zhematrix_small<n>& A)
206 {CPPL_VERBOSE_REPORT;
207  double max(-1.);
208  for(CPPL_INT i=0; i<n; i++){
209  for(CPPL_INT j=0; j<=i; j++){
210  if( max<fabs(A(i,j)) ){
211  I=i;
212  J=j;
213  max =fabs(A(i,j));
214  }
215  }
216  }
217  return;
218 }
219 
220 //=============================================================================
221 /*! return the maximum component */
222 template<CPPL_INT n>
223 inline comple damax(const zhematrix_small<n>& A)
224 {CPPL_VERBOSE_REPORT;
225  CPPL_INT i(0),j(0);
226  idamax(i,j,A);
227  return A(i,j);
228 }
229 
230 ///////////////////////////////////////////////////////////////////////////////
231 ///////////////////////////////////////////////////////////////////////////////
232 ///////////////////////////////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////////////////////////////
234 ///////////////////////////////////////////////////////////////////////////////
235 ///////////////////////////////////////////////////////////////////////////////
236 
237 //=============================================================================
238 /*! zhematrix_small+=zhematrix_small operator */
239 template<CPPL_INT n>
241 {CPPL_VERBOSE_REPORT;
242  for(CPPL_INT k=0; k<(n*(n+1))/2; k++){
243  A.array[k] +=B.array[k];
244  }
245  return A;
246 }
247 
248 //=============================================================================
249 /*! zhematrix_small-=zhematrix_small operator */
250 template<CPPL_INT n>
252 {CPPL_VERBOSE_REPORT;
253  for(CPPL_INT k=0; k<(n*(n+1))/2; k++){
254  A.array[k] -=B.array[k];
255  }
256  return A;
257 }
258 
259 //=============================================================================
260 /*! zhematrix_small*=double operator */
261 template<CPPL_INT n>
263 {CPPL_VERBOSE_REPORT;
264  for(CPPL_INT i=0; i<n; i++){
265  for(CPPL_INT j=0; j<=i; j++){
266  A(i,j) *=v;
267  }
268  }
269  return A;
270 }
271 
272 //=============================================================================
273 /*! zhematrix_small*=comple operator */
274 template<CPPL_INT n>
276 {CPPL_VERBOSE_REPORT;
277  for(CPPL_INT i=0; i<n; i++){
278  for(CPPL_INT j=0; j<=i; j++){
279  A(i,j) *=v;
280  }
281  }
282  return A;
283 }
284 
285 //=============================================================================
286 /*! zhematrix_small/=double operator */
287 template<CPPL_INT n>
289 {CPPL_VERBOSE_REPORT;
290  for(CPPL_INT i=0; i<n; i++){
291  for(CPPL_INT j=0; j<=i; j++){
292  A(i,j) /=v;
293  }
294  }
295  return A;
296 }
297 
298 //=============================================================================
299 /*! zhematrix_small/=comple operator */
300 template<CPPL_INT n>
302 {CPPL_VERBOSE_REPORT;
303  for(CPPL_INT i=0; i<n; i++){
304  for(CPPL_INT j=0; j<=i; j++){
305  A(i,j) /=v;
306  }
307  }
308  return A;
309 }
310 
311 ///////////////////////////////////////////////////////////////////////////////
312 ///////////////////////////////////////////////////////////////////////////////
313 ///////////////////////////////////////////////////////////////////////////////
314 
315 //=============================================================================
316 /*! unary */
317 template<CPPL_INT n>
319 {CPPL_VERBOSE_REPORT;
320  return A;
321 }
322 
323 //=============================================================================
324 /*! unary */
325 template<CPPL_INT n>
327 {CPPL_VERBOSE_REPORT;
329  for(CPPL_INT i=0; i<n; i++){
330  for(CPPL_INT j=0; j<=i; j++){
331  X(i,j) =-A(i,j);
332  }
333  }
334  return X;
335 }
336 
337 ///////////////////////////////////////////////////////////////////////////////
338 ///////////////////////////////////////////////////////////////////////////////
339 ///////////////////////////////////////////////////////////////////////////////
340 
341 //=============================================================================
342 /*! zhematrix_small+zgematrix_small operator */
343 template<CPPL_INT n>
345 {CPPL_VERBOSE_REPORT;
347  for(CPPL_INT i=0; i<n; i++){
348  for(CPPL_INT j=0; j<i; j++){
349  X(i,j) =A(i,j)+B(i,j);
350  }
351  for(CPPL_INT j=i; j<n; j++){
352  X(i,j) =A(j,i)+B(i,j);
353  }
354  }
355  return X;
356 }
357 
358 //=============================================================================
359 /*! zhematrix_small+zhematrix_small operator */
360 template<CPPL_INT n>
362 {CPPL_VERBOSE_REPORT;
364  for(CPPL_INT i=0; i<n; i++){
365  for(CPPL_INT j=0; j<=i; j++){
366  X(i,j) =A(i,j)+B(i,j);
367  }
368  }
369  return X;
370 }
371 
372 ///////////////////////////////////////////////////////////////////////////////
373 ///////////////////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////////////////
375 
376 //=============================================================================
377 /*! zhematrix_small-zgematrix_small operator */
378 template<CPPL_INT n>
380 {CPPL_VERBOSE_REPORT;
382  for(CPPL_INT i=0; i<n; i++){
383  for(CPPL_INT j=0; j<i; j++){
384  X(i,j) =A(i,j)-B(i,j);
385  }
386  for(CPPL_INT j=i; j<n; j++){
387  X(i,j) =A(j,i)-B(i,j);
388  }
389  }
390  return X;
391 }
392 
393 //=============================================================================
394 /*! zhematrix_small-zhematrix_small operator */
395 template<CPPL_INT n>
397 {CPPL_VERBOSE_REPORT;
399  for(CPPL_INT i=0; i<n; i++){
400  for(CPPL_INT j=0; j<=i; j++){
401  X(i,j) =A(i,j)-B(i,j);
402  }
403  }
404  return X;
405 }
406 
407 ///////////////////////////////////////////////////////////////////////////////
408 ///////////////////////////////////////////////////////////////////////////////
409 ///////////////////////////////////////////////////////////////////////////////
410 
411 //=============================================================================
412 /*! zhematrix_small*zcovector_small operator */
413 template<CPPL_INT n>
415 {CPPL_VERBOSE_REPORT;
417  C.zero();
418  for(CPPL_INT i=0; i<n; i++){
419  for(CPPL_INT j=0; j<i; j++){
420  C(i) +=A(i,j)*B(j);
421  }
422  for(CPPL_INT j=i; j<n; j++){
423  C(i) +=A(j,i)*B(j);
424  }
425  }
426  return C;
427 }
428 
429 //=============================================================================
430 /*! zhematrix_small*zgematrix_small operator */
431 template<CPPL_INT m, CPPL_INT n>
433 {CPPL_VERBOSE_REPORT;
435  X.zero();
436  for(CPPL_INT i=0; i<m; i++){
437  for(CPPL_INT j=0; j<n; j++){
438  for(CPPL_INT k=0; k<i; k++){
439  X(i,j) +=A(i,k)*B(k,j);
440  }
441  for(CPPL_INT k=i; k<m; k++){
442  X(i,j) +=A(k,i)*B(k,j);
443  }
444  }
445  }
446  return X;
447 }
448 
449 //=============================================================================
450 /*! zhematrix_small*zhematrix_small operator */
451 template<CPPL_INT n>
453 {CPPL_VERBOSE_REPORT;
455  X.zero();
456  for(CPPL_INT i=0; i<n; i++){
457  for(CPPL_INT j=0; j<i; j++){
458  for(CPPL_INT k=0; k<j; k++){
459  X(i,j) +=A(i,k)*B(j,k);
460  }
461  for(CPPL_INT k=j; k<i; k++){
462  X(i,j) +=A(i,k)*B(k,j);
463  }
464  for(CPPL_INT k=i; k<n; k++){
465  X(i,j) +=A(k,i)*B(k,j);
466  }
467  }
468  for(CPPL_INT j=i; j<n; j++){
469  for(CPPL_INT k=0; k<i; k++){
470  X(i,j) +=A(i,k)*B(j,k);
471  }
472  for(CPPL_INT k=i; k<j; k++){
473  X(i,j) +=A(k,i)*B(j,k);
474  }
475  for(CPPL_INT k=j; k<n; k++){
476  X(i,j) +=A(k,i)*B(k,j);
477  }
478  }
479  }
480  return X;
481 }
482 
483 //=============================================================================
484 /*! zhematrix_small*double operator */
485 template<CPPL_INT n>
486 inline zhematrix_small<n> operator*(const zhematrix_small<n>& A, const double& v)
487 {CPPL_VERBOSE_REPORT;
489  for(CPPL_INT i=0; i<n; i++){
490  for(CPPL_INT j=0; j<=i; j++){
491  C(i,j) =A(i,j)*v;
492  }
493  }
494  return C;
495 }
496 
497 //=============================================================================
498 /*! zhematrix_small*comple operator */
499 template<CPPL_INT n>
500 inline zhematrix_small<n> operator*(const zhematrix_small<n>& A, const comple& v)
501 {CPPL_VERBOSE_REPORT;
503  for(CPPL_INT i=0; i<n; i++){
504  for(CPPL_INT j=0; j<=i; j++){
505  C(i,j) =A(i,j)*v;
506  }
507  }
508  return C;
509 }
510 
511 ///////////////////////////////////////////////////////////////////////////////
512 ///////////////////////////////////////////////////////////////////////////////
513 ///////////////////////////////////////////////////////////////////////////////
514 
515 //=============================================================================
516 /*! zhematrix_small/double operator */
517 template<CPPL_INT n>
518 inline zhematrix_small<n> operator/(const zhematrix_small<n>& A, const double& v)
519 {CPPL_VERBOSE_REPORT;
521  for(CPPL_INT i=0; i<n; i++){
522  for(CPPL_INT j=0; j<=i; j++){
523  C(i,j) =A(i,j)/v;
524  }
525  }
526  return C;
527 }
528 
529 //=============================================================================
530 /*! zhematrix_small/comple operator */
531 template<CPPL_INT n>
532 inline zhematrix_small<n> operator/(const zhematrix_small<n>& A, const comple& v)
533 {CPPL_VERBOSE_REPORT;
535  for(CPPL_INT i=0; i<n; i++){
536  for(CPPL_INT j=0; j<=i; j++){
537  C(i,j) =A(i,j)/v;
538  }
539  }
540  return C;
541 }
542 
543 ///////////////////////////////////////////////////////////////////////////////
544 ///////////////////////////////////////////////////////////////////////////////
545 ///////////////////////////////////////////////////////////////////////////////
546 
547 //=============================================================================
548 /*! Hadamard product */
549 template<CPPL_INT n>
551 {CPPL_VERBOSE_REPORT;
553  for(CPPL_INT i=0; i<n; i++){
554  for(CPPL_INT j=0; j<=i; j++){
555  C(i,j) =A(i,j)*B(i,j);
556  }
557  for(CPPL_INT j=i+1; j<n; j++){
558  C(i,j) =conj(A(j,i))*B(i,j);
559  }
560  }
561  return C;
562 }
563 
564 //=============================================================================
565 /*! Hadamard product */
566 template<CPPL_INT n>
568 {CPPL_VERBOSE_REPORT;
570  for(CPPL_INT i=0; i<n; i++){
571  for(CPPL_INT j=0; j<=i; j++){
572  C(i,j) =A(i,j)*B(i,j);
573  }
574  }
575  return C;
576 }
comple & operator()(const CPPL_INT &i, const CPPL_INT &j)
comple array[(n *(n+1))/2]
zgematrix_small< n, n > hadamard(const zhematrix_small< n > &A, const zgematrix_small< n, n > &B)
void read(const char *filename)
comple damax(const zhematrix_small< n > &A)
zgematrix_small< n, n > to_zgematrix_small() const
_dgematrix i(const _dgbmatrix &mat)
zhematrix_small< n > operator-(const zhematrix_small< n > &A)
Samll Complex Double-precision Column Vector Class.
_zcovector conj(const _zcovector &vec)
zcovector_small< l > & zero()
zhematrix_small< n > & set(const CPPL_INT &, const CPPL_INT &, const comple &)
zhematrix_small< n > & identity()
zhematrix_small< n > & operator+=(zhematrix_small< n > &A, const zhematrix_small< n > &B)
zgematrix_small< m, n > & zero()
comple trace(const zhematrix_small< n > &A)
const zhematrix_small< n > & operator+(const zhematrix_small< n > &A)
zhematrix_small< n > & operator*=(zhematrix_small< n > &A, const double &v)
zcovector_small< n > operator*(const zhematrix_small< n > &A, const zcovector_small< n > &B)
zhematrix_small< n > & operator/=(zhematrix_small< n > &A, const double &v)
Complex Double-precision Hermitian Matrix Class [l-type (UPLO=l) Strage].
Definition: zhematrix.hpp:4
zhematrix_small< n > & zero()
void write(const char *filename) const
Samll Complex Double-precision General Dence Matrix Class.
void idamax(CPPL_INT &I, CPPL_INT &J, const zhematrix_small< n > &A)
Samll Complex Double-precision Symmetric Matrix Class.
zhematrix to_zhematrix() const
zhematrix_small< n > operator/(const zhematrix_small< n > &A, const double &v)
zhematrix_small< n > & operator-=(zhematrix_small< n > &A, const zhematrix_small< n > &B)