Go to the documentation of this file.00001
00011 #include <R.h>
00012 #include <Rmath.h>
00013 #include <Rdefines.h>
00014
00039 SEXP R_cpermdist2(SEXP score_a, SEXP score_b, SEXP m_a, SEXP m_b,
00040 SEXP retProb) {
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 int n, im_a, im_b;
00051
00052 SEXP H, x;
00053
00054
00055 int i, j, k, sum_a = 0, sum_b = 0, s_a = 0, s_b = 0, isb;
00056 double msum = 0.0;
00057
00058 int *iscore_a, *iscore_b;
00059 double *dH, *dx;
00060
00061
00062
00063 if (!isVector(score_a))
00064 error("score_a is not a vector");
00065
00066 n = LENGTH(score_a);
00067
00068 if (!isVector(score_b))
00069 error("score_b is not a vector");
00070
00071 if (LENGTH(score_b) != n)
00072 error("length of score_a and score_b differ");
00073
00074 iscore_a = INTEGER(score_a);
00075 iscore_b = INTEGER(score_b);
00076
00077 if (TYPEOF(retProb) != LGLSXP)
00078 error("retProb is not a logical");
00079
00080 im_a = INTEGER(m_a)[0];
00081 im_b = INTEGER(m_b)[0];
00082
00083
00084
00085 for (i = 0; i < n; i++) {
00086 if (iscore_a[i] < 0)
00087 error("score_a for observation number %d is negative", i);
00088 if (iscore_b[i] < 0)
00089 error("score_b for observation number %d is negative", i);
00090 sum_a += iscore_a[i];
00091 sum_b += iscore_b[i];
00092 }
00093
00094
00095
00096
00097
00098 sum_a = imin2(sum_a, im_a);
00099 sum_b = imin2(sum_b, im_b);
00100
00101
00102
00103
00104
00105 PROTECT(H = allocVector(REALSXP, (sum_a + 1) * (sum_b + 1)));
00106 dH = REAL(H);
00107
00108 for (i = 0; i <= sum_a; i++) {
00109 isb = i * (sum_b + 1);
00110 for (j = 0; j <= sum_b; j++) dH[isb + j] = 0.0;
00111 }
00112
00113
00114
00115
00116
00117 dH[0] = 1.0;
00118
00119 for (k = 0; k < n; k++) {
00120 s_a += iscore_a[k];
00121 s_b += iscore_b[k];
00122
00123
00124
00125
00126
00127
00128
00129
00130 for (i = imin2(im_a, s_a); i >= iscore_a[k]; i--) {
00131 isb = i * (sum_b + 1);
00132 for (j = imin2(im_b,s_b); j >= iscore_b[k]; j--)
00133 dH[isb + j] +=
00134 dH[(i - iscore_a[k]) * (sum_b + 1) + (j - iscore_b[k])];
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143 if (!LOGICAL(retProb)[0]) {
00144 UNPROTECT(1);
00145 return(H);
00146 } else {
00147 PROTECT(x = allocVector(REALSXP, sum_b));
00148 dx = REAL(x);
00149
00150
00151
00152
00153
00154 isb = im_a * (sum_b + 1);
00155 for (j = 0; j < sum_b; j++) {
00156 if (!R_FINITE(dH[isb + j + 1]))
00157 error("overflow error; cannot compute exact distribution");
00158 dx[j] = dH[isb + j + 1];
00159 msum += dx[j];
00160 }
00161 if (!R_FINITE(msum) || msum == 0.0)
00162 error("overflow error; cannot compute exact distribution");
00163
00164
00165
00166
00167
00168
00169
00170 for (j = 0; j < sum_b; j++)
00171 dx[j] = dx[j]/msum;
00172
00173 UNPROTECT(2);
00174 return(x);
00175 }
00176 }
00177
00198 SEXP R_cpermdist1(SEXP scores) {
00199
00200
00201
00202
00203
00204
00205 int n;
00206 SEXP H;
00207
00208 int i, k, sum_a = 0, s_a = 0;
00209 int *iscores;
00210 double msum = 0.0;
00211 double *dH;
00212
00213 n = LENGTH(scores);
00214 iscores = INTEGER(scores);
00215
00216 for (i = 0; i < n; i++) sum_a += iscores[i];
00217
00218
00219
00220
00221
00222 PROTECT(H = allocVector(REALSXP, sum_a + 1));
00223 dH = REAL(H);
00224 for (i = 0; i <= sum_a; i++) dH[i] = 0.0;
00225
00226
00227
00228
00229
00230 dH[0] = 1.0;
00231
00232 for (k = 0; k < n; k++) {
00233 s_a = s_a + iscores[k];
00234 for (i = s_a; i >= iscores[k]; i--)
00235 dH[i] = dH[i] + dH[i - iscores[k]];
00236 }
00237
00238
00239
00240
00241
00242
00243 for (i = 0; i <= sum_a; i++) {
00244 if (!R_FINITE(dH[i]))
00245 error("overflow error: cannot compute exact distribution");
00246 msum += dH[i];
00247 }
00248 if (!R_FINITE(msum) || msum == 0.0)
00249 error("overflow error: cannot compute exact distribution");
00250
00251
00252
00253
00254
00255
00256 for (i = 0; i <= sum_a; i++)
00257 dH[i] = dH[i]/msum;
00258
00259 UNPROTECT(1);
00260 return(H);
00261 }