annotate _FullBNT/KPMtools/ind2subvKPM.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 /* C mex version of ind2subv.m in misc directory */
matthiasm@8 2 /* 2 input, 1 output */
matthiasm@8 3 /* siz, ndx */
matthiasm@8 4 /* sub */
matthiasm@8 5
matthiasm@8 6 #include "mex.h"
matthiasm@8 7 #include <math.h>
matthiasm@8 8
matthiasm@8 9 void rbinary(int num, int n, double *rbits){
matthiasm@8 10 int i, mask;
matthiasm@8 11 num = num - 1;
matthiasm@8 12
matthiasm@8 13 mask = 1 << (n-1); /* mask = 00100...0 , where the 1 is in column n (rightmost = col 1) */
matthiasm@8 14 for (i = 0; i < n; i++) {
matthiasm@8 15 rbits[n-i-1] = ((num & mask) == 0) ? 1 : 2;
matthiasm@8 16 num <<= 1;
matthiasm@8 17 }
matthiasm@8 18 }
matthiasm@8 19
matthiasm@8 20 void ind_subv(int num, const double *sizes, int n, double *rbits){
matthiasm@8 21 int i;
matthiasm@8 22 int *cumprod;
matthiasm@8 23
matthiasm@8 24 cumprod = malloc(n * sizeof(int));
matthiasm@8 25 num = num - 1;
matthiasm@8 26 cumprod[0] = 1;
matthiasm@8 27 for (i = 0; i < n-1; i++)
matthiasm@8 28 cumprod[i+1] = cumprod[i] * (int)sizes[i];
matthiasm@8 29 for (i = n-1; i >= 0; i--) {
matthiasm@8 30 rbits[i] = ((int)floor(num / cumprod[i])) + 1;
matthiasm@8 31 num = num % cumprod[i];
matthiasm@8 32 }
matthiasm@8 33 free(cumprod);
matthiasm@8 34 }
matthiasm@8 35
matthiasm@8 36
matthiasm@8 37 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
matthiasm@8 38 int i, j, k, nCol, nRow, nnRow, binary, count, temp, temp1, start;
matthiasm@8 39 double *pSize, *pNdx, *pr;
matthiasm@8 40 double ndx;
matthiasm@8 41 int *subv, *cumprod, *templai;
matthiasm@8 42
matthiasm@8 43 pSize = mxGetPr(prhs[0]);
matthiasm@8 44 pNdx = mxGetPr(prhs[1]);
matthiasm@8 45 nCol = mxGetNumberOfElements(prhs[0]);
matthiasm@8 46 nnRow = mxGetNumberOfElements(prhs[1]);
matthiasm@8 47
matthiasm@8 48 nRow = 1;
matthiasm@8 49 for(i=0; i<nCol; i++){
matthiasm@8 50 nRow *= (int)pSize[i];
matthiasm@8 51 }
matthiasm@8 52
matthiasm@8 53 if(nCol == 0){
matthiasm@8 54 plhs[0] = mxDuplicateArray(prhs[1]);
matthiasm@8 55 return;
matthiasm@8 56 }
matthiasm@8 57
matthiasm@8 58 binary = 2;
matthiasm@8 59 for (i = 0; i < nCol; i++){
matthiasm@8 60 if (pSize[i] > 2.0){
matthiasm@8 61 binary = 0;
matthiasm@8 62 break;
matthiasm@8 63 }
matthiasm@8 64 else if((int)pSize[i] == 1){
matthiasm@8 65 binary = 1;
matthiasm@8 66 }
matthiasm@8 67 }
matthiasm@8 68
matthiasm@8 69 if(nnRow == 1){
matthiasm@8 70 ndx = mxGetScalar(prhs[1]);
matthiasm@8 71 plhs[0] = mxCreateDoubleMatrix(1, nCol, mxREAL);
matthiasm@8 72 pr = mxGetPr(plhs[0]);
matthiasm@8 73 if(binary == 2)rbinary((int)ndx, nCol, pr);
matthiasm@8 74 else ind_subv((int)ndx, pSize, nCol, pr);
matthiasm@8 75 return;
matthiasm@8 76 }
matthiasm@8 77
matthiasm@8 78 plhs[0] = mxCreateDoubleMatrix(nnRow, nCol, mxREAL);
matthiasm@8 79 pr = mxGetPr(plhs[0]);
matthiasm@8 80
matthiasm@8 81 subv = malloc(nRow * nCol * sizeof(int));
matthiasm@8 82
matthiasm@8 83 if (binary == 2) {
matthiasm@8 84 for(j=0; j<nCol; j++){
matthiasm@8 85 temp = (1 << j);
matthiasm@8 86 temp1 = j * nRow;
matthiasm@8 87 for(i=0; i<nRow; i++){
matthiasm@8 88 subv[temp1 + i] = ((i & temp) == 0) ? 1 : 2;
matthiasm@8 89 }
matthiasm@8 90 }
matthiasm@8 91 }
matthiasm@8 92 else if(binary == 1){
matthiasm@8 93 cumprod = (int *)malloc(nCol * sizeof(int));
matthiasm@8 94 templai = (int *)malloc(nCol * sizeof(int));
matthiasm@8 95 cumprod[0] = 1;
matthiasm@8 96 templai[0] = nRow;
matthiasm@8 97 for(i=1; i<nCol; i++){
matthiasm@8 98 k = (int)pSize[i-1] - 1;
matthiasm@8 99 cumprod[i] = cumprod[i-1] << k;
matthiasm@8 100 templai[i] = templai[i-1] >> k;
matthiasm@8 101 }
matthiasm@8 102 for(j=0; j<nCol; j++){
matthiasm@8 103 temp1 = j * nRow;
matthiasm@8 104 if(pSize[j] == 1.0){
matthiasm@8 105 for(i=0; i<nRow; i++){
matthiasm@8 106 subv[temp1 + i] = 1;
matthiasm@8 107 }
matthiasm@8 108 }
matthiasm@8 109 else{
matthiasm@8 110 temp = 1;
matthiasm@8 111 count = 0;
matthiasm@8 112 for(i=0; i<templai[j]; i++){
matthiasm@8 113 if(temp > 2) temp = 1;
matthiasm@8 114 for(k=0; k<cumprod[j]; k++){
matthiasm@8 115 subv[temp1 + count] = temp;
matthiasm@8 116 count++;
matthiasm@8 117 }
matthiasm@8 118 temp++;
matthiasm@8 119 }
matthiasm@8 120 }
matthiasm@8 121 }
matthiasm@8 122 free(templai);
matthiasm@8 123 free(cumprod);
matthiasm@8 124 }
matthiasm@8 125 else {
matthiasm@8 126 cumprod = (int *)malloc(nCol * sizeof(int));
matthiasm@8 127 templai = (int *)malloc(nCol * sizeof(int));
matthiasm@8 128 cumprod[0] = 1;
matthiasm@8 129 for(i=1; i<nCol; i++){
matthiasm@8 130 cumprod[i] = (int)(cumprod[i-1] * pSize[i-1]);
matthiasm@8 131 }
matthiasm@8 132 templai[0] = nRow;
matthiasm@8 133 for(i=1; i<nCol; i++){
matthiasm@8 134 templai[i] = nRow / cumprod[i];
matthiasm@8 135 }
matthiasm@8 136 for(j=0; j<nCol; j++){
matthiasm@8 137 temp1 = j * nRow;
matthiasm@8 138 if(pSize[j] == 1.0){
matthiasm@8 139 for(i=0; i<nRow; i++){
matthiasm@8 140 subv[temp1 + i] = 1;
matthiasm@8 141 }
matthiasm@8 142 }
matthiasm@8 143 else{
matthiasm@8 144 temp = 1;
matthiasm@8 145 count = 0;
matthiasm@8 146 for(i=0; i<templai[j]; i++){
matthiasm@8 147 if(temp > (int)pSize[j]) temp = 1;
matthiasm@8 148 for(k=0; k<cumprod[j]; k++){
matthiasm@8 149 subv[temp1 + count] = temp;
matthiasm@8 150 count++;
matthiasm@8 151 }
matthiasm@8 152 temp++;
matthiasm@8 153 }
matthiasm@8 154 }
matthiasm@8 155 }
matthiasm@8 156 free(cumprod);
matthiasm@8 157 free(templai);
matthiasm@8 158 }
matthiasm@8 159
matthiasm@8 160 count = 0;
matthiasm@8 161 for(j=0; j<nCol; j++){
matthiasm@8 162 temp1 = j * nRow;
matthiasm@8 163 for(i=0; i<nnRow; i++){
matthiasm@8 164 start = (int)pNdx[i] - 1;
matthiasm@8 165 pr[count] = subv[temp1 + start];
matthiasm@8 166 count++;
matthiasm@8 167 }
matthiasm@8 168 }
matthiasm@8 169 free(subv);
matthiasm@8 170 }
matthiasm@8 171
matthiasm@8 172
matthiasm@8 173
matthiasm@8 174
matthiasm@8 175
matthiasm@8 176