forked from neurodata/SPORF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
121 lines (115 loc) · 2.6 KB
/
split.cpp
File metadata and controls
121 lines (115 loc) · 2.6 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
# include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
List findSplit(const NumericVector x, const IntegerVector y, const int & ndSize, const double & I,
double maxdI, int bv, double bs, const int nzidx, arma::vec cc) {
double xl, xr, dI, epsilon;
int yl, yr, cons, bsidx, potsplit;
bool multiy;
arma::vec ccl(cc.n_elem, arma::fill::zeros);
arma::vec ccr = cc;
arma::vec cpl, cpr, potccl, potccr;
bsidx = 0;
cons = 0;
xl = x[0];
yl = y[0] - 1;
potsplit = 0;
multiy = false;
epsilon = std::numeric_limits<double>::epsilon() *10;
// iterate over split locations from left to right
for (int i = 0; i < ndSize - 1; ++i) {
xr = x[i+1];
yr = y[i+1] - 1;
if (std::abs(xl-xr) < epsilon) { //is xl == xr
cons += 1;
if (yl == yr) {
continue;
} else {
if (~multiy) {
multiy = true;
if (potsplit != 0) {
cpl = potccl/potsplit;
cpr = potccr/(ndSize - potsplit);
dI = I - dot(ccl,(1 - cpl)) - dot(ccr,(1 - cpr));
if (dI > maxdI) {
// save current best split information
maxdI = dI;
bsidx = potsplit;
bv = nzidx;
}
potsplit = 0;
}
}
}
ccl[yl] += cons;
ccr[yl] -= cons;
cons = 0;
yl = yr;
} else if ((xl + xr)/2 == xr) {
cons += 1;
if (yl == yr) {
continue;
} else {
if (~multiy) {
multiy = true;
if (potsplit != 0) {
cpl = potccl/potsplit;
cpr = potccr/(ndSize - potsplit);
dI = I - dot(ccl,(1 - cpl)) - dot(ccr,(1 - cpr));
if (dI > maxdI) {
// save current best split information
maxdI = dI;
bsidx = potsplit;
bv = nzidx;
}
potsplit = 0;
}
}
}
ccl[yl] += cons;
ccr[yl] -= cons;
cons = 0;
xl = xr;
yl = yr;
} else {
cons += 1;
ccl[yl] += cons;
ccr[yl] -= cons;
cons = 0;
if (yl == yr) {
if (multiy) {
cpl = ccl/(i + 1);
cpr = ccr/(ndSize - (i + 1));
dI = I - dot(ccl,(1 - cpl)) - dot(ccr,(1 - cpr));
if (dI > maxdI) {
// save current best split information
maxdI = dI;
bsidx = i + 1;
bv = nzidx;
}
} else {
potsplit = i + 1;
potccl = ccl;
potccr = ccr;
}
} else {
cpl = ccl/(i + 1);
cpr = ccr/(ndSize - (i + 1));
dI = I - dot(ccl,(1 - cpl)) - dot(ccr,(1 - cpr));
if (dI > maxdI) {
// save current best split information
maxdI = dI;
bsidx = i + 1;
bv = nzidx;
}
yl = yr;
}
multiy = false;
xl = xr;
}
}
if (bsidx != 0) {
bs = (x[bsidx - 1] + x[bsidx])/2;
}
return List::create(_["MaxDeltaI"] = maxdI, _["BestVar"] = bv, _["BestSplit"] = bs);
}