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