Advertisement
Alexandre_lsv

Untitled

Dec 14th, 2016
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <experimental/array>
  3. #define mar experimental::make_array
  4. using namespace std;
  5. array<array<long double, 100>, 50> table {};
  6. array<long double, 50> val {};
  7. int n{};
  8. int mfd=4;
  9. long double h=0.1;
  10. long double y {1.683635};
  11. array<long double, 3> arX{0.151316, 0.815359, 0.281586};
  12. void finDiff(int k);
  13. long double intBegTable(long double x);
  14. long double intEndTable(long double x);
  15. long double intCenterTable(long double x);
  16. long double revIntTable(long double y);
  17. void crTable();
  18. void solveForX();
  19. void solveForY();
  20. long double findT(long double y, int ind);
  21. long double fact(int a);
  22. int main(){
  23.   freopen("in","r",stdin);
  24.   while(cin >> val[n] >> table[n<<1][0] && ++n){}
  25.   crTable();
  26.   solveForX();
  27.   solveForY();
  28.   return 0;
  29. }
  30. void solveForX(){
  31.   for(int i=0; i<3; i++){
  32.     double res=-1337;
  33.     for(int j=0; j<n; j++){
  34.       if (arX[i]<val[j]){
  35.         if (j<n/3)
  36.           res=intBegTable(arX[i]);
  37.         if (j>=n/3 && j<2*n/3)
  38.           res=intCenterTable(arX[i]);
  39.         if (j>=2*n/3)
  40.           res=intEndTable(arX[i]);
  41.         break;
  42.       }
  43.     }
  44.     cout << "\np("<< arX[i] << ") ~=~ "  << setprecision(7)<< res << "\n\n";
  45.     if(res!=-1337)
  46.       continue;
  47.     res=intEndTable(arX[i]);
  48.   }
  49.   double res{};
  50.   res=intBegTable(arX[2]);
  51.   cout << "p("<< arX[2] << ") ~=~ "  << setprecision(7)<< res << "\n\n";
  52.   res=intEndTable(arX[2]);
  53.   cout << "\np("<< arX[2] << ") ~=~ " << setprecision(7) << res << "\n\n";
  54. }
  55. void solveForY(){
  56.   double res{},
  57.          resX{};
  58.   res=revIntTable(y);    
  59.   cout << "\n\np^-1("<< y << ") ~=~ " << setprecision(7) << res << "\n\n";
  60.   cout << "Check: \n";
  61.   for(int j=0; j<n; j++){
  62.     if (res<val[j]){
  63.       if (j<n/3)
  64.         resX=intBegTable(res);
  65.       if (j>=n/3 && j<2*n/3)
  66.         resX=intCenterTable(res);
  67.       if (j>=2*n/3)
  68.         resX=intEndTable(res);
  69.       break;
  70.     }
  71.   }
  72.   cout << "\np("<< res << ") ~=~ " << resX << "\n\n";
  73.   cout << y << " == " <<  resX << "?\n\n\n\n";
  74. }
  75. void finDiff(int k){
  76.   for(int i=k; i<(n<<1)-k; i++)
  77.     table[i][k]=table[i+1][k-1]-table[i-1][k-1];
  78. }
  79. void crTable(){
  80.   mfd=min(mfd, n-1);
  81.   cout << "x\t\t px\t ";
  82.   for(int i=1; i<=mfd; i++)
  83.     finDiff(i),
  84.       cout << "d^{" << i << "}" << mar("\t", "\n\n")[i==mfd];
  85.   for(int i=0; i<(n<<1)-1; i++){
  86.     cout << mar("\t", to_string(val[i>>1]))[i%2==0] << " |\t";
  87.     for(int j=0; j<=mfd; j++)
  88.       cout << " \a"[table[i][j]<0] << mar("\t", to_string(table[i][j]))[table[i][j]!=0] << "\a\n"[j==mfd];
  89.   }
  90. }
  91. long double intBegTable(long double x){
  92.   int ind{};
  93.   for(ind=0; ind<n; ind++)
  94.     if (val[ind]>x){
  95.       ind--;
  96.       break;
  97.     }
  98.   long double t=(x-val[ind])/h,
  99.        res=0,
  100.        tt=1;
  101.   cout << "\n\nInterp BEGIN table start, x=" << x << ", x_0="<<val[ind] << ", t=" << t << endl;
  102.   for(int i=0; i<5; i++){
  103.     //cout << table[(ind<<1)+i][i] << ' ' << tt << endl;
  104.     res+=tt*table[(ind<<1)+i][i]/fact(i), tt=tt*(t-i);
  105.   }
  106.   return res;
  107. }
  108. long double intEndTable(long double x){
  109.   int ind{};
  110.   for(ind=n-1; ind>=0; ind--)
  111.     if (val[ind]<x){
  112.       ind++;
  113.       break;
  114.     }
  115.   long double t=(x-val[ind])/h,
  116.        res=0,
  117.        tt=1;
  118.   cout << "Interp END table start, x=" << x << ", x_0="<<val[ind] << ", t=" << t << endl;
  119.   for(int i=0; i<5; i++){
  120.     //cout << table[(ind<<1)-i][i] << ' ' << tt << endl;
  121.     res+=tt*table[(ind<<1)-i][i]/fact(i), tt=tt*(t+i);
  122.   }
  123.   return res;
  124. }
  125. long double intCenterTable(long double x){
  126.   int ind{};
  127.   for(ind=0; ind<n; ind++)
  128.     if (val[ind]>x){
  129.       if (fabs(val[ind]-x)>fabs(val[ind-1]-x))
  130.         ind--;
  131.       break;
  132.     }
  133.   long double t=(x-val[ind])/h,
  134.        res=0,
  135.        tt=1;
  136.   cout << "Interp CENTER table start, x=" << x << ", x_0="<<val[ind] << ", t=" << t << endl;
  137.   for(int i=0; i<5; i++){
  138.     //cout << table[(ind<<1)+1*(i%2==1)][i] << ' ' << tt << endl;
  139.     res+=tt*table[(ind<<1)+1*(i%2==1)][i]/fact(i), tt=tt*(t+(((i+1)>>1))*pow(-1,i));
  140.   }
  141.   return res;
  142. }
  143. long double revIntTable(long double y){
  144.   int ind{};
  145.   for(ind=0; ind<(n<<1)-1; ind++)
  146.     if (table[ind<<1][0]>y){
  147.       ind--;
  148.       break;
  149.     }
  150.   long double res=y,
  151.        tt=1,
  152.        t{};
  153.   t=findT(y, ind);
  154.   res=t*h+val[ind];
  155.   return res;
  156. }
  157. long double findT(long double y, int ind){
  158.   long double res{-1},
  159.        t{1},
  160.        tt;
  161.   while(fabs(res-t)>1e-4){
  162.     t=res;
  163.     cout << "t=" << t << endl;
  164.     res=0;
  165.     tt=1;
  166.     res=y-table[ind<<1][0]-t*(t-1)/2.*table[(ind<<1)+2][2]-t*(t-1)*(t-2)/6.*table[(ind<<1)+3][3];
  167.     res/=table[(ind<<1)+1][1];
  168.   }
  169.   return res;
  170. }
  171. long double fact(int a){
  172.   if (a<2)
  173.     return 1.;
  174.   return a*fact(a-1);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement