CPPLapack
 All Classes Files Functions Variables Friends Pages
drovector_small-functions.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! convert drovector_small to drovector */
3 template<CPPL_INT l>
5 {CPPL_VERBOSE_REPORT;
6  drovector 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& drovector_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 drovector_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 function */
53 template<CPPL_INT l>
54 inline drovector_small<l>& drovector_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 drovector_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::flush;
68  }
69  s << std::endl;
70  return s;
71 }
72 
73 //=============================================================================
74 /*! write to file */
75 template<CPPL_INT l>
76 inline void drovector_small<l>::write(const char* filename) const
77 {CPPL_VERBOSE_REPORT;
78  std::ofstream ofs(filename, std::ios::trunc);
79  ofs.setf(std::cout.flags());
80  ofs.precision(std::cout.precision());
81  ofs.width(std::cout.width());
82  ofs.fill(std::cout.fill());
83 
84  ofs << "#drovector" << " " << l << std::endl;
85  for(CPPL_INT k=0; k<l; k++){
86  ofs << (*this)(k) << " ";
87  }
88  ofs << std::endl;
89  ofs.close();
90 }
91 
92 //=============================================================================
93 /*! read from file */
94 template<CPPL_INT l>
95 inline void drovector_small<l>::read(const char* filename)
96 {CPPL_VERBOSE_REPORT;
97  std::ifstream s( filename );
98  if(!s){
99  ERROR_REPORT;
100  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
101  exit(1);
102  }
103 
104  std::string id;
105  s >> id;
106  if( id != "drovector" && id != "#drovector" ){
107  ERROR_REPORT;
108  std::cerr << "The type name of the file \"" << filename << "\" is not drovector." << std::endl
109  << "Its type name was " << id << " ." << std::endl;
110  exit(1);
111  }
112 
113  CPPL_INT _l;
114  s >> _l;
115  if(l!=_l){
116  ERROR_REPORT;
117  std::cerr << "Matrix size is invalid." << std::endl;
118  exit(1);
119  }
120  for(CPPL_INT k=0; k<l; k++){
121  s >> (*this)(k);
122  }
123  if(s.eof()){
124  ERROR_REPORT;
125  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
126  << "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;
127  exit(1);
128  }
129 
130  s >> id;//tmp
131  if(!s.eof()){
132  ERROR_REPORT;
133  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
134  << "Most likely, there are extra data components." << std::endl;
135  exit(1);
136  }
137 
138  s.close();
139 }
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////////////////////
144 ///////////////////////////////////////////////////////////////////////////////
145 ///////////////////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////////////////
147 
148 //=============================================================================
149 /*! return transposed column vector */
150 template<CPPL_INT n>
152 {CPPL_VERBOSE_REPORT;
154  for(CPPL_INT i=0; i<n; i++){
155  X(i)=A(i);
156  }
157  return X;
158 }
159 
160 //=============================================================================
161 /*! */
162 template<CPPL_INT l>
163 inline double nrm2(const drovector_small<l>& A)
164 {CPPL_VERBOSE_REPORT;
165  double v(0.);
166  for(CPPL_INT i=0; i<l; i++){
167  v+=A(i)*A(i);
168  }
169  return std::sqrt(v);
170 }
171 
172 //=============================================================================
173 /*! */
174 template<CPPL_INT l>
175 inline void idamax(CPPL_INT& K, const drovector_small<l>& A)
176 {CPPL_VERBOSE_REPORT;
177  double max(-1.);
178  for(int k=0; k<l; k++){
179  if( max<fabs(A(k)) ){
180  K=k;
181  max =fabs(A(k));
182  }
183  }
184  return;
185 }
186 
187 //=============================================================================
188 /*! */
189 template<CPPL_INT l>
190 inline double damax(const drovector_small<l>& A)
191 {CPPL_VERBOSE_REPORT;
192  CPPL_INT k(0);
193  idamax(k,A);
194  return A(k);
195 }
196 
197 ///////////////////////////////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////////////////////////////////////////////
200 ///////////////////////////////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////////////////
203 
204 //=============================================================================
205 /*! */
206 template<CPPL_INT l>
208 {CPPL_VERBOSE_REPORT;
209  for(CPPL_INT k=0; k<l; k++){
210  array[k] =0.;
211  }
212  return *this;
213 }
214 
215 ///////////////////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////////////////////////////
218 ///////////////////////////////////////////////////////////////////////////////
219 ///////////////////////////////////////////////////////////////////////////////
220 ///////////////////////////////////////////////////////////////////////////////
221 
222 //=============================================================================
223 /*! */
224 template<CPPL_INT l>
226 {CPPL_VERBOSE_REPORT;
227  for(CPPL_INT i=0; i<l; i++){
228  A(i) +=B(i);
229  }
230  return A;
231 }
232 
233 //=============================================================================
234 /*! */
235 template<CPPL_INT l>
237 {CPPL_VERBOSE_REPORT;
238  for(CPPL_INT i=0; i<l; i++){
239  A(i) -=B(i);
240  }
241  return A;
242 }
243 
244 //=============================================================================
245 /*! */
246 template<CPPL_INT l>
248 {CPPL_VERBOSE_REPORT;
249  for(CPPL_INT i=0; i<l; i++){
250  A(i) *=d;
251  }
252  return A;
253 }
254 
255 //=============================================================================
256 /*! */
257 template<CPPL_INT l>
259 {CPPL_VERBOSE_REPORT;
260  for(CPPL_INT i=0; i<l; i++){
261  A(i) /=d;
262  }
263  return A;
264 }
265 
266 ///////////////////////////////////////////////////////////////////////////////
267 ///////////////////////////////////////////////////////////////////////////////
268 ///////////////////////////////////////////////////////////////////////////////
269 
270 //=============================================================================
271 /*! unary */
272 template<CPPL_INT l>
274 {CPPL_VERBOSE_REPORT;
275  return A;
276 }
277 
278 //=============================================================================
279 /*! unary */
280 template<CPPL_INT l>
282 {CPPL_VERBOSE_REPORT;
284  for(CPPL_INT i=0; i<l; i++){
285  X(i) =-A(i);
286  }
287  return X;
288 }
289 
290 ///////////////////////////////////////////////////////////////////////////////
291 ///////////////////////////////////////////////////////////////////////////////
292 ///////////////////////////////////////////////////////////////////////////////
293 
294 //=============================================================================
295 /*! */
296 template<CPPL_INT l>
298 {CPPL_VERBOSE_REPORT;
300  for(CPPL_INT i=0; i<l; i++){
301  X(i) =A(i)+B(i);
302  }
303  return X;
304 }
305 
306 //=============================================================================
307 /*! */
308 template<CPPL_INT l>
310 {CPPL_VERBOSE_REPORT;
312  for(CPPL_INT i=0; i<l; i++){
313  X(i) =A(i)-B(i);
314  }
315  return X;
316 }
317 
318 ///////////////////////////////////////////////////////////////////////////////
319 ///////////////////////////////////////////////////////////////////////////////
320 ///////////////////////////////////////////////////////////////////////////////
321 
322 //=============================================================================
323 /*! */
324 template<CPPL_INT l>
325 inline double operator*(const drovector_small<l>& A, const dcovector_small<l>& B)
326 {CPPL_VERBOSE_REPORT;
327  double x =0.;
328  for(CPPL_INT i=0; i<l; i++){
329  x +=A(i)*B(i);
330  }
331  return x;
332 }
333 
334 //=============================================================================
335 /*! */
336 template<CPPL_INT m, CPPL_INT n>
338 {CPPL_VERBOSE_REPORT;
340  C.zero();
341  for(CPPL_INT j=0; j<n; j++){
342  for(CPPL_INT i=0; i<m; i++){
343  C(j) +=A(i)*B(i,j);
344  }
345  }
346  return C;
347 }
348 
349 //=============================================================================
350 /*! */
351 template<CPPL_INT l>
353 {CPPL_VERBOSE_REPORT;
355  C.zero();
356  for(CPPL_INT j=0; j<l; j++){
357  for(CPPL_INT i=0; i<j; i++){
358  C(j) +=A(i)*B(j,i);
359  }
360  for(CPPL_INT i=j; i<l; i++){
361  C(j) +=A(i)*B(i,j);
362  }
363  }
364  return C;
365 }
366 
367 //=============================================================================
368 /*! */
369 template<CPPL_INT l>
370 inline drovector_small<l> operator*(const drovector_small<l>& A, const double& v)
371 {CPPL_VERBOSE_REPORT;
373  for(CPPL_INT i=0; i<l; i++){
374  C(i) =A(i)*v;
375  }
376  return C;
377 }
378 
379 ///////////////////////////////////////////////////////////////////////////////
380 ///////////////////////////////////////////////////////////////////////////////
381 ///////////////////////////////////////////////////////////////////////////////
382 
383 //=============================================================================
384 /*! */
385 template<CPPL_INT l>
386 inline drovector_small<l> operator/(const drovector_small<l>& A, const double& v)
387 {CPPL_VERBOSE_REPORT;
389  for(CPPL_INT i=0; i<l; i++){
390  C(i) =A(i)/v;
391  }
392  return C;
393 }
394 
395 ///////////////////////////////////////////////////////////////////////////////
396 ///////////////////////////////////////////////////////////////////////////////
397 ///////////////////////////////////////////////////////////////////////////////
398 
399 //=============================================================================
400 /*! drovector_small%drovector_small (inner product) operator */
401 template<CPPL_INT l>
402 inline double operator%(const drovector_small<l>& A, const drovector_small<l>& B)
403 {CPPL_VERBOSE_REPORT;
404  double v(0.);
405  for(CPPL_INT i=0; i<l; i++){
406  v +=A(i)*B(i);
407  }
408  return v;
409 }
410 
411 ///////////////////////////////////////////////////////////////////////////////
412 ///////////////////////////////////////////////////////////////////////////////
413 ///////////////////////////////////////////////////////////////////////////////
414 
415 //=============================================================================
416 /*! Hadamard product */
417 template<CPPL_INT l>
419 {CPPL_VERBOSE_REPORT;
421  for(CPPL_INT i=0; i<l; i++){
422  C(i) =A(i)*B(i);
423  }
424  return C;
425 }
drovector_small< l > hadamard(const drovector_small< l > &A, const drovector_small< l > &B)
double operator%(const drovector_small< l > &A, const drovector_small< l > &B)
drovector_small< l > & zero()
double & operator()(const CPPL_INT &)
Samll Real Double-precision Symmetric Matrix Class.
drovector_small< l > & operator-=(drovector_small< l > &A, const drovector_small< l > &B)
Samll Real Double-precision General Dence Matrix Class.
drovector_small< l > operator/(const drovector_small< l > &A, const double &v)
_dgematrix i(const _dgbmatrix &mat)
drovector_small< l > & set(const CPPL_INT &, const double &)
const drovector_small< l > & operator+(const drovector_small< l > &A)
Samll Real Double-precision Row Vector Class.
Real Double-precision Row Vector Class.
Definition: drovector.hpp:3
_drovector to_drovector() const
void read(const char *filename)
(DO NOT USE) Smart-temporary Real Double-precision Row Vector Class
Definition: _drovector.hpp:3
dcovector_small< n > t(const drovector_small< n > &A)
drovector_small< l > & operator+=(drovector_small< l > &A, const drovector_small< l > &B)
drovector_small< l > & operator*=(drovector_small< l > &A, const double &d)
double nrm2(const drovector_small< l > &A)
drovector_small< l > & operator/=(drovector_small< l > &A, const double &d)
void write(const char *filename) const
double operator*(const drovector_small< l > &A, const dcovector_small< l > &B)
Samll Real Double-precision Column Vector Class.
void idamax(CPPL_INT &K, const drovector_small< l > &A)
double damax(const drovector_small< l > &A)
drovector_small< l > operator-(const drovector_small< l > &A)
_dcovector _(dcovector &vec)