-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.cpp
55 lines (50 loc) · 1.68 KB
/
integration.cpp
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
//
// Created by kwh44 on 3/31/19.
//
#include <thread>
#include <vector>
#include <atomic>
#include "integration.h"
double function(const double &x1, const double &x2) {
double sum_result = 0.002;
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
sum_result += 1 / (5 * (i + 2) + j + 3 + std::pow(x1 - 16 * j, 6) + std::pow(x2 - 16 * i, 6));
}
}
return 1 / sum_result;
}
template<class T>
void double_integral_compute(double a, const double &b, double c, const double &d, const size_t &steps,
T &result) {
double dx = (b - a) / steps;
double dy = (d - c) / steps;
double cur_result(0);
for (int i = 0; i < steps; ++i) {
for (int j = 0; j < steps; ++j) {
cur_result += function(a + dx / 2, c + dy / 2);
c += dy;
}
a += dx;
}
result = result + cur_result * dx * dy;
}
double integrate(double a, const double &b, double c, const double &d, const size_t &steps, const size_t &thread_num) {
if (thread_num == 1) {
double result = 0;
double_integral_compute<double>(a, b, c, d, steps, result);
return result;
}
std::atomic<double> result(0);
std::vector<std::thread> threads_list;
threads_list.reserve(thread_num);
double x_range_step = (b - a) / thread_num;
size_t steps_part = steps / thread_num;
for (size_t i = 0; i < thread_num; ++i) {
threads_list.emplace_back(
double_integral_compute<std::atomic<double> >, a + x_range_step * i,
a + x_range_step * (i + 1), c, d, steps_part, std::ref(result));
}
for (auto &v: threads_list) v.join();
return result;
}