Пример #1
0
void testGeometry() {
    cout << "running testGeometry" << endl;
    cout << "Creating Variables" << endl;

    // Variable Declarations
    // Points
    const Point a = Point(0, 0), b = Point(100, 100), c = Point(0, 100);
    const Point e = Point(200, 0), d = Point(100, 0), f = Point(100, 200);
    // Poly's
    const cnt tri {a, e, b};  // non-equal sides
    const cnt square {a, c, b, d};
    const cnt rect {a, c*2, f, d};
    const cnt bigSquare {a*2-b, c*2-b, b*2-b, d*2-b};
    // Fp's
    const Fp testFp1 = Fp({bigSquare, square});
    const Fp testFp2 = Fp({bigSquare});  // not a valid Fp
    const Fp testFp3 = Fp({square});  // not a valid Fp

    cout << "Creating Vectors" << endl;

    // Vectors
    // Poly's
    const vector<cnt> vpoly1 {tri, square, bigSquare};
    const vector<cnt> vpoly2 {tri, square};
    const vector<cnt> vpoly3 {square, bigSquare};
    const vector<cnt> vpoly4 {tri, bigSquare};
    const vector<cnt> vpoly5 {tri};
    const vector<cnt> vpoly6 {square};
    const vector<cnt> vpoly7 {bigSquare};
    // Fp's
    const vector<Fp> vfp1 {testFp1, testFp2, testFp3};
    const vector<Fp> vfp2 {testFp1, testFp2};
    const vector<Fp> vfp3 {testFp2, testFp3};
    const vector<Fp> vfp4 {testFp1, testFp3};
    const vector<Fp> vfp5 {testFp1};
    const vector<Fp> vfp6 {testFp2};
    const vector<Fp> vfp7 {testFp3};

    cout << "Test Methods..." << endl;

    // Test Methods
    // Centroid
    Point tric = centroid(tri);
    Point squarec = centroid(square);
    Point bigsc = centroid(bigSquare);
    cout << "Triangle" << tostr(tri) << tostr(tric) << endl;
    cout << "Square" << tostr(square) << tostr(squarec) << endl;
    cout << "Big Square" << tostr(bigSquare) << tostr(bigsc) << endl;
    cout << "Square & BigSquare Centroid: " << tostr(centroid(vpoly3)) << endl;

    // Dist & Angs
    vector<double> dts = dists(tri);
    vector<double> ans = angles(tri);
    cout << "Triangle lengths: " << vtostr(dts) << endl;
    cout << "Triangle angles: " << vtostr(ans) << endl;
    cout << "Unit Angle: " << "Origin - " << tostr(a) << "; Point - " << tostr(b) << "; Ang - " << angle(a,b) << endl;

    // All same length
    cout << "Triangle all same length? " << allSameLength(tri, (double)0.0) << endl;
    cout << "Square all same length? " << allSameLength(square, (double)0.0) << endl;

    // isPoly
    bool test1 = isPoly(tri, 3, false, false, 0, 0);  // True
    bool test2 = isPoly(tri, 3, true, true, 0, 0);  // False
    bool test3 = isPoly(tri, 4, false, false, 0, 0);  // False
    bool test4 = isRectangle(rect, false, 0, 0);  // True
    bool test5 = isRectangle(rect, true, 0, 0);  // False
    bool test6 = isSquare(square, 0, 0);  // True
    cout << test1 << test2 << test3 << test4 << test5 << test6 << endl;
}
Пример #2
0
/*
 * adds a new row (constraint)
 */
BP_RETCODE bp_put(
   BinaryProgram* bp,          /**< binary program */
   TYPE* coefs,                /**< array of coefficients */
   TYPE rhs                    /**< right hand side */
)
{
   assert(bp_is_valid(bp));

   /* abort if more rows (constraints) wants to be added than announced */
   if (bp->m == bp->size)
   {
      fprintf(stderr, "BinaryProgram overflow size=%d\n", bp->m);
      abort();
   }
   assert(bp->m < bp->size);

   /* compute maximal and minimal activity */
   TYPE max = 0;
   TYPE min = 0;
#ifdef MORE_DEBUG
   fprintf(stdout, "Add consttraint: ");
   char* str;
   str = allocate(MAX_STR_LEN, sizeof(*str));
#endif
   for (int j = 0; j < bp->n; j++)
   {
      if (coefs[j] > 0)
      {
         max += coefs[j];
         if (max < coefs[j])
         {
            printf("Reading detected possible overflow.\n");
            return BP_ERROR;
         }
      }
      else
      {
         if (coefs[j] < 0)
         {
            min += coefs[j];
            if (min > coefs[j])
            {
               printf("Reading detected possible underflow.\n");
               return BP_ERROR;
            }
         }
      }
#ifdef MORE_DEBUG
      if (j > 0)
         fprintf(stdout, "+ ");
      vtostr(str, coefs[j]);
      fprintf(stdout, "%s x_%i ", str, j+1);
#endif
   }
#ifdef MORE_DEBUG
   vtostr(str, rhs);
   fprintf(stdout, "<= %s\n", str);
   deallocate(str);
#endif

   /* constraint is redundant, when maximal activity is smaller or equal to rhight-hand side
    * => constraint is not added
    */
   if (max <= rhs)
   {
#ifdef MORE_DEBUG
      printf("Constraint is redundant.\n");
#endif
      bp->redundant++;
      return BP_OKAY;
   }

   /* constraint is infeasible, when minimal activity is greater than rhight-hand side
    * => return infeasible
    */
   if (min > rhs)
      return BP_INFEASIBLE;

   /* check for redundancy (multiple of other constraint)
    * => constraint is not added, when it is redundant
    */
   for (int i = 0; i < bp->m; i++)
   {
      if ((rhs == 0 && bp->rhs[i] == 0) || (rhs != 0 && bp->rhs[i] != 0))
      {
         double quotposmax = 0;
         double quotposmin = 0;
         double quotnegmax = 0;
         double quotnegmin = 0;
         double quot = 0;
         int dom = 0;

         int j = 0;
         for (; j < bp->n; j++)
         {
            /* nothing to do, when both coefficients are zero */
            if (coefs[j] == 0 && bp->coefs[j] == 0)
               continue;
            /* break, when one coefficient is zero and the other is negative */
            else if ((coefs[j] == 0 && bp->coefs[j] < 0) || (coefs[j] < 0 && bp->coefs[j] == 0))
               break;
            /* only stored constraint can dominates new one, when coefficient of new one is zero */
            else if (coefs[j] == 0 && bp->coefs[j] > 0)
            {
               if (dom == -1 || quot != 0)
                  break;
               dom = 1;
            }
            /* only new constraint can dominates stored one, when coefficient of stored one is zero */
            else if (coefs[j] > 0 && bp->coefs[j] == 0)
            {
               if (dom == 1 || quot != 0)
                  break;
               dom = -1;
            }
            else
            {
               assert(coefs[j] != 0);
               assert(bp->coefs[j] != 0);
               double q = coefs[j] / bp->coefs[i*bp->n+j];

               /* compute quotient for positive coefficients */
               if (coefs[j] > 0 && bp->coefs[i*bp->n+j] > 0)
               {
                  if (quot != 0)
                     break;
                  if (quotposmax == 0)
                  {
                     assert(quotposmin == 0);
                     quotposmax = q;
                     quotposmin = q;
                  }
                  else
                  {
                     quotposmax = MAX(quotposmax, q);
                     quotposmin = MIN(quotposmin, q);
                  }
                  if (quotnegmax != 0 && ((dom == 1 && quotposmax > quotnegmin) || (dom == -1 && quotposmin > quotnegmax)))
                     break;
               }
               /* compute quotient for negative coefficients */
               else if (coefs[j] < 0 && bp->coefs[i*bp->n+j] < 0)
               {
                  if (quot != 0)
                     break;
                  if (quotnegmax == 0)
                  {
                     assert(quotnegmin == 0);
                     quotnegmax = q;
                     quotnegmin = q;
                  }
                  else
                  {
                     quotnegmax = MAX(quotnegmax, q);
                     quotnegmin = MIN(quotnegmin, q);
                  }
                  if (quotposmax != 0 && ((dom == 1 && quotposmax > quotnegmin) || (dom == -1 && quotposmin > quotnegmax)))
                     break;
               }
               /* compute quotient for coefficients with different sign */
               else
               {
                  if (quotnegmax != 0 || quotposmax != 0 || dom != 0)
                     break;
                  if (quot == 0)
                     quot = q;
                  else if (abs(quot) - abs(q) < EPSILON )
                     break;
               }
               if (quotposmax == 0 && quotnegmax == 0)
                  continue;
               if (quotposmax != 0 && quotnegmax != 0 && (quotposmax > quotnegmin || quotposmin < quotnegmax))
                  break;
               if (quotposmax != 0 || quotnegmax != 0)
               {
                  /* check whether stored constraint dominates new one */
                  if ((quotposmax > 0 && rhs >= bp->rhs[i] * quotposmax) || (quotposmax == 0 && rhs >= bp->rhs[i] * quotnegmin))
                  {
                     if (dom == -1)
                        break;
                     dom = 1;
                  }
                  /* check whether new constraint dominates stored one */
                  else if ((quotposmin > 0 && rhs <= bp->rhs[i] * quotposmin) || (quotposmin == 0 && rhs <= bp->rhs[i] * quotnegmax))
                  {
                     if (dom == 1)
                        break;
                     dom = -1;
                  }
                  /* none of the two constraints is dominating */
                  else
                     break;
               }
               /* should be dead code */
               if (dom != 0 && quot != 0)
                  break;
            }
         }
         if (j == bp->n)
         {
            assert( quotposmax != 0 || quotnegmax != 0 || quot != 0);
            if (quot != 0)
            {
               if (rhs / quot + bp->rhs[i] < -EPSILON && dom == 0)
                  return BP_INFEASIBLE;
            }
            else
            {
               if (quotposmax == quotposmin && quotnegmax == quotnegmin)
               {
                  assert(quotposmax != 0 || quotnegmax != 0);
                  assert(quotposmax == quotnegmax || quotposmax == 0 || quotnegmax == 0);
                  if (dom == 0)
                     bp->rhs[i] = MIN(bp->rhs[i], (TYPE)(rhs / quotposmax));
                  else if (dom == -1)
                  {
                     if ((quotposmin > 0 && rhs > bp->rhs[i] * quotposmin) || (quotposmin == 0 && rhs > bp->rhs[i] * quotnegmax))
                        continue;
                     for (int k= 0; k < bp->n; k++)
                        bp->coefs[i*bp->n+k] = coefs[k];
                     bp->rhs[i] = rhs;
                  }
                  else if ((quotposmax > 0 && rhs < bp->rhs[i] * quotposmax) || (quotposmax == 0 && rhs < bp->rhs[i] * quotnegmin))
                     continue;
               }
               else
               {
                  assert(dom != 0);
                  if (dom == -1)
                  {
                     if ((quotposmin > 0 && rhs > bp->rhs[i] * quotposmin) || (quotposmin == 0 && rhs > bp->rhs[i] * quotnegmax))
                        continue;
                     for (int k= 0; k < bp->n; k++)
                        bp->coefs[i*bp->n+k] = coefs[k];
                     bp->rhs[i] = rhs;
                  }
                  else if ((quotposmax > 0 && rhs < bp->rhs[i] * quotposmax) || (quotposmax == 0 && rhs < bp->rhs[i] * quotnegmin))
                     continue;
               }
#ifdef MORE_DEBUG
               printf("redundant constraint.\n");
#endif
               bp->redundant++;
               return BP_OKAY;
            }
         }
      }
   }

   /* add row (constraint) to binary program */
   for (int i = 0; i < bp->n; i++)
      bp->coefs[bp->m * bp->n + i] = coefs[i];
   bp->rhs[bp->m] = rhs;
   bp->m++;

   assert(bp_is_valid(bp));
   return BP_OKAY;
}
Пример #3
0
Файл: conf.c Проект: rvs/libtc
static char *
vtostr(char *name, void *_ts)
{
    tcconf_section_t *ts = _ts;
    tclist_item_t *li = NULL;
    tcc_value *tv;
    tcc_entry *te;
    int l = 0;
    char *s = NULL;
    char *p = s;
    int sp = 0;

#define ext(n) do {				\
    int o = p - s;				\
    s = realloc(s, l += n);			\
    p = s + o;					\
} while(0)

    tcref(ts);

    if(!(te = getvalue(ts->sec, name, &ts)))
	return NULL;

    while((tv = tclist_next(te->value.values, &li))){
	union {
	    uint64_t i;
	    double d;
	    char *s;
	} v;
	int sl;

	cp_val(ts, tv, tv->type | TCC_LONG, &v);

	if(sp)
	    *p++ = ' ';

	switch(tv->type & TCC_TYPEMASK){
	case TCC_INTEGER:
	    ext(22);
	    p += snprintf(p, 22, "%lli", v.i);
	    break;
	case TCC_FLOAT:
	    ext(40);
	    p += snprintf(p, 40, "%lf", v.d);
	    break;
	case TCC_REF:
	    v.s = vtostr(tv->value.string, ts);
	case TCC_STRING:
	    sl = strlen(v.s);
	    ext(sl + 1);
	    strcpy(p, v.s);
	    free(v.s);
	    p += sl;
	    break;
	}
	sp = 1;
    }

    *p = 0;

#undef ext
    tcfree(ts);
    return s;
}