-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHyperplane.cpp
More file actions
122 lines (94 loc) · 3.27 KB
/
Hyperplane.cpp
File metadata and controls
122 lines (94 loc) · 3.27 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
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
/**
* @file Hyperplane.cpp
*
* @author btran
*
* @date 2019-08-07
*
* Copyright (c) organization
*
*/
#include <cmath>
#include <iostream>
#include <vector>
#include <matplotlib_cpp/MatplotlibCpp.hpp>
std::vector<double> linspace(const double min, const double max, const size_t numPoint = 20);
template <typename DATA_TYPE>
std::array<std::vector<std::vector<DATA_TYPE>>, 2> meshgrid(const std::vector<DATA_TYPE>& xs,
const std::vector<DATA_TYPE>& ys);
int main(int argc, char* argv[])
{
// import modules of matplotlib library
pe::vis::Matplotlib mpllib;
// check if the modules are imported successully or not
if (!mpllib.imported()) {
std::cout << "Failed to import matplotlib library\n";
exit(EXIT_FAILURE);
}
// plot R(3) hyperplane (2x + 3y + 2.5z - 2 = 0)
// normal p = (2,3,2.5)
const double min = 0.0;
const double max = 10.0;
const size_t num = 50;
std::vector<double> xs = linspace(-10.0, 10.0, 50);
std::vector<double> ys = linspace(-10.0, 10.0, 50);
const std::array<double, 3> normal = {2, 3, 2.5};
auto gridXY = meshgrid(xs, ys);
std::vector<std::vector<double>> Z;
Z.reserve(gridXY[0].size());
for (size_t ih = 0; ih < gridXY[0].size(); ++ih) {
std::vector<double> curV;
curV.reserve(gridXY[0][0].size());
for (size_t iw = 0; iw < gridXY[0][0].size(); ++iw) {
curV.emplace_back((2 - 2 * gridXY[0][ih][iw] - 3 * gridXY[1][ih][iw]) / 2.5);
}
Z.emplace_back(curV);
}
// half space 2x + 3y + 2.5z - 2 >= 0
// point(1,2,z_axis)
// choose z_axis = (2 - (2 * x_axis + 3 * y_axis) / 2.5) + epsilon
const double x = 1;
const double y = 2;
const double epsilon = 7.9;
const double z = (2 - (2 * x + 3 * y)) / 2.5 + epsilon;
mpllib.initializeAxes3D();
mpllib.plot_surface(gridXY[0], gridXY[1], Z,
{{"cmap", pe::vis::Matplotlib::createAnyBaseMapData<std::string>("plasma")},
{"linewidth", pe::vis::Matplotlib::createAnyBaseMapData<double>(0)}});
mpllib.scatterAxes3D<double>({x}, {y}, {z});
mpllib.savefig("Hyperplane.png");
mpllib.show();
return EXIT_SUCCESS;
}
std::vector<double> linspace(const double min, const double max, const size_t num)
{
std::vector<double> result;
result.reserve(num);
result.emplace_back(min);
if (num < 2) {
throw std::runtime_error("num should be bigger than 2");
}
double delta = (max - min) / (num - 1);
double curV = min;
for (size_t i = 0; i < num - 2; ++i) {
curV += delta;
result.emplace_back(curV);
}
result.emplace_back(max);
return result;
}
template <typename DATA_TYPE>
std::array<std::vector<std::vector<DATA_TYPE>>, 2> meshgrid(const std::vector<DATA_TYPE>& xs,
const std::vector<DATA_TYPE>& ys)
{
std::vector<std::vector<DATA_TYPE>> X, Y;
X.reserve(ys.size());
Y.reserve(ys.size());
for (size_t i = 0; i < ys.size(); ++i) {
X.emplace_back(xs);
std::vector<DATA_TYPE> curV(xs.size());
std::fill(curV.begin(), curV.end(), ys[i]);
Y.emplace_back(curV);
}
return {X, Y};
}