This repository was archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHermite.cpp
More file actions
58 lines (48 loc) · 1.48 KB
/
Hermite.cpp
File metadata and controls
58 lines (48 loc) · 1.48 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
#include "Hermite.h"
Hermite::Hermite()
{
}
Hermite::Hermite(double* x, double* y, int64_t n)
:x(x), y(y), n(n)
{
if (n < 2) {
return;
}
this->delta = new double[n];
this->caculate();
}
Hermite::~Hermite()
{
if (this->delta != nullptr) {
delete[] this->delta;
this->delta = nullptr;
}
}
void Hermite::caculate()
{
this->delta[0] = 0;
this->delta[n - 1] = 0;
for (uint64_t i = 1; i < this->n - 1; i++) {
this->delta[i] = (this->y[i + 1] - this->y[i - 1]) / (this->x[i + 1] - this->x[i - 1]);
}
}
double Hermite::result(double t)
{
if (this->n < 2) {
return 0;
}
auto f1 = [](double& T1, double& T2, double& D1, double& D2)->double {return (1.0 + 2.0 * T1) * pow(T2, 2.0); };
auto f2 = [](double& T1, double& T2, double& D1, double& D2)->double {return (1.0 + 2.0 * T2) * pow(T1, 2.0); };
auto f3 = [](double& T1, double& T2, double& D1, double& D2)->double {return D1 * pow(T2, 2.0); };
auto f4 = [](double& T1, double& T2, double& D1, double& D2)->double {return D2 * pow(T1, 2.0); };
for (uint64_t i = 0; i < this->n - 1; i++) {
if (t >= this->x[i] && t < this->x[i + 1]) {
double delta1 = (t - this->x[i]);
double delta2 = (t - this->x[i + 1]);
double T1 = delta1 / (this->x[i + 1] - this->x[i]);
double T2 = delta2 / (this->x[i] - this->x[i + 1]);
return f1(T1, T2, delta1, delta2) * this->y[i] + f2(T1, T2, delta1, delta2) * this->y[i + 1] + f3(T1, T2, delta1, delta2) * this->delta[i] + f4(T1, T2, delta1, delta2) * this->delta[i + 1];
}
}
return 0;
}