mirror of
https://github.com/vale981/arb
synced 2025-03-05 09:21:38 -05:00
more documentation and test code
This commit is contained in:
parent
32763c5a2e
commit
9a4b57ec91
6 changed files with 244 additions and 31 deletions
|
@ -124,7 +124,7 @@ Computation of single Bernoulli numbers
|
|||
of the Bernoulli number `B_n`.
|
||||
|
||||
This function computes the denominator `d` using von Staudt-Clausen
|
||||
theorem, numerically approximates `B_n` using :func:`bernoulli_fmprb_ui_zeta`,
|
||||
theorem, numerically approximates `B_n` using :func:`arb_bernoulli_ui_zeta`,
|
||||
and then rounds `d B_n` to the correct numerator.
|
||||
If the working precision is insufficient to determine the numerator,
|
||||
the function prints a warning message and retries with increased
|
||||
|
|
|
@ -137,7 +137,7 @@ Memory management
|
|||
Error bounding
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
.. function:: long hypgeom_estimate_terms(const fmpr_t z, int r, long d)
|
||||
.. function:: long hypgeom_estimate_terms(const mag_t z, int r, long d)
|
||||
|
||||
Computes an approximation of the largest `n` such
|
||||
that `|z|^n/(n!)^r = 2^{-d}`, giving a first-order estimate of the
|
||||
|
@ -154,7 +154,7 @@ Error bounding
|
|||
The function aborts if the computed value of `n` is greater
|
||||
than or equal to LONG_MAX / 2.
|
||||
|
||||
.. function:: long hypgeom_bound(fmpr_t error, int r, long C, long D, long K, const fmpr_t TK, const fmpr_t z, long prec)
|
||||
.. function:: long hypgeom_bound(mag_t error, int r, long C, long D, long K, const mag_t TK, const mag_t z, long prec)
|
||||
|
||||
Computes a truncation parameter sufficient to achieve *prec* bits
|
||||
of absolute accuracy, according to the strategy described above.
|
||||
|
@ -179,13 +179,29 @@ Error bounding
|
|||
Summation
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
.. function:: void hypgeom_fmprb_sum(fmprb_t P, fmprb_t Q, const hypgeom_t hyp, const long n, long prec)
|
||||
.. function:: void fmprb_hypgeom_sum(fmprb_t P, fmprb_t Q, const hypgeom_t hyp, const long n, long prec)
|
||||
|
||||
Computes `P, Q` such that `P / Q = \sum_{k=0}^{n-1} T(k)` where `T(k)`
|
||||
is defined by *hyp*,
|
||||
using binary splitting and a working precision of *prec* bits.
|
||||
|
||||
.. function:: void hypgeom_fmprb_infsum(fmprb_t P, fmprb_t Q, hypgeom_t hyp, long tol, long prec)
|
||||
.. function:: void fmprb_hypgeom_infsum(fmprb_t P, fmprb_t Q, hypgeom_t hyp, long tol, long prec)
|
||||
|
||||
Computes `P, Q` such that `P / Q = \sum_{k=0}^{\infty} T(k)` where `T(k)`
|
||||
is defined by *hyp*, using binary splitting and
|
||||
working precision of *prec* bits.
|
||||
The number of terms is chosen automatically to bound the
|
||||
truncation error by at most `2^{-\mathrm{tol}}`.
|
||||
The bound for the truncation error is included in the output
|
||||
as part of *P*.
|
||||
|
||||
.. function:: void arb_hypgeom_sum(arb_t P, arb_t Q, const hypgeom_t hyp, const long n, long prec)
|
||||
|
||||
Computes `P, Q` such that `P / Q = \sum_{k=0}^{n-1} T(k)` where `T(k)`
|
||||
is defined by *hyp*,
|
||||
using binary splitting and a working precision of *prec* bits.
|
||||
|
||||
.. function:: void arb_hypgeom_infsum(arb_t P, arb_t Q, hypgeom_t hyp, long tol, long prec)
|
||||
|
||||
Computes `P, Q` such that `P / Q = \sum_{k=0}^{\infty} T(k)` where `T(k)`
|
||||
is defined by *hyp*, using binary splitting and
|
||||
|
|
|
@ -40,10 +40,24 @@ Memory management
|
|||
|
||||
.. function:: void mag_init_set(mag_t x, const mag_t y)
|
||||
|
||||
Initializes *x* and sets it to the value of *y*.
|
||||
|
||||
.. function:: void mag_swap(mag_t x, mag_t y)
|
||||
|
||||
Swaps *x* and *y* efficiently.
|
||||
|
||||
.. function:: void mag_set(mag_t x, const mag_t y)
|
||||
|
||||
Sets *x* to the value of *y*.
|
||||
|
||||
.. function:: mag_ptr _mag_vec_init(long n)
|
||||
|
||||
Allocates a vector of length *n*. All entries are set to zero.
|
||||
|
||||
.. function:: void _mag_vec_clear(mag_ptr v, long n)
|
||||
|
||||
Clears a vector of length *n*.
|
||||
|
||||
Special values
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
@ -100,25 +114,57 @@ Arithmetic
|
|||
|
||||
.. function:: void mag_mul_2exp_fmpz(mag_t z, const mag_t x, const fmpz_t y)
|
||||
|
||||
Sets `z` to `x \times 2^y`. This operation is exact.
|
||||
Sets *z* to `x \times 2^y`. This operation is exact.
|
||||
|
||||
.. function:: void mag_mul(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets `z` to an upper bound for `xy`.
|
||||
.. function:: void mag_mul_ui(mag_t z, const mag_t x, ulong y)
|
||||
|
||||
.. function:: void mag_mul_fmpz(mag_t z, const mag_t x, const fmpz_t y)
|
||||
|
||||
Sets *z* to an upper bound for `xy`.
|
||||
|
||||
.. function:: void mag_add(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets *z* to an upper bound for `x + y`.
|
||||
|
||||
.. function:: void mag_addmul(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets `z` to an upper bound for `z + xy`.
|
||||
Sets *z* to an upper bound for `z + xy`.
|
||||
|
||||
.. function:: void mag_add_2exp_fmpz(mag_t z, const mag_t x, const fmpz_t e)
|
||||
|
||||
Sets `z` to an upper bound for `x + 2^e`.
|
||||
Sets *z* to an upper bound for `x + 2^e`.
|
||||
|
||||
.. function:: void mag_div(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets `z` to an upper bound for `x / y`.
|
||||
.. function:: void mag_div_ui(mag_t z, const mag_t x, ulong y)
|
||||
|
||||
Fast versions
|
||||
.. function:: void mag_div_fmpz(mag_t z, const mag_t x, const fmpz_t y)
|
||||
|
||||
Sets *z* to an upper bound for `x / y`.
|
||||
|
||||
.. function:: void mag_mul_lower(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
.. function:: void mag_mul_ui_lower(mag_t z, const mag_t x, ulong y)
|
||||
|
||||
.. function:: void mag_mul_fmpz_lower(mag_t z, const mag_t x, const fmpz_t y)
|
||||
|
||||
Sets *z* to a lower bound for `xy`.
|
||||
|
||||
.. function:: void mag_add_lower(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets *z* to a lower bound for `x + y`.
|
||||
|
||||
.. function:: void mag_pow_ui(mag_t z, const mag_t x, ulong e)
|
||||
|
||||
Sets *z* to an upper bound for `x^e`.
|
||||
|
||||
.. function:: void mag_pow_ui_lower(mag_t z, const mag_t x, ulong e)
|
||||
|
||||
Sets *z* to a lower bound for `x^e`.
|
||||
|
||||
Fast, unsafe versions
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
The following methods assume that all inputs are finite and that all exponents
|
||||
|
@ -140,15 +186,15 @@ as they will be overwritten directly (thus leaking memory).
|
|||
|
||||
.. function:: void mag_fast_mul(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets `z` to an upper bound for `xy`.
|
||||
Sets *z* to an upper bound for `xy`.
|
||||
|
||||
.. function:: void mag_fast_addmul(mag_t z, const mag_t x, const mag_t y)
|
||||
|
||||
Sets `z` to an upper bound for `z + xy`.
|
||||
Sets *z* to an upper bound for `z + xy`.
|
||||
|
||||
.. function:: void mag_fast_add_2exp_si(mag_t z, const mag_t x, long e)
|
||||
|
||||
Sets `z` to an upper bound for `x + 2^e`.
|
||||
Sets *z* to an upper bound for `x + 2^e`.
|
||||
|
||||
Input and output
|
||||
-------------------------------------------------------------------------------
|
||||
|
@ -172,15 +218,23 @@ Random generation
|
|||
Conversions
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
.. function:: void mag_set_d(mag_t y, double x)
|
||||
|
||||
.. function:: void mag_set_fmpr(mag_t y, const fmpr_t x)
|
||||
|
||||
.. function:: void mag_set_ui(mag_t y, ulong x)
|
||||
|
||||
.. function:: void mag_set_fmpz(mag_t y, const fmpz_t x)
|
||||
|
||||
Sets *y* to an upper bound for `|x|`.
|
||||
|
||||
.. function:: void mag_set_d_2exp_fmpz(mag_t z, double x, const fmpz_t y)
|
||||
|
||||
.. function:: void mag_set_fmpz_2exp_fmpz(mag_t z, const fmpz_t x, const fmpz_t y)
|
||||
|
||||
Sets *z* to an upper bound for `x \times 2^y`.
|
||||
.. function:: void mag_set_ui_2exp_si(mag_t z, ulong x, long y)
|
||||
|
||||
.. function:: void mag_set_fmpr(mag_t y, const fmpr_t x)
|
||||
|
||||
Sets *y* to an upper bound for *x*.
|
||||
Sets *z* to an upper bound for `|x| \times 2^y`.
|
||||
|
||||
.. function:: void mag_get_fmpr(fmpr_t y, const mag_t x)
|
||||
|
||||
|
@ -190,3 +244,34 @@ Conversions
|
|||
|
||||
Sets *y* exactly to *x*. Assumes that no overflow occurs.
|
||||
|
||||
.. function:: void mag_set_ui_lower(mag_t z, ulong x)
|
||||
|
||||
.. function:: void mag_set_fmpz_lower(mag_t z, const fmpz_t x)
|
||||
|
||||
Sets *y* to a lower bound for `|x|`.
|
||||
|
||||
.. function:: void mag_set_fmpz_2exp_fmpz_lower(mag_t z, const fmpz_t x, const fmpz_t y)
|
||||
|
||||
Sets *z* to a lower bound for `|x| \times 2^y`.
|
||||
|
||||
Special functions
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
.. function:: void mag_log1p(mag_t z, const mag_t x)
|
||||
|
||||
Sets *z* to an upper bound for `\log(1+x)`. The bound is computed
|
||||
accurately for small *x*.
|
||||
|
||||
.. function:: void mag_fac_ui(mag_t z, ulong n)
|
||||
|
||||
Sets *z* to an upper bound for `n!`.
|
||||
|
||||
.. function:: void mag_rfac_ui(mag_t z, ulong n)
|
||||
|
||||
Sets *z* to an upper bound for `1/n!`.
|
||||
|
||||
.. function:: void mag_bernoulli_div_fac_ui(mag_t z, ulong n)
|
||||
|
||||
Sets *z* to an upper bound for `|B_n| / n!` where `B_n` denotes
|
||||
a Bernoulli number.
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ a correct error bound for the numerical approximation of `p(n)`.
|
|||
Optionally, hardware double arithmetic can be used for low-precision
|
||||
terms. This gives a significant speedup for small (e.g. `n < 10^6`).
|
||||
|
||||
.. function:: void partitions_rademacher_bound(fmpr_t b, const fmpz_t n, ulong N)
|
||||
.. function:: void partitions_rademacher_bound(arf_t b, const fmpz_t n, ulong N)
|
||||
|
||||
Sets `b` to an upper bound for
|
||||
|
||||
|
@ -33,7 +33,7 @@ terms. This gives a significant speedup for small (e.g. `n < 10^6`).
|
|||
Hardy-Ramanujan-Rademacher formula when the series is taken up
|
||||
to the term `t(n,N)` inclusive.
|
||||
|
||||
.. function:: partitions_hrr_sum_fmprb(fmprb_t x, const fmpz_t n, long N0, long N, int use_doubles)
|
||||
.. function:: partitions_hrr_sum_arb(arb_t x, const fmpz_t n, long N0, long N, int use_doubles)
|
||||
|
||||
Evaluates the partial sum `\sum_{k=N_0}^N t(n,k)` of the
|
||||
Hardy-Ramanujan-Rademacher series.
|
||||
|
@ -57,7 +57,7 @@ terms. This gives a significant speedup for small (e.g. `n < 10^6`).
|
|||
has been selected with :func:`flint_set_num_threads()`, the computation
|
||||
time will be reduced by using two threads.
|
||||
|
||||
See :func:`partitions_hrr_sum_fmprb` for an explanation of the
|
||||
See :func:`partitions_hrr_sum_arb` for an explanation of the
|
||||
*use_doubles* option.
|
||||
|
||||
.. function:: void partitions_fmpz_ui(fmpz_t p, ulong n)
|
||||
|
@ -71,5 +71,5 @@ terms. This gives a significant speedup for small (e.g. `n < 10^6`).
|
|||
Computes the partition function `p(n)`, enabling the use of doubles
|
||||
internally. This significantly speeds up evaluation for small `n`
|
||||
(e.g. `n < 10^6`), but the error bounds are not certified
|
||||
(see remarks for :func:`partitions_hrr_sum_fmprb`).
|
||||
(see remarks for :func:`partitions_hrr_sum_arb`).
|
||||
|
||||
|
|
14
mag.h
14
mag.h
|
@ -288,7 +288,6 @@ mag_equal(const mag_t x, const mag_t y)
|
|||
|
||||
void mag_mul(mag_t z, const mag_t x, const mag_t y);
|
||||
|
||||
/* TODO: document */
|
||||
void mag_mul_lower(mag_t z, const mag_t x, const mag_t y);
|
||||
|
||||
void mag_addmul(mag_t z, const mag_t x, const mag_t y);
|
||||
|
@ -297,7 +296,6 @@ void mag_add_2exp_fmpz(mag_t z, const mag_t x, const fmpz_t e);
|
|||
|
||||
void mag_add(mag_t z, const mag_t x, const mag_t y);
|
||||
|
||||
/* TODO: document */
|
||||
void mag_add_lower(mag_t z, const mag_t x, const mag_t y);
|
||||
|
||||
void mag_div(mag_t z, const mag_t x, const mag_t y);
|
||||
|
@ -616,7 +614,6 @@ mag_cmp_2exp_si(const mag_t x, long e)
|
|||
return (fmpz_cmp_si(MAG_EXPREF(x), e) <= 0) ? -1 : 1;
|
||||
}
|
||||
|
||||
/* TODO: document */
|
||||
static __inline__ mag_ptr
|
||||
_mag_vec_init(long n)
|
||||
{
|
||||
|
@ -638,7 +635,6 @@ _mag_vec_clear(mag_ptr v, long n)
|
|||
flint_free(v);
|
||||
}
|
||||
|
||||
/* TODO: document/test */
|
||||
static __inline__ void mag_set_d(mag_t z, double x)
|
||||
{
|
||||
fmpz_t e;
|
||||
|
@ -651,23 +647,23 @@ static __inline__ void mag_set_d(mag_t z, double x)
|
|||
double mag_d_log_upper_bound(double x);
|
||||
double mag_d_log_lower_bound(double x);
|
||||
|
||||
/* TODO: document */
|
||||
void mag_log1p(mag_t z, const mag_t x);
|
||||
|
||||
/* TODO: document/test */
|
||||
/* TODO: test */
|
||||
void mag_pow_ui(mag_t z, const mag_t x, ulong e);
|
||||
void mag_pow_ui_lower(mag_t z, const mag_t x, ulong e);
|
||||
|
||||
/* TODO: document/test */
|
||||
/* TODO: test */
|
||||
void mag_fac_ui(mag_t z, ulong n);
|
||||
void mag_rfac_ui(mag_t z, ulong n);
|
||||
|
||||
/* TODO: document/test */
|
||||
/* TODO: test */
|
||||
void mag_bernoulli_div_fac_ui(mag_t z, ulong n);
|
||||
|
||||
/* TODO: document/test */
|
||||
/* TODO: test */
|
||||
void mag_set_fmpz_2exp_fmpz_lower(mag_t z, const fmpz_t man, const fmpz_t exp);
|
||||
|
||||
/* TODO: test functions below */
|
||||
static __inline__ void
|
||||
mag_set_ui(mag_t z, ulong x)
|
||||
{
|
||||
|
|
116
mag/test/t-set_d.c
Normal file
116
mag/test/t-set_d.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*=============================================================================
|
||||
|
||||
This file is part of ARB.
|
||||
|
||||
ARB is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ARB is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with ARB; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
=============================================================================*/
|
||||
/******************************************************************************
|
||||
|
||||
Copyright (C) 2014 Fredrik Johansson
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
#include "double_extras.h"
|
||||
#include "mag.h"
|
||||
|
||||
/* XXX: d_randtest is not good enough */
|
||||
|
||||
#define EXP_MINUS_32 2.3283064365386962891e-10
|
||||
#define EXP_MINUS_64 5.42101086242752217e-20
|
||||
|
||||
double
|
||||
d_randtest2(flint_rand_t state)
|
||||
{
|
||||
mp_limb_t m1, m2;
|
||||
double t;
|
||||
|
||||
if (FLINT_BITS == 64)
|
||||
{
|
||||
m1 = n_randtest(state) | (UWORD(1) << (FLINT_BITS - 1));
|
||||
|
||||
t = ((double) m1) * EXP_MINUS_64;
|
||||
}
|
||||
else
|
||||
{
|
||||
m1 = n_randtest(state) | (UWORD(1) << (FLINT_BITS - 1));
|
||||
m2 = n_randtest(state);
|
||||
|
||||
t = ((double) m1) * EXP_MINUS_32 +
|
||||
((double) m2) * EXP_MINUS_64;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
long iter;
|
||||
flint_rand_t state;
|
||||
|
||||
printf("set_d....");
|
||||
fflush(stdout);
|
||||
|
||||
flint_randinit(state);
|
||||
|
||||
for (iter = 0; iter < 100000; iter++)
|
||||
{
|
||||
fmpr_t a, b, c;
|
||||
mag_t m;
|
||||
double x;
|
||||
|
||||
fmpr_init(a);
|
||||
fmpr_init(b);
|
||||
fmpr_init(c);
|
||||
mag_init(m);
|
||||
|
||||
x = d_randtest2(state);
|
||||
x = ldexp(x, 100 - n_randint(state, 200));
|
||||
|
||||
if (n_randint(state, 100) == 0)
|
||||
x = 0.0;
|
||||
|
||||
fmpr_set_d(a, x);
|
||||
mag_set_d(m, x);
|
||||
|
||||
mag_get_fmpr(b, m);
|
||||
|
||||
fmpr_set(c, a);
|
||||
fmpr_mul_ui(c, c, 1025, MAG_BITS, FMPR_RND_UP);
|
||||
fmpr_mul_2exp_si(c, c, -10);
|
||||
|
||||
MAG_CHECK_BITS(m)
|
||||
|
||||
if (!(fmpr_cmpabs(a, b) <= 0 && fmpr_cmpabs(b, c) <= 0))
|
||||
{
|
||||
printf("FAIL\n\n");
|
||||
printf("a = "); fmpr_print(a); printf("\n\n");
|
||||
printf("b = "); fmpr_print(b); printf("\n\n");
|
||||
printf("c = "); fmpr_print(c); printf("\n\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
fmpr_clear(a);
|
||||
fmpr_clear(b);
|
||||
fmpr_clear(c);
|
||||
mag_clear(m);
|
||||
}
|
||||
|
||||
flint_randclear(state);
|
||||
flint_cleanup();
|
||||
printf("PASS\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue