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