1 | /* compile with: cmex gammaln.c mexutil.c util.c -lm |
---|
2 | * test in matlab: |
---|
3 | * gammaln(1:10) |
---|
4 | */ |
---|
5 | /* Written by Tom Minka |
---|
6 | * (c) Microsoft Corporation. All rights reserved. |
---|
7 | */ |
---|
8 | #include "mex.h" |
---|
9 | #include "util.h" |
---|
10 | |
---|
11 | void mexFunction(int nlhs, mxArray *plhs[], |
---|
12 | int nrhs, const mxArray *prhs[]) |
---|
13 | { |
---|
14 | int ndims, len, i; |
---|
15 | int *dims; |
---|
16 | double *indata, *outdata, d; |
---|
17 | |
---|
18 | if((nlhs > 1) || (nrhs < 1) || (nrhs > 2)) |
---|
19 | mexErrMsgTxt("Usage: x = gammaln(n) or gammaln(n,d)"); |
---|
20 | |
---|
21 | /* prhs[0] is first argument. |
---|
22 | * mxGetPr returns double* (data, col-major) |
---|
23 | */ |
---|
24 | ndims = mxGetNumberOfDimensions(prhs[0]); |
---|
25 | dims = (int*)mxGetDimensions(prhs[0]); |
---|
26 | indata = mxGetPr(prhs[0]); |
---|
27 | len = mxGetNumberOfElements(prhs[0]); |
---|
28 | |
---|
29 | if(mxIsSparse(prhs[0])) |
---|
30 | mexErrMsgTxt("Cannot handle sparse matrices. Sorry."); |
---|
31 | |
---|
32 | /* plhs[0] is first output */ |
---|
33 | plhs[0] = mxCreateNumericArray(ndims, dims, mxDOUBLE_CLASS, mxREAL); |
---|
34 | outdata = mxGetPr(plhs[0]); |
---|
35 | |
---|
36 | /* compute gammaln of every element */ |
---|
37 | if(nrhs == 1) { |
---|
38 | for(i=0;i<len;i++) |
---|
39 | *outdata++ = gammaln(*indata++); |
---|
40 | } else { |
---|
41 | d = *mxGetPr(prhs[1]); |
---|
42 | for(i=0;i<len;i++) |
---|
43 | *outdata++ = gammaln2(*indata++,d); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|