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
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
#define ld (long double);
vector <vector <double> > matrix;
void matrix_transpose(vector <vector <double> >& mat) {
for (int i = 0; i < mat.size(); ++i) {
for (int j = 0; j < i; ++j) {
double cur = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = cur;
}
}
}
double matrix_mult(vector <vector <double> >& x, vector <vector <double> >& y) {
vector <vector <double> > r (x.size(), vector <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[0][0];
}
double dot_product (vector <double>& x, vector <double>& y) {
double ret = 0.0;
for (int i = 0; i < x.size(); ++i) {
ret += x[i] * y[i];
}
return ret;
}
void matrix_subtr(vector <double>& x, vector <double>& y) {
for (int i = 0; i < x.size(); ++i) {
x[i] -= y[i];
}
}
vector <double> matrix_mult_const (vector <double> x, double d) {
for (int i = 0; i < x.size(); ++i) {
x[i] *= d;
}
return x;
}
vector <double> sqrt_dot_product (vector <double>& x) {
x = matrix_mult_const(x, 1.0 / sqrt(dot_product(x, x)));
return x;
}
vector <vector <double> > test(vector <vector <double> >& x, vector <vector <double> >& y) {
vector <vector <double> > r (x.size(), vector <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 <double> (n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >>matrix[i][j];
}
}
matrix_transpose(matrix);
vector <vector <double> > e(n, vector <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 <double> > r(n, vector <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 <double> > t(n, vector <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;
}
return 0;
}