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