Advertisement
bs9o

Richardson iterative using Chebyshev polynomials

Oct 1st, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. #define sign(x) (x < 0 ? (-1) : (1))
  6. #define eps 0.00000001
  7.  
  8. using namespace std;
  9. ofstream fout;
  10. ifstream fin;
  11.  
  12. double dotProduct(double **x, double **y, int n) {
  13.     double product = 0;
  14.     for (int i = 0; i < n; i++)
  15.         product += x[i][0] * y[i][0];
  16.     return product;
  17. }
  18.  
  19. double **matrixMultL(double **A, double **B, int row, int column, int n) {
  20.     int i, j;
  21.     double **product = new double*[row]();
  22.     for (i = 0; i < row; i++) {
  23.         product[i] = new double[column]();
  24.         for (j = 0; j < column; j++) {
  25.             for (int inner = 0; inner < n; inner++) {
  26.                 product[i][j] += A[i][inner] * B[inner][j];
  27.             }
  28.         }
  29.     }
  30.     //for (i = 0; i < row; i++) for (j = 0; j < column; j++) if (abs(product[i][j]) <= eps) product[i][j] = 0;
  31.     return product;
  32. }
  33.  
  34. double **matrixTranspose(double **M, int row, int column) {
  35.     double **product = new double*[column];
  36.     for (int i = 0; i < column; i++) product[i] = new double[row];
  37.     for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) product[j][i] = M[i][j];
  38.     return product;
  39. }
  40.  
  41. double **squareMatrixSub(double **A, double **B, int n) {
  42.     double **product = new double*[n]();
  43.     for (int i = 0; i < n; i++) {
  44.         product[i] = new double[n];
  45.         for (int j = 0; j < n; j++) product[i][j] = A[i][j] - B[i][j];
  46.     }
  47.     return product;
  48. }
  49.  
  50. double **vectorSub(double **x, double **y, int n) {
  51.     double ** product = new double*[n];
  52.     for (int i = 0; i < n; i++) {
  53.         product[i] = new double[1];
  54.         product[i][0] = x[i][0] - y[i][0];
  55.     }
  56.     return product;
  57. }
  58.  
  59. double **multByScalar(double **M, int row, int column, double a) {
  60.     for (int i = 0; i < row; i++)
  61.     for (int j = 0; j < column; ++j)
  62.         M[i][j] *= a;
  63.     return M;
  64. }
  65.  
  66. void rowSwap(double **M, int row1, int row2, int n) {
  67.     int i, j, t;
  68.     for (j = 0; j < n; j++) {
  69.         t = M[row1][j];
  70.         M[row1][j] = M[row2][j];
  71.         M[row2][j] = t;
  72.     }
  73. }
  74.  
  75. int check(double **M, int row, int column, int n) {
  76.     if (M[row][column] != 0) return 1;
  77.     else return 0;
  78. }
  79.  
  80. double **richardsonIterative(double **M, double **V, double **X0, double **X1, double w1, double w2, int n, double a, double b, int *cnt) {
  81.     double **X = new double*[n], **Y = new double*[n], **product = new double*[n], u;
  82.     for (int i = 0; i < n; i++) {
  83.         X[i] = new double[1];
  84.         Y[i] = new double[1];
  85.     }
  86.     u = 1 / (2 * (1 / w1) - w2);
  87.     X = vectorSub(vectorSub(X1, multByScalar(vectorSub(X1, X0, n), n, 1, -u * w2), n), multByScalar(vectorSub(matrixMultL(M, X1, n, 1, n), V, n), n, 1, (1 + u * w2) * 2 / (b + a)), n);
  88.     product = vectorSub(X, X1, n);
  89.     if (sqrt(dotProduct(product, product, n)) <= eps) return X1;
  90.     else {
  91.         (*cnt)++;
  92.         X = richardsonIterative(M, V, X1, X, w1, u, n, a, b, cnt);
  93.     }
  94.     return X;
  95. }
  96.  
  97.  
  98. int main() {
  99.     fin.open("input.txt");
  100.     fout.open("output.txt");
  101.     int n, cntTmp = 0;
  102.     int *cnt = &cntTmp;
  103.     double a, b, u1, u2, w1, w2;
  104.     fin >> a >> b;
  105.     fin >> n;
  106.     double **X = new double*[n](), **M = new double*[n], **V = new double*[n], **X0 = new double*[n], **Y = new double*[n];
  107.     for (int i = 0; i < n; i++) {
  108.         M[i] = new double[n + 1];
  109.         V[i] = new double[1];
  110.         X0[i] = new double[1]();
  111.         X[i] = new double[1];
  112.         for (int j = 0; j <= n; j++) {
  113.             X0[i][0] = 1;
  114.             if (j == n) fin >> V[i][0];
  115.             else fin >> M[i][j];
  116.         }
  117.     }
  118.     w1 = -(b - a) / (b + a);
  119.     w2 = 1 / (2 * (1 / w1) - w1);
  120.     X = vectorSub(X0, multByScalar((vectorSub(matrixMultL(M, X0, n, 1, n), V, n)), n, 1, 2 / (a + b)), n);
  121.     Y = vectorSub(vectorSub(X, multByScalar(vectorSub(X, X0, n), n, 1, w1 * w2), n), multByScalar(vectorSub(matrixMultL(M, X, n, 1, n), V, n), n, 1, (1 + w1 * w2) * 2 / (b + a)), n);
  122.     X = richardsonIterative(M, V, X, Y, w1, w2, n, a, b, cnt);
  123.     fout << "Ax = b solution set:" << endl;
  124.     for (int i = 0; i < n; i++) fout << "x[" << i << "] = " << setprecision(10) << X[i][0] << endl;
  125.     fout << "Number of iterations:" << endl << *cnt;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement