include iostream include vector include iomanip using namespace std de

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
#define ld (long long double);
vector <vector <long double> > matrix;
void matrix_transpose(vector <vector <long double> >& mat) {
for (int i = 0; i < mat.size(); ++i) {
for (int j = 0; j < i; ++j) {
long double cur = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = cur;
}
}
}
vector <vector <long double> > matrix_mult(vector <vector <long double> >& x, vector <vector <long double> >& y) {
vector <vector <long double> > r(x.size(), vector <long double>(y[0].size()));
for (int i = 0; i < x.size(); i++) {
for (int j = 0; j < y[0].size(); j++) {
for (int k = 0; k < x.size(); k++) {
r[i][j] += x[i][k] * y[k][j];
}
}
}
return r;
}
long double dot_product(vector <long double>& x, vector <long double>& y) {
long double ret = 0.0;
for (int i = 0; i < x.size(); ++i) {
ret += x[i] * y[i];
}
return ret;
}
void matrix_subtr(vector <long double>& x, vector <long double>& y) {
for (int i = 0; i < x.size(); ++i) {
x[i] -= y[i];
}
}
vector <long double> matrix_mult_const(vector <long double> x, long double d) {
for (int i = 0; i < x.size(); ++i) {
x[i] *= d;
}
return x;
}
vector <long double> sqrt_dot_product(vector <long double>& x) {
x = matrix_mult_const(x, 1.0 / sqrt(dot_product(x, x)));
return x;
}
vector <vector <long double> > test(vector <vector <long double> >& x, vector <vector <long double> >& y) {
vector <vector <long double> > r(x.size(), vector <long double>(y[0].size()));
for (int i = 0; i < x.size(); i++) {
for (int j = 0; j < y[0].size(); j++) {
for (int k = 0; k < x.size(); k++) {
r[i][j] += x[i][k] * y[k][j];
}
}
}
return r;
}
int main() {
freopen("input.txt", "r", stdin);
int n;
cin >> n;
matrix.resize(n, vector <long double>(n));
vector <vector <long double> > b(n, vector <long double>(1));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> matrix[i][j];
}
cin >> b[i][0];
}
matrix_transpose(matrix);
vector <vector <long double> > e(n, vector <long double>(n));
for (int i = 0; i < n; ++i) {
e[i] = matrix[i];
for (int j = 0; j < i; ++j) {
//matrix_subtr(e[i], matrix_mult_const(e[j], dot_product(e[j], matrix[i]) / dot_product(e[j], e[j])));
matrix_subtr(e[i], matrix_mult_const(e[j], dot_product(e[j], matrix[i])));
}
e[i] = sqrt_dot_product(e[i]);
}
vector <vector <long double> > r(n, vector <long double>(n));
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
r[i][j] = dot_product(e[i], matrix[j]);
}
}
matrix_transpose(e);
cout << "Q" << endl;
cout << fixed << setprecision(3);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << e[i][j] << ' ';
}
cout << endl;
}
cout << "R" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << r[i][j] << ' ';
}
cout << endl;
}
vector <vector <long double> > t(n, vector <long double>(n));
cout << "Test" << endl;
t = test(e, r);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << t[i][j] << ' ';
}
cout << endl;
}
vector <vector <long double> > ans(n, vector <long double>(n));
matrix_transpose(e);
b = matrix_mult(e, b);
return 0;
}