CPPLapack
 All Classes Files Functions Variables Friends Pages
dgrmatrix-io.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! operator() for const object */
3 inline double dgrmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j) const
4 {CPPL_VERBOSE_REPORT;
5 #ifdef CPPL_DEBUG
6  if( i<0 || j<0 || m<=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 " << m << "x" << n << "." << std::endl;
10  exit(1);
11  }
12 #endif//CPPL_DEBUG
13 
14  //// search (i,j) component ////
15  int k_beg =ia[i]-1;
16  int k_end =ia[i+1]-1;
17  for(int k=k_beg; k<k_end; k++){
18  if(j==ja[k]-1){
19  return a[k];
20  }
21  }
22 
23  //// (i,j) component was not found ////
24  return 0.0;
25 }
26 
27 //=============================================================================
28 /*! operator() for const object */
29 inline double& dgrmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j)
30 {CPPL_VERBOSE_REPORT;
31 #ifdef CPPL_DEBUG
32  if( i<0 || j<0 || m<=i || n<=j ){
33  ERROR_REPORT;
34  std::cerr << "The required component is out of the matrix size." << std::endl
35  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << "." << std::endl;
36  exit(1);
37  }
38 #endif//CPPL_DEBUG
39 
40  //// search (i,j) component ////
41  int k_beg =ia[i]-1;
42  int k_end =ia[i+1]-1;
43  for(int k=k_beg; k<k_end; k++){
44  if(j==ja[k]-1){
45  return a[k];
46  }
47  }
48 
49  //// (i,j) component was not found ////
50  ERROR_REPORT;
51  std::cerr << "dgrmatrix does not allow component addition with operator()." << std::endl;
52  exit(1);
53 }
54 
55 ///////////////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////////
58 
59 //=============================================================================
60 inline std::ostream& operator<<(std::ostream& s, const dgrmatrix& mat)
61 {CPPL_VERBOSE_REPORT;
62  for(CPPL_INT i=0; i<mat.m; i++){
63  int k_beg =mat.ia[i]-1;
64  int k_end =mat.ia[i+1]-1;
65  int j =0;
66  for(int k=k_beg; k<k_end; k++){
67  if(j<mat.ja[k]-1){
68  for(; j<mat.ja[k]-1; j++){
69  s << " x";
70  }
71  }
72  s << " " << mat.a[k];
73  j++;
74  }
75  for(; j<mat.n; j++){
76  s << " x";
77  }
78  s << std::endl;
79  }
80 
81  return s;
82 }
83 
84 ///////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////////////////
87 
88 //=============================================================================
89 inline void dgrmatrix::write(const char* filename) const
90 {CPPL_VERBOSE_REPORT;
91  std::ofstream ofs(filename, std::ios::trunc);
92  ofs.setf(std::cout.flags());
93  ofs.precision(std::cout.precision());
94  ofs.width(std::cout.width());
95  ofs.fill(std::cout.fill());
96 
97  ofs << "#dgrmatrix" << " " << m << " " << n << " " << a.size() << std::endl;
98 
99  size_t a_size =a.size();
100  for(size_t k=0; k<a_size; k++){
101  ofs << " " << a[k];
102  }
103  ofs << "\n";
104 
105  size_t ia_size =ia.size();
106  for(size_t k=0; k<ia_size; k++){
107  ofs << " " << ia[k];
108  }
109  ofs << "\n";
110 
111  size_t ja_size =ja.size();
112  for(size_t k=0; k<ja_size; k++){
113  ofs << " " << ja[k];
114  }
115  ofs << "\n" << std::flush;
116 
117  ofs.close();
118 }
119 
120 //=============================================================================
121 inline void dgrmatrix::read(const char* filename)
122 {CPPL_VERBOSE_REPORT;
123  std::ifstream s( filename );
124  if(!s){
125  ERROR_REPORT;
126  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
127  exit(1);
128  }
129 
130  std::string id;
131  s >> id;
132  if( id != "dgrmatrix" && id != "#dgrmatrix" ){
133  ERROR_REPORT;
134  std::cerr << "The type name of the file \"" << filename << "\" is not dgrmatrix." << std::endl
135  << "Its type name was " << id << " ." << std::endl;
136  exit(1);
137  }
138 
139  //////// read ////////
140  size_t a_size;
141  s >> m >> n >> a_size;
142  a.resize(a_size);
143  ia.resize(m+1);
144  ja.resize(a_size);
145 
146  for(size_t k=0; k<a_size; k++){
147  s >> a[k];
148  if(s.fail()){
149  ERROR_REPORT;
150  exit(1);
151  }
152  }
153  for(CPPL_INT k=0; k<=m; k++){
154  s >> ia[k];
155  if(s.fail()){
156  ERROR_REPORT;
157  exit(1);
158  }
159  }
160  for(size_t k=0; k<a_size; k++){
161  s >> ja[k];
162  if(s.fail()){
163  ERROR_REPORT;
164  exit(1);
165  }
166  }
167 
168  //////// garbage ////////
169  s >> a_size;
170  if(!s.eof()){
171  ERROR_REPORT;
172  std::cerr << "There is something is wrong with the file \"" << filename << " ." << std::endl;
173  exit(1);
174  }
175  s.close();
176 }
void read(const char *)
std::ostream & operator<<(std::ostream &s, const dgrmatrix &mat)
std::vector< double > a
matrix component values
Definition: dgrmatrix.hpp:11
void write(const char *) const
std::vector< CPPL_INT > ia
rowIndex (NOT zero-based BUT one-based indexing)
Definition: dgrmatrix.hpp:12
_dgematrix i(const _dgbmatrix &mat)
CPPL_INT n
matrix column size
Definition: dgrmatrix.hpp:10
CPPL_INT m
matrix row size
Definition: dgrmatrix.hpp:9
double operator()(const CPPL_INT &, const CPPL_INT &) const
Definition: dgrmatrix-io.hpp:3
std::vector< CPPL_INT > ja
columns (NOT zero-based BUT one-based indexing)
Definition: dgrmatrix.hpp:13
Real Double-precision General Compressed Sparse Row (CSR) Matrix Class.
Definition: dgrmatrix.hpp:3