-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.h
166 lines (150 loc) · 4.58 KB
/
point.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
// A Header that is used to generate and operate n-d points
#ifndef POINT_HEADER
#define POINT_HEADER
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
using namespace std;
struct Point {
Point(float Xdata[], float Ydata, int Dimention, int DataSize) :y(Ydata) {
x = {};
// We'd better allocate the space for the vector
// prior to pushing back the element
x.reserve(DataSize);
for ( int a = 0; a < Dimention; a += 1 ) {
x.push_back(Xdata[a]);
}
}
Point(vector<float> Xdata, float Ydata) : x(Xdata), y(Ydata) {};
// Each point has a series of x and a output y
vector<float> x;
float y;
};
class Point_Utility {
public:
static void PrintPoint(Point& P) {
cout << "x: ( ";
for ( auto i = P.x.begin(); i != P.x.end(); i++ ) {
cout << *i << " , ";
}
cout << " ), y: " << P.y << endl;
}
static void PrintVPoint(vector<Point>& VP) {
for ( auto i = VP.begin(); i != VP.end(); i++ ) {
PrintPoint(*i);
}
cout << endl;
}
static void PrintVVPoint(vector<vector<Point>>& VVP) {
for ( auto i = VVP.begin(); i != VVP.end(); i++ ) {
PrintVPoint(*i);
}
cout << endl;
}
static void Sort(vector<Point>& VP, int Dimention) {
int sz = VP.size();
for ( int a = 0; a < sz; a += 1 ) {
for ( int b = a + 1; b < sz; b += 1 ) {
if ( VP[a].x[Dimention] > VP[b].x[Dimention] ) {
SwapPoints(VP, a, b);
}
}
}
}
// Quick sort for points according to a specific dimention (Priority)
static void QuickPSort(vector<Point>& VP, int dimention, int bottom, int top) {
if ( bottom < top ) {
int split_point = Split(VP, dimention, bottom, top);
QuickPSort(VP, dimention, bottom, split_point - 1);
QuickPSort(VP, dimention, split_point + 1, top);
}
};
// Assign a series of value to a specific demention
static void Assign(vector<Point>& VP, vector<float>& Data, int Dimention) {
int a = 0;
if ( Dimention >= VP[0].x.size() ) {
for ( auto i = VP.begin(); i != VP.end(); i++ ) {
i->y = Data[a];
a++;
}
}
else {
for ( auto i = VP.begin(); i != VP.end(); i++ ) {
i->x[Dimention] = Data[a];
a++;
}
}
}
static void BulkInit(vector<Point>& VP, int Dimention, int size) {
vector<float> zeros;
zeros.reserve(Dimention);
for ( int p = 0; p < Dimention; p += 1 ) {
zeros.push_back(0.0);
}
for ( int x = 0; x < size; x += 1 ) {
VP.push_back(Point(zeros, 0));
}
}
protected:
inline static void SwapPoints(vector<Point>& VP, int target_idx_A, int target_idx_B) {
Point p = VP[target_idx_A];
VP[target_idx_A] = VP[target_idx_B];
VP[target_idx_B] = p;
};
inline static int Split(vector<Point>& VP, int Dimention, int bottom, int upper) {
int lo_bottom = bottom + 1; int lo_top = upper;
Point pivot_point = VP[bottom];
float pivot_number = pivot_point.x[Dimention];
while ( true ) {
//find the bottom position, which bottom number>pivot number
while ( VP[lo_bottom].x[Dimention] <= pivot_number && lo_bottom < lo_top ) { lo_bottom = lo_bottom + 1; }
while ( VP[lo_top].x[Dimention] > pivot_number && lo_bottom < lo_top ) { lo_top = lo_top - 1; }
if ( lo_bottom != lo_top ) { SwapPoints(VP, lo_bottom, lo_top); }
if ( lo_bottom >= lo_top ) { break; }
}
if ( pivot_number < VP[lo_bottom].x[Dimention] ) {
VP[bottom] = VP[lo_top - 1];
VP[lo_top - 1] = pivot_point;
return lo_top - 1;
}
else {
VP[bottom] = VP[lo_top];
VP[lo_top] = pivot_point;
return lo_top;
}
};
};
class Random_Utility {
public:
Random_Utility() {};
~Random_Utility() {};
// mean: mean
// sigma: Standard deviation
static vector<float> Sequence(int lower, int upper, float mean, float sigma, int step = 1) {
default_random_engine engine{ static_cast<unsigned int>(rand() % 1000) };
normal_distribution<float> Noise(mean, sigma);
vector<float> temp;
temp.reserve(upper - lower);
for ( int a = lower; a < upper; a += 1 ) {
temp.push_back(a*step + Noise(engine));
}
return temp;
}
// For a given x data, calculate the corresponding y data
static void Quadratic(vector<Point>& VP, float mean, float sigma, float scaler = 1.0f, float bias = 0.0f) {
default_random_engine engine{ static_cast<unsigned int>(rand() % 1000) };
normal_distribution<float> Noise(mean, sigma);
for ( auto i = VP.begin(); i != VP.end(); i++ ) {
float sum = 0;
for ( auto n = i->x.begin(); n != i->x.end(); n++ ) {
sum += scaler * pow((*n), 2) + bias;
}
sum += Noise(engine);
i->y = sum;
}
}
private:
};
#endif //