-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrals.cpp
39 lines (31 loc) · 909 Bytes
/
integrals.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
#include "complex.hpp"
#include "integrals.hpp"
/*
* интеграл по отрезку в комплексной плоскости
*/
cmplx integrate(func f, cmplx z1, cmplx z2, int n) {
cmplx result = 0;
cmplx step = (z2 - z1) / double(n);
for (int i = 0; i < n; ++i) {
cmplx z = z1 + double(i) * step;
result += f(z) * step;
}
return result;
}
cmplx integrateLog(func f, cmplx z1, cmplx z2){
cmplx r = 0;
cmplx z = z1, dz;
int n = 100;
double zero = 1e-9;
dz = (z2 - z1) / double(n);
if(std::abs(f(z)) > zero)
r += 0.5 * (f(z + dz / 2.) - f(z - dz / 2.)) / f(z);
for (int i = 1; i < n; ++i, z += dz) {
if(std::abs(f(z)) > zero)
r += (f(z + dz / 2.) - f(z - dz / 2.)) / f(z);
}
z += dz;
if(std::abs(f(z)) > zero)
r += 0.5 * (f(z + dz / 2.) - f(z - dz / 2.)) / f(z);
return r;
}