implement evaluation of Chebyshev polynomials

This commit is contained in:
Fredrik Johansson 2014-11-14 19:05:06 +01:00
parent 8a93d36afa
commit bad99d8c7f
9 changed files with 567 additions and 6 deletions

5
arb.h
View file

@ -619,6 +619,11 @@ void arb_bernoulli_ui_zeta(arb_t b, ulong n, long prec);
void arb_polylog(arb_t w, const arb_t s, const arb_t z, long prec);
void arb_polylog_si(arb_t w, long s, const arb_t z, long prec);
void arb_chebyshev_t_ui(arb_t a, ulong n, const arb_t x, long prec);
void arb_chebyshev_t2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec);
void arb_chebyshev_u_ui(arb_t a, ulong n, const arb_t x, long prec);
void arb_chebyshev_u2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec);
#define ARB_DEF_CACHED_CONSTANT(name, comp_func) \
TLS_PREFIX long name ## _cached_prec = 0; \
TLS_PREFIX arb_t name ## _cached_value; \

65
arb/chebyshev_t2_ui.c Normal file
View file

@ -0,0 +1,65 @@
/*=============================================================================
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 "arb.h"
void
arb_chebyshev_t2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec)
{
int i;
arb_set_round(a, x, prec);
arb_one(b);
if (n <= 1)
{
if (n == 0)
arb_swap(a, b);
return;
}
for (i = FLINT_BIT_COUNT(n - 1) - 1; i >= 0; i--)
{
if (((n - 1) >> i) & 1)
{
arb_mul(b, b, a, prec);
arb_mul_2exp_si(b, b, 1);
arb_sub(b, b, x, prec);
arb_mul(a, a, a, prec);
arb_mul_2exp_si(a, a, 1);
arb_sub_ui(a, a, 1, prec);
}
else
{
arb_mul(a, a, b, prec);
arb_mul_2exp_si(a, a, 1);
arb_sub(a, a, x, prec);
arb_mul(b, b, b, prec);
arb_mul_2exp_si(b, b, 1);
arb_sub_ui(b, b, 1, prec);
}
}
}

72
arb/chebyshev_t_ui.c Normal file
View file

@ -0,0 +1,72 @@
/*=============================================================================
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 "arb.h"
void
arb_chebyshev_t_ui(arb_t y, ulong n, const arb_t x, long prec)
{
int i, r;
if (n <= 1)
{
if (n == 0)
arb_one(y);
else
arb_set_round(y, x, prec);
return;
}
count_trailing_zeros(r, n);
if ((n >> r) == 1)
{
arb_mul(y, x, x, prec);
arb_mul_2exp_si(y, y, 1);
arb_sub_ui(y, y, 1, prec);
r -= 1;
}
else
{
/* we only need one value, so break out final iteration */
arb_t t, u;
arb_init(t);
arb_init(u);
arb_chebyshev_t2_ui(t, u, (n >> (r + 1)) + 1, x, prec);
arb_mul(t, t, u, prec);
arb_mul_2exp_si(t, t, 1);
arb_sub(y, t, x, prec);
arb_clear(t);
arb_clear(u);
}
for (i = 0; i < r; i++)
{
arb_mul(y, y, y, prec);
arb_mul_2exp_si(y, y, 1);
arb_sub_ui(y, y, 1, prec);
}
}

76
arb/chebyshev_u2_ui.c Normal file
View file

