Example #1
0
Value *compute_poly(Enumeration *en,Value *list_args) {

  Value *tmp;
  /*        double d; int i; */

  tmp = (Value *) malloc (sizeof(Value));
  assert(tmp != NULL);
  value_init(*tmp);
  value_set_si(*tmp,0);

  if(!en)
    return(tmp);        /* no ehrhart polynomial */
  if(en->ValidityDomain) {
    if(!en->ValidityDomain->Dimension) { /* no parameters */
      value_set_double(*tmp,compute_evalue(&en->EP,list_args)+.25);
      return(tmp);
    }
  }  
  else 
    return(tmp);  /* no Validity Domain */    
  while(en) {
    if(in_domain(en->ValidityDomain,list_args)) {
      
#ifdef EVAL_EHRHART_DEBUG
      Print_Domain(stdout,en->ValidityDomain,NULL);
      print_evalue(stdout,&en->EP,NULL);
#endif
      
      /*                        d = compute_evalue(&en->EP,list_args);
                                i = d;
                                printf("(double)%lf = %d\n", d, i ); */
      value_set_double(*tmp,compute_evalue(&en->EP,list_args)+.25);
      return(tmp);
    }
    else
      en=en->next;
  }
  value_set_si(*tmp,0);
  return(tmp); /* no compatible domain with the arguments */
} /* compute_poly */ 
Example #2
0
static void
vbbox_read_row (VirtualBBoxCursorPtr cursor)
{
    /* trying to read a row from the BoundingBox real-table */
    struct splite_internal_cache *cache =
        (struct splite_internal_cache *) cursor->pVtab->p_cache;
    sqlite3_stmt *stmt;
    int ret;
    int ic;
    int icx;
    const char *text;
    const unsigned char *blob;
    int size;
    sqlite3_int64 pk;
    double minx;
    double miny;
    double maxx;
    double maxy;
    int srid;
    char ok_minx = 'N';
    char ok_miny = 'N';
    char ok_maxx = 'N';
    char ok_maxy = 'N';
    char ok_srid = 'N';
    stmt = cursor->stmt;
    sqlite3_bind_int64 (stmt, 1, cursor->current_row);
    ret = sqlite3_step (stmt);
    if (ret == SQLITE_ROW)
    {
        pk = sqlite3_column_int64 (stmt, 0);
        if (sqlite3_column_type (stmt, 1) == SQLITE_FLOAT)
        {
            minx = sqlite3_column_double (stmt, 1);
            ok_minx = 'Y';
        }
        if (sqlite3_column_type (stmt, 2) == SQLITE_FLOAT)
        {
            miny = sqlite3_column_double (stmt, 2);
            ok_miny = 'Y';
        }
        if (sqlite3_column_type (stmt, 3) == SQLITE_FLOAT)
        {
            maxx = sqlite3_column_double (stmt, 3);
            ok_maxx = 'Y';
        }
        if (sqlite3_column_type (stmt, 4) == SQLITE_FLOAT)
        {
            maxy = sqlite3_column_double (stmt, 4);
            ok_maxy = 'Y';
        }
        if (sqlite3_column_type (stmt, 5) == SQLITE_INTEGER)
        {
            srid = sqlite3_column_int (stmt, 5);
            ok_srid = 'Y';
        }
        if (cursor->pVtab->BBoxGeom)
            gaiaFreeGeomColl (cursor->pVtab->BBoxGeom);
        cursor->pVtab->BBoxGeom = NULL;
        if (ok_minx == 'Y' && ok_miny == 'Y' && ok_maxx == 'Y'
                && ok_maxy == 'Y')
        {
            gaiaGeomCollPtr geom = gaiaAllocGeomColl ();
            gaiaPolygonPtr pg = gaiaAddPolygonToGeomColl (geom, 5, 0);
            gaiaRingPtr rng = pg->Exterior;
            gaiaSetPoint (rng->Coords, 0, minx, miny);
            gaiaSetPoint (rng->Coords, 1, maxx, miny);
            gaiaSetPoint (rng->Coords, 2, maxx, maxy);
            gaiaSetPoint (rng->Coords, 3, minx, maxy);
            gaiaSetPoint (rng->Coords, 4, minx, miny);
            if (ok_srid == 'Y')
            {
                if (cursor->pVtab->ForceWGS84)
                {
                    /* converting to WGS84 long-lat */
                    gaiaGeomCollPtr geom2 = NULL;
                    char *proj_from = NULL;
                    char *proj_to = NULL;
                    geom->Srid = srid;
                    getProjParams (cursor->pVtab->db, srid, &proj_from);
                    getProjParams (cursor->pVtab->db, 4326, &proj_to);
                    if (proj_to == NULL || proj_from == NULL)
                        geom2 = NULL;
                    else
#ifndef OMIT_PROJ		/* including PROJ.4 */
                        if (cache != NULL)
                            geom2 =
                                gaiaTransform_r (cache, geom, proj_from,
                                                 proj_to);
                        else
                            geom2 =
                                gaiaTransform (geom, proj_from, proj_to);
#endif /* end including PROJ.4 */
                    geom2 = NULL;
                    if (geom2 != NULL)
                        geom2->Srid = 4326;
                    cursor->pVtab->BBoxGeom = geom2;
                    gaiaFreeGeomColl (geom);
                    if (proj_from)
                        free (proj_from);
                    if (proj_to)
                        free (proj_to);
                }
                else
                {
                    geom->Srid = srid;
                    cursor->pVtab->BBoxGeom = geom;
                }
            }
            else
            {
                geom->Srid = cursor->pVtab->Srid;
                cursor->pVtab->BBoxGeom = geom;
            }
        }
        icx = 5;
        for (ic = 0; ic < cursor->pVtab->nColumns; ic++)
        {
            if (*(cursor->pVtab->Visible + ic) != 'Y')
                continue;
            icx++;
            switch (sqlite3_column_type (stmt, icx))
            {
            case SQLITE_INTEGER:
                value_set_int (*(cursor->pVtab->Value + ic),
                               sqlite3_column_int64 (stmt, icx));
                break;
            case SQLITE_FLOAT:
                value_set_double (*(cursor->pVtab->Value + ic),
                                  sqlite3_column_double (stmt, icx));
                break;
            case SQLITE_TEXT:
                text = (char *) sqlite3_column_text (stmt, icx);
                size = sqlite3_column_bytes (stmt, icx);
                value_set_text (*(cursor->pVtab->Value + ic), text, size);
                break;
            case SQLITE_BLOB:
                blob = sqlite3_column_blob (stmt, icx);
                size = sqlite3_column_bytes (stmt, icx);
                value_set_blob (*(cursor->pVtab->Value + ic), blob, size);
                break;
            case SQLITE_NULL:
            default:
                value_set_null (*(cursor->pVtab->Value + ic));
                break;
            };
        }
    }
    else
    {
        /* an error occurred */
        cursor->eof = 1;
        return;
    }
    cursor->eof = 0;
    cursor->current_row = pk;
}
Example #3
0
/* 
 * For 32,000 elements
 * NIL: 0.01 sec
 * all: 0.17 sec
 * MPZ: 0.21 sec
 * MPF: 0.19 sec
 * STR: 0.24 sec
 * ID : 0.18 sec
 * LST: 0.04 sec
 * 
 * 
 */ 
