Advertisement
noctual

Givens rotation / Поворот Гивенса

Jul 5th, 2021
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. /*
  2.  * basis for python code: https://github.com/AndrosovAS/QR-decomposition/blob/main/QR-decomposition.py
  3.  * example: https://www.youtube.com/watch?v=MxZy0LLEDLY
  4.  */
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11.     const int eps = 10000;
  12.     const int N = 3;
  13.     double A[N][N] = {
  14.         {0., -1., 1.},
  15.         {4., 2., 0.},
  16.         {3., 4., 0.}
  17.     };
  18.    
  19.     for (int col = 0; col < N; col++) {
  20.         for (int row = col+1; row < N; row++) {
  21.             if (A[row][col] != 0) {
  22.                 double r = sqrt(A[col][col] * A[col][col] + A[row][col] * A[row][col]);
  23.                 double c = A[col][col] / r;
  24.                 double s = -A[row][col] / r;
  25.  
  26.                 for (int i = 0; i < N; i++) {
  27.                     double row_i, col_i;
  28.                     col_i = A[col][i] * c + A[row][i] * (-s);
  29.                     row_i = A[col][i] * s + A[row][i] * c;
  30.                     A[col][i] = col_i, A[row][i] = row_i;
  31.                 }
  32.             }
  33.         }  
  34.     }
  35.  
  36.     for (int i = 0; i < N; i++) {
  37.         for (int j = 0; j < N; j++) {
  38.             cout << floor(A[i][j] * eps) / eps << '\t';
  39.         }
  40.         cout << '\n';
  41.     }
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement