From 5fd2122c60998e543a800dbe096498d930063cf2 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Fri, 18 Apr 2008 21:45:34 +0200 Subject: [PATCH] binomial.c: extract binomial and factorial from euler.cc --- Makefile.am | 2 ++ binomial.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ binomial.h | 12 +++++++++++ euler.cc | 64 +-------------------------------------------------------- 4 files changed, 83 insertions(+), 63 deletions(-) create mode 100644 binomial.c create mode 100644 binomial.h diff --git a/Makefile.am b/Makefile.am index 640b270..6fa24d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -93,6 +93,8 @@ libbarvinok_core_la_SOURCES = \ bernoulli.h \ bfcounter.cc \ bfcounter.h \ + binomial.c \ + binomial.h \ conversion.cc \ conversion.h \ counter.cc \ diff --git a/binomial.c b/binomial.c new file mode 100644 index 0000000..2a4467f --- /dev/null +++ b/binomial.c @@ -0,0 +1,68 @@ +#include "binomial.h" + +struct binom { + Vector **binom; + unsigned size; + unsigned n; +}; +static struct binom binom; + +Value *binomial(unsigned n, unsigned k) +{ + int i, j; + + if (n < binom.n) + return &binom.binom[n]->p[k]; + + if (n >= binom.size) { + int size = 3*(n + 5)/2; + + binom.binom = (Vector **)realloc(binom.binom, size*sizeof(Vector *)); + binom.size = size; + } + for (i = binom.n; i <= n; ++i) { + binom.binom[i] = Vector_Alloc(i+1); + if (!i) + value_set_si(binom.binom[0]->p[0], 1); + else { + value_set_si(binom.binom[i]->p[0], 1); + value_set_si(binom.binom[i]->p[i], 1); + for (j = 1; j < i; ++j) + value_addto(binom.binom[i]->p[j], + binom.binom[i-1]->p[j-1], binom.binom[i-1]->p[j]); + } + } + binom.n = n+1; + return &binom.binom[n]->p[k]; +} + +struct fact { + Value *fact; + unsigned size; + unsigned n; +}; +static struct fact fact; + +Value *factorial(unsigned n) +{ + int i; + + if (n < fact.n) + return &fact.fact[n]; + + if (n >= fact.size) { + int size = 3*(n + 5)/2; + + fact.fact = (Value *)realloc(fact.fact, size*sizeof(Value)); + fact.size = size; + } + for (i = fact.n; i <= n; ++i) { + value_init(fact.fact[i]); + if (!i) + value_set_si(fact.fact[0], 1); + else + mpz_mul_ui(fact.fact[i], fact.fact[i-1], i); + } + fact.n = n+1; + return &fact.fact[n]; +} diff --git a/binomial.h b/binomial.h new file mode 100644 index 0000000..d93c0bb --- /dev/null +++ b/binomial.h @@ -0,0 +1,12 @@ +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +Value *binomial(unsigned n, unsigned k); +Value *factorial(unsigned n); + +#if defined(__cplusplus) +} +#endif diff --git a/euler.cc b/euler.cc index 1f4f602..81874fb 100644 --- a/euler.cc +++ b/euler.cc @@ -7,6 +7,7 @@ #include #include #include "bernoulli.h" +#include "binomial.h" #include "conversion.h" #include "decomposer.h" #include "euler.h" @@ -36,69 +37,6 @@ static unsigned total_degree(const evalue *e, unsigned nvar) return max_degree; } -struct fact { - Value *fact; - unsigned size; - unsigned n; -}; -struct fact fact; - -Value *factorial(unsigned n) -{ - if (n < fact.n) - return &fact.fact[n]; - - if (n >= fact.size) { - int size = 3*(n + 5)/2; - - fact.fact = (Value *)realloc(fact.fact, size*sizeof(Value)); - fact.size = size; - } - for (int i = fact.n; i <= n; ++i) { - value_init(fact.fact[i]); - if (!i) - value_set_si(fact.fact[0], 1); - else - mpz_mul_ui(fact.fact[i], fact.fact[i-1], i); - } - fact.n = n+1; - return &fact.fact[n]; -} - -struct binom { - Vector **binom; - unsigned size; - unsigned n; -}; -struct binom binom; - -Value *binomial(unsigned n, unsigned k) -{ - if (n < binom.n) - return &binom.binom[n]->p[k]; - - if (n >= binom.size) { - int size = 3*(n + 5)/2; - - binom.binom = (Vector **)realloc(binom.binom, size*sizeof(Vector *)); - binom.size = size; - } - for (int i = binom.n; i <= n; ++i) { - binom.binom[i] = Vector_Alloc(i+1); - if (!i) - value_set_si(binom.binom[0]->p[0], 1); - else { - value_set_si(binom.binom[i]->p[0], 1); - value_set_si(binom.binom[i]->p[i], 1); - for (int j = 1; j < i; ++j) - value_addto(binom.binom[i]->p[j], - binom.binom[i-1]->p[j-1], binom.binom[i-1]->p[j]); - } - } - binom.n = n+1; - return &binom.binom[n]->p[k]; -} - /* * Computes the coefficients of * -- 2.11.4.GIT