Exemple #1
0
static void print_normal_form(offset_table_t *table, polynomial_t *p) {
  poly_buffer_t aux;
  offset_desc_t *d;
  uint32_t i, n;
  thvar_t x;
  int32_t j;

  init_poly_buffer(&aux);
  n = p->nterms;
  for (i=0; i<n; i++) {
    if (p->mono[i].var == const_idx) {
      assert(i == 0);
      poly_buffer_add_const(&aux, &p->mono[i].coeff);
    } else {
      x = p->mono[i].var;
      assert(0 <= x && x < table->var2offset_var.size);
      j = table->var2offset_var.data[x];
      assert(0 < j && j < table->nvars);

      d = table->desc + j;
      poly_buffer_addmul_monomial(&aux, const_idx, &p->mono[i].coeff, &d->offset);
      if (d->root > 0) {
	poly_buffer_add_monomial(&aux, d->root, &p->mono[i].coeff);
      }
    }
  }

  normalize_poly_buffer(&aux);
  show_poly_buffer(&aux);
  delete_poly_buffer(&aux);
}
Exemple #2
0
static void check_equality(test_bench_t *bench, int32_t x, int32_t y, ivector_t *expl) {
  substitution_t *subst;
  poly_buffer_t bx;
  poly_buffer_t by;

  assert(0 <= x && x < bench->ptable->npolys &&
	 0 <= y && y < bench->ptable->npolys &&
	 x != y);

  subst = subst_from_explanation(bench, expl);
  if (expl->size > 0) {
    printf("BUG: invalid explanation\n");
    fflush(stdout);
    exit(1);
  }

  // apply subst to x: result in bx
  init_poly_buffer(&bx);
  subst_poly_idx(subst, bench->ptable, &bx, x);

  // subst to y: result in by
  init_poly_buffer(&by);
  subst_poly_idx(subst, bench->ptable, &by, y);

  // check that the normal forms are the same
  if (! equal_poly_buffers(&bx, &by)) {
    printf("Explanation for x%"PRId32" == x%"PRId32":\n", x, y);
    print_poly_def(bench->ptable, x);
    print_poly_def(bench->ptable, y);
    print_explanation(bench, expl);
    printf("BUG: explantion does not imply x%"PRId32" == x%"PRId32"\n", x, y);
    printf("  subst for x%"PRId32" --> ", x);
    print_buffer(&bx);
    printf("\n");
    printf("  subst for x%"PRId32" --> ", y);
    print_buffer(&by);
    printf("\n");

    fflush(stdout);
    exit(1);
  }

  delete_poly_buffer(&bx);
  delete_poly_buffer(&by);
  free_substitution(subst);
}
Exemple #3
0
static void build_polys(void) {
  poly_buffer_t aux;
  int32_t a[4];
  int32_t x[4];

  init_poly_buffer(&aux);
  a[0] = 0;
  poly[0] = make_poly(&aux, a, NULL, 1);  // 0

  a[0] = 1;
  poly[1] = make_poly(&aux, a, NULL, 1); // 1

  a[0] = -100;
  poly[2] = make_poly(&aux, a, NULL, 1); // -100

  poly[3] = NULL;
  poly[4] = NULL;
  poly[5] = NULL;

  a[0] = 0;
  a[1] = 1;  x[1] = 3;
  a[2] = -1; x[2] = 4;
  poly[6] = make_poly(&aux, a, x, 3); // x3 - x4

  a[0] = 0;
  a[1] = 1;  x[1] = 5;
  a[2] = -1; x[2] = 4;
  poly[7] = make_poly(&aux, a, x, 3); // x5 - x4

  a[0] = 2;
  a[1] = 1; x[1] = 3;
  a[2] = 1; x[2] = 4;
  poly[8] = make_poly(&aux, a, x, 3); // 2 + x3 + x4

  a[0] = 0;
  a[1] = 1; x[1] = 3;
  a[2] = 2; x[2] = 5;
  poly[9] = make_poly(&aux, a, x, 3); // x3 + 2 * x5

  delete_poly_buffer(&aux);
}
Exemple #4
0
/*
 * Test sum and difference of triples
 */
