Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define EPSILON 10e-6
  10.  
  11. typedef vector<double> Vec;
  12. typedef vector< Vec > VecMatrix;
  13.  
  14. double alpha(const VecMatrix &A, const Vec &N, const int j, const int n)
  15. {
  16.     double sum = 0;
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         sum += A[i][j] * N[i];
  20.     }
  21.     return sum;
  22. }
  23.  
  24. double beta(const Vec &N, const int n)
  25. {
  26.     double sum = 0;
  27.     for (int i = 0; i < n; i++)
  28.     {
  29.         sum += N[i] * N[i];
  30.     }
  31.     sum *= 0.5;
  32.     return sum;
  33. }
  34.  
  35. int sign(double arg)
  36. {
  37.     if (arg > 0) return 1;
  38.     else if (arg < 0) return -1;
  39.     else return 0;
  40. }
  41.  
  42. void printMatrix(const VecMatrix &mat, const int N)
  43. {
  44.     cout << endl;
  45.     for (int i = 0; i < N; i++)
  46.     {
  47.         for (int j = 0; j < N; j++)
  48.         {
  49.             cout.width(8);
  50.             cout << mat[i][j];
  51.         }
  52.         cout << "   |";
  53.         cout.width(6);
  54.         cout << mat[i][N] << endl;
  55.     }
  56.     cout << endl;
  57. }
  58.  
  59.  
  60. void householder(VecMatrix &mat, const int n)
  61. {
  62.     for (int k = 0; k < n - 1; k++)
  63.     {
  64.         Vec N(n);
  65.  
  66.         for (int i = 0; i < n; i++)
  67.             N[i] = mat[i][k];
  68.        
  69.  
  70.         double A = 0;
  71.         for (int i = 0; i < n; i++)
  72.             A += mat[i][k] * mat[i][k];
  73.         A = sqrt(A);
  74.  
  75.         N[k] += sign(mat[k][k]) * A;
  76.  
  77.         double b = beta(N, n);
  78.  
  79.         cout << "A: " << A << endl;
  80.         cout << "Beta: " << b << endl;
  81.  
  82.         for (int j = k; j < n + 1; j++)
  83.         {
  84.             double a = alpha(mat, N, j, n);
  85.             double ab = a / b;
  86.  
  87.             if (fabs(ab) < EPSILON)
  88.                 ab = 0;
  89.  
  90.             cout << "Alpha" << j << ": " << a << endl;
  91.  
  92.             for (int i = k; i < n; i++)
  93.             {
  94.                 mat[i][j] -= ab * N[i];
  95.  
  96.                 if (fabs(mat[i][j]) < EPSILON)
  97.                     mat[i][j] = 0;
  98.             }
  99.         }
  100.  
  101.         printMatrix(mat, n);
  102.     }
  103. }
  104.  
  105. bool processSolutions(const VecMatrix &mat, Vec &X, const int N)
  106. {
  107.     for (int i = N - 1; i >= 0; i--)
  108.     {
  109.         if (mat[i][i] == 0)
  110.             return false;
  111.  
  112.         X[i] = mat[i][N];
  113.  
  114.         for (int j = i + 1; j < N; j++)
  115.             X[i] -= mat[i][j] * X[j];
  116.  
  117.         X[i] /= mat[i][i];
  118.  
  119.         if (fabs(X[i]) < EPSILON)
  120.             X[i] = 0;
  121.     }
  122.  
  123.     return true;
  124. }
  125.  
  126. int main()
  127. {
  128.     cout << setprecision(3);
  129.    
  130.     const int N = 4;
  131.     VecMatrix matrix(N, Vec(N + 1));
  132.     Vec solutions(N);
  133.  
  134.  
  135.     matrix[0][0] = 1;
  136.     matrix[0][1] = 2;
  137.     matrix[0][2] = -1;
  138.     matrix[0][3] = 2;
  139.     matrix[0][4] = 3;
  140.  
  141.     matrix[1][0] = 2;
  142.     matrix[1][1] = -1;
  143.     matrix[1][2] = 3;
  144.     matrix[1][3] = -2;
  145.     matrix[1][4] = 0;
  146.  
  147.     matrix[2][0] = 0;
  148.     matrix[2][1] = 1;
  149.     matrix[2][2] = 2;
  150.     matrix[2][3] = 1;
  151.     matrix[2][4] = 1;
  152.  
  153.     matrix[3][0] = -2;
  154.     matrix[3][1] = 3;
  155.     matrix[3][2] = 1;
  156.     matrix[3][3] = 0;
  157.     matrix[3][4] = 2;
  158.    
  159.     householder(matrix, N);
  160.     processSolutions(matrix, solutions, N);
  161.  
  162.     cout << endl;
  163.     for (int i = 0; i < N; i++)
  164.     {
  165.         cout << "x" << i + 1 << " = " << solutions[i] << endl;
  166.     }
  167.  
  168.     cin.get();
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement