forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjd.cpp
More file actions
68 lines (59 loc) · 1.75 KB
/
jd.cpp
File metadata and controls
68 lines (59 loc) · 1.75 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
59
60
61
62
63
64
65
66
67
68
#define USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <math.h>
#include <cmath>
#include <Python.h>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
double JointDist(double x, double y, double rho){
double factor = 1.0/(2.0*M_PI*sqrt(1 - pow(rho, 2)));
double exponent = (pow(x, 2) - 2*x*y*rho + pow(y, 2))/(2.0*(1 - pow(rho, 2)));
return factor*exp(-exponent);
}
std::map<std::string, std::vector<std::vector<double>>> Graph(double rho){
std::map<std::string, std::vector<std::vector<double>>> result;
std::vector<double> tx, ty, tz;
double a0 = -3.5, a1 = 3.5;
double an = 60;
double da = (a1 - a0)/(an - 1);
for(int i = 0; i < an; ++i){
tx.clear();
ty.clear();
tz.clear();
double xi = a0 + i*da;
for(int j = 0; j < an; ++j){
double yi = a0 + j*da;
tx.push_back(xi);
ty.push_back(yi);
tz.push_back(JointDist(xi, yi, rho));
}
result["x"].push_back(tx);
result["y"].push_back(ty);
result["z"].push_back(tz);
}
return result;
}
int main()
{
std::map<std::string, std::vector<std::vector<double>>> graph;
PyObject * ax = plt::chart(121);
PyObject * ay = plt::chart2D(122);
double r0 = -0.99, r1 = 0.99;
int rn = 150;
double dr = (r1 - r0)/(rn - 1);
plt::pause(4);
for(int i = 0; i < rn; ++i){
double rho = r0 + i*dr;
plt::Clear3DChart(ax);
plt::Clear3DChart(ay);
graph = Graph(rho);
plt::surface3DMap(ax, graph["x"], graph["y"], graph["z"], "hsv", 1.0);
plt::contour3D(ay, graph["x"], graph["y"], graph["z"], "jet");
plt::pause(0.001);
}
plt::show();
return 0;
}