Menu

[r174]: / trunk / include / small / dcovector_small-functions.hpp  Maximize  Restore  History

Download this file

388 lines (345 with data), 12.0 kB

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