-
Notifications
You must be signed in to change notification settings - Fork 0
/
lagrange.cpp
145 lines (111 loc) · 3.22 KB
/
lagrange.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
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
#include "lagrange.h"
Lagrange::Lagrange() {;}
Lagrange::~Lagrange() {
if(this->polymonial != nullptr)
delete [] this->polymonial;
}
void Lagrange::addNode(double x, double y) {
nodes.push_back(Node(x, y));
if(x > this->max_x || isnan(this->max_x)) this->max_x = x;
if(x < this->min_x || isnan(this->min_x)) this->min_x = x;
}
void Lagrange::calculate() {
this->polymonial = new double[this->size()];
double denominator;
double *numerator;
for(int i = 0; i < this->size(); i++)
this->polymonial[i] = 0;
for(int i = 0; i < this->size(); i++) {
denominator = this->denominator(i);
numerator = this->numerator(i);
for(int j = 0; j < this->size(); j++) {
this->polymonial[j] += this->nodes[i].y * (numerator[j] / denominator);
}
delete [] numerator;
}
if(this->argument_set) {
if(this->min_x <= this->argument && this->max_x >= this->argument) {
for(int i = 0; i < this->size(); i++)
this->value += pow(this->argument, i) * this->polymonial[i];
} else
this->argument_set = false;
}
}
double Lagrange::getMinArg()
{
return this->min_x;
}
double Lagrange::getMaxArg()
{
return this->max_x;
}
string Lagrange::getPolymonial() {
stringstream ss;
for(int i = this->size() - 1; i >= 0; i--) {
if(this->polymonial[i] == 0) continue;
else if(this->polymonial[i] < 0) ss << " - ";
if(i != this->size() - 1)
if(this->polymonial[i] >= 0) ss << " + ";
ss << abs(this->polymonial[i]);
if(i != 0) ss << " \u2981 " <<'x' << this->exponent[i];
}
return ss.str();
}
double Lagrange::getValue() {
return this->value;
}
vector<Lagrange::Node> Lagrange::getNodes()
{
return this->nodes;
}
void Lagrange::reset() {
argument_set = false;
value = 0;
if(this->polymonial != nullptr)
delete [] this->polymonial;
this->polymonial = nullptr;
nodes.clear();
}
void Lagrange::setArgument(double x) {
this->argument = x;
this->argument_set = true;
}
int Lagrange::size() {
return this->nodes.size();
}
double Lagrange::denominator(int k) {
double result = 1;
for(int i = 0; i < this->size(); i++) {
if(i == k) continue;
result *= nodes[k].x - nodes[i].x;
}
return result;
}
double *Lagrange::numerator(int k) {
int count = this->size();
double* result = new double[count];
double* temp = new double[count];
int n = 2, init;
for(int i = 0; i < this->size(); i++)
result[i] = 0;
// init first values
if(k) init = 0;
else init = 1;
result[0] = -nodes[init].x;
result[1] = 1;
for(int node_index = 1; node_index < count; node_index++) {
if(node_index == k or node_index == init) continue;
for(int res_index = 0; res_index < n; res_index++) {
temp[res_index + 1] += result[res_index];
temp[res_index] += nodes[node_index].x * result[res_index] * (-1);
}
n++;
// copy from temp to result and clear temp
for(int j = 0; j < n; j++) {
result[j] = temp[j];
temp[j] = 0;
}
}
delete [] temp;
return result;
}