annotate toolboxes/FullBNT-1.0.7/bnt/potentials/Tables/marg_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 /* marg_sparse_table.c ../potential/tables*/
Daniel@0 2
Daniel@0 3 /******************************************/
Daniel@0 4 /* 5 input & 1 output */
Daniel@0 5 /* Big sparse table */
Daniel@0 6 /* Big domain */
Daniel@0 7 /* Big sizes */
Daniel@0 8 /* onto */
Daniel@0 9 /* maximize, if missed, maximize=0 */
Daniel@0 10 /* */
Daniel@0 11 /* small sparse table */
Daniel@0 12 /******************************************/
Daniel@0 13
Daniel@0 14 #include <math.h>
Daniel@0 15 #include <stdlib.h>
Daniel@0 16 #include "mex.h"
Daniel@0 17
Daniel@0 18 int compare(const void* src1, const void* src2){
Daniel@0 19 int i1 = *(int*)src1 ;
Daniel@0 20 int i2 = *(int*)src2 ;
Daniel@0 21 return i1-i2 ;
Daniel@0 22 }
Daniel@0 23
Daniel@0 24 void ind_subv(int index, const int *cumprod, int n, int *bsubv){
Daniel@0 25 int i;
Daniel@0 26
Daniel@0 27 for (i = n-1; i >= 0; i--) {
Daniel@0 28 bsubv[i] = ((int)floor(index / cumprod[i]));
Daniel@0 29 index = index % cumprod[i];
Daniel@0 30 }
Daniel@0 31 }
Daniel@0 32
Daniel@0 33 int subv_ind(const int n, const int *cumprod, const int *subv){
Daniel@0 34 int i, index=0;
Daniel@0 35
Daniel@0 36 for(i=0; i<n; i++){
Daniel@0 37 index += subv[i] * cumprod[i];
Daniel@0 38 }
Daniel@0 39 return index;
Daniel@0 40 }
Daniel@0 41
Daniel@0 42 mxArray* convert_table_to_sparse(const double *Table, const int *sequence, const int nzCounts, const int N){
Daniel@0 43 mxArray *spTable;
Daniel@0 44 int i, temp, *irs, *jcs, count=0;
Daniel@0 45 double *sr;
Daniel@0 46
Daniel@0 47 spTable = mxCreateSparse(N, 1, nzCounts, mxREAL);
Daniel@0 48 sr = mxGetPr(spTable);
Daniel@0 49 irs = mxGetIr(spTable);
Daniel@0 50 jcs = mxGetJc(spTable);
Daniel@0 51
Daniel@0 52 jcs[0] = 0;
Daniel@0 53 jcs[1] = nzCounts;
Daniel@0 54
Daniel@0 55 for(i=0; i<nzCounts; i++){
Daniel@0 56 irs[i] = sequence[count];
Daniel@0 57 count++;
Daniel@0 58 temp = sequence[count];
Daniel@0 59 sr[i] = Table[temp];
Daniel@0 60 count++;
Daniel@0 61 }
Daniel@0 62 return spTable;
Daniel@0 63 }
Daniel@0 64
Daniel@0 65 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
Daniel@0 66 int i, j, count, bdim, sdim, NS, NZB, position, bindex, sindex, maximize, nzCounts=0;
Daniel@0 67 int *mask, *sequence, *result, *bir, *bjc, *ssize, *bCumprod, *sCumprod, *bsubv, *ssubv;
Daniel@0 68 double *sTable, *pbDomain, *psDomain, *pbSize, *bpr, *spr;
Daniel@0 69 const char *field_names[] = {"domain", "T", "sizes"};
Daniel@0 70
Daniel@0 71 if(nrhs < 5) maximize = 0;
Daniel@0 72 else maximize = (int)mxGetScalar(prhs[4]);
Daniel@0 73
Daniel@0 74 bdim = mxGetNumberOfElements(prhs[1]);
Daniel@0 75 sdim = mxGetNumberOfElements(prhs[3]);
Daniel@0 76 pbSize = mxGetPr(prhs[2]);
Daniel@0 77 pbDomain = mxGetPr(prhs[1]);
Daniel@0 78 psDomain = mxGetPr(prhs[3]);
Daniel@0 79 bpr = mxGetPr(prhs[0]);
Daniel@0 80 bir = mxGetIr(prhs[0]);
Daniel@0 81 bjc = mxGetJc(prhs[0]);
Daniel@0 82 NZB = bjc[1];
Daniel@0 83
Daniel@0 84 if(sdim == 0){
Daniel@0 85 plhs[0] = mxCreateSparse(1, 1, 1, mxREAL);
Daniel@0 86 spr = mxGetPr(plhs[0]);
Daniel@0 87 bir = mxGetIr(plhs[0]);
Daniel@0 88 bjc = mxGetJc(plhs[0]);
Daniel@0 89 *spr = 0;
Daniel@0 90 *bir = 0;
Daniel@0 91 bjc[0] = 0;
Daniel@0 92 bjc[1] = 1;
Daniel@0 93 if(maximize){
Daniel@0 94 for(i=0; i<NZB; i++){
Daniel@0 95 *spr = (*spr < bpr[i])? bpr[i] : *spr;
Daniel@0 96 }
Daniel@0 97 }
Daniel@0 98 else{
Daniel@0 99 for(i=0; i<NZB; i++){
Daniel@0 100 *spr += bpr[i];
Daniel@0 101 }
Daniel@0 102 }
Daniel@0 103 return;
Daniel@0 104 }
Daniel@0 105
Daniel@0 106 mask = malloc(sdim * sizeof(int));
Daniel@0 107 count = 0;
Daniel@0 108 for(i=0; i<sdim; i++){
Daniel@0 109 for(j=0; j<bdim; j++){
Daniel@0 110 if(psDomain[i] == pbDomain[j]){
Daniel@0 111 mask[count] = j;
Daniel@0 112 count++;
Daniel@0 113 break;
Daniel@0 114 }
Daniel@0 115 }
Daniel@0 116 }
Daniel@0 117
Daniel@0 118 sTable = malloc(NZB * sizeof(double));
Daniel@0 119 sequence = malloc(NZB * 2 * sizeof(double));
Daniel@0 120 bCumprod = malloc(bdim * sizeof(int));
Daniel@0 121 sCumprod = malloc(sdim * sizeof(int));
Daniel@0 122 bsubv = malloc(bdim * sizeof(int));
Daniel@0 123 ssubv = malloc(sdim * sizeof(int));
Daniel@0 124 ssize = malloc(sdim * sizeof(int));
Daniel@0 125
Daniel@0 126 NS = 1;
Daniel@0 127 for(i=0; i<count; i++){
Daniel@0 128 ssize[i] = (int)pbSize[mask[i]];
Daniel@0 129 NS *= ssize[i];
Daniel@0 130 }
Daniel@0 131
Daniel@0 132 for(i=0; i<NZB; i++)sTable[i] = 0;
Daniel@0 133
Daniel@0 134 bCumprod[0] = 1;
Daniel@0 135 for(i=0; i<bdim-1; i++){
Daniel@0 136 bCumprod[i+1] = bCumprod[i] * (int)pbSize[i];
Daniel@0 137 }
Daniel@0 138 sCumprod[0] = 1;
Daniel@0 139 for(i=0; i<sdim-1; i++){
Daniel@0 140 sCumprod[i+1] = sCumprod[i] * ssize[i];
Daniel@0 141 }
Daniel@0 142
Daniel@0 143 count = 0;
Daniel@0 144 for(i=0; i<NZB; i++){
Daniel@0 145 bindex = bir[i];
Daniel@0 146 ind_subv(bindex, bCumprod, bdim, bsubv);
Daniel@0 147 for(j=0; j<sdim; j++){
Daniel@0 148 ssubv[j] = bsubv[mask[j]];
Daniel@0 149 }
Daniel@0 150 sindex = subv_ind(sdim, sCumprod, ssubv);
Daniel@0 151 result = (int *) bsearch(&sindex, sequence, nzCounts, sizeof(int)*2, compare);
Daniel@0 152 if(result){
Daniel@0 153 position = (result - sequence) / 2;
Daniel@0 154 if(maximize)
Daniel@0 155 sTable[position] = (sTable[position] < bpr[i]) ? bpr[i] : sTable[position];
Daniel@0 156 else sTable[position] += bpr[i];
Daniel@0 157 }
Daniel@0 158 else {
Daniel@0 159 if(maximize)
Daniel@0 160 sTable[nzCounts] = (sTable[nzCounts] < bpr[i]) ? bpr[i] : sTable[nzCounts];
Daniel@0 161 else sTable[nzCounts] += bpr[i];
Daniel@0 162 sequence[count] = sindex;
Daniel@0 163 count++;
Daniel@0 164 sequence[count] = nzCounts;
Daniel@0 165 nzCounts++;
Daniel@0 166 count++;
Daniel@0 167 }
Daniel@0 168 }
Daniel@0 169
Daniel@0 170 qsort(sequence, nzCounts, sizeof(int) * 2, compare);
Daniel@0 171 plhs[0] = convert_table_to_sparse(sTable, sequence, nzCounts, NS);
Daniel@0 172
Daniel@0 173 free(sTable);
Daniel@0 174 free(sequence);
Daniel@0 175 free(mask);
Daniel@0 176 free(bCumprod);
Daniel@0 177 free(sCumprod);
Daniel@0 178 free(bsubv);
Daniel@0 179 free(ssubv);
Daniel@0 180 free(ssize);
Daniel@0 181 }