1
所以我在使用RCPP創建的函數返回值時遇到了一些問題。它只返回NumericVector的第一個值。問題是當我在它自己內部調用函數並將NumericVector傳遞迴out變量時。任何關於我所做的不正確的輸入將不勝感激。Rcpp數字矢量輸出只返回一個值
NumericVector advantage(double tRos, double tTat, double cRos, double cTat){
double tRosAd = 0;
double tTatAd = 0;
double ja = 0;
double rosAtt = 0;
double tatAtt = 0;
double ttlAd = 0;
NumericVector out(1);
if ((tRos >= 0 || cRos >=0) && cRos >= tRos && cTat <= tTat){
//case 1.1//
tRosAd = (cRos - tRos) * cTat * -1;
tTatAd = (tTat - cTat) * tRos;
out[0] = tRosAd;
out[1] = tTatAd;
}
else if((tRos >= 0 && cRos >= 0) && cRos <= tRos && cTat >= tTat){
//case 1.2//
out = advantage(cRos, cTat, tRos, tTat);
out[0] = out[0] * -1;
out[1] = out[1] * -1;
}
else if(tRos >= 0 && cRos <= 0 && tTat >= cTat){
//case 2.1//
tRosAd = (tRos - cRos) * cTat;
tTatAd = (tTat - cTat) * tRos;
out[0] = tRosAd;
out[1] = tTatAd;
}
else if(tRos <= 0 && cRos >= 0 && tTat <= cTat){
//case 2.2//
out = advantage(cRos, cTat, tRos, tTat);
out[0] = out[0] * -1;
out[1] = out[1] * -1;
}
else if(tRos <= 0 && cRos <= 0 && tTat >= cTat & tRos >= cRos){
//case 3.1//
tRosAd = (tRos - cRos) * cTat;
tTatAd = (tTat - cTat) * tRos;
out[0] = tRosAd;
out[1] = tTatAd;
}
else if(tRos <= 0 && cRos <= 0 && tTat <= cTat && tRos <= cRos){
//case 3.2//
out = advantage(cRos, cTat, tRos, tTat);
out[0] = out[0] * -1;
out[1] = out[1] * -1;
}
else if(tRos >= 0 && cRos >= 0 && tTat >= cTat && tRos >= cRos){
//case 4.1//
//calculate two attribution advantages//
tRosAd = (tRos - cRos) * cTat;
tTatAd = (tTat - cTat) * cRos;
//calculate total & joint advantage//
ttlAd = (tTat * tRos) - (cTat * cRos);
ja = ttlAd - (tRosAd + tTatAd);
rosAtt = tRosAd/(tRosAd/tTatAd);
tatAtt = tTatAd/(tRosAd/tTatAd);
//add joint advantage//
tRosAd = ja * rosAtt + tRosAd;
tTatAd = ja * tatAtt + tTatAd;
out[0] = tRosAd;
out[1] = tTatAd;
}
else if(tRos >= 0 && cRos >= 0 && tTat <= cTat && tRos <= cRos){
//case 4.2//
out = advantage(cRos, cTat, tRos, tTat);
out[0] = out[0] * -1;
out[1] = out[1] * -1;
}
else if(tRos <= 0 && cRos <= 0 && tTat <= cTat && tRos >= cRos){
//case 5.1//
tRosAd = (tRos - cRos) * tTat;
tTatAd = (cTat - tTat) * tRos * -1;
//calculate total and joint advantage//
ttlAd = (tTat * tRos) - (cTat * cRos);
ja = ttlAd - (tRosAd + tTatAd);
rosAtt = tRosAd/(tRosAd + tTatAd);
tatAtt = tTatAd/(tRosAd + tTatAd);
tRosAd = ja * rosAtt + tRosAd;
tTatAd = ja * tatAtt + tTatAd;
out[0] = tRosAd;
out[1] = tTatAd;
}
else if(tRos <= 0 && cRos <= 0 && tTat >= cTat && tRos <= cRos){
//case 5.2//
out = advantage(cRos, cTat, tRos, tTat);
out[0] = out[0] * -1;
out[1] = out[1] * -1;
}
return out;
}
'NumericVector out(1)'創建一個長度爲1的向量... – hadley