int test_hash()
{
#define NUMBER ((1 << 15) + 1)
#define REPEATS 20
	
	value x = value_init_nil();
	
	value val = value_set_str("result");
	size_t i, j, k, start, finish;
	
	value keys[NUMBER];
	char *skeys[NUMBER];
	for (i = 0; i < NUMBER; ++i) {
		skeys[i] = value_malloc(NULL, 30);
		sprintf(skeys[i], "%ld", (long) i);
		
		value num = value_set_long(i);
		
		switch (i % 4) {
			case 0:
				keys[i] = value_set(num);
				break;
			case 1:
				keys[i] = value_set_double(i);
				break;
			case 2:
				keys[i] = value_cast(num, VALUE_STR);
				break;
			case 3:
				keys[i] = value_cast(num, VALUE_STR);
				keys[i].type = VALUE_ID;
				break;
			case 4:
				keys[i] = value_init(VALUE_LST);
				value_cons_now2(&num, &keys[i]);
				break;
			default:
				keys[i] = value_init_nil();
				break;
		}
		
		value_clear(&num);
		
		// This overrides the setting done above.
//		keys[i] = value_cast(value_set_long(i), VALUE_STR);
	}
	
	for (i = 1 << 4; i < NUMBER; i <<= 1) {
		long average = 0;
		long difference = 0;
		
		for (j = 0; j < REPEATS; ++j) {
			x = value_init(VALUE_HSH);
//			StrMap *map = strmap_new(HASH_DEFAULT_CAPACITY);
			
			start = usec();
			
			for (k = 0; k < i; ++k)
				value_hash_put(&x, keys[k], val);
//				strmap_put(map, skeys[k], sval);
			
			finish = usec();
			average += finish - start;
			if (labs((long) (average / (j+1)) - (finish - start)) > difference)
				difference = labs((long) (average / (j+1)) - (finish - start));

			value_clear(&x);
//			strmap_delete(map);
		}
		
		printf("time for %ld elements: %ld usec +/= %ld\n", (long) i, average / j, difference);
	}

#undef NUMBER
#undef REPEATS

	return 0;
}
Example #4
0
int main(int argc, char **argv)
{
    isl_ctx *ctx;
    int i, nbPol, nbVec, nbMat, func, j, n;
    Polyhedron *A, *B, *C, *D, *E, *F, *G;
    char s[128];
    struct barvinok_options *options = barvinok_options_new_with_defaults();

    argc = barvinok_options_parse(options, argc, argv, ISL_ARG_ALL);
    ctx = isl_ctx_alloc_with_options(&barvinok_options_args, options);

    nbPol = nbVec = nbMat = 0;
    fgets(s, 128, stdin);
    while ((*s=='#') ||
	    ((sscanf(s, "D %d", &nbPol) < 1) &&
	     (sscanf(s, "V %d", &nbVec) < 1) &&
	     (sscanf(s, "M %d", &nbMat) < 1)))
	fgets(s, 128, stdin);

    for (i = 0; i < nbPol; ++i) {
	Matrix *M = Matrix_Read();
	A = Constraints2Polyhedron(M, options->MaxRays);
	Matrix_Free(M);
	fgets(s, 128, stdin);
	while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
	    fgets(s, 128, stdin);

	switch(func) {
	case 0: {
	    Value cb, ck;
	    value_init(cb);
	    value_init(ck);
	    fgets(s, 128, stdin);
	    /* workaround for apparent bug in older gmps */
	    *strchr(s, '\n') = '\0';
	    while ((*s=='#') || (value_read(ck, s) != 0)) {
		fgets(s, 128, stdin);
		/* workaround for apparent bug in older gmps */
		*strchr(s, '\n') = '\0';
	    }
	    barvinok_count_with_options(A, &cb, options);
	    if (value_ne(cb, ck))
		return -1;
	    value_clear(cb);
	    value_clear(ck);
	    break;
	}
	case 1:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    B = Polyhedron_Polar(A, options->MaxRays);
	    Polyhedron_Print(stdout, P_VALUE_FMT, B);
	    C = Polyhedron_Polar(B, options->MaxRays);
	    Polyhedron_Print(stdout, P_VALUE_FMT, C);
	    Polyhedron_Free(C);
	    Polyhedron_Free(B);
	    break;
	case 2:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    for (j = 0; j < A->NbRays; ++j) {
		B = supporting_cone(A, j);
		Polyhedron_Print(stdout, P_VALUE_FMT, B);
		Polyhedron_Free(B);
	    }
	    break;
	case 3:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    C = B = NULL;
	    barvinok_decompose(A,&B,&C);
	    puts("Pos:");
	    Polyhedron_Print(stdout, P_VALUE_FMT, B);
	    puts("Neg:");
	    Polyhedron_Print(stdout, P_VALUE_FMT, C);
	    Domain_Free(B);
	    Domain_Free(C);
	    break;
	case 4: {
	    Value cm, cb;
	    struct tms tms_before, tms_between, tms_after;
	    value_init(cm);
	    value_init(cb);
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    times(&tms_before);
	    manual_count(A, &cm);
	    times(&tms_between);
	    barvinok_count(A, &cb, 100);
	    times(&tms_after);
	    printf("manual: ");
	    value_print(stdout, P_VALUE_FMT, cm);
	    puts("");
	    time_diff(&tms_before, &tms_between);
	    printf("Barvinok: ");
	    value_print(stdout, P_VALUE_FMT, cb);
	    puts("");
	    time_diff(&tms_between, &tms_after);
	    value_clear(cm);
	    value_clear(cb);
	    break;
	}
	case 5:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    B = triangulate_cone(A, 100);
	    Polyhedron_Print(stdout, P_VALUE_FMT, B);
	    check_triangulization(A, B);
	    Domain_Free(B);
	    break;
	case 6:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    B = remove_equalities(A, options->MaxRays);
	    Polyhedron_Print(stdout, P_VALUE_FMT, B);
	    Polyhedron_Free(B);
	    break;
	case 8: {
	    evalue *EP;
	    Matrix *M = Matrix_Read();
	    const char **param_name;
	    C = Constraints2Polyhedron(M, options->MaxRays);
	    Matrix_Free(M);
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    Polyhedron_Print(stdout, P_VALUE_FMT, C);
	    EP = barvinok_enumerate_with_options(A, C, options);
	    param_name = Read_ParamNames(stdin, C->Dimension);
	    print_evalue(stdout, EP, (const char**)param_name);
	    evalue_free(EP);
	    Polyhedron_Free(C);
	}
	case 9:
	    Polyhedron_Print(stdout, P_VALUE_FMT, A);
	    Polyhedron_Polarize(A);
	    C = B = NULL;
	    barvinok_decompose(A,&B,&C);
	    for (D = B; D; D = D->next)
		Polyhedron_Polarize(D);
	    for (D = C; D; D = D->next)
		Polyhedron_Polarize(D);
	    puts("Pos:");
	    Polyhedron_Print(stdout, P_VALUE_FMT, B);
	    puts("Neg:");
	    Polyhedron_Print(stdout, P_VALUE_FMT, C);
	    Domain_Free(B);
	    Domain_Free(C);
	    break;
	case 10: {
	    evalue *EP;
	    Value cb, ck;

	    value_init(cb);
	    value_init(ck);
	    fgets(s, 128, stdin);
	    sscanf(s, "%d", &n);
	    for (j = 0; j < n; ++j) {
		Polyhedron *P;
		M = Matrix_Read();
		P = Constraints2Polyhedron(M, options->MaxRays);
		Matrix_Free(M);
		A = DomainConcat(P, A);
	    }
	    fgets(s, 128, stdin);
	    /* workaround for apparent bug in older gmps */
	    *strchr(s, '\n') = '\0';
	    while ((*s=='#') || (value_read(ck, s) != 0)) {
		fgets(s, 128, stdin);
		/* workaround for apparent bug in older gmps */
		*strchr(s, '\n') = '\0';
	    }
	    C = Universe_Polyhedron(0);
	    EP = barvinok_enumerate_union(A, C, options->MaxRays);
	    value_set_double(cb, compute_evalue(EP, &ck)+.25);
	    if (value_ne(cb, ck))
		return -1;
	    Domain_Free(C);
	    value_clear(cb);
	    value_clear(ck);
	    evalue_free(EP);
	    break;
	}
	case 11: {
	    isl_space *dim;
	    isl_basic_set *bset;
	    isl_pw_qpolynomial *expected, *computed;
	    unsigned nparam;

	    expected = isl_pw_qpolynomial_read_from_file(ctx, stdin);
	    nparam = isl_pw_qpolynomial_dim(expected, isl_dim_param);
	    dim = isl_space_set_alloc(ctx, nparam, A->Dimension - nparam);
	    bset = isl_basic_set_new_from_polylib(A, dim);
	    computed = isl_basic_set_lattice_width(bset);
	    computed = isl_pw_qpolynomial_sub(computed, expected);
	    if (!isl_pw_qpolynomial_is_zero(computed))
		return -1;
	    isl_pw_qpolynomial_free(computed);
	    break;
	}
	case 12: {
	    Vector *sample;
	    int has_sample;
	    fgets(s, 128, stdin);
	    sscanf(s, "%d", &has_sample);

	    sample = Polyhedron_Sample(A, options);
	    if (!sample && has_sample)
		return -1;
	    if (sample && !has_sample)
		return -1;
	    if (sample && !in_domain(A, sample->p))
		return -1;
	    Vector_Free(sample);
	}
	}
	Domain_Free(A);
    }
    for (i = 0; i < nbVec; ++i) {
	int ok;
	Vector *V = Vector_Read();
	Matrix *M = Matrix_Alloc(V->Size, V->Size);
	Vector_Copy(V->p, M->p[0], V->Size);
	ok = unimodular_complete(M, 1);
	assert(ok);
	Matrix_Print(stdout, P_VALUE_FMT, M);
	Matrix_Free(M);
	Vector_Free(V);
    }
    for (i = 0; i < nbMat; ++i) {
	Matrix *U, *V, *S;
	Matrix *M = Matrix_Read();
	Smith(M, &U, &V, &S);
	Matrix_Print(stdout, P_VALUE_FMT, U);
	Matrix_Print(stdout, P_VALUE_FMT, V);
	Matrix_Print(stdout, P_VALUE_FMT, S);
	Matrix_Free(M);
	Matrix_Free(U);
	Matrix_Free(V);
	Matrix_Free(S);
    }

    isl_ctx_free(ctx);
    return 0;
}