Ejemplo n.º 1
0
/**
  * Just one of many ways of computing a quantile from a discrete set.
  * This corresponds to R's default method.
  */
double quantile( double *x, int n, double p ) {
	const double index = (n-1)*p;
	const double lo = floor( index );
	const double hi =  ceil( index );
	double xlo = qselect( x, n, (int)lo );
	double xhi = qselect( x, n, (int)hi );
	double Q = xlo;
	if( index > lo ) {
		double h = index - lo;
		Q = (1-h)*Q + h*xhi;
	}
	return Q;
}
Ejemplo n.º 2
0
Archivo: ops.c Proyecto: vr3d/heman
static float qselect(float *v, int len, int k)
{
    int i, st;
    for(st = i = 0; i < len - 1; i++) {
        if(v[i] > v[len - 1]) {
            continue;
        }
        SWAP(float, v[i], v[st]);
        st++;
    }
    SWAP(float, v[len - 1], v[st]);
    return k == st ? v[st] : st > k ? qselect(v, st, k) : qselect(v + st, len - st, k - st);
}
Ejemplo n.º 3
0
int qselect(int *v, int len, int k)
{
#	define SWAP(a, b) { tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
	int i, st, tmp;
	for (st = i = 0; i < len - 1; i++) {
		if (v[i] > v[len - 1]) continue;
		SWAP(i, st);
		st++;
	}
	SWAP(len - 1, st);

	return k == st ? v[st]
		: st > k ? qselect(v, st, k)
		: qselect(v + st, len - st, k - st);
}
Ejemplo n.º 4
0
double qselectFunction(double *v, int len, int k){

	int i, st, tmp;

	for (st = i = 0; i < len - 1; i++) {
		if (v[i] > v[len-1]) continue;
		SWAP(i, st);
		st++;
	}

	SWAP(len-1, st);

	return k == st	?v[st]
			:st > k	? qselect(v, st, k)
				: qselect(v + st, len - st, k - st);
}
Ejemplo n.º 5
0
int main(void)
{
	int num, result;
	scanf("%d %d", &num, &result);
	for (int i = 0; i < num; i++) {
		scanf("%d", &arr2[i]);
	}
	printf("%d\n", qselect(arr2, num, result-1));
	return 0;
}
Ejemplo n.º 6
0
static void* extractThread(void* param) {

    ExtractContext* context = (ExtractContext*) param;
    
    DbAlignmentData** dbAlignmentData = context->dbAlignmentData;
    int* dbAlignmentLen = context->dbAlignmentLen;
    Chain* query = context->query;
    Chain** database = context->database;
    int databaseLen = context->databaseLen;
    int* scores = context->scores;
    int maxAlignments = context->maxAlignments;
    ValueFunction valueFunction = context->valueFunction;
    void* valueFunctionParam = context->valueFunctionParam;
    double valueThreshold = context->valueThreshold;
    int* cards = context->cards;
    int cardsLen = context->cardsLen;

    int i;
    
    size_t packedSize = databaseLen * sizeof(DbAlignmentData);
    DbAlignmentData* packed = (DbAlignmentData*) malloc(packedSize);
    double* values = (double*) malloc(databaseLen * sizeof(double));
    
    valueFunction(values, scores, query, database, databaseLen, 
        cards, cardsLen, valueFunctionParam);

    int thresholded = 0;
    for (i = 0; i < databaseLen; ++i) {
    
        packed[i].idx = i;
        packed[i].value = values[i];
        packed[i].score = scores[i];
        packed[i].name = chainGetName(database[i]);

        if (packed[i].value <= valueThreshold) {
            thresholded++;
        }
    }
        
    int k = MIN(thresholded, maxAlignments);
    qselect((void*) packed, databaseLen, sizeof(DbAlignmentData), k, dbAlignmentDataCmp);
    qsort((void*) packed, k, sizeof(DbAlignmentData), dbAlignmentDataCmp);

    *dbAlignmentData = (DbAlignmentData*) malloc(k * sizeof(DbAlignmentData));
    *dbAlignmentLen = k;

    for (i = 0; i < k; ++i) {
        (*dbAlignmentData)[i] = packed[i];
    }

    free(packed);
    free(values);

    return NULL;
}
Ejemplo n.º 7
0
double qselect(double *v, int len, int k){

	int i, st, tmp;

	for (st = i = 0; i < len - 1; i++) {
		if (v[i] > v[len-1]) 
			continue;
		//SWAP(i, st);
		tmp = v[i];
		v[i] = v[st];
		v[st] = tmp;
		st++;
	}

	//SWAP(len-1, st);
	tmp = v[len-1];
	v[len-1] = v[st];
	v[st] = tmp;

	return k == st	?v[st]
			:st > k	? qselect(v, st, k)
				: qselect(v + st, len - st, k - st);
}
int main(void)
{
#	define N (sizeof(x)/sizeof(x[0]))
	int x[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
	int y[N];

	int i;
	for (i = 0; i < 10; i++) {
		memcpy(y, x, sizeof(x)); // qselect modifies array
		printf("%d: %d\n", i, qselect(y, 10, i));
	}

	return 0;
}
Ejemplo n.º 9
0
Archivo: ops.c Proyecto: vr3d/heman
heman_image* heman_ops_percentiles(heman_image* hmap, int nsteps,
    heman_image* mask, heman_color mask_color, int invert_mask,
    HEMAN_FLOAT offset)
{
    assert(hmap->nbands == 1);
    assert(!mask || mask->nbands == 3);
    int size = hmap->height * hmap->width;
    HEMAN_FLOAT* src = hmap->data;
    HEMAN_FLOAT minv = 1000;
    HEMAN_FLOAT maxv = -1000;
    int npixels = 0;
    for (int i = 0; i < size; ++i) {
        if (!mask || _match(mask, mask_color, invert_mask, i)) {
            minv = MIN(minv, src[i]);
            maxv = MAX(maxv, src[i]);
            npixels++;
        }
    }

    HEMAN_FLOAT* vals = malloc(sizeof(HEMAN_FLOAT) * npixels);
    npixels = 0;
    for (int i = 0; i < size; ++i) {
        if (!mask || _match(mask, mask_color, invert_mask, i)) {
            vals[npixels++] = src[i];
        }
    }
    HEMAN_FLOAT* percentiles = malloc(sizeof(HEMAN_FLOAT) * nsteps);
    for (int tier = 0; tier < nsteps; tier++) {
        float height = qselect(vals, npixels, tier * npixels / nsteps);
        percentiles[tier] = height;
    }
    free(vals);

    for (int i = 0; i < size; ++i) {
        HEMAN_FLOAT e = *src;
        if (!mask || _match(mask, mask_color, invert_mask, i)) {
            for (int tier = nsteps - 1; tier >= 0; tier--) {
                if (e > percentiles[tier]) {
                    e = percentiles[tier];
                    break;
                }
            }
        }
        *src++ = e + offset;
    }
    free(percentiles);

    return hmap;
}
Ejemplo n.º 10
0
/**
 * get the median of HiDistanceMatrixActual(x, x1, x2)
 * for every x in our dataset
 *
 **/
double GetMedian(value* x1, value* x2) {
	int i;
	value* x = malloc(sizeof(value));
	double* values = malloc(sizeof(double) * dm->dataNum);
	for (i = 0; i < dm->dataNum; ++i) {
		GetIthData(i, x);
		values[i] = HiDistanceMatrixActual(x, x1, x2);
	}

//	for (i = 0; i < dm->dataNum; ++i) {
//	}

//	return values[2];
//	return 0;
//
	double res = qselect(values,dm->dataNum,0);

	free(values);
	free(x);
	return res;
}