00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef martingale_array_h
00024 #define martingale_array_h
00025
00026
00027 #include "TypedefsMacros.h"
00028 #include <cstdlib>
00029 #include <string>
00030
00031 MTGL_BEGIN_NAMESPACE(Martingale)
00032
00033
00034
00056
00057
00058
00059
00060
00061
00062
00063 struct SubscriptCheck {
00064
00066 static void checkSubscript(int i, int b1, int n1, string str)
00067 {
00068 if((i<b1)||(i>b1+n1-1)){
00069
00070 cout << "\n\nSubscript out of range: " << str
00071 << "\ni = " << i << " not in [" << b1 << ", " << b1+n1-1 << "]"
00072 << "\n index base = " << b1 << ", dimension = " << n1
00073 << "\nTerminating.";
00074 exit(0);
00075 }
00076 }
00077
00078
00080 static void checkSubscript(int i, int j, int b1, int b2, int n1, int n2, string str)
00081 {
00082 if((i<b1)||(i>b1+n1-1)){
00083
00084 cout << "\n\nSubscript out of range: " << str
00085 << "\ni = " << i << " not in [" << b1 << ", " << b1+n1-1 << "]"
00086 << "\n index base = " << b1 << ", rows = " << n1
00087 << "\nTerminating.";
00088 exit(0);
00089 }
00090
00091
00092 if((j<b2)||(j>b2+n2-1)){
00093
00094 cout << "\n\n Subscript out of range: " << str
00095 << "\nj = " << j << " not in [" << b2 << ", " << b2+n2-1 << "]"
00096 << "\n index base = " << b2 << ", cols = " << n2
00097 << "\nTerminating.";
00098 exit(0);
00099 }
00100 }
00101
00102
00103
00107 static void checkSubscript
00108 (int i, int j, int k, int b1, int b2, int b3, int n1, int n2, int n3, string str)
00109 {
00110 if((i<b1)||(i>b1+n1-1)){
00111
00112 cout << "\n\nSubscript out of range: " << str
00113 << "\ni = " << i << " not in [" << b1 << ", " << b1+n1-1 << "]"
00114 << "\nTerminating.";
00115 exit(0);
00116 }
00117
00118
00119 if((j<b2)||(j>b2+n2-1)){
00120
00121 cout << "\n\n Subscript out of range: " << str
00122 << "\nj = " << j << " not in [" << b2 << ", " << b2+n2-1 << "]"
00123 << "\nTerminating.";
00124 exit(0);
00125 }
00126
00127
00128 if((k<b3)||(k>b3+n3-1)){
00129
00130 cout << "\n\n Subscript out of range: " << str
00131 << "\nj = " << k << " not in [" << b3 << ", " << b3+n3-1 << "]"
00132 << "\nTerminating.";
00133 exit(0);
00134 }
00135 }
00136
00137
00138 };
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00154 template<typename S>
00155 class Array1D {
00156
00157 protected:
00158
00160 int b;
00162 int n;
00163
00165 S* dptr;
00166
00167
00168 public:
00169
00170
00171
00172
00175 int getIndexBase() const { return b; }
00176
00177 void setIndexBase(int base){ b=base; }
00178
00180 int getDimension() const { return n; }
00181
00183 void setDimension(int q) { n=q; }
00184
00186 S* getData() const { return dptr; }
00187
00188
00189
00190
00194 explicit Array1D(int n_, int b_=0) :
00195 b(b_), n(n_)
00196 {
00197 dptr=new S[n];
00198 for(int i=0;i<n;i++) dptr[i]=0;
00199 }
00200
00201
00202 Array1D(const Array1D& x) :
00203 b(x.getIndexBase()),
00204 n(x.getDimension())
00205 {
00206 dptr=new S[n];
00207 S* xdptr=x.getData();
00208 for(int i=0;i<n;i++) dptr[i]=xdptr[i];
00209 }
00210
00211
00212 ~Array1D(){ delete[] dptr; }
00213
00215 const S& operator[](int i) const
00216 {
00217 #ifdef SUBSCRIPT_CHECK
00218 SubscriptCheck::
00219 checkSubscript(i,b,n,"Array1D");
00220 #endif
00221 return dptr[i-b];
00222 }
00223
00225 S& operator[](int i)
00226 {
00227 #ifdef SUBSCRIPT_CHECK
00228 SubscriptCheck::
00229 checkSubscript(i,b,n,"Array1D");
00230 #endif
00231 return dptr[i-b];
00232 }
00233
00237 Array1D<S>& operator=(const Array1D<S>& B)
00238 {
00239 S* dptrB=B.getData();
00240 for(int j=0;j<n;j++) dptr[j]=dptrB[j];
00241 b=B.getIndexBase();
00242
00243 return *this;
00244 }
00245
00246
00247
00249 Real norm() const
00250 {
00251 Real u=0;
00252 for(int i=0;i<n;i++) u+=dptr[i]*dptr[i];
00253 return sqrt(u);
00254 }
00255
00257 Array1D& scale(S f)
00258 {
00259 for(int i=0;i<n;i++) dptr[i]*=f;
00260 return *this;
00261 }
00262
00266 S dotProduct(Array1D<S>& X) const
00267 {
00268 Real* a=dptr;
00269 Real* x=X.getData();
00270 Real sum=0.0;
00271 for(int j=0;j<n;j++) sum+=a[j]*x[j];
00272
00273 return sum;
00274 }
00275
00276 std::ostream& printSelf(std::ostream& os) const
00277 {
00278 os << endl << "Array1D of dimension " << n << ":" << endl;
00279 for(int i=0;i<n-1;i++) os << dptr[i] << ", ";
00280 return os << dptr[n-1] << endl << endl;
00281 }
00282
00283
00284 };
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00299 template<typename S>
00300 class Array2D {
00301
00302 protected:
00303
00305 int b1,b2;
00307 int n1,n2;
00308
00310 S** dptr;
00311
00312
00313 public:
00314
00315
00316
00317
00320 int getIndexBase(int j) const
00321 {
00322 switch(j){
00323
00324 case 1 : return b1;
00325 case 2 : return b2;
00326 }
00327 }
00328
00330 int getSize(int j) const
00331 {
00332 switch(j){
00333
00334 case 1 : return n1;
00335 case 2 : return n2;
00336 }
00337 }
00338
00339
00340
00341
00345 Array2D(int n_1, int n_2, int b_1=0, int b_2=0) :
00346 b1(b_1), b2(b_2),
00347 n1(n_1), n2(n_2)
00348 {
00349 dptr=new S*[n_1];
00350 for(int i=0;i<n1;i++){
00351
00352 dptr[i]=new S[n2];
00353 for(int j=0;j<n2;j++)dptr[i][j]=0;
00354 }
00355 }
00356
00357
00358 ~Array2D()
00359 {
00360 for(int i=0;i<n1;i++) delete[] dptr[i];
00361 delete[] dptr;
00362 }
00363
00365 const S& operator()(int i, int j) const
00366 {
00367 #ifdef SUBSCRIPT_CHECK
00368 SubscriptCheck::
00369 checkSubscript(i,j,b1,b2,n1,n2,"Array2D");
00370 #endif
00371 return dptr[i-b1][j-b2];
00372 }
00373
00375 S& operator()(int i, int j)
00376 {
00377 #ifdef SUBSCRIPT_CHECK
00378 SubscriptCheck::
00379 checkSubscript(i,j,b1,b2,n1,n2,"Array2D");
00380 #endif
00381 return dptr[i-b1][j-b2];
00382 }
00383
00387 S* operator[](int i)
00388 {
00389 #ifdef SUBSCRIPT_CHECK
00390 SubscriptCheck::
00391 checkSubscript(i,b1,n1,"Array2D, row i");
00392 #endif
00393 return dptr[i-b1];
00394 }
00395
00396 std::ostream& printSelf(std::ostream& os) const
00397 {
00398 os << endl << "Rectangular " << n1 << " by " << n2 << " array:"
00399 << endl << endl;
00400 for(int i=0;i<n1;i++){
00401
00402 for(int j=0;j<n2-1;j++) os << dptr[i][j] << ", ";
00403 os << dptr[i][n2-1] << endl;
00404 }
00405 return os << endl << endl;
00406 }
00407
00408
00409 };
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00428 template<typename S>
00429 class Array3D {
00430
00431 protected:
00432
00434 int b1,b2,b3;
00436 int n1,n2,n3;
00437
00439 S*** dptr;
00440
00441
00442 public:
00443
00444
00445
00446
00449 int getIndexBase(int j) const
00450 {
00451 switch(j){
00452
00453 case 1 : return b1;
00454 case 2 : return b2;
00455 case 3 : return b3;
00456 }
00457 }
00458
00460 int getSize(int j) const
00461 {
00462 switch(j){
00463
00464 case 1 : return n1;
00465 case 2 : return n2;
00466 case 3 : return n3;
00467 }
00468 }
00469
00470
00471
00472
00476 Array3D(int n_1, int n_2, int n_3, int b_1=0, int b_2=0, int b_3=0) :
00477 b1(b_1), b2(b_2), b3(b_3),
00478 n1(n_1), n2(n_2), n3(n_3)
00479 {
00480 dptr=new S**[n_1];
00481 for(int i=0;i<n1;i++){
00482
00483 dptr[i]=new S*[n2];
00484 for(int j=0;j<n2;j++){
00485
00486 dptr[i][j]=new S[n3];
00487 for(int k=0;k<n3;k++) dptr[i][j][k]=0;
00488 }
00489 }
00490 }
00491
00492 ~Array3D()
00493 {
00494 for(int i=0;i<n1;i++){
00495
00496 for(int j=0;j<n2;j++) delete[] dptr[i][j];
00497 delete[] dptr[i];
00498 }
00499 delete[] dptr;
00500 }
00501
00504 const S& operator()(int i, int j, int k) const
00505 {
00506 #ifdef SUBSCRIPT_CHECK
00507 SubscriptCheck::
00508 checkSubscript(i,j,k,b1,b2,b3,n1,n2,n3,"Array3D");
00509 #endif
00510 return dptr[i-b1][j-b2][k-b3];
00511 }
00512
00515 S& operator()(int i, int j, int k)
00516 {
00517 #ifdef SUBSCRIPT_CHECK
00518 SubscriptCheck::
00519 checkSubscript(i,j,k,b1,b2,b3,n1,n2,n3,"Array3D");
00520 #endif
00521 return dptr[i-b1][j-b2][k-b3];
00522 }
00523
00524
00525
00526
00527 };
00528
00529
00530
00531
00532 typedef Array1D<Real> RealArray1D;
00533 typedef Array2D<Real> RealArray2D;
00534 typedef Array3D<Real> RealArray3D;
00535
00536 typedef Array1D<int> IntArray1D;
00537 typedef Array2D<int> IntArray2D;
00538 typedef Array3D<int> IntArray3D;
00539
00540 typedef Array1D<long> LongArray1D;
00541 typedef Array2D<long> LongArray2D;
00542
00543 typedef Array1D<unsigned long> UnsignedLongArray1D;
00544 typedef Array2D<unsigned long> UnsignedLongArray2D;
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00570 template<typename S>
00571 class LiborArray2D {
00572
00573 protected:
00574
00576 int n;
00577
00581 int nSteps;
00582
00584 S** dptr;
00585
00586
00587 public:
00588
00589
00590
00591
00592
00596 LiborArray2D(int dim, int steps) :
00597 n(dim), nSteps(steps)
00598 {
00599 dptr=new S*[n*nSteps];
00600 for(int t=0;t<n-1;t++)
00601 for(int u=0;u<nSteps;u++){
00602
00603 int s=t*nSteps+u;
00604 dptr[s]=new S[n-t-1];
00605 for(int j=0;j<n-t-1;j++)dptr[s][j]=0;
00606 }
00607 }
00608
00609
00610 ~LiborArray2D()
00611 {
00612 for(int t=0;t<n-1;t++)
00613 for(int u=0;u<nSteps;u++) delete[] dptr[t*nSteps+u];
00614 delete[] dptr;
00615 }
00616
00619 const S& operator()(int s, int j) const
00620 {
00621 #ifdef SUBSCRIPT_CHECK
00622 checkSubscripts(s,j);
00623 #endif
00624 return dptr[s][j-1-s/nSteps];
00625 }
00626
00629 S& operator()(int s, int j)
00630 {
00631 #ifdef SUBSCRIPT_CHECK
00632 checkSubscripts(s,j);
00633 #endif
00634 return dptr[s][j-1-s/nSteps];
00635 }
00636
00637 private:
00638
00639
00640 int get_t(int s)
00641 {
00642 if(s%nSteps==0) return s/nSteps;
00643 return s/nSteps+1;
00644 }
00645
00646 void checkSubscripts(int s, int j)
00647 {
00648 if((s<0)||(s>n*nSteps-1)){
00649
00650 cout << "\n\nLiborArray2D: time step s = " << s
00651 << " not in [0," << n*nSteps-1 << "]"
00652 << "\nTerminating.";
00653 exit(0);
00654 }
00655
00656 int t=get_t(s);
00657 if((j<t)||(j>n-1)){
00658
00659 cout << "\n\nLiborArray2D: time step s = " << s << ", t = " << t
00660
00661 << "\nLibor index j = " << j << " not in ["<<t<<","<<n-1<<"]"
00662 << "\nTerminating.";
00663 exit(0);
00664 }
00665 }
00666
00667
00668
00669 };
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 MTGL_END_NAMESPACE(Martingale)
00680
00681 #endif
00682