annotate toolboxes/FullBNT-1.0.7/bnt/potentials/Tables/divide_by_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 /* divide_by_sparse_table.c ../potential/tables*/
Daniel@0 2
Daniel@0 3 /******************************************/
Daniel@0 4 /* 6 input & 1 output */
Daniel@0 5 /* Big table [0] */
Daniel@0 6 /* Big domain [1] */
Daniel@0 7 /* big sizes [2] */
Daniel@0 8 /* Small table [3] */
Daniel@0 9 /* small domain [4] */
Daniel@0 10 /* small sizes [5] */
Daniel@0 11 /* */
Daniel@0 12 /* New big table[0] */
Daniel@0 13 /******************************************/
Daniel@0 14
Daniel@0 15 #include <math.h>
Daniel@0 16 #include <stdlib.h>
Daniel@0 17 #include "mex.h"
Daniel@0 18
Daniel@0 19 int compare(const void* src1, const void* src2){
Daniel@0 20 int i1 = *(int*)src1 ;
Daniel@0 21 int i2 = *(int*)src2 ;
Daniel@0 22 return i1-i2 ;
Daniel@0 23 }
Daniel@0 24
Daniel@0 25 void ind_subv(int index, const int *cumprod, int n, int *bsubv){
Daniel@0 26 int i;
Daniel@0 27
Daniel@0 28 for (i = n-1; i >= 0; i--) {
Daniel@0 29 bsubv[i] = ((int)floor(index / cumprod[i]));
Daniel@0 30 index = index % cumprod[i];
Daniel@0 31 }
Daniel@0 32 }
Daniel@0 33
Daniel@0 34 int subv_ind(const int n, const int *cumprod, const int *subv){
Daniel@0 35 int i, index=0;
Daniel@0 36
Daniel@0 37 for(i=0; i<n; i++){
Daniel@0 38 index += subv[i] * cumprod[i];
Daniel@0 39 }
Daniel@0 40 return index;
Daniel@0 41 }
Daniel@0 42
Daniel@0 43 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
Daniel@0 44 int i, j, count, bdim, sdim, NB, NZB, NZS, position, bindex, sindex;
Daniel@0 45 int *mask, *result, *bir, *sir, *bjc, *sjc, *bCumprod, *sCumprod, *bsubv, *ssubv;
Daniel@0 46 double *pbDomain, *psDomain, *pbSize, *psSize, *bpr, *spr, value;
Daniel@0 47
Daniel@0 48 plhs[0] = mxDuplicateArray(prhs[0]);
Daniel@0 49 pbDomain = mxGetPr(prhs[1]);
Daniel@0 50 bdim = mxGetNumberOfElements(prhs[1]);
Daniel@0 51 psDomain = mxGetPr(prhs[4]);
Daniel@0 52 sdim = mxGetNumberOfElements(prhs[4]);
Daniel@0 53
Daniel@0 54 pbSize = mxGetPr(prhs[2]);
Daniel@0 55 psSize = mxGetPr(prhs[5]);
Daniel@0 56
Daniel@0 57 NB = 1;
Daniel@0 58 for(i=0; i<bdim; i++){
Daniel@0 59 NB *= (int)pbSize[i];
Daniel@0 60 }
Daniel@0 61
Daniel@0 62 bpr = mxGetPr(plhs[0]);
Daniel@0 63 bir = mxGetIr(plhs[0]);
Daniel@0 64 bjc = mxGetJc(plhs[0]);
Daniel@0 65 NZB = bjc[1];
Daniel@0 66
Daniel@0 67 spr = mxGetPr(prhs[3]);
Daniel@0 68 sir = mxGetIr(prhs[3]);
Daniel@0 69 sjc = mxGetJc(prhs[3]);
Daniel@0 70 NZS = sjc[1];
Daniel@0 71
Daniel@0 72 if(sdim == 0){
Daniel@0 73 value = *spr;
Daniel@0 74 if(value == 0)value = 1;
Daniel@0 75 for(i=0; i<NZB; i++){
Daniel@0 76 bpr[i] /= value;
Daniel@0 77 }
Daniel@0 78 return;
Daniel@0 79 }
Daniel@0 80
Daniel@0 81 mask = malloc(sdim * sizeof(int));
Daniel@0 82 bCumprod = malloc(bdim * sizeof(int));
Daniel@0 83 sCumprod = malloc(sdim * sizeof(int));
Daniel@0 84 bsubv = malloc(bdim * sizeof(int));
Daniel@0 85 ssubv = malloc(sdim * sizeof(int));
Daniel@0 86
Daniel@0 87 count = 0;
Daniel@0 88 for(i=0; i<sdim; i++){
Daniel@0 89 for(j=0; j<bdim; j++){
Daniel@0 90 if(psDomain[i] == pbDomain[j]){
Daniel@0 91 mask[count] = j;
Daniel@0 92 count++;
Daniel@0 93 break;
Daniel@0 94 }
Daniel@0 95 }
Daniel@0 96 }
Daniel@0 97
Daniel@0 98 bCumprod[0] = 1;
Daniel@0 99 for(i=0; i<bdim-1; i++){
Daniel@0 100 bCumprod[i+1] = bCumprod[i] * (int)pbSize[i];
Daniel@0 101 }
Daniel@0 102 sCumprod[0] = 1;
Daniel@0 103 for(i=0; i<sdim-1; i++){
Daniel@0 104 sCumprod[i+1] = sCumprod[i] * (int)psSize[i];
Daniel@0 105 }
Daniel@0 106
Daniel@0 107 for(i=0; i<NZB; i++){
Daniel@0 108 bindex = bir[i];
Daniel@0 109 ind_subv(bindex, bCumprod, bdim, bsubv);
Daniel@0 110 for(j=0; j<sdim; j++){
Daniel@0 111 ssubv[j] = bsubv[mask[j]];
Daniel@0 112 }
Daniel@0 113 sindex = subv_ind(sdim, sCumprod, ssubv);
Daniel@0 114 result = (int *) bsearch(&sindex, sir, NZS, sizeof(int), compare);
Daniel@0 115 if(result){
Daniel@0 116 position = result - sir;
Daniel@0 117 bpr[i] /= spr[position];
Daniel@0 118 }
Daniel@0 119 }
Daniel@0 120
Daniel@0 121 free(mask);
Daniel@0 122 free(bCumprod);
Daniel@0 123 free(sCumprod);
Daniel@0 124 free(bsubv);
Daniel@0 125 free(ssubv);
Daniel@0 126 }