arb/bernoulli/cache_compute.c

67 lines
1.6 KiB
C
Raw Normal View History

/*
2012-12-19 14:04:00 +01:00
Copyright (C) 2012 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
2012-12-19 14:04:00 +01:00
#include "bernoulli.h"
TLS_PREFIX slong bernoulli_cache_num = 0;
2012-12-19 14:04:00 +01:00
2013-04-27 21:07:57 +02:00
TLS_PREFIX fmpq * bernoulli_cache = NULL;
2012-12-19 14:04:00 +01:00
2013-07-30 13:06:08 +02:00
void
bernoulli_cleanup(void)
{
slong i;
2013-07-30 13:06:08 +02:00
for (i = 0; i < bernoulli_cache_num; i++)
fmpq_clear(bernoulli_cache + i);
flint_free(bernoulli_cache);
bernoulli_cache = NULL;
2013-07-30 13:06:08 +02:00
bernoulli_cache_num = 0;
}
2012-12-19 14:04:00 +01:00
void
bernoulli_cache_compute(slong n)
2012-12-19 14:04:00 +01:00
{
if (bernoulli_cache_num < n)
{
slong i, new_num;
2012-12-19 14:04:00 +01:00
bernoulli_rev_t iter;
2013-07-30 13:06:08 +02:00
if (bernoulli_cache_num == 0)
{
flint_register_cleanup_function(bernoulli_cleanup);
}
new_num = FLINT_MAX(bernoulli_cache_num + 128, n);
2012-12-19 14:04:00 +01:00
bernoulli_cache = flint_realloc(bernoulli_cache, new_num * sizeof(fmpq));
2012-12-19 16:56:12 +01:00
for (i = bernoulli_cache_num; i < new_num; i++)
2012-12-19 14:04:00 +01:00
fmpq_init(bernoulli_cache + i);
i = new_num - 1;
i -= (i % 2);
bernoulli_rev_init(iter, i);
for ( ; i >= bernoulli_cache_num; i -= 2)
{
2012-12-19 14:04:00 +01:00
bernoulli_rev_next(fmpq_numref(bernoulli_cache + i),
2012-12-19 14:43:08 +01:00
fmpq_denref(bernoulli_cache + i), iter);
}
2012-12-19 14:04:00 +01:00
bernoulli_rev_clear(iter);
if (new_num > 1)
2012-12-19 16:56:12 +01:00
fmpq_set_si(bernoulli_cache + 1, -1, 2);
2012-12-19 14:04:00 +01:00
bernoulli_cache_num = new_num;
}
}