-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathfunctions.cpp
More file actions
53 lines (49 loc) · 1.07 KB
/
Copy pathmathfunctions.cpp
File metadata and controls
53 lines (49 loc) · 1.07 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
#include "mathfunctions.h"
double trapz_intergal(std::vector<double> x, std::vector<double> y)
{
if(x.begin() == x.end() || x.size() != y.size())
{
return 0;
}
size_t last_index = x.size() - 1;
double sum = y[0] * (x[1] - x[0]) / 2;
for(size_t i = 1; i < last_index; ++i)
{
sum += y[i] * (x[i + 1] - x[i]);
}
sum += y[last_index] * (x[last_index] - x[last_index - 1]);
return sum;
}
std::vector<double> logspace(double start, double stop, size_t number, double base, bool endpoint)
{
std::vector<double> space;
space.resize(number);
double step = (stop - start) / (number - int(endpoint));
for(size_t i = 0; i < number; ++i)
{
space[i] = pow(base, start);
start += step;
}
return space;
}
int get_power(double num)
{
int power = 0;
if(num < 1)
{
while(!floor(num))
{
num *= 10;
++power;
}
}
else
{
while(!floor(num))
{
num /= 10;
--power;
}
}
return power;
}