samuel21119

chunwailin's closet little girl

Oct 17th, 2020
961
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************************************************************************
  2.   > File Name: a.cpp
  3.   > datauthor: Samuel
  4.   > Mail: enminghuang21119@gmail.com
  5.   > Created Time: Sun Oct 18 10:15:00 2020
  6. *************************************************************************/
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. using namespace std;
  11.  
  12. struct Vector {
  13.     long double data[2];
  14.     Vector() {data[0] = data[1] = 0;}
  15.     Vector(long double x[2]) {
  16.         data[0] = x[0];
  17.         data[1] = x[1];
  18.     }
  19.     void out() {
  20.         for (int i = 0; i < 2; i++)
  21.             cout << setw(10) << data[i] << '\n';
  22.     }
  23. };
  24. struct Matrix {
  25.     long double data[2][2];
  26.     Matrix() {data[0][0] = data[0][1] = data[1][0] = data[1][1] = 0;}
  27.     Matrix(long double x[2][2]) {
  28.         data[0][0] = x[0][0];
  29.         data[1][0] = x[1][0];
  30.         data[0][1] = x[0][1];
  31.         data[1][1] = x[1][1];
  32.     }
  33.     long double det() {
  34.         return data[0][0] * data[1][1] - data[0][1] * data[1][0];
  35.     }
  36.     Matrix operator* (const Matrix &b) const {
  37.         Matrix re;
  38.         for (int i = 0; i < 2; i++)
  39.             for (int k = 0; k < 2; k++)
  40.                 for (int j = 0; j < 2; j++)
  41.                     re.data[i][k] += data[i][j] * b.data[j][k];
  42.         return re;
  43.     }
  44.     Vector operator* (const Vector &b) const {
  45.         Vector re;
  46.         for (int i = 0; i < 2; i++)
  47.             for (int j = 0; j < 2; j++)
  48.                 re.data[i] += data[i][j] * b.data[j];
  49.         return re;
  50.     }
  51.     Matrix inv() {
  52.         Matrix re;
  53.         long double d = det();
  54.         re.data[0][0] = data[1][1];
  55.         re.data[1][1] = data[0][0];
  56.         re.data[0][1] = -data[0][1];
  57.         re.data[1][0] = -data[1][0];
  58.         for (int i = 0; i < 2; i++)
  59.             for (int j = 0; j < 2; j++)
  60.                 re.data[i][j] /= d;
  61.         return re;
  62.     }
  63.     Matrix transpose() {
  64.         Matrix re;
  65.         re.data[0][0] = data[0][0];
  66.         re.data[1][1] = data[1][1];
  67.         re.data[0][1] = data[1][0];
  68.         re.data[1][0] = data[0][1];
  69.         return re;
  70.     }
  71.     void out() {
  72.         for (int i = 0; i < 2; i++) {
  73.             for (int j = 0; j < 2; j++)
  74.                 cout << setw(10) << data[i][j];
  75.             cout << '\n';
  76.         }
  77.     }
  78. };
  79. Vector project(Matrix &A, Vector &b) {
  80.     return A.transpose() * (A.transpose() * A).inv() * A * b;
  81. }
  82.  
  83. long double in1[2], in2[2][2];
  84. int main() {
  85.     for (int i = 0; i < 2; i++)
  86.         for (int j = 0; j < 2; j++)
  87.             cin >> in2[i][j];
  88.     cin >> in1[0] >> in1[1];
  89.     Matrix A(in2);
  90.     Vector b(in1);
  91.    
  92.     cout << "Transpose Matrix of A:\n";
  93.     A.transpose().out();
  94.  
  95.     cout << "Inverse Matrix of A:\n";;
  96.     A.inv().out();
  97.  
  98.     cout << "Project b on A:\n";
  99.     project(A, b).out();
  100.  
  101.     return 0;
  102. }
  103.  
RAW Paste Data