This repository was archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (144 loc) · 4.89 KB
/
main.cpp
File metadata and controls
199 lines (144 loc) · 4.89 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "fredholm.hpp"
#include "invert.hpp"
#include <iostream>
#include <iomanip>
#include <cmath>
const unsigned WIDTH = 10;
const unsigned PRECISION = 7;
double ker(double t, double s) {
return cos(t) + cos(s);
}
double f(double s) {
return -sin(1) - cos(1) - .5 * cos(s) + 1 - s;
}
double phi_exact(double s) {
return -s;
}
double matrix_norm(double** a, unsigned n) {
double norm = 0;
for (unsigned i = 0; i < n; i++) {
double curr_str_len = 0;
for (unsigned j = 0; j < n; j++)
curr_str_len += fabs(a[i][j]);
if (curr_str_len > norm)
norm = curr_str_len;
}
return norm;
}
double vector_norm(double* v, unsigned n) {
double norm = 0;
for (unsigned i = 0; i < n; i++)
if (fabs(v[i]) > norm)
norm = fabs(v[i]);
return norm;
}
double inv_matrix_norm(double** a, unsigned n) {
double** a_inv = new double*[n];
for (unsigned i = 0; i < n; i++) {
a_inv[i] = new double[n];
for (unsigned j = 0; j < n; j++)
a_inv[i][j] = a[i][j];
}
invert(a_inv, n);
double norm = matrix_norm(a_inv, n);
for (unsigned i = 0; i < n; i++)
delete[] a_inv[i];
delete[] a_inv;
return norm;
}
void print(double** a, double* f_n, unsigned n) {
std::cout.setf(std::ios::right | std::ios::fixed | std::ios::showpoint);
std::cout.precision(PRECISION);
for (unsigned i = 0; i < n; i++) {
for (unsigned j = 0; j < n; j++)
std::cout << std::setw(WIDTH) << a[i][j] << '\t';
std::cout << '\t' << std::setw(WIDTH) << f_n[i] << std::endl;
}
std::cout << std::endl;
}
void print(double* x, unsigned n) {
std::cout.flags(std::ios::left | std::ios::fixed | std::ios::showpoint);
std::cout << std::setw(WIDTH) << "Computed" << '\t';
std::cout << std::setw(WIDTH) << "Exact" << '\t';
std::cout << std::setw(WIDTH) << "Error" << std::endl;
for (unsigned i = 0; i < n; i++) {
double exact = phi_exact(1. / n * ((double)i + .5));
std::cout << std::setw(WIDTH) << x[i] << '\t';
std::cout << std::setw(WIDTH) << exact << '\t';
std::cout << std::setw(WIDTH) << exact - x[i] << std::endl;
}
std::cout << std::endl;
}
double error_norm(double* x, unsigned n) {
double norm = 0;
for (unsigned i = 0; i < n; i++) {
double exact = phi_exact(1. / n * ((double)i + .5));
double z_i = exact - x[i];
if (norm < fabs(z_i))
norm = fabs(z_i);
}
return norm;
}
double discrepancy_norm(double** a, double* x, double* f_n, unsigned n) {
double norm = 0;
for (unsigned i = 0; i < n; i++) {
double r_i = 0;
for (unsigned j = 0; j < n; j++)
r_i += a[i][j] * x[j];
r_i -= f_n[i];
if (fabs(r_i) > norm)
norm = fabs(r_i);
}
return norm;
}
int main(int argc, const char* argv[]) {
const unsigned N = 100;
double** a = new double*[N];
for (unsigned i = 0; i < N; i++)
a[i] = new double[N];
double* f_n = new double[N];
double* x = new double[N];
for (unsigned i = 0; i < N; i++)
x[i] = 0;
create_matrixes(-1, ker, f, a, f_n, N);
// print(a, f_n, N);
double** a_copy = new double*[N];
for (unsigned i = 0; i < N; i++) {
a_copy[i] = new double[N];
for (unsigned j = 0; j < N; j++)
a_copy[i][j] = a[i][j];
}
double* f_copy = new double[N];
for (unsigned i = 0; i < N; i++)
f_copy[i] = f_n[i];
unsigned it;
double accuracy;
int ret = solve(a, f_n, x, N, 1E-10, 1000, it, accuracy);
// print(x, N);
double a_norm = matrix_norm(a_copy, N);
double a_inv_norm = inv_matrix_norm(a_copy, N);
double x_norm = vector_norm(x, N);
double z_norm = error_norm(x, N);
double r_norm = discrepancy_norm(a_copy, x, f_copy, N);
std::cout.flags(std::ios::scientific);
std::cout << "Status: " << (ret? "accuracy not achieved" : "OK") << std::endl;
std::cout << "Quantity of iterations: " << it << std::endl;
std::cout << "Accuracy: " << accuracy << std::endl;
std::cout << "||A|| = " << a_norm << std::endl;
std::cout << "||A^-1|| = " << a_inv_norm << std::endl;
std::cout << "ν(A) = " << a_norm * a_inv_norm << std::endl;
std::cout << "||x|| = " << x_norm << std::endl;
std::cout << "||z|| = " << z_norm << std::endl;
std::cout << "ζ = " << z_norm / x_norm << std::endl;
std::cout << "||r|| = " << r_norm << std::endl;
std::cout << "ρ = " << r_norm / vector_norm(f_copy, N) << std::endl << std::endl;
for (unsigned i = 0; i < N; i++) {
delete[] a[i];
delete[] a_copy[i];
}
delete[] a;
delete[] a_copy;
delete[] f_n;
delete[] f_copy;
delete[] x;
}