static void test_add_diff(dl_vartable_t *table) {
  uint32_t i, j, n;
  dl_triple_t test;
  dl_triple_t test2;
  poly_buffer_t buffer;

  init_poly_buffer(&buffer);
  q_init(&test.constant);
  q_init(&test2.constant);

  n = table->nvars;
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) {
      printf("Testing sum: var!%"PRIu32" + var!%"PRIu32":\n", i, j);
      printf("--> var!%"PRIu32" : ", i);
      print_dl_triple(dl_var_triple(table, i));
      printf("\n");
      printf("--> var!%"PRIu32" : ", j);
      print_dl_triple(dl_var_triple(table, j));
      printf("\n");
      test_sum_buffer(table, &buffer, i, j, &test2);
      test_sum(table, i, j, &test);
      printf("\n");

      printf("Testing diff: var!%"PRIu32" - var!%"PRIu32":\n", i, j);
      printf("--> var!%"PRIu32" : ", i);
      print_dl_triple(dl_var_triple(table, i));
      printf("\n");
      printf("--> var!%"PRIu32" : ", j);
      print_dl_triple(dl_var_triple(table, j));
      printf("\n");
      test_diff_buffer(table, &buffer, i, j, &test2);
      test_diff(table, i, j, &test);
      printf("\n");
    }
  }

  q_clear(&test.constant);
  q_clear(&test2.constant);
  delete_poly_buffer(&buffer);
}
Exemple #5
0
/*
 * Fill table with random polys:
 * - nvars = number of initial variables (must be positive)
 *   poly[1 ... nvars] will all be variables
 * - n = total number of polynomials to build (including nvars)
 */
static void build_poly_table(poly_table_t *table, uint32_t nvars, uint32_t n) {
  poly_buffer_t buffer;
  polynomial_t *p;
  uint32_t i;

  for (i=0; i<nvars; i++) {
    add_poly(table, NULL);
  }

  init_poly_buffer(&buffer);

  while (i<n) {
    p = NULL;
    if (random_index(10) != 0) {
      p = random_poly(&buffer, table);
    }
    add_poly(table, p);
    i ++;
  }

  delete_poly_buffer(&buffer);
}
Exemple #6
0
static void init_test_bench(test_bench_t *bench, poly_table_t *ptable) {
  uint32_t nv, np;

  np = ptable->npolys;
  nv = ptable->nvars;

  bench->base_level = 0;
  bench->decision_level = 0;
  bench->conflict = false;
  bench->conflict_eq = -1;
  bench->mngr_conflict = false;
  bench->show_details = false;

  bench->ptable = ptable;
  init_substitution(&bench->subst, np);
  init_subst_queue(&bench->squeue, nv);
  init_active_polys(&bench->act, np);
  init_op_stack(&bench->stack);
  init_equality_queue(&bench->equeue, np);

  init_offset_manager(&bench->manager, &bench->equeue, notify_equality);

  init_poly_buffer(&bench->buffer);
}
/*
 * Tests
 */
int main(void) {
  rational_t alpha;
  uint32_t n;
  int32_t v;

  init_rationals();
  q_init(&alpha);
  init_poly_buffer(&buffer);

  printf("--- Init ---\n");
  print_poly_buffer_details(&buffer);

  reset_poly_buffer(&buffer);
  printf("--- Reset ---\n");
  print_poly_buffer_details(&buffer);

  normalize_poly_buffer(&buffer);
  printf("--- Normalize ---\n");
  print_poly_buffer_details(&buffer);

  reset_poly_buffer(&buffer);
  printf("--- Reset ---\n");
  print_poly_buffer_details(&buffer);

  random_rational(&alpha);
  poly_buffer_add_monomial(&buffer, 2, &alpha);
  printf("--- Add monomial x!2 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_add_const(&buffer, &alpha);
  printf("--- Add constant alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_add_monomial(&buffer, 1, &alpha);
  printf("--- Add monomial x!1 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_sub_monomial(&buffer, 2, &alpha);
  printf("--- Sub monomial x!2 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  normalize_poly_buffer(&buffer);
  printf("--- Normalize ---\n");
  print_poly_buffer_details(&buffer);

  reset_poly_buffer(&buffer);
  printf("--- Reset ---\n");
  print_poly_buffer_details(&buffer);

  random_rational(&alpha);
  poly_buffer_sub_monomial(&buffer, 2, &alpha);
  printf("--- Sub monomial x!2 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_sub_const(&buffer, &alpha);
  printf("--- Sub constant alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_add_monomial(&buffer, 1, &alpha);
  printf("--- Add monomial x!1 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_sub_monomial(&buffer, 2, &alpha);
  printf("--- Sub monomial x!2 * alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_add_const(&buffer, &alpha);
  printf("--- Add constant alpha ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_add_var(&buffer, 3);
  printf("--- Add var x!3 ---\n");
  print_poly_buffer_details(&buffer);

  poly_buffer_sub_var(&buffer, 3);
  printf("--- Sub var x!3 ---\n");
  print_poly_buffer_details(&buffer);

  normalize_poly_buffer(&buffer);
  printf("--- Normalize ---\n");
  print_poly_buffer_details(&buffer);

  reset_poly_buffer(&buffer);
  for (n=0; n<400; n++) {
    v = random() % 200;
    random_rational(&alpha);
    poly_buffer_add_monomial(&buffer, v, &alpha);
  }
  printf("--- 400 random monomials ---\n");
  print_poly_buffer_details(&buffer);

  normalize_poly_buffer(&buffer);
  printf("--- Normalize ---\n");
  print_poly_buffer_details(&buffer);


  delete_poly_buffer(&buffer);
  q_clear(&alpha);
  cleanup_rationals();

  return 0;
}