Example #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);
}
Example #2
0
/*
 * Random polynomial:
 * - use variables defined in table
 */
static polynomial_t *random_poly(poly_buffer_t *b, poly_table_t *table) {
  rational_t q;
  uint32_t i, n;
  int32_t x, a;

  q_init(&q);
  reset_poly_buffer(b);

  a = random_constant();
  q_set32(&q, a);
  poly_buffer_add_const(b, &q);

  n = random_nterms();
  for (i=0; i<n; i++) {
    a = random_coeff();
    x = random_var(table);
    assert(x > 0);

    q_set32(&q, a);
    poly_buffer_add_monomial(b, x, &q);
  }

  normalize_poly_buffer(b);
  q_clear(&q);

  return poly_buffer_get_poly(b);
}
Example #3
0
/*
 * Apply s to a monomial array mono
 * - store the result in buffer b
 */
static void subst_poly(substitution_t *s, poly_buffer_t *b, monomial_t *mono) {
  offset_pair_t aux;
  rational_t q;
  int32_t x;

  q_init(&q);
  reset_poly_buffer(b);

  x = mono->var;
  while (x != max_idx) {
    if (x == const_idx) {
      poly_buffer_add_const(b, &mono->coeff);
    } else {
      aux.var = x;
      aux.delta = 0;
      subst_var(s, &aux); // aux constains S[x] = y + delta

      // add a * y + a * delta to b
      if (aux.var > 0) {
	poly_buffer_add_monomial(b, aux.var, &mono->coeff);
      }
      q_set32(&q, aux.delta);
      poly_buffer_addmul_monomial(b, const_idx, &mono->coeff, &q);
    }
    mono ++;
    x = mono->var;
  }

  normalize_poly_buffer(b);
  q_clear(&q);
}
Example #4
0
/*
 * Build a polynomial:
 * - a = coefficient array
 * - x = variable array
 * - n = number of coefficients
 *
 * a[0] is the constant (x[0] is ignored)
 */
static polynomial_t *make_poly(poly_buffer_t *b, int32_t *a, int32_t *x, uint32_t n) {
  rational_t q;
  uint32_t i;

  assert(n > 0);

  q_init(&q);
  reset_poly_buffer(b);

  q_set32(&q, a[0]);
  poly_buffer_add_const(b, &q);

  for (i=1; i<n; i++) {
    q_set32(&q, a[i]);
    poly_buffer_add_monomial(b, x[i], &q);
  }

  normalize_poly_buffer(b);
  q_clear(&q);

  return poly_buffer_get_poly(b);
}
Example #5
0
/*
 * 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;
}