@ -0,0 +1,76 @@
/*=============================================================================
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 "arb.h"
void
arb_chebyshev_u2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec)
{
int i;
arb_t t, u;
if (n == 0)
{
arb_one(a);
arb_zero(b);
return;
}
arb_set_round(a, x, prec);
arb_mul_2exp_si(a, a, 1);
arb_one(b);
if (n == 1)
return;
arb_init(t);
arb_init(u);
for (i = FLINT_BIT_COUNT(n) - 2; i >= 0; i--)
{
arb_add(t, a, b, prec);
arb_sub(u, a, b, prec);
if ((n >> i) & 1)
{
arb_submul(b, x, a, prec);
arb_mul(a, a, b, prec);
arb_neg(a, a);
arb_mul_2exp_si(a, a, 1);
arb_mul(b, t, u, prec);
}
else
{
arb_submul(a, x, b, prec);
arb_mul(b, a, b, prec);
arb_mul_2exp_si(b, b, 1);
arb_mul(a, t, u, prec);
}
}
arb_clear(t);
arb_clear(u);
}

69
arb/chebyshev_u_ui.c Normal file
View file

@ -0,0 +1,69 @@
/*=============================================================================
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 "arb.h"
void
arb_chebyshev_u_ui(arb_t y, ulong n, const arb_t x, long prec)
{
arb_t a, b;
if (n <= 1)
{
if (n == 0)
{
arb_one(y);
}
else
{
arb_set_round(y, x, prec);
arb_mul_2exp_si(y, y, 1);
}
return;
}
arb_init(a);
arb_init(b);
arb_chebyshev_u2_ui(a, b, n / 2, x, prec);
if (n % 2 == 0)
{
arb_add(y, a, b, prec);
arb_sub(b, a, b, prec);
arb_mul(y, y, b, prec);
}
else
{
arb_submul(b, a, x, prec);
arb_mul(y, a, b, prec);
arb_mul_2exp_si(y, y, 1);
arb_neg(y, y);
}
arb_clear(a);
arb_clear(b);
}

127
arb/test/t-chebyshev_t_ui.c Normal file
View file

@ -0,0 +1,127 @@
/*=============================================================================
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 "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("chebyshev_t_ui....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d;
ulong n;
long prec;
n = n_randtest(state);
prec = 2 + n_randint(state, 300);
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
arb_randtest(a, state, 1 + n_randint(state, 300), 5);
arb_randtest(c, state, 1 + n_randint(state, 300), 5);
arb_cos(b, a, prec);
arb_chebyshev_t_ui(c, n, b, prec);
arb_mul_ui(d, a, n, prec);
arb_cos(d, d, prec);
if (!arb_overlaps(c, d))
{
printf("FAIL: c = T_n(cos(a)) = d = cos(n*a)\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("d = "); arb_print(c); printf("\n\n");
abort();
}
arb_chebyshev_t_ui(b, n, b, prec);
if (!arb_equal(b, c))
{
printf("FAIL: aliasing\n\n");
printf("n = %lu\n\n", n);
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
abort();
}
arb_randtest(a, state, 1 + n_randint(state, 300), 5);
arb_randtest(b, state, 1 + n_randint(state, 300), 5);
arb_randtest(c, state, 1 + n_randint(state, 300), 5);
arb_chebyshev_t2_ui(b, c, n, a, prec);
arb_chebyshev_t_ui(d, n, a, prec);
if (!arb_overlaps(b, d))
{
printf("FAIL: T_n\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
abort();
}
if (n == 0)
arb_set(d, a);
else
arb_chebyshev_t_ui(d, n - 1, a, prec);
if (!arb_overlaps(c, d))
{
printf("FAIL: T_{n-1}\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

137
arb/test/t-chebyshev_u_ui.c Normal file
View file

@ -0,0 +1,137 @@
/*=============================================================================
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 "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("chebyshev_u_ui....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d, e;
ulong n;
long prec;
n = n_randtest(state);
prec = 2 + n_randint(state, 300);
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
arb_init(e);
arb_randtest_precise(a, state, 1 + n_randint(state, 300), 5);
arb_randtest_precise(c, state, 1 + n_randint(state, 300), 5);
arb_sin_cos(d, b, a, prec);
arb_chebyshev_u_ui(c, n, b, prec);
arb_mul(d, c, d, prec);
if (n == LIMB_ONES)
arb_mul_2exp_si(e, a, FLINT_BITS);
else
arb_mul_ui(e, a, n + 1, prec);
arb_sin(e, e, prec);
if (!arb_overlaps(d, e))
{
printf("FAIL: sin(a)*U_n(cos(a)) = sin((n+1)a)\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_printd(a, 15); printf("\n\n");
printf("b = "); arb_printd(b, 15); printf("\n\n");
printf("c = "); arb_printd(c, 15); printf("\n\n");
printf("d = "); arb_printd(d, 15); printf("\n\n");
printf("e = "); arb_printd(e, 15); printf("\n\n");
abort();
}
arb_chebyshev_u_ui(b, n, b, prec);
if (!arb_equal(b, c))
{
printf("FAIL: aliasing\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_printd(a, 15); printf("\n\n");
printf("b = "); arb_printd(b, 15); printf("\n\n");
printf("c = "); arb_printd(c, 15); printf("\n\n");
abort();
}
arb_randtest(a, state, 1 + n_randint(state, 300), 5);
arb_randtest(b, state, 1 + n_randint(state, 300), 5);
arb_randtest(c, state, 1 + n_randint(state, 300), 5);
arb_chebyshev_u2_ui(b, c, n, a, prec);
arb_chebyshev_u_ui(d, n, a, prec);
if (!arb_overlaps(b, d))
{
printf("FAIL: U_n\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
abort();
}
if (n == 0)
arb_zero(d);
else
arb_chebyshev_u_ui(d, n - 1, a, prec);
if (!arb_overlaps(c, d))
{
printf("FAIL: U_{n-1}\n\n");
printf("n = %lu\n\n", n);
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
arb_clear(e);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -88,7 +88,7 @@ int main()
for (iter = 0; iter < 1000000; iter++)
{
arb_t a, b, c, d;
long prec0, prec1, prec2, prec3;
long prec0, prec1, prec2;
if (iter % 10 == 0)
prec0 = 10000;
@ -98,11 +98,6 @@ int main()
prec1 = 2 + n_randint(state, prec0);
prec2 = 2 + n_randint(state, prec0);
if (iter % 10 == 0)
prec3 = 50000;
else
prec3 = 100;
arb_init(a);
arb_init(b);
arb_init(c);

View file

@ -1009,6 +1009,21 @@ Other special functions
Sets *z* to the arithmetic-geometric mean of *x* and *y*.
.. function:: void arb_chebyshev_t_ui(arb_t a, ulong n, const arb_t x, long prec)
.. function:: void arb_chebyshev_u_ui(arb_t a, ulong n, const arb_t x, long prec)
Evaluates the Chebyshev polynomial of the first kind `a = T_n(x)`
or the Chebyshev polynomial of the second kind `a = U_n(x)`.
.. function:: void arb_chebyshev_t2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec)
.. function:: void arb_chebyshev_u2_ui(arb_t a, arb_t b, ulong n, const arb_t x, long prec)
Simultaneously evaluates `a = T_n(x), b = T_{n-1}(x)` or
`a = U_n(x), b = U_{n-1}(x)`.
Aliasing between the intput and the outputs is not permitted.
Internal helper functions
-------------------------------------------------------------------------------