forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmd.cpp
127 lines (109 loc) · 3 KB
/
vmd.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
#include <iostream>
#include <vector>
#include <Python.h>
#include "matplotlibcpp.h"
#include <math.h>
#include <cmath>
namespace plt = matplotlibcpp;
void PRINTX(std::vector<std::vector<double>> x){
for(auto & row : x){
for(auto & col : row){
std::cout << col << "\t";
}
std::cout << std::endl;
}
}
std::vector<std::vector<double>> MMULT(std::vector<std::vector<double>> x, std::vector<std::vector<double>> y){
std::vector<std::vector<double>> z;
std::vector<double> t;
double total = 0;
for(int i = 0; i < x.size(); ++i){
t.clear();
for(int j = 0; j < y[0].size(); ++j){
total = 0;
for(int k = 0; k < x[0].size(); ++k){
total += x[i][k]*y[k][j];
}
t.push_back(total);
}
z.push_back(t);
}
return z;
}
std::vector<std::vector<double>> INVERSE(std::vector<std::vector<double>> x){
std::vector<std::vector<double>> I;
std::vector<double> temp;
int n = x.size();
for(int i = 0; i < n; ++i){
temp.clear();
for(int j = 0; j < n; ++j){
if(i == j){
temp.push_back(1.0);
} else {
temp.push_back(0.0);
}
}
I.push_back(temp);
}
for(int i = 1; i < n; ++i){
for(int j = 0; j < i; ++j){
double A = x[i][j];
double B = x[j][j];
for(int k = 0; k < n; ++k){
x[i][k] -= (A/B)*x[j][k];
I[i][k] -= (A/B)*I[j][k];
}
}
}
for(int i = 1; i < n; ++i){
for(int j = 0; j < i; ++j){
double A = x[j][i];
double B = x[i][i];
for(int k = 0; k < n; ++k){
x[j][k] -= (A/B)*x[i][k];
I[j][k] -= (A/B)*I[i][k];
}
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
I[i][j] /= x[i][i];
}
}
return I;
}
int main()
{
PyObject * ax = plt::chart2D(111);
std::vector<double> x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<double> y = {5, 6, 3, 1, 2, 9, 1, 2, 5, 7};
std::vector<std::vector<double>> X, Y;
std::vector<double> temp;
for(int i = 0; i < x.size(); ++i){
temp.clear();
for(int j = 0; j < x.size(); ++j){
temp.push_back(pow(x[i], j));
}
X.push_back(temp);
Y.push_back({y[i]});
}
std::vector<std::vector<double>> coef = MMULT(INVERSE(X), Y);
std::vector<double> Fx, Fy;
int steps = 50;
double x0 = 1;
double x1 = 10;
double dx = (x1 - x0)/((double) steps - 1);
for(int i = 0; i < steps; ++i){
double summation = 0;
double uxp = x0 + i*dx;
for(int j = 0; j < coef.size(); ++j){
summation += coef[j][0]*pow(uxp, j);
}
Fx.push_back(uxp);
Fy.push_back(summation);
}
plt::scatter2D(ax, x, y, "red");
plt::plot2D(ax, Fx, Fy, "blue");
plt::show();
return 0;
}