include iostream include vector include iomanip include cmath using na

  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
139
140
141
142
143
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
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;
}
}
}
vector <vector <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;
}
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, const 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;
}
pair <vector <vector <double> >, vector <vector <double> > > qrDecomposition(vector <vector <double> >& matrix, int n){
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])));
}
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);
return make_pair(e, r);
}
double maxSubdiagonalElement (vector <vector <double> >& matrix, int n) {
double max = matrix[1][0];
for (int i = 2; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
return max;
}
int main() {
freopen("input.txt", "r", stdin);
int n;
cin >> n;
double eps = 1e-2;
matrix.resize(n, vector <double>(n));
vector <vector <double> > b(n, vector <double>(1));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> matrix[i][j];
cout << matrix[i][j] << " ";
}
cout << endl;
}
pair <vector <vector <double> >, vector <vector <double> > > result;
double maxElement;
do {
result = qrDecomposition(matrix, n);
matrix = matrix_mult(result.second, result.first);
maxElement = maxSubdiagonalElement(matrix, n);
} while (maxElement >= eps);
cout << eps << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout <<endl;
return 0;
}