00001 #ifndef TNT_SPARSE_VECTOR_H
00002
00003 #define TNT_SPARSE_VECTOR_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <vector>
00050
00051 #include "tnt_vector.h"
00052
00053
00054
00055 namespace TNT
00056
00057 {
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 template <class T, class Integer>
00070
00071 class Sparse_Vector_Element
00072
00073 {
00074
00075 private:
00076
00077
00078
00079 T val_;
00080
00081 Integer index_;
00082
00083
00084
00085 public:
00086
00087
00088
00089 Sparse_Vector_Element(const T& a, const Integer &i) :
00090
00091 val_(a), index_(i) {}
00092
00093
00094
00095
00096
00097 const T& value() const { return val_; }
00098
00099 Integer index() const { return index_; }
00100
00101
00102
00103 T& value() { return val_; }
00104
00105 Integer& index() { return index_; }
00106
00107
00108
00109
00110
00111
00112
00113 };
00114
00115
00116
00117
00118
00149 template <class T>
00150
00151 class Sparse_Vector
00152
00153 {
00154
00155
00156
00157
00158
00159 private:
00160
00161
00162
00163
00164
00165
00166
00167 std::vector< Sparse_Vector_Element<T, Subscript> > s_;
00168
00169
00170
00171 int dim_;
00172
00173 int num_nonzeros_;
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 public:
00184
00185 typedef typename
00186
00187 std::vector< Sparse_Vector_Element<T, Subscript> >::const_iterator
00188
00189 const_iterator;
00190
00191
00192
00193
00194
00195 const_iterator begin() const { return s_.begin(); }
00196
00197 const_iterator end() const { return s_.end(); }
00198
00199
00200
00201 inline const T& value(Subscript i) const { return s_[i].value(); }
00202
00203 inline Subscript index(Subscript i) const {return s_[i].index(); }
00204
00205
00206
00207 inline T dot_product(const Vector<T>& x) const
00208
00209 {
00210
00211 T sum(0);
00212
00213
00214
00215 for ( const_iterator p = begin(); p < end(); p++)
00216
00217 {
00218
00219 sum += p->value() * x[p->index()];
00220
00221 }
00222
00223 return sum;
00224
00225 }
00226
00227
00228
00229 Sparse_Vector() : s_(), dim_(0), num_nonzeros_(0) {}
00230
00231 Sparse_Vector(Subscript N): dim_(N), num_nonzeros_(0) {}
00232
00233
00234
00235
00236
00237
00238
00239 Sparse_Vector(Subscript N, Subscript nz, const T* Val, const Subscript *I):
00240
00241 dim_(N),
00242
00243 num_nonzeros_(0)
00244
00245 {
00246
00247 insert(nz, Val, I);
00248
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 void insert(const T& val, Subscript i)
00258
00259 {
00260
00261
00262
00263 s_.push_back( Sparse_Vector_Element<T, Subscript>(val,i) );
00264
00265 num_nonzeros_++;
00266
00267 }
00268
00269
00270
00271 void insert(Subscript nz, const T* Val, const Subscript *I)
00272
00273 {
00274
00275
00276
00277 for (int count=0; count<nz; count++)
00278
00279 {
00280
00281 insert(Val[count], I[count]);
00282
00283 }
00284
00285 }
00286
00287
00288
00289
00290
00291 void insert_base_one(const T& val, Subscript i)
00292
00293 {
00294
00295 insert(val, i-1);
00296
00297 }
00298
00299
00300
00301 void insert_base_one(Subscript nz, const T* Val, const Subscript *I)
00302
00303 {
00304
00305 for (int count=0; count<nz; count++)
00306
00307 {
00308
00309 insert(Val[count], I[count]-1);
00310
00311 }
00312
00313 }
00314
00315
00316
00317
00318
00319
00320
00321 inline int dim() const {return dim_;}
00322
00323 int num_nonzeros() const {return num_nonzeros_;}
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 inline double norm() const
00334
00335 {
00336
00337 T sum(0.0);
00338
00339
00340
00341 for (const_iterator p = begin(); p < end(); p++)
00342
00343 {
00344
00345 sum += p->value() * p->value();
00346
00347 }
00348
00349
00350
00351 return sqrt(sum);
00352
00353 }
00354
00355
00356
00357 std::ostream & print(std::ostream &s) const
00358
00359 {
00360
00361 for (typename Sparse_Vector<T>::const_iterator p = begin();
00362
00363 p < end(); p++)
00364
00365 {
00366
00367 s << "( " << p->value() << ", " << p->index() << " ) \n";
00368
00369 }
00370
00371 return s;
00372
00373 }
00374
00375
00376
00377 std::ostream & print_base_one(std::ostream &s) const
00378
00379 {
00380
00381 for (typename Sparse_Vector<T>::const_iterator p = begin();
00382
00383 p < end(); p++)
00384
00385 {
00386
00387 s << "( " << p->value() << ", " << p->index()+1 << " ) \n";
00388
00389 }
00390
00391 return s;
00392
00393 }
00394
00395
00396
00397
00398
00399 };
00400
00401
00402
00403
00404
00405
00406
00407 template <class T>
00408
00409 inline T dot_product(const Sparse_Vector<T> &s, const Vector<T> &x)
00410
00411 {
00412
00413 return s.dot_product(x);
00414
00415 }
00416
00417
00418
00419 template <class T>
00420
00421 inline T dot_product(const Vector<T> &x, const Sparse_Vector<T> &s)
00422
00423 {
00424
00425 return s.dot_product(x);
00426
00427 }
00428
00429
00430
00431 template <class T>
00432
00433 inline T operator*(const Vector<T> &x, const Sparse_Vector<T> &s)
00434
00435 {
00436
00437 return dot_product(x,s);
00438
00439 }
00440
00441
00442
00443 template <class T>
00444
00445 inline T operator*(const Sparse_Vector<T> &s, const Vector<T> &x)
00446
00447 {
00448
00449 return dot_product(x,s);
00450
00451 }
00452
00453
00454
00455
00456
00457 template <class T>
00458
00459 inline double norm( const Sparse_Vector<T> & s)
00460
00461 {
00462
00463 return s.norm();
00464
00465 }
00466
00467
00468
00469
00470
00471 template <class T>
00472
00473 std::ostream& operator<<(std::ostream &s, const Sparse_Vector<T> &A)
00474
00475 {
00476
00477
00478
00479 return A.print(s);
00480
00481 }
00482
00483
00484
00485
00486
00487
00488
00489 }
00490
00491
00492
00493 #endif
00494
00495
00496