annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@discrete_CPD/convert_to_sparse_table.c @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 /* convert_to_sparse_table.c convert a sparse discrete CPD with evidence into sparse table */
Daniel@0 2 /* convert_to_pot.m located in ../CPDs/discrete_CPD call it */
Daniel@0 3 /* 3 input */
Daniel@0 4 /* CPD prhs[0] with 1D sparse CPT */
Daniel@0 5 /* domain prhs[1] */
Daniel@0 6 /* evidence prhs[2] */
Daniel@0 7 /* 1 output */
Daniel@0 8 /* T plhs[0] sparse table */
Daniel@0 9
Daniel@0 10 #include <math.h>
Daniel@0 11 #include "mex.h"
Daniel@0 12
Daniel@0 13 void ind_subv(int index, const int *cumprod, const int n, int *bsubv){
Daniel@0 14 int i;
Daniel@0 15
Daniel@0 16 for (i = n-1; i >= 0; i--) {
Daniel@0 17 bsubv[i] = ((int)floor(index / cumprod[i]));
Daniel@0 18 index = index % cumprod[i];
Daniel@0 19 }
Daniel@0 20 }
Daniel@0 21
Daniel@0 22 int subv_ind(const int n, const int *cumprod, const int *subv){
Daniel@0 23 int i, index=0;
Daniel@0 24
Daniel@0 25 for(i=0; i<n; i++){
Daniel@0 26 index += subv[i] * cumprod[i];
Daniel@0 27 }
Daniel@0 28 return index;
Daniel@0 29 }
Daniel@0 30
Daniel@0 31 void reset_nzmax(mxArray *spArray, const int old_nzmax, const int new_nzmax){
Daniel@0 32 double *ptr;
Daniel@0 33 void *newptr;
Daniel@0 34 int *ir, *jc;
Daniel@0 35 int nbytes;
Daniel@0 36
Daniel@0 37 if(new_nzmax == old_nzmax) return;
Daniel@0 38 nbytes = new_nzmax * sizeof(*ptr);
Daniel@0 39 ptr = mxGetPr(spArray);
Daniel@0 40 newptr = mxRealloc(ptr, nbytes);
Daniel@0 41 mxSetPr(spArray, newptr);
Daniel@0 42 nbytes = new_nzmax * sizeof(*ir);
Daniel@0 43 ir = mxGetIr(spArray);
Daniel@0 44 newptr = mxRealloc(ir, nbytes);
Daniel@0 45 mxSetIr(spArray, newptr);
Daniel@0 46 jc = mxGetJc(spArray);
Daniel@0 47 jc[0] = 0;
Daniel@0 48 jc[1] = new_nzmax;
Daniel@0 49 mxSetNzmax(spArray, new_nzmax);
Daniel@0 50 }
Daniel@0 51
Daniel@0 52
Daniel@0 53 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
Daniel@0 54 int i, j, NS, NZB, count, bdim, match, domain, bindex, sindex, nzCounts=0;
Daniel@0 55 int *observed, *bsubv, *ssubv, *bir, *sir, *bjc, *sjc, *mask, *ssize, *bcumprod, *scumprod;
Daniel@0 56 double *pDomain, *pSize, *bpr, *spr;
Daniel@0 57 mxArray *pTemp;
Daniel@0 58
Daniel@0 59 pTemp = mxGetField(prhs[0], 0, "CPT");
Daniel@0 60 bpr = mxGetPr(pTemp);
Daniel@0 61 bir = mxGetIr(pTemp);
Daniel@0 62 bjc = mxGetJc(pTemp);
Daniel@0 63 NZB = bjc[1];
Daniel@0 64 pTemp = mxGetField(prhs[0], 0, "sizes");
Daniel@0 65 pSize = mxGetPr(pTemp);
Daniel@0 66
Daniel@0 67 pDomain = mxGetPr(prhs[1]);
Daniel@0 68 bdim = mxGetNumberOfElements(prhs[1]);
Daniel@0 69
Daniel@0 70 mask = malloc(bdim * sizeof(int));
Daniel@0 71 ssize = malloc(bdim * sizeof(int));
Daniel@0 72 observed = malloc(bdim * sizeof(int));
Daniel@0 73
Daniel@0 74 for(i=0; i<bdim; i++){
Daniel@0 75 ssize[i] = (int)pSize[i];
Daniel@0 76 }
Daniel@0 77
Daniel@0 78 count = 0;
Daniel@0 79 for(i=0; i<bdim; i++){
Daniel@0 80 domain = (int)pDomain[i] - 1;
Daniel@0 81 pTemp = mxGetCell(prhs[2], domain);
Daniel@0 82 if(pTemp){
Daniel@0 83 mask[count] = i;
Daniel@0 84 ssize[i] = 1;
Daniel@0 85 observed[count] = (int)mxGetScalar(pTemp) - 1;
Daniel@0 86 count++;
Daniel@0 87 }
Daniel@0 88 }
Daniel@0 89
Daniel@0 90 if(count == 0){
Daniel@0 91 pTemp = mxGetField(prhs[0], 0, "CPT");
Daniel@0 92 plhs[0] = mxDuplicateArray(pTemp);
Daniel@0 93 free(mask);
Daniel@0 94 free(ssize);
Daniel@0 95 free(observed);
Daniel@0 96 return;
Daniel@0 97 }
Daniel@0 98
Daniel@0 99 bsubv = malloc(bdim * sizeof(int));
Daniel@0 100 ssubv = malloc(count * sizeof(int));
Daniel@0 101 bcumprod = malloc(bdim * sizeof(int));
Daniel@0 102 scumprod = malloc(bdim * sizeof(int));
Daniel@0 103
Daniel@0 104 NS = 1;
Daniel@0 105 for(i=0; i<bdim; i++){
Daniel@0 106 NS *= ssize[i];
Daniel@0 107 }
Daniel@0 108
Daniel@0 109 plhs[0] = mxCreateSparse(NS, 1, NS, mxREAL);
Daniel@0 110 spr = mxGetPr(plhs[0]);
Daniel@0 111 sir = mxGetIr(plhs[0]);
Daniel@0 112 sjc = mxGetJc(plhs[0]);
Daniel@0 113 sjc[0] = 0;
Daniel@0 114 sjc[1] = NS;
Daniel@0 115
Daniel@0 116 bcumprod[0] = 1;
Daniel@0 117 scumprod[0] = 1;
Daniel@0 118 for(i=0; i<bdim-1; i++){
Daniel@0 119 bcumprod[i+1] = bcumprod[i] * (int)pSize[i];
Daniel@0 120 scumprod[i+1] = scumprod[i] * ssize[i];
Daniel@0 121 }
Daniel@0 122
Daniel@0 123 nzCounts = 0;
Daniel@0 124 for(i=0; i<NZB; i++){
Daniel@0 125 bindex = bir[i];
Daniel@0 126 ind_subv(bindex, bcumprod, bdim, bsubv);
Daniel@0 127 for(j=0; j<count; j++){
Daniel@0 128 ssubv[j] = bsubv[mask[j]];
Daniel@0 129 }
Daniel@0 130 match = 1;
Daniel@0 131 for(j=0; j<count; j++){
Daniel@0 132 if((ssubv[j]) != observed[j]){
Daniel@0 133 match = 0;
Daniel@0 134 break;
Daniel@0 135 }
Daniel@0 136 }
Daniel@0 137 if(match){
Daniel@0 138 spr[nzCounts] = bpr[i];
Daniel@0 139 sindex = subv_ind(bdim, scumprod, bsubv);
Daniel@0 140 sir[nzCounts] = sindex;
Daniel@0 141 nzCounts++;
Daniel@0 142 }
Daniel@0 143 }
Daniel@0 144
Daniel@0 145 reset_nzmax(plhs[0], NS, nzCounts);
Daniel@0 146 free(mask);
Daniel@0 147 free(ssize);
Daniel@0 148 free(observed);
Daniel@0 149 free(bsubv);
Daniel@0 150 free(ssubv);
Daniel@0 151 free(bcumprod);
Daniel@0 152 free(scumprod);
Daniel@0 153 }
Daniel@0 154