00001
00002
00003
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 #ifndef TNT_VECTOR_H
00044
00045 #define TNT_VECTOR_H
00046
00047
00048
00049 #include "tnt_subscript.h"
00050
00051 #include <cstdlib>
00052
00053 #include <cassert>
00054
00055 #include <iostream>
00056
00057 #include <sstream>
00058
00059 #include <cmath>
00060
00061
00062
00063 using namespace std;
00064
00065
00066
00067 namespace TNT
00068
00069 {
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00093 template <class T>
00094
00095 class Vector
00096
00097 {
00098
00099
00100
00101
00102
00103 public:
00104
00105
00106
00107 typedef Subscript size_type;
00108
00109 typedef T value_type;
00110
00111 typedef T element_type;
00112
00113 typedef T* pointer;
00114
00115 typedef T* iterator;
00116
00117 typedef T& reference;
00118
00119 typedef const T* const_iterator;
00120
00121 typedef const T& const_reference;
00122
00123
00124
00125 Subscript lbound() const { return 1;}
00126
00127
00128
00129 private:
00130
00131 T* v_;
00132
00133 T* vm1_;
00134
00135 Subscript n_;
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 void initialize(Subscript N)
00146
00147 {
00148
00149
00150
00151
00152
00153
00154
00155 assert(v_ == NULL);
00156
00157 v_ = new T[N];
00158
00159 assert(v_ != NULL);
00160
00161 vm1_ = v_-1;
00162
00163 n_ = N;
00164
00165 }
00166
00167
00168
00169 void copy(const T* v)
00170
00171 {
00172
00173 Subscript N = n_;
00174
00175 Subscript i;
00176
00177
00178
00179 #ifdef TNT_UNROLL_LOOPS
00180
00181 Subscript Nmod4 = N & 3;
00182
00183 Subscript N4 = N - Nmod4;
00184
00185
00186
00187 for (i=0; i<N4; i+=4)
00188
00189 {
00190
00191 v_[i] = v[i];
00192
00193 v_[i+1] = v[i+1];
00194
00195 v_[i+2] = v[i+2];
00196
00197 v_[i+3] = v[i+3];
00198
00199 }
00200
00201
00202
00203 for (i=N4; i< N; i++)
00204
00205 v_[i] = v[i];
00206
00207 #else
00208
00209
00210
00211 for (i=0; i< N; i++)
00212
00213 v_[i] = v[i];
00214
00215 #endif
00216
00217 }
00218
00219
00220
00221 void set(const T& val)
00222
00223 {
00224
00225 Subscript N = n_;
00226
00227 Subscript i;
00228
00229
00230
00231 #ifdef TNT_UNROLL_LOOPS
00232
00233 Subscript Nmod4 = N & 3;
00234
00235 Subscript N4 = N - Nmod4;
00236
00237
00238
00239 for (i=0; i<N4; i+=4)
00240
00241 {
00242
00243 v_[i] = val;
00244
00245 v_[i+1] = val;
00246
00247 v_[i+2] = val;
00248
00249 v_[i+3] = val;
00250
00251 }
00252
00253
00254
00255 for (i=N4; i< N; i++)
00256
00257 v_[i] = val;
00258
00259 #else
00260
00261
00262
00263 for (i=0; i< N; i++)
00264
00265 v_[i] = val;
00266
00267
00268
00269 #endif
00270
00271 }
00272
00273
00274
00275
00276
00277
00278
00279 void destroy()
00280
00281 {
00282
00283
00284
00285 if (v_ == NULL) return ;
00286
00287
00288
00289
00290
00291 delete [] (v_);
00292
00293
00294
00295 v_ = NULL;
00296
00297 vm1_ = NULL;
00298
00299 }
00300
00301
00302
00303
00304
00305 public:
00306
00307
00308
00309
00310
00311
00312
00313 iterator begin() { return v_;}
00314
00315 iterator end() { return v_ + n_; }
00316
00317 const iterator begin() const { return v_;}
00318
00319 const iterator end() const { return v_ + n_; }
00320
00321
00322
00323 operator const T* const() { return v_; }
00324
00325 operator T*() { return v_; }
00326
00327
00328
00329
00330
00331
00332
00333 ~Vector()
00334
00335 {
00336
00337 destroy();
00338
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 Vector() : v_(0), vm1_(0), n_(0) {};
00348
00349
00350
00351 Vector(const Vector<T> &A) : v_(0), vm1_(0), n_(0)
00352
00353 {
00354
00355 initialize(A.n_);
00356
00357 copy(A.v_);
00358
00359 }
00360
00361
00362
00363 Vector(Subscript N, const T& value = T()) : v_(0), vm1_(0), n_(0)
00364
00365 {
00366
00367 initialize(N);
00368
00369 set(value);
00370
00371 }
00372
00373
00374
00375 Vector(Subscript N, const T* v) : v_(0), vm1_(0), n_(0)
00376
00377 {
00378
00379 initialize(N);
00380
00381 copy(v);
00382
00383 }
00384
00385
00386
00387 Vector(Subscript N, char *s) : v_(0), vm1_(0), n_(0)
00388
00389 {
00390
00391 initialize(N);
00392
00393 std::istringstream ins(s);
00394
00395
00396
00397 Subscript i;
00398
00399
00400
00401 for (i=0; i<N; i++)
00402
00403 ins >> v_[i];
00404
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 Vector<T>& newsize(Subscript N)
00416
00417 {
00418
00419 if (n_ == N) return *this;
00420
00421
00422
00423 destroy();
00424
00425 initialize(N);
00426
00427
00428
00429 return *this;
00430
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 Vector<T>& operator=(const Vector<T> &A)
00442
00443 {
00444
00445 if (v_ == A.v_)
00446
00447 return *this;
00448
00449
00450
00451 if (n_ == A.n_)
00452
00453 copy(A.v_);
00454
00455
00456
00457 else
00458
00459 {
00460
00461 destroy();
00462
00463 initialize(A.n_);
00464
00465 copy(A.v_);
00466
00467 }
00468
00469
00470
00471 return *this;
00472
00473 }
00474
00475
00476
00477 Vector<T>& operator=(const T& scalar)
00478
00479 {
00480
00481 set(scalar);
00482
00483 return *this;
00484
00485 }
00486
00487
00488
00489 inline Subscript dim() const
00490
00491 {
00492
00493 return n_;
00494
00495 }
00496
00497
00498
00499 inline Subscript size() const
00500
00501 {
00502
00503 return n_;
00504
00505 }
00506
00507
00508
00509
00510
00511 inline reference operator()(Subscript i)
00512
00513 {
00514
00515 #ifdef TNT_BOUNDS_CHECK
00516
00517 assert(1<=i);
00518
00519 assert(i <= n_) ;
00520
00521 #endif
00522
00523 return vm1_[i];
00524
00525 }
00526
00527
00528
00529 inline const_reference operator() (Subscript i) const
00530
00531 {
00532
00533 #ifdef TNT_BOUNDS_CHECK
00534
00535 assert(1<=i);
00536
00537 assert(i <= n_) ;
00538
00539 #endif
00540
00541 return vm1_[i];
00542
00543 }
00544
00545
00546
00547 inline reference operator[](Subscript i)
00548
00549 {
00550
00551 #ifdef TNT_BOUNDS_CHECK
00552
00553 assert(0<=i);
00554
00555 assert(i < n_) ;
00556
00557 #endif
00558
00559 return v_[i];
00560
00561 }
00562
00563
00564
00565 inline const_reference operator[](Subscript i) const
00566
00567 {
00568
00569 #ifdef TNT_BOUNDS_CHECK
00570
00571 assert(0<=i);
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 assert(i < n_) ;
00586
00587 #endif
00588
00589 return v_[i];
00590
00591 }
00592
00593
00594
00595
00596
00597
00598
00599 };
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 template <class T>
00610
00611 std::ostream& operator<<(std::ostream &s, const Vector<T> &A)
00612
00613 {
00614
00615 Subscript N=A.dim();
00616
00617
00618
00619 s << N << "\n";
00620
00621
00622
00623 for (Subscript i=0; i<N; i++)
00624
00625 s << A[i] << " " << "\n";
00626
00627 s << "\n";
00628
00629
00630
00631 return s;
00632
00633 }
00634
00635
00636
00637 template <class T>
00638
00639 std::istream & operator>>(std::istream &s, Vector<T> &A)
00640
00641 {
00642
00643
00644
00645 Subscript N;
00646
00647
00648
00649 s >> N;
00650
00651
00652
00653 if ( !(N == A.size() ))
00654
00655 {
00656
00657 A.newsize(N);
00658
00659 }
00660
00661
00662
00663
00664
00665 for (Subscript i=0; i<N; i++)
00666
00667 s >> A[i];
00668
00669
00670
00671
00672
00673 return s;
00674
00675 }
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685 template <class T>
00686
00687 Vector<T> operator+(const Vector<T> &A,
00688
00689 const Vector<T> &B)
00690
00691 {
00692
00693 Subscript N = A.dim();
00694
00695
00696
00697 assert(N==B.dim());
00698
00699
00700
00701 Vector<T> tmp(N);
00702
00703 Subscript i;
00704
00705
00706
00707 for (i=0; i<N; i++)
00708
00709 tmp[i] = A[i] + B[i];
00710
00711
00712
00713 return tmp;
00714
00715 }
00716
00717
00718
00719 template <class T>
00720
00721 Vector<T> operator+=(Vector<T> &A,
00722
00723 const Vector<T> &B)
00724
00725 {
00726
00727 Subscript N = A.dim();
00728
00729
00730
00731 assert(N==B.dim());
00732
00733
00734
00735 Subscript i;
00736
00737
00738
00739 for (i=0; i<N; i++)
00740
00741 A[i] += B[i];
00742
00743
00744
00745 return A;
00746
00747 }
00748
00749
00750
00751 template <class T>
00752
00753 Vector<T> operator-(const Vector<T> &A,
00754
00755 const Vector<T> &B)
00756
00757 {
00758
00759 Subscript N = A.dim();
00760
00761
00762
00763 assert(N==B.dim());
00764
00765
00766
00767 Vector<T> tmp(N);
00768
00769 Subscript i;
00770
00771
00772
00773 for (i=0; i<N; i++)
00774
00775 tmp[i] = A[i] - B[i];
00776
00777
00778
00779 return tmp;
00780
00781 }
00782
00783
00784
00785 template <class T>
00786
00787 Vector<T> operator-=(Vector<T> &A,
00788
00789 const Vector<T> &B)
00790
00791 {
00792
00793 Subscript N = A.dim();
00794
00795
00796
00797 assert(N==B.dim());
00798
00799
00800
00801 Subscript i;
00802
00803
00804
00805 for (i=0; i<N; i++)
00806
00807 A[i] -= B[i];
00808
00809
00810
00811 return A;
00812
00813 }
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823 template <class T>
00824
00825 Vector<T> elementwise_mult(const Vector<T> &A, const Vector<T> &B)
00826
00827 {
00828
00829 Subscript N = A.dim();
00830
00831
00832
00833 assert(N==B.dim());
00834
00835
00836
00837 Vector<T> tmp(N);
00838
00839 Subscript i;
00840
00841
00842
00843 for (i=0; i<N; i++)
00844
00845 tmp[i] = A[i] * B[i];
00846
00847
00848
00849 return tmp;
00850
00851 }
00852
00853
00854
00855
00856
00857 template <class T>
00858
00859 double norm(const Vector<T> &A)
00860
00861 {
00862
00863 Subscript N = A.dim();
00864
00865
00866
00867 double sum = 0.0;
00868
00869 for (int i=0; i<N; i++)
00870
00871 sum += abs(A[i])*abs(A[i]);
00872
00873 return sqrt(sum);
00874
00875 }
00876
00877
00878
00879
00880
00881
00882
00883 template <class T>
00884
00885 T dot_prod(const Vector<T> &A, const Vector<T> &B)
00886
00887 {
00888
00889 Subscript N = A.dim();
00890
00891 assert(N == B.dim());
00892
00893
00894
00895 Subscript i;
00896
00897 T sum = 0;
00898
00899
00900
00901 for (i=0; i<N; i++)
00902
00903 sum += A[i] * B[i];
00904
00905
00906
00907 return sum;
00908
00909 }
00910
00911
00912
00913 template <class T>
00914
00915 inline T dot_product(const Vector<T> &A, const Vector<T> &B)
00916
00917 {
00918
00919 return dot_prod(A, B);
00920
00921 }
00922
00923
00924
00925
00926
00927 template <class T>
00928
00929 inline T operator*(const Vector<T> &A,
00930
00931 const Vector<T> &B)
00932
00933 {
00934
00935 return dot_prod(A,B);
00936
00937 }
00938
00939
00940
00941
00942
00943 template <class T>
00944
00945 Vector<T> operator*(const T &a, const Vector<T> &A)
00946
00947 {
00948
00949 Subscript N = A.dim();
00950
00951 Vector<T> r(N);
00952
00953
00954
00955 for (int i=0; i<N; i++)
00956
00957 r[i] = A[i] * a;
00958
00959
00960
00961 return r;
00962
00963 }
00964
00965
00966
00967 template <class T>
00968
00969 inline Vector<T> operator*(const Vector<T> &A, const T& a)
00970
00971 {
00972
00973 return a * A;
00974
00975 }
00976
00977
00978
00979
00980
00981 }
00982
00983
00984
00985
00986
00987 #endif
00988
00989
00990