-
Notifications
You must be signed in to change notification settings - Fork 36
/
randbinom.c
219 lines (206 loc) · 5.28 KB
/
randbinom.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* compile with:
Windows: mex randbinom.c util.obj
Others: cmex randbinom.c util.o -lm
*/
#include "mexutil.h"
#include "util.h"
#define CrossLoop32(T) \
{ T *outdata = mxGetData(outArray); \
for(j=0;j<nCount;j++) { \
unsigned n32 = ((T *)nData)[j]; \
for(i=0;i<pCount;i++) { \
*outdata++ = BinoRand32(pdata[i], n32); \
} } }
#define CrossLoop64 \
for(i=0;i<pCount;i++) { \
*outdata++ = BinoRand(pdata[i], n); \
}
void RandBinomCross(mxArray *outArray, double *pdata, void *nData,
mxClassID class,
mwSize pCount, mwSize nCount)
{
mwSize i,j;
switch(class) {
case mxDOUBLE_CLASS: {
double *outdata = mxGetData(outArray);
for(j=0;j<nCount;j++) {
double nDouble = ((double*)nData)[j];
mwSize n = (mwSize)nDouble;
if((double)n != nDouble)
mexErrMsgTxt("n is not integer or out of 64-bit range");
CrossLoop64;
}
} break;
case mxSINGLE_CLASS: {
float *outdata = mxGetData(outArray);
for(j=0;j<nCount;j++) {
float nSingle = ((float*)nData)[j];
mwSize n = (mwSize)nSingle;
if((float)n != nSingle)
mexErrMsgTxt("n is not integer or out of 64-bit range");
CrossLoop64;
}
} break;
case mxUINT64_CLASS: {
uint64_T *outdata = mxGetData(outArray);
for(j=0;j<nCount;j++) {
mwSize n = ((uint64_T*)nData)[j];
CrossLoop64;
}
} break;
case mxINT64_CLASS: {
int64_T *outdata = mxGetData(outArray);
for(j=0;j<nCount;j++) {
mwSize n = ((int64_T*)nData)[j];
CrossLoop64;
}
} break;
case mxUINT8_CLASS:
CrossLoop32(uint8_T);
break;
case mxUINT16_CLASS:
CrossLoop32(uint16_T);
break;
case mxUINT32_CLASS:
CrossLoop32(uint32_T);
break;
case mxINT8_CLASS:
CrossLoop32(int8_T);
break;
case mxINT16_CLASS:
CrossLoop32(int16_T);
break;
case mxINT32_CLASS:
CrossLoop32(int32_T);
break;
default:
mexErrMsgTxt("Second argument is not a supported type");
}
}
#define Loop32(T) \
{ T *outdata = mxGetData(outArray); \
for(i=0;i<nCount;i++) { \
unsigned n32 = ((T *)nData)[i]; \
outdata[i] = BinoRand32(pdata[i], n32); \
} }
/* pdata and nArray must be the same size
*/
void RandBinom(mxArray *outArray, double *pdata, void *nData,
mxClassID class,
mwSize nCount)
{
mwSize i;
switch(class) {
case mxDOUBLE_CLASS: {
double *outdata = mxGetData(outArray);
for(i=0;i<nCount;i++) {
double nDouble = ((double*)nData)[i];
mwSize n = (mwSize)nDouble;
if((double)n != nDouble)
mexErrMsgTxt("n is not integer or out of 64-bit range");
outdata[i] = BinoRand(pdata[i], n);
}
} break;
case mxSINGLE_CLASS: {
float *outdata = mxGetData(outArray);
for(i=0;i<nCount;i++) {
float nSingle = ((float*)nData)[i];
mwSize n = (mwSize)nSingle;
if((float)n != nSingle)
mexErrMsgTxt("n is not integer or out of 64-bit range");
outdata[i] = BinoRand(pdata[i], n);
}
} break;
case mxUINT64_CLASS: {
uint64_T *outdata = mxGetData(outArray);
for(i=0;i<nCount;i++) {
mwSize n = ((uint64_T*)nData)[i];
outdata[i] = BinoRand(pdata[i], n);
}
} break;
case mxINT64_CLASS: {
int64_T *outdata = mxGetData(outArray);
for(i=0;i<nCount;i++) {
mwSize n = ((int64_T*)nData)[i];
outdata[i] = BinoRand(pdata[i], n);
}
} break;
case mxUINT8_CLASS:
Loop32(uint8_T);
break;
case mxUINT16_CLASS:
Loop32(uint16_T);
break;
case mxUINT32_CLASS:
Loop32(uint32_T);
break;
case mxINT8_CLASS:
Loop32(int8_T);
break;
case mxINT16_CLASS:
Loop32(int16_T);
break;
case mxINT32_CLASS:
Loop32(int32_T);
break;
default:
mexErrMsgTxt("Second argument is not a supported type");
}
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mwSize ndims, *dims, pCount, nCount, i;
mwSize ndims2, *dims2, noutdims, *outdims;
double *pData;
void *nData;
const mxArray *nArray, *pArray;
mxClassID nClass;
if(nrhs != 2)
mexErrMsgTxt("Usage: r = bino_sample(p, n)");
/* prhs[0] is first argument.
* mxGetPr returns double* (data, col-major)
*/
pArray = prhs[0];
ndims = mxGetNumberOfDimensions(pArray);
dims = (mwSize*)mxGetDimensions(pArray);
pCount = mxGetNumberOfElements(pArray);
pData = mxGetPr(pArray);
nArray = prhs[1];
ndims2 = mxGetNumberOfDimensions(nArray);
dims2 = (mwSize*)mxGetDimensions(nArray);
nCount = mxGetNumberOfElements(nArray);
nData = mxGetData(nArray);
/* plhs[0] is first output */
if(nCount == 1) {
noutdims = ndims;
outdims = dims;
} else {
noutdims = ndims2;
outdims = dims2;
if(pCount > 1) {
/* check that dimensions match */
for(i=0;i<ndims;i++) {
if(i>=ndims2) {
/* dims2[i] == 1 */
if(dims[i] != 1)
mexErrMsgTxt("P and N have mismatched array dimensions");
} else if(dims[i] != dims2[i]) {
mexErrMsgTxt("P and N have mismatched array dimensions");
}
}
for(;i<ndims2;i++) {
if(dims2[i] != 1)
mexErrMsgTxt("P and N have mismatched array dimensions");
}
}
}
nClass = mxGetClassID(nArray);
plhs[0] = mxCreateNumericArrayE(noutdims, outdims, nClass, mxREAL);
if(mxIsSparse(prhs[0]) || mxIsSparse(nArray))
mexErrMsgTxt("Cannot handle sparse matrices. Sorry.");
if(nCount == 1 || pCount == 1)
RandBinomCross(plhs[0], pData, nData, nClass, pCount, nCount);
else
RandBinom(plhs[0], pData, nData, nClass, pCount);
}