void func5(double *x_j, double *y_j, double *arrayX, double *arrayY, double *weights, double *cfd, double *u, int n) { int i, j; #pragma omp parallel { #pragma omp for private (i) for(j = 0; j < n; j++){ //i = findIndex(cfd, n, u[j]); i = findIndexBin(cfd, 0, n, u[j]); if(i == -1) i = n-1; x_j[j] = arrayX[i]; y_j[j] = arrayY[i]; } double w = 1/((double)n); #pragma omp for for(i = 0; i < n; i++){ arrayX[i] = x_j[i]; arrayY[i] = y_j[i]; weights[i] = w; } } }
/** * Finds the first element in the CDF that is greater than or equal to the provided value and returns that index * @note This function uses binary search before switching to sequential search * @param CDF The CDF * @param beginIndex The index to start searching from * @param endIndex The index to stop searching * @param value The value to find * @return The index of value in the CDF; if value is never found, returns the last index * @warning Use at your own risk; not fully tested */ int findIndexBin(double * CDF, int beginIndex, int endIndex, double value){ if(endIndex < beginIndex) return -1; int middleIndex = beginIndex + ((endIndex - beginIndex)/2); /*check the value*/ if(CDF[middleIndex] >= value) { /*check that it's good*/ if(middleIndex == 0) return middleIndex; else if(CDF[middleIndex-1] < value) return middleIndex; else if(CDF[middleIndex-1] == value) { while(middleIndex > 0 && CDF[middleIndex-1] == value) middleIndex--; return middleIndex; } } if(CDF[middleIndex] > value) return findIndexBin(CDF, beginIndex, middleIndex+1, value); return findIndexBin(CDF, middleIndex-1, endIndex, value); }
void func5(double *x_j, double *y_j, double *arrayX, double *arrayY, double *weights, double *cfd, double *u, int n) { int i, j; for(j = 0; j < n; j++){ //i = findIndex(cfd, n, u[j]); i = findIndexBin(cfd, 0, n, u[j]); if(i == -1) i = n-1; x_j[j] = arrayX[i]; y_j[j] = arrayY[i]; } for(i = 0; i < n; i++){ arrayX[i] = x_j[i]; arrayY[i] = y_j[i]; weights[i] = 1/((double)(n)); } }