comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@gibbs_sampling_inf_engine/private/get_slice_dbn.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 #include "mex.h"
2
3 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray
4 *prhs[])
5 {
6 double *pn, *pi, *pj, *pm, *y, *ecElts, *pcpt, *famElts, *strideElts,
7 *ev, *nsElts;
8 int i, k, j, m, n;
9 mxArray *ec, *cpt, *fam, *ns;
10 int c1, famSize, nsj;
11 int strideStride, startInd, stride, pos, numNodes;
12
13 const int BNET = 0;
14 const int STATE = 1;
15 const int STRIDES = 6;
16 const int FAMILIES = 7;
17 const int CPT = 8;
18
19 pn = mxGetPr(prhs[3]);
20 n = (int) pn[0];
21 pi = mxGetPr(prhs[2]);
22 i = (int) pi[0];
23 pj = mxGetPr(prhs[4]);
24 j = (int) pj[0];
25 pm = mxGetPr(prhs[5]);
26 m = (int) pm[0];
27 ev = mxGetPr(prhs[STATE]);
28 ns = mxGetField (prhs[BNET], 0, "node_sizes");
29 nsElts = mxGetPr (ns);
30 numNodes = mxGetM(ns);
31
32 strideStride = mxGetM(prhs[STRIDES]);
33 strideElts = mxGetPr(prhs[STRIDES]);
34
35
36
37 /* Treat the case n = 1 separately */
38 if (pn[0] == 1) {
39
40 /* Get the appropriate CPT */
41 ec = mxGetField (prhs[BNET], 0, "eclass1");
42 ecElts = mxGetPr(ec);
43 k = (int) ecElts[i-1];
44 cpt = mxGetCell (prhs[8], k-1);
45 pcpt = mxGetPr(cpt);
46
47 nsj = (int) nsElts[j-1];
48
49 /* Get the correct family vector */
50 /* (Note : MEX is painful) */
51 fam = mxGetCell (prhs[FAMILIES], i - 1);
52 famSize = mxGetNumberOfElements(fam);
53 famElts = mxGetPr(fam);
54
55
56 /* Figure out starting position and stride */
57 startInd = 0;
58 for (c1 = 0, pos = k-1; c1 < famSize; c1++, pos+=strideStride) {
59 if (famElts[c1] != j) {
60 startInd += strideElts[pos]*(ev[(int)famElts[c1]-1]-1);
61 }
62 else {
63 stride = strideElts[pos];
64 }
65 }
66
67 plhs[0] = mxCreateDoubleMatrix (1, nsj, mxREAL);
68 y = mxGetPr(plhs[0]);
69 for (c1 = 0, pos = startInd; c1 < nsj; c1++, pos+=stride) {
70 y[c1] = pcpt[pos];
71 }
72 }
73
74 /* Handle the case n > 1 */
75 else {
76
77 /* Get the appropriate CPT */
78 ec = mxGetField (prhs[BNET], 0, "eclass2");
79 ecElts = mxGetPr(ec);
80 k = (int) ecElts[i-1];
81 cpt = mxGetCell (prhs[8], k-1);
82 pcpt = mxGetPr(cpt);
83
84 /* Figure out size of slice */
85 if (m == 1) {
86 nsj = (int) nsElts[j-1];
87 }
88 else {
89 nsj = (int) nsElts[j-1+numNodes];
90 }
91
92 /* Figure out family */
93 fam = mxGetCell (prhs[FAMILIES], i - 1 + numNodes);
94 famSize = mxGetNumberOfElements(fam);
95 famElts = mxGetPr(fam);
96
97 startInd = 0;
98 for (c1 = 0, pos = k-1; c1 < famSize; c1++, pos+=strideStride) {
99 int f = (int) famElts[c1];
100
101 if (((f == j+numNodes) && (m == n)) || ((f == j) && (m ==
102 n-1))) {
103 stride = strideElts[pos];
104 }
105 else {
106 startInd += strideElts[pos] * (ev[f-1+((n-2)*numNodes)]-1);
107 }
108 }
109
110 plhs[0] = mxCreateDoubleMatrix(1,nsj, mxREAL);
111 y = mxGetPr(plhs[0]);
112 for (c1 = 0, pos = startInd; c1 < nsj; c1++, pos+=stride) {
113 y[c1] = pcpt[pos];
114 }
115 }
116 }