Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #ifdef _MSC_VER
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4.  
  5. #include <gsl/gsl_math.h>
  6. #include <gsl/gsl_linalg.h>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <chrono>
  10. #include <iostream>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <cmath>
  15. #define max(X,Y) ((X)>(Y)? (X):(Y))
  16. #define min(X,Y) ((X)<(Y)? (X):(Y))
  17. #define abs(X) ((X)>0? (X):-(X))
  18.  
  19. #define N 10000
  20. #define M 5
  21.  
  22. using namespace std;
  23.  
  24. void multVM(double** A, double* x, double* y) {
  25.     for (int i = 0; i < N; ++i) {
  26.         y[i] = 0.0;
  27.         for (int j = max(0, i - M); j <= min(N - 1, i + M); ++j) {
  28.             y[i] += A[i][j] * x[j];
  29.         }
  30.     }
  31. }
  32.  
  33. double scalarVV(double* x, double* y) {
  34.     double z = 0;
  35.     for (int i = 0; i < N; ++i) {
  36.         z += x[i] * y[i];
  37.     }
  38.     return z;
  39. }
  40.  
  41. void addVector(double *x, double* y, double* tmp, double modifier) {
  42.     for (int i = 0; i < N; ++i) {
  43.         tmp[i] = x[i] + (modifier*y[i]);
  44.     }
  45. }
  46.  
  47. void decVector(double *x, double* y, double* tmp, double modifier) {
  48.     for (int i = 0; i < N; ++i) {
  49.         tmp[i] = x[i] - (modifier*y[i]);
  50.     }
  51. }
  52.  
  53. int main() {
  54.     time_t t1, t2;
  55.     double **A = (double**)malloc(N * sizeof(double*));
  56.     for (int i = 0; i < N; ++i) {
  57.         A[i] = (double*)malloc(N * sizeof(double));
  58.     }
  59.     double x[N];
  60.     double y[N];
  61.     double b[N];
  62.     double* v1 = new double[N];
  63.     for (int i = 0; i < N; ++i) {
  64.         for (int j = 0; j < N; ++j) {
  65.             if (abs(i - j) <= M)A[i][j] = 1 / (1.0 + abs(i - j));
  66.             else A[i][j] = 0;
  67.         }
  68.         x[i] = 0.0;
  69.         b[i] = i + 1;
  70.     }
  71.     //inicjalizacja
  72.     multVM(A, x, v1);
  73.     decVector(b, v1, v1, 1);
  74.  
  75.     double aK = 0;
  76.     double bK = 0;
  77.     double* r = new double[N];
  78.     double* tmp = new double[N];
  79.     for (int i = 0; i < N; ++i) {
  80.         r[i] = v1[i];
  81.     }
  82.     double normr, normx;
  83.     int iter = 0;
  84.     //
  85.     FILE* f = fopen("wyniki.dat", "w");
  86.  
  87.     std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  88.  
  89.     while (sqrt(scalarVV(r, r))>0.000001) {
  90.         normr = sqrt(scalarVV(r, r));
  91.         normx = sqrt(scalarVV(x, x));
  92.  
  93.         printf("x(k):%12lf    r(k):%12lf    ", normx, normr);
  94.         if (f)fprintf(f, "x(k):%12lf    r(k):%12lf    ", normx, normr);
  95.         double rrt = scalarVV(r, r);
  96.         multVM(A, v1, tmp);
  97.         aK = scalarVV(r, r) / scalarVV(v1, tmp);
  98.         addVector(x, v1, x, aK);
  99.         decVector(r, tmp, r, aK);
  100.         bK = scalarVV(r, r) / rrt;
  101.         printf("ak:%9lf    bk:%9lf    \n", aK, bK);
  102.         if (f)fprintf(f, "ak:%9lf    bk:%9lf    \n", aK, bK);
  103.         addVector(r, v1, v1, bK);
  104.     }
  105.     std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  106.     fprintf(f, "x(k) koncowe: %12lf  ", normx);
  107.    
  108.     std::cout << "norma x: " << normx <<std::endl;
  109.     std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
  110.     std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << std::endl;
  111.  
  112.     delete v1;
  113.     delete r;
  114.     delete tmp;
  115.     for (int i = 0; i < N; ++i) {
  116.         free(A[i]);
  117.     }
  118.     free(A);
  119.  
  120.  
  121.     int signum;
  122.  
  123.     gsl_matrix *B = gsl_matrix_calloc(N, N);
  124.     gsl_vector *x1 = gsl_vector_calloc(N);
  125.     gsl_vector *b1 = gsl_vector_calloc(N);
  126.     gsl_permutation *p = gsl_permutation_calloc(N);
  127.  
  128.     for (int i = 0; i < N; i++)
  129.     {
  130.         gsl_vector_set(b1, i, i + 1);
  131.     }
  132.  
  133.     for (int i = 0; i<N; i++)
  134.     {
  135.         for (int j = 0; j<N; j++)
  136.         {
  137.             gsl_matrix_set(B, i, j, 0.0);
  138.             if (abs(i - j) <= M)
  139.                 gsl_matrix_set(B, i, j, 1 / (1.0 + abs(i - j)));
  140.             else
  141.                 gsl_matrix_set(B, i, j, 0.0);
  142.         }
  143.     }
  144.  
  145.     begin = std::chrono::steady_clock::now();
  146.  
  147.     gsl_linalg_LU_decomp(B, p, &signum);
  148.     gsl_linalg_LU_solve(B, p, b1, x1);
  149.  
  150.     end = std::chrono::steady_clock::now();
  151.  
  152.     double x2[N];
  153.  
  154.     for (int i = 0; i < N; i++)
  155.     {
  156.         x2[i] = gsl_vector_get(x1, i);
  157.     }
  158.    
  159.     double normx2 = sqrt(scalarVV(x2, x2));
  160.  
  161.     std::cout << std::endl << "norma x: " << std::endl;
  162.     std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
  163.     std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << std::endl;
  164.     fprintf(f, "x(k)(LU) koncowe: %12lf\n", normx2);
  165.  
  166.     getchar();
  167.     fclose(f);
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement