CPPLapack
 All Classes Files Functions Variables Friends Pages
dcovector_small-functions.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! convert dcovector_small to dcovector */
3 template<CPPL_INT l>
5 {CPPL_VERBOSE_REPORT;
6  dcovector vec(l);
7  for(CPPL_INT k=0; k<l; k++){
8  vec(k)=(*this)(k);
9  }
10  return _(vec);
11 }
12 
13 ///////////////////////////////////////////////////////////////////////////////
14 ///////////////////////////////////////////////////////////////////////////////
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 //=============================================================================
18 /*! operator() */
19 template<CPPL_INT l>
20 inline double& dcovector_small<l>::operator()(const CPPL_INT& k)
21 {CPPL_VERBOSE_REPORT;
22 #ifdef CPPL_DEBUG
23  if( k<0 || l<=k ){
24  ERROR_REPORT;
25  std::cerr << "The required component is out of the vector size." << std::endl
26  << "Your input is (" << k << "), whereas the vector size is " << l << "." << std::endl;
27  exit(1);
28  }
29 #endif//CPPL_DEBUG
30 
31  return array[k];
32 }
33 
34 //=============================================================================
35 /*! operator() for const */
36 template<CPPL_INT l>
37 inline double dcovector_small<l>::operator()(const CPPL_INT& k) const
38 {CPPL_VERBOSE_REPORT;
39 #ifdef CPPL_DEBUG
40  if( k<0 || l<=k ){
41  ERROR_REPORT;
42  std::cerr << "The required component is out of the vector size." << std::endl
43  << "Your input is (" << k << "), whereas the vector size is " << l << "." << std::endl;
44  exit(1);
45  }
46 #endif//CPPL_DEBUG
47 
48  return array[k];
49 }
50 
51 //=============================================================================
52 /*! set */
53 template<CPPL_INT l>
54 inline dcovector_small<l>& dcovector_small<l>::set(const CPPL_INT& k, const double& v)
55 {CPPL_VERBOSE_REPORT;
56  (*this)(k) =v;
57  return *this;
58 }
59 
60 //=============================================================================
61 /*! operator<< */
62 template<CPPL_INT l>
63 inline std::ostream& operator<<(std::ostream& s, const dcovector_small<l>& A)
64 {CPPL_VERBOSE_REPORT;
65  s << std::setiosflags(std::ios::showpos);
66  for(CPPL_INT i=0; i<l; i++){
67  s << A(i) << std::endl;
68  }
69  return s;
70 }
71 
72 //=============================================================================
73 /*! write to file */
74 template<CPPL_INT l>
75 inline void dcovector_small<l>::write(const char* filename) const
76 {CPPL_VERBOSE_REPORT;
77  std::ofstream ofs(filename, std::ios::trunc);
78  ofs.setf(std::cout.flags());
79  ofs.precision(std::cout.precision());
80  ofs.width(std::cout.width());
81  ofs.fill(std::cout.fill());
82 
83  ofs << "#dcovector" << " " << l << std::endl;
84  for(CPPL_INT k=0; k<l; k++){
85  ofs << (*this)(k) << std::endl;
86  }
87  ofs.close();
88 }
89 
90 //=============================================================================
91 /*! read from file */
92 template<CPPL_INT l>
93 inline void dcovector_small<l>::read(const char* filename)
94 {CPPL_VERBOSE_REPORT;
95  std::ifstream s( filename );
96  if(!s){
97  ERROR_REPORT;
98  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
99  exit(1);
100  }
101 
102  std::string id;
103  s >> id;
104  if( id != "dcovector" && id != "#dcovector" ){
105  ERROR_REPORT;
106  std::cerr << "The type name of the file \"" << filename << "\" is not dcovector." << std::endl
107  << "Its type name was " << id << " ." << std::endl;
108  exit(1);
109  }
110 
111  CPPL_INT _l;
112  s >> _l;
113  if(l!=_l){
114  ERROR_REPORT;
115  std::cerr << "Matrix size is invalid." << std::endl;
116  exit(1);
117  }
118  for(CPPL_INT k=0; k<l; k++){
119  s >> (*this)(k);
120  }
121  if(s.eof()){
122  ERROR_REPORT;
123  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
124  << "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;
125  exit(1);
126  }
127 
128  s >> id;//tmp
129  if(!s.eof()){
130  ERROR_REPORT;
131  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
132  << "Most likely, there are extra data components." << std::endl;
133  exit(1);
134  }
135 
136  s.close();
137 }
138 
139 ///////////////////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////////////////////
144 ///////////////////////////////////////////////////////////////////////////////
145 
146 //=============================================================================
147 /*! return transposed drovector_small */
148 template<CPPL_INT l>
150 {CPPL_VERBOSE_REPORT;
152  for(CPPL_INT i=0; i<l; i++){
153  X(i) =A(i);
154  }
155  return X;
156 }
157 
158 //=============================================================================
159 /*! return its 2-norm */
160 template<CPPL_INT l>
161 inline double nrm2(const dcovector_small<l>& A)
162 {CPPL_VERBOSE_REPORT;
163  double v(0);
164  for(CPPL_INT i=0; i<l; i++){
165  v+=A(i)*A(i);
166  }
167  return std::sqrt(v);
168 }
169 
170 //=============================================================================
171 /*! return index of the maximum component */
172 template<CPPL_INT l>
173 inline void idamax(CPPL_INT& K, const dcovector_small<l>& A)
174 {CPPL_VERBOSE_REPORT;
175  double max(-1.);
176  for(int k=0; k<l; k++){
177  if( max<fabs(A(k)) ){
178  K=k;
179  max =fabs(A(k));
180  }
181  }
182  return;
183 }
184 
185 //=============================================================================
186 /*! return the maximum component */
187 template<CPPL_INT l>
188 inline double damax(const dcovector_small<l>& A)
189 {CPPL_VERBOSE_REPORT;
190  CPPL_INT k(0);
191  idamax(k,A);
192  return A(k);
193 }
194 
195 ///////////////////////////////////////////////////////////////////////////////
196 ///////////////////////////////////////////////////////////////////////////////
197 ///////////////////////////////////////////////////////////////////////////////
198 
199 //=============================================================================
200 /*! zero */
201 template<CPPL_INT l>
203 {CPPL_VERBOSE_REPORT;
204  for(CPPL_INT k=0; k<l; k++){
205  array[k] =0.;
206  }
207  return *this;
208 }
209 
210 ///////////////////////////////////////////////////////////////////////////////
211 ///////////////////////////////////////////////////////////////////////////////
212 ///////////////////////////////////////////////////////////////////////////////
213 
214 //=============================================================================
215 /*! dcovector_small+=dcovector_small operator */
216 template<CPPL_INT l>
218 {CPPL_VERBOSE_REPORT;
219  for(CPPL_INT i=0; i<l; i++){
220  A(i) +=B(i);
221  }
222  return A;
223 }
224 
225 //=============================================================================
226 /*! dcovector_small-=dcovector_small operator */
227 template<CPPL_INT l>
229 {CPPL_VERBOSE_REPORT;
230  for(CPPL_INT i=0; i<l; i++){
231  A(i) -=B(i);
232  }
233  return A;
234 }
235 
236 //=============================================================================
237 /*! dcovector_small*=double operator */
238 template<CPPL_INT l>
240 {CPPL_VERBOSE_REPORT;
241  for(CPPL_INT i=0; i<l; i++){
242  A(i) *=d;
243  }
244  return A;
245 }
246 
247 //=============================================================================
248 /*! dcovector_small/=double operator */
249 template<CPPL_INT l>
251 {CPPL_VERBOSE_REPORT;
252  for(CPPL_INT i=0; i<l; i++){
253  A(i) /=d;
254  }
255  return A;
256 }
257 
258 ///////////////////////////////////////////////////////////////////////////////
259 ///////////////////////////////////////////////////////////////////////////////
260 ///////////////////////////////////////////////////////////////////////////////
261 
262 //=============================================================================
263 /*! unary + operator*/
264 template<CPPL_INT l>
266 {CPPL_VERBOSE_REPORT;
267  return A;
268 }
269 
270 //=============================================================================
271 /*! unary - operator*/
272 template<CPPL_INT l>
274 {CPPL_VERBOSE_REPORT;
276  for(CPPL_INT i=0; i<l; i++){
277  X(i) =-A(i);
278  }
279  return X;
280 }
281 
282 ///////////////////////////////////////////////////////////////////////////////
283 ///////////////////////////////////////////////////////////////////////////////
284 ///////////////////////////////////////////////////////////////////////////////
285 
286 //=============================================================================
287 /*! dcovector_small+dcovector_small operator */
288 template<CPPL_INT l>
290 {CPPL_VERBOSE_REPORT;
292  for(CPPL_INT i=0; i<l; i++){
293  X(i) =A(i)+B(i);
294  }
295  return X;
296 }
297 
298 //=============================================================================
299 /*! dcovector_small-dcovector_small operator */
300 template<CPPL_INT l>
302 {CPPL_VERBOSE_REPORT;
304  for(CPPL_INT i=0; i<l; i++){
305  X(i) =A(i)-B(i);
306  }
307  return X;
308 }
309 
310 ///////////////////////////////////////////////////////////////////////////////
311 ///////////////////////////////////////////////////////////////////////////////
312 ///////////////////////////////////////////////////////////////////////////////
313 
314 //=============================================================================
315 /*! dcovector_small*double operator */
316 template<CPPL_INT n>
317 inline dcovector_small<n> operator*(const dcovector_small<n>& A, const double& v)
318 {CPPL_VERBOSE_REPORT;
320  for(CPPL_INT i=0; i<n; i++){
321  C(i) =A(i)*v;
322  }
323  return C;
324 }
325 
326 //=============================================================================
327 /*! dcovector_small*drovector_small operator */
328 template<CPPL_INT m, CPPL_INT n>
330 {CPPL_VERBOSE_REPORT;
332  for(CPPL_INT i=0; i<m; i++){
333  for(CPPL_INT j=0; j<n; j++){
334  mat(i,j) =A(i)*B(j);
335  }
336  }
337  return mat;
338 }
339 
340 ///////////////////////////////////////////////////////////////////////////////
341 ///////////////////////////////////////////////////////////////////////////////
342 ///////////////////////////////////////////////////////////////////////////////
343 
344 //=============================================================================
345 /*! dcovector_small/double operator */
346 template<CPPL_INT n>
347 inline dcovector_small<n> operator/(const dcovector_small<n>& A, const double& v)
348 {CPPL_VERBOSE_REPORT;
350  for(CPPL_INT i=0; i<n; i++){
351  C(i) =A(i)/v;
352  }
353  return C;
354 }
355 
356 ///////////////////////////////////////////////////////////////////////////////
357 ///////////////////////////////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////////////////////////////
359 
360 //=============================================================================
361 /*! dcovector_small%dcovector_small (inner product) operator */
362 template<CPPL_INT l>
363 inline double operator%(const dcovector_small<l>& A, const dcovector_small<l>& B)
364 {CPPL_VERBOSE_REPORT;
365  double v(0.);
366  for(CPPL_INT i=0; i<l; i++){
367  v +=A(i)*B(i);
368  }
369  return v;
370 }
371 
372 ///////////////////////////////////////////////////////////////////////////////
373 ///////////////////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////////////////
375 
376 //=============================================================================
377 /*! Hadamard product */
378 template<CPPL_INT l>
380 {CPPL_VERBOSE_REPORT;
382  for(CPPL_INT i=0; i<l; i++){
383  C(i) =A(i)*B(i);
384  }
385  return C;
386 }
387 
dcovector_small< l > & operator-=(dcovector_small< l > &A, const dcovector_small< l > &B)
dcovector_small< l > operator-(const dcovector_small< l > &A)
dcovector_small< l > & zero()
dcovector_small< l > hadamard(const dcovector_small< l > &A, const dcovector_small< l > &B)
void write(const char *filename) const
double & operator()(const CPPL_INT &)
dcovector_small< n > operator/(const dcovector_small< n > &A, const double &v)
dcovector_small< l > & operator/=(dcovector_small< l > &A, const double &d)
Samll Real Double-precision General Dence Matrix Class.
dcovector_small< l > & set(const CPPL_INT &, const double &)
dcovector_small< l > & operator+=(dcovector_small< l > &A, const dcovector_small< l > &B)
_dgematrix i(const _dgbmatrix &mat)
Samll Real Double-precision Row Vector Class.
double damax(const dcovector_small< l > &A)
dcovector_small< l > & operator*=(dcovector_small< l > &A, const double &d)
double operator%(const dcovector_small< l > &A, const dcovector_small< l > &B)
void read(const char *filename)
Samll Real Double-precision Column Vector Class.
const dcovector_small< l > & operator+(const dcovector_small< l > &A)
Real Double-precision Column Vector Class.
Definition: dcovector.hpp:3
dcovector_small< n > operator*(const dcovector_small< n > &A, const double &v)
drovector_small< l > t(const dcovector_small< l > &A)
_dcovector to_dcovector() const
void idamax(CPPL_INT &K, const dcovector_small< l > &A)
(DO NOT USE) Smart-temporary Real Double-precision Column Vector Class
Definition: _dcovector.hpp:3
double nrm2(const dcovector_small< l > &A)
_dcovector _(dcovector &vec)