Skip to content

Commit ab9c905

Browse files
committed
Adds changes to C++ styling
1 parent faa2922 commit ab9c905

8 files changed

+660
-625
lines changed

src/0_classesAndMethods.cpp

+29-18
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,79 @@ using namespace Rcpp;
77
bool isProb(double prob) {
88
return (prob >= 0 && prob <= 1);
99
}
10+
1011
// doubt
1112
// [[Rcpp::export(.isGenRcpp)]]
1213
bool isGen(NumericMatrix gen) {
13-
for(int i = 0; i < gen.nrow(); i++)
14-
for(int j = 0; j < gen.ncol(); j++)
15-
if((i == j && gen(i, j) > 0) || (i != j && gen(i, j) < 0))
14+
for (int i = 0; i < gen.nrow(); i++)
15+
for (int j = 0; j < gen.ncol(); j++)
16+
if ((i == j && gen(i, j) > 0) || (i != j && gen(i, j) < 0))
1617
return false;
18+
1719
return true;
1820
}
1921

2022
SEXP commClassesKernel(NumericMatrix P);
2123

2224
// method to convert into canonic form a markovchain object
2325
// [[Rcpp::export(.canonicFormRcpp)]]
24-
SEXP canonicForm (S4 object)
25-
{
26+
SEXP canonicForm(S4 object) {
2627
NumericMatrix P = object.slot("transitionMatrix");
2728
List comclasList = commClassesKernel(P);
2829
LogicalVector vu = comclasList["v"];
2930
NumericVector u, w;
30-
for(int i = 0; i < vu.size(); i ++) {
31+
32+
for (int i = 0; i < vu.size(); i ++) {
3133
if(vu[i]) u.push_back(i);
3234
else w.push_back(i);
3335
}
3436

3537
LogicalMatrix Cmatr = comclasList["C"];
3638
NumericVector R, p;
3739
LogicalVector crow;
38-
while(u.size()>0)
39-
{
40+
41+
while (u.size() > 0) {
4042
R.push_back(u[0]);
4143
crow = Cmatr(u[0], _);
42-
for(int i = 0; i < crow.size(); i++)
44+
45+
for (int i = 0; i < crow.size(); i++)
4346
vu[i] = vu[i] * !crow[i];
47+
4448
u = NumericVector::create();
45-
for(int i = 0; i < vu.size(); i ++)
49+
50+
for (int i = 0; i < vu.size(); i ++)
4651
if(vu[i]) u.push_back(i);
4752
}
48-
for (int i = 0; i < R.size(); i ++)
49-
{
53+
54+
for (int i = 0; i < R.size(); i ++) {
5055
crow = Cmatr(R[i], _);
51-
for(int j = 0; j < crow.size(); j++)
56+
57+
for (int j = 0; j < crow.size(); j++)
5258
if(crow[j]) p.push_back(j);
5359
}
54-
for(NumericVector::iterator it = w.begin(); it != w.end(); it++)
60+
61+
for (NumericVector::iterator it = w.begin(); it != w.end(); it++)
5562
p.push_back(*it);
63+
5664
NumericMatrix Q(p.size());
5765
CharacterVector rnames(P.nrow());
5866
CharacterVector cnames(P.ncol());
5967
CharacterVector r = rownames(P);
6068
CharacterVector c = colnames(P);
61-
for(int i = 0; i < p.size(); i ++) {
69+
70+
for (int i = 0; i < p.size(); i ++) {
6271
rnames[i] = r[p[i]];
63-
for(int j = 0; j < p.size(); j ++) {
72+
for (int j = 0; j < p.size(); j ++) {
6473
Q(i, j) = P(p[i], p[j]);
6574
cnames[j] = c[p[j]];
6675
}
6776
}
77+
6878
Q.attr("dimnames") = List::create(rnames, cnames);
6979
S4 out("markovchain");
7080
out.slot("transitionMatrix") = Q;
7181
out.slot("name") = object.slot("name");
82+
7283
return out;
7384

7485
}
@@ -83,8 +94,8 @@ SEXP lexicographicalSort(SEXP y) {
8394
NumericMatrix m(y);
8495
std::vector< std::vector<double> > x(m.nrow(), std::vector<double>(m.ncol()));
8596

86-
for(int i=0; i<m.nrow(); i++)
87-
for(int j=0; j<m.ncol(); j++)
97+
for (int i=0; i<m.nrow(); i++)
98+
for (int j=0; j<m.ncol(); j++)
8899
x[i][j] = m(i,j);
89100

90101
sort(x.begin(), x.end());

src/0_ctmcClassesAndMethodsSAI.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
#include <Rcpp.h>
22
using namespace Rcpp;
3+
34
//obtain transition probability matrix from the generator matrix
45
// [[Rcpp::export]]
56
NumericMatrix generatorToTransitionMatrix(NumericMatrix gen, bool byrow = true){
67
NumericMatrix transMatr(gen.nrow());
78
transMatr.attr("dimnames") = gen.attr("dimnames");
89

9-
if(byrow)
10-
for(int i = 0; i < gen.nrow(); i++){
11-
for(int j = 0; j < gen.ncol(); j++){
12-
if(i != j)
13-
transMatr(i, j) = -gen(i, j) / gen(i, i);
10+
if (byrow) {
11+
for (int i = 0; i < gen.nrow(); i++){
12+
for (int j = 0; j < gen.ncol(); j++){
13+
if (i != j)
14+
transMatr(i, j) = -gen(i, j) / gen(i, i);
15+
}
1416
}
15-
}
16-
else
17-
for(int j = 0; j < gen.ncol(); j++){
18-
for(int i = 0; i < gen.nrow(); i++){
19-
if(i != j)
20-
transMatr(i, j) = -gen(i, j) / gen(j, j);
17+
} else {
18+
for (int j = 0; j < gen.ncol(); j++){
19+
for (int i = 0; i < gen.nrow(); i++){
20+
if (i != j)
21+
transMatr(i, j) = -gen(i, j) / gen(j, j);
22+
}
2123
}
2224
}
2325

src/1_ctmcFunctions4Fitting.cpp

+35-25
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,44 @@ using namespace Rcpp;
55

66
#include <math.h>
77

8-
List markovchainFit(SEXP data, String method="mle", bool byrow=true, int nboot=10, double laplacian=0
9-
, String name="", bool parallel=false, double confidencelevel=0.95, bool confint = true
10-
, NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false, CharacterVector possibleStates = CharacterVector());
8+
List markovchainFit(SEXP data, String method = "mle", bool byrow = true,
9+
int nboot = 10, double laplacian = 0, String name = "",
10+
bool parallel = false, double confidencelevel = 0.95, bool confint = true,
11+
NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false,
12+
CharacterVector possibleStates = CharacterVector());
1113

1214
// [[Rcpp::export]]
13-
List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel = 0.95)
14-
{
15+
List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel = 0.95) {
16+
1517
CharacterVector stateData(as<CharacterVector>(data[0]).size());
16-
for(int i = 0; i < as<CharacterVector>(data[0]).size(); i++)
18+
19+
for (int i = 0; i < as<CharacterVector>(data[0]).size(); i++)
1720
stateData[i] = as<CharacterVector>(data[0])[i];
21+
1822
NumericVector transData = data[1];
1923
CharacterVector sortedStates = unique(as<CharacterVector>(data[0])).sort();
2024
NumericVector stateCount(sortedStates.size());
2125
NumericVector stateSojournTime(sortedStates.size());
2226

2327
List dtmcData = markovchainFit(stateData, "mle", byrow, 10, 0, name, false, confidencelevel);
2428

25-
for(int i = 0; i < stateData.size() - 1; i++){
26-
int idx = std::find(sortedStates.begin(), sortedStates.end(), stateData[i]) - sortedStates.begin();
29+
for (int i = 0; i < stateData.size() - 1; i++){
30+
int idx = std::find(sortedStates.begin(),
31+
sortedStates.end(),
32+
stateData[i]) - sortedStates.begin();
2733
stateCount[idx]++;
2834
stateSojournTime[idx] += transData[i+1] - transData[i];
2935
}
3036

3137
S4 dtmcEst = dtmcData["estimate"];
3238
NumericMatrix gen = dtmcEst.slot("transitionMatrix");
3339

34-
for(int i = 0; i < gen.nrow(); i++){
35-
for(int j = 0; j < gen.ncol(); j++){
36-
if(stateCount[i] > 0)
40+
for (int i = 0; i < gen.nrow(); i++){
41+
for (int j = 0; j < gen.ncol(); j++){
42+
if (stateCount[i] > 0)
3743
gen(i, j) *= stateCount[i] / stateSojournTime[i];
3844
}
39-
if(stateCount[i] > 0)
45+
if (stateCount[i] > 0)
4046
gen(i, i) = - stateCount[i] / stateSojournTime[i];
4147
else
4248
gen(i, i) = -1;
@@ -45,12 +51,13 @@ List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel
4551
double zscore = stats::qnorm_0(confidencelevel, 1.0, 0.0);
4652
NumericVector lowerConfVecLambda(sortedStates.size()), upperConfVecLambda(sortedStates.size());
4753

48-
for(int i = 0; i < sortedStates.size(); i++){
49-
if(stateCount[i] > 0){
50-
lowerConfVecLambda(i) = std::max(0., stateCount[i] / stateSojournTime[i] * (1 - zscore / sqrt(stateCount[i])));
51-
upperConfVecLambda(i) = std::min(1., stateCount[i] / stateSojournTime[i] * (1 + zscore / sqrt(stateCount[i])));
52-
}
53-
else{
54+
for (int i = 0; i < sortedStates.size(); i++){
55+
56+
if (stateCount[i] > 0){
57+
auto factor = stateCount[i] / stateSojournTime[i] * (1 - zscore / sqrt(stateCount[i]));
58+
lowerConfVecLambda(i) = std::max(0., factor);
59+
upperConfVecLambda(i) = std::min(1., factor);
60+
} else {
5461
lowerConfVecLambda(i) = 1;
5562
upperConfVecLambda(i) = 1;
5663
}
@@ -62,10 +69,13 @@ List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel
6269
outCtmc.slot("name") = name;
6370

6471
return List::create(_["estimate"] = outCtmc,
65-
_["errors"] = List::create(_["dtmcConfidenceInterval"] = List::create(
66-
_["confidenceLevel"] = dtmcData["confidenceLevel"],
67-
_["lowerEndpointMatrix"] = dtmcData["lowerEndpointMatrix"],
68-
_["upperEndpointMatrix"] = dtmcData["upperEndpointMatrix"]),
69-
_["lambdaConfidenceInterval"] = List::create(_["lowerEndpointVector"] = lowerConfVecLambda,
70-
_["upperEndpointVector"] = upperConfVecLambda)));
71-
}
72+
_["errors"] = List::create(
73+
_["dtmcConfidenceInterval"] = List::create(
74+
_["confidenceLevel"] = dtmcData["confidenceLevel"],
75+
_["lowerEndpointMatrix"] = dtmcData["lowerEndpointMatrix"],
76+
_["upperEndpointMatrix"] = dtmcData["upperEndpointMatrix"]),
77+
_["lambdaConfidenceInterval"] = List::create(
78+
_["lowerEndpointVector"] = lowerConfVecLambda,
79+
_["upperEndpointVector"] = upperConfVecLambda))
80+
);
81+
}

0 commit comments

Comments
 (0)