CPPLapack
 All Classes Files Functions Variables Friends Pages
zhematrix-io.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! operator() for non-const object */
3 inline zhecomplex zhematrix::operator()(const CPPL_INT& i, const CPPL_INT& j)
4 {CPPL_VERBOSE_REPORT;
5 #ifdef CPPL_DEBUG
6  if( i<0 || j<0 || n<=i || n<=j ){
7  ERROR_REPORT;
8  std::cerr << "The required component is out of the matrix size." << std::endl
9  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
10  exit(1);
11  }
12 #endif//CPPL_DEBUG
13 
14  if(i>=j){ return zhecomplex(i,j, darray[j][i]); }
15  else { return zhecomplex(i,j, darray[i][j]); }
16 }
17 
18 //=============================================================================
19 /*! operator() for const object */
20 inline comple zhematrix::operator()(const CPPL_INT& i, const CPPL_INT& j) const
21 {CPPL_VERBOSE_REPORT;
22 #ifdef CPPL_DEBUG
23  if( i<0 || j<0 || n<=i || n<=j ){
24  ERROR_REPORT;
25  std::cerr << "The required component is out of the matrix size." << std::endl
26  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
27  exit(1);
28  }
29 #endif//CPPL_DEBUG
30 
31  if(i>=j){ return darray[j][i]; }
32  else { return std::conj(darray[i][j]); }
33 }
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 //=============================================================================
40 /*! set value for const object */
41 inline zhematrix& zhematrix::set(const CPPL_INT& i, const CPPL_INT& j, const comple& v)
42 {CPPL_VERBOSE_REPORT;
43 #ifdef CPPL_DEBUG
44  if( i<0 || j<0 || n<=i || n<=j ){
45  ERROR_REPORT;
46  std::cerr << "The required component is out of the matrix size." << std::endl
47  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
48  exit(1);
49  }
50 #endif//CPPL_DEBUG
51 
52  //if(i>=j){ array[i+n*j] = v; }
53  //else{ array[j+n*i] = std::conj(v); }
54  if(i>=j){ darray[j][i] = v; }
55  else{ darray[i][j] = std::conj(v); }
56 
57  return *this;
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////////
63 
64 //=============================================================================
65 inline std::ostream& operator<<(std::ostream& s, const zhematrix& mat)
66 {CPPL_VERBOSE_REPORT;
67  for(CPPL_INT i=0; i<mat.n; i++){
68  for(CPPL_INT j=0; j<mat.n; j++){
69  if(i>j){ s << " " << mat(i,j) << " "; }
70  else if(i==j){ s << " " << std::real(mat(i,i)) << " "; }
71  else{ s << "{" << std::conj(mat(j,i)) << "} "; }
72  }
73  s << std::endl;
74 
75 #ifdef CPPL_DEBUG
76  if(std::fabs(std::imag(mat(i,i))) > DBL_MIN){
77  WARNING_REPORT;
78  std::cerr << "The " << i << "th diagonal component of the zhematrix is not a real number." << std::endl;
79  }
80 #endif//CPPL_DEBUG
81  }
82 
83  return s;
84 }
85 
86 ///////////////////////////////////////////////////////////////////////////////
87 ///////////////////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////////////////
89 
90 //=============================================================================
91 inline void zhematrix::write(const char* filename) const
92 {CPPL_VERBOSE_REPORT;
93  std::ofstream ofs(filename, std::ios::trunc);
94  ofs.setf(std::cout.flags());
95  ofs.precision(std::cout.precision());
96  ofs.width(std::cout.width());
97  ofs.fill(std::cout.fill());
98 
99  ofs << "#zhematrix " << n << std::endl;
100  for(CPPL_INT i=0; i<n; i++){
101  for(CPPL_INT j=0; j<=i; j++ ){
102  ofs << operator()(i,j) << " ";
103  }
104  ofs << std::endl;
105 
106 #ifdef CPPL_DEBUG
107  if(std::fabs(std::imag(operator()(i,i))) > DBL_MIN){
108  WARNING_REPORT;
109  std::cerr << "The " << i << "th diagonal component of the zhematrix is not a real number." << std::endl;
110  }
111 #endif//CPPL_DEBUG
112  }
113 
114  ofs.close();
115 }
116 
117 //=============================================================================
118 inline void zhematrix::read(const char* filename)
119 {CPPL_VERBOSE_REPORT;
120  std::ifstream s(filename);
121  if(!s){
122  ERROR_REPORT;
123  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
124  exit(1);
125  }
126 
127  std::string id;
128  s >> id;
129  if( id != "zhematrix" && id != "#zhematrix" ){
130  ERROR_REPORT;
131  std::cerr << "The type name of the file \"" << filename << "\" is not zhematrix." << std::endl
132  << "Its type name was " << id << " ." << std::endl;
133  exit(1);
134  }
135 
136  s >> n;
137  resize(n);
138  for(CPPL_INT i=0; i<n; i++){
139  for(CPPL_INT j=0; j<=i; j++ ){
140  s >> darray[j][i];
141  //s >> operator()(i,j); //NG
142  }
143  }
144  if(s.eof()){
145  ERROR_REPORT;
146  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
147  << "Most likely, there is not enough data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
148  exit(1);
149  }
150 
151  s >> id;
152  if(!s.eof()){
153  ERROR_REPORT;
154  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
155  << "Most likely, there are extra data components." << std::endl;
156  exit(1);
157  }
158 
159  s.close();
160 }
zhecomplex operator()(const CPPL_INT &, const CPPL_INT &)
Definition: zhematrix-io.hpp:3
std::ostream & operator<<(std::ostream &s, const zhematrix &mat)
void read(const char *)
zhematrix & set(const CPPL_INT &, const CPPL_INT &, const comple &)
_dgematrix i(const _dgbmatrix &mat)
_zcovector conj(const _zcovector &vec)
CPPL_INT n
matrix column size
Definition: zhematrix.hpp:11
friend _zgematrix i(const zhematrix &)
(DO NOT USE) Complex-double Class for Hermitian matrices
Definition: zhecomplex.hpp:3
dcovec3 imag(const dquater &q)
void resize(const CPPL_INT &)
Complex Double-precision Hermitian Matrix Class [l-type (UPLO=l) Strage].
Definition: zhematrix.hpp:4
comple ** darray
array of pointers of column head addresses
Definition: zhematrix.hpp:13
void write(const char *) const