annotate _FullBNT/BNT/potentials/Tables/mult_by_sparse_table.c @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 /* mult_by_sparse_table.c ../potential/tables*/
matthiasm@8 2
matthiasm@8 3
matthiasm@8 4 /******************************************/
matthiasm@8 5 /* 6 input & 1 output */
matthiasm@8 6 /* Big table [0] */
matthiasm@8 7 /* Big domain [1] */
matthiasm@8 8 /* big sizes [2] */
matthiasm@8 9 /* Small table [3] */
matthiasm@8 10 /* small domain [4] */
matthiasm@8 11 /* small sizes [5] */
matthiasm@8 12 /* */
matthiasm@8 13 /* New big table[0] */
matthiasm@8 14 /******************************************/
matthiasm@8 15
matthiasm@8 16 #include <math.h>
matthiasm@8 17 #include <stdlib.h>
matthiasm@8 18 #include "mex.h"
matthiasm@8 19
matthiasm@8 20 int compare(const void* src1, const void* src2){
matthiasm@8 21 int i1 = *(int*)src1 ;
matthiasm@8 22 int i2 = *(int*)src2 ;
matthiasm@8 23 return i1-i2 ;
matthiasm@8 24 }
matthiasm@8 25
matthiasm@8 26 void ind_subv(int index, const int *cumprod, int n, int *bsubv){
matthiasm@8 27 int i;
matthiasm@8 28
matthiasm@8 29 for (i = n-1; i >= 0; i--) {
matthiasm@8 30 bsubv[i] = ((int)floor(index / cumprod[i]));
matthiasm@8 31 index = index % cumprod[i];
matthiasm@8 32 }
matthiasm@8 33 }
matthiasm@8 34
matthiasm@8 35 int subv_ind(const int n, const int *cumprod, const int *subv){
matthiasm@8 36 int i, index=0;
matthiasm@8 37
matthiasm@8 38 for(i=0; i<n; i++){
matthiasm@8 39 index += subv[i] * cumprod[i];
matthiasm@8 40 }
matthiasm@8 41 return index;
matthiasm@8 42 }
matthiasm@8 43
matthiasm@8 44 void reset_nzmax(mxArray *spArray, const int old_nzmax, const int new_nzmax){
matthiasm@8 45 double *ptr;
matthiasm@8 46 void *newptr;
matthiasm@8 47 int *ir, *jc;
matthiasm@8 48 int nbytes;
matthiasm@8 49
matthiasm@8 50 if(new_nzmax == old_nzmax) return;
matthiasm@8 51 nbytes = new_nzmax * sizeof(*ptr);
matthiasm@8 52 ptr = mxGetPr(spArray);
matthiasm@8 53 newptr = mxRealloc(ptr, nbytes);
matthiasm@8 54 mxSetPr(spArray, newptr);
matthiasm@8 55 nbytes = new_nzmax * sizeof(*ir);
matthiasm@8 56 ir = mxGetIr(spArray);
matthiasm@8 57 newptr = mxRealloc(ir, nbytes);
matthiasm@8 58 mxSetIr(spArray, newptr);
matthiasm@8 59 jc = mxGetJc(spArray);
matthiasm@8 60 jc[0] = 0;
matthiasm@8 61 jc[1] = new_nzmax;
matthiasm@8 62 mxSetNzmax(spArray, new_nzmax);
matthiasm@8 63 }
matthiasm@8 64
matthiasm@8 65 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
matthiasm@8 66 int i, j, count, bdim, sdim, NB, NZB, NZS, position, bindex, sindex, nzCounts=0;
matthiasm@8 67 int *mask, *result, *bir, *sir, *rir, *bjc, *sjc, *rjc, *bCumprod, *sCumprod, *bsubv, *ssubv;
matthiasm@8 68 double *pbDomain, *psDomain, *pbSize, *psSize, *bpr, *spr, *rpr;
matthiasm@8 69
matthiasm@8 70 pbDomain = mxGetPr(prhs[1]);
matthiasm@8 71 bdim = mxGetNumberOfElements(prhs[1]);
matthiasm@8 72 psDomain = mxGetPr(prhs[4]);
matthiasm@8 73 sdim = mxGetNumberOfElements(prhs[4]);
matthiasm@8 74
matthiasm@8 75 pbSize = mxGetPr(prhs[2]);
matthiasm@8 76 psSize = mxGetPr(prhs[5]);
matthiasm@8 77
matthiasm@8 78 NB = 1;
matthiasm@8 79 for(i=0; i<bdim; i++){
matthiasm@8 80 NB *= (int)pbSize[i];
matthiasm@8 81 }
matthiasm@8 82
matthiasm@8 83 bpr = mxGetPr(prhs[0]);
matthiasm@8 84 bir = mxGetIr(prhs[0]);
matthiasm@8 85 bjc = mxGetJc(prhs[0]);
matthiasm@8 86 NZB = bjc[1];
matthiasm@8 87
matthiasm@8 88 spr = mxGetPr(prhs[3]);
matthiasm@8 89 sir = mxGetIr(prhs[3]);
matthiasm@8 90 sjc = mxGetJc(prhs[3]);
matthiasm@8 91 NZS = sjc[1];
matthiasm@8 92
matthiasm@8 93 plhs[0] = mxDuplicateArray(prhs[0]);
matthiasm@8 94 rpr = mxGetPr(plhs[0]);
matthiasm@8 95 rir = mxGetIr(plhs[0]);
matthiasm@8 96 rjc = mxGetJc(plhs[0]);
matthiasm@8 97 rjc[0] = 0;
matthiasm@8 98 rjc[1] = NZB;
matthiasm@8 99
matthiasm@8 100 if(sdim == 0){
matthiasm@8 101 for(i=0; i<NZB; i++){
matthiasm@8 102 rpr[i] *= *spr;
matthiasm@8 103 }
matthiasm@8 104 return;
matthiasm@8 105 }
matthiasm@8 106
matthiasm@8 107 mask = malloc(sdim * sizeof(int));
matthiasm@8 108 bCumprod = malloc(bdim * sizeof(int));
matthiasm@8 109 sCumprod = malloc(sdim * sizeof(int));
matthiasm@8 110 bsubv = malloc(bdim * sizeof(int));
matthiasm@8 111 ssubv = malloc(sdim * sizeof(int));
matthiasm@8 112
matthiasm@8 113 count = 0;
matthiasm@8 114 for(i=0; i<sdim; i++){
matthiasm@8 115 for(j=0; j<bdim; j++){
matthiasm@8 116 if(psDomain[i] == pbDomain[j]){
matthiasm@8 117 mask[count] = j;
matthiasm@8 118 count++;
matthiasm@8 119 break;
matthiasm@8 120 }
matthiasm@8 121 }
matthiasm@8 122 }
matthiasm@8 123
matthiasm@8 124 bCumprod[0] = 1;
matthiasm@8 125 for(i=0; i<bdim-1; i++){
matthiasm@8 126 bCumprod[i+1] = bCumprod[i] * (int)pbSize[i];
matthiasm@8 127 }
matthiasm@8 128 sCumprod[0] = 1;
matthiasm@8 129 for(i=0; i<sdim-1; i++){
matthiasm@8 130 sCumprod[i+1] = sCumprod[i] * (int)psSize[i];
matthiasm@8 131 }
matthiasm@8 132
matthiasm@8 133 for(i=0; i<NZB; i++){
matthiasm@8 134 bindex = bir[i];
matthiasm@8 135 ind_subv(bindex, bCumprod, bdim, bsubv);
matthiasm@8 136 for(j=0; j<sdim; j++){
matthiasm@8 137 ssubv[j] = bsubv[mask[j]];
matthiasm@8 138 }
matthiasm@8 139 sindex = subv_ind(sdim, sCumprod, ssubv);
matthiasm@8 140 result = (int *) bsearch(&sindex, sir, NZS, sizeof(int), compare);
matthiasm@8 141 if(result){
matthiasm@8 142 position = result - sir;
matthiasm@8 143 rpr[nzCounts] = bpr[i] * spr[position];
matthiasm@8 144 rir[nzCounts] = bindex;
matthiasm@8 145 nzCounts++;
matthiasm@8 146 }
matthiasm@8 147 }
matthiasm@8 148
matthiasm@8 149 reset_nzmax(plhs[0], NZB, nzCounts);
matthiasm@8 150 free(mask);
matthiasm@8 151 free(bCumprod);
matthiasm@8 152 free(sCumprod);
matthiasm@8 153 free(bsubv);
matthiasm@8 154 free(ssubv);
matthiasm@8 155 }