implement Legendre functions

This commit is contained in:
Fredrik Johansson 2015-10-27 23:53:46 +01:00
parent 6823692209
commit ef64707fa9
5 changed files with 652 additions and 0 deletions

View file

@ -160,6 +160,9 @@ int acb_hypgeom_2f1_choose(const acb_t z);
void acb_hypgeom_2f1(acb_t res, const acb_t a, const acb_t b, const acb_t c, const acb_t z, int regularized, long prec);
void acb_hypgeom_legendre_p(acb_t res, const acb_t n, const acb_t m, const acb_t z, int type, long prec);
void acb_hypgeom_legendre_q(acb_t res, const acb_t n, const acb_t m, const acb_t z, int type, long prec);
#ifdef __cplusplus
}
#endif

81
acb_hypgeom/legendre_p.c Normal file
View file

@ -0,0 +1,81 @@
/*=============================================================================
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) 2015 Fredrik Johansson
******************************************************************************/
#include "acb_hypgeom.h"
void
acb_hypgeom_legendre_p(acb_t res, const acb_t n, const acb_t m,
const acb_t z, int type, long prec)
{
acb_t a, b, c, w;
acb_init(a);
acb_init(b);
acb_init(c);
acb_init(w);
acb_neg(a, n);
acb_add_ui(b, n, 1, prec);
acb_sub_ui(c, m, 1, prec);
acb_neg(c, c);
acb_sub_ui(w, z, 1, prec);
acb_neg(w, w);
acb_mul_2exp_si(w, w, -1);
acb_hypgeom_2f1(w, a, b, c, w, 1, prec);
if (!acb_is_zero(m))
{
acb_add_ui(a, z, 1, prec);
acb_sub_ui(b, z, 1, prec);
if (type == 0)
{
acb_neg(b, b);
}
else if (type != 1)
{
printf("unsupported 'type' %d for legendre p\n", type);
abort();
}
acb_mul_2exp_si(c, m, -1);
acb_pow(a, a, c, prec);
acb_neg(c, c);
acb_pow(b, b, c, prec);
acb_mul(w, w, a, prec);
acb_mul(w, w, b, prec);
}
acb_set(res, w);
acb_clear(a);
acb_clear(b);
acb_clear(c);
acb_clear(w);
}

320
acb_hypgeom/legendre_q.c Normal file
View file

@ -0,0 +1,320 @@
/*=============================================================================
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) 2015 Fredrik Johansson
******************************************************************************/
#include "acb_hypgeom.h"
/* invalid in (-1,0) */
int
_acb_hypgeom_legendre_q_single_valid(const acb_t z)
{
arb_t t;
int ok;
if (!arb_contains_zero(acb_imagref(z)))
return 1;
if (arb_is_positive(acb_imagref(z)))
return 1;
arb_init(t);
arb_one(t);
arb_neg(t, t);
ok = arb_lt(acb_realref(z), t);
return ok;
}
void
_acb_hypgeom_legendre_q_double(acb_t res, const acb_t n, const acb_t m,
const acb_t z, long prec)
{
acb_t t, u, v;
acb_init(t);
acb_init(u);
acb_init(v);
if (acb_is_int(m))
{
acb_sub_ui(t, z, 1, prec);
acb_mul_2exp_si(u, m, -1);
acb_pow(v, t, u, prec);
acb_neg(t, t);
acb_neg(u, u);
acb_pow(t, t, u, prec);
acb_mul(t, t, v, prec);
acb_hypgeom_legendre_q(u, n, m, z, 0, prec);
acb_mul(t, t, u, prec);
acb_mul_2exp_si(u, m, -1);
if (!acb_is_int(u))
acb_neg(t, t);
acb_sub_ui(u, z, 1, prec);
acb_sqrt(u, u, prec);
acb_sub_ui(v, z, 1, prec);
acb_neg(v, v);
acb_rsqrt(v, v, prec);
acb_mul(u, u, v, prec);
acb_hypgeom_legendre_p(v, n, m, z, 1, prec);
acb_mul(u, u, v, prec);
acb_const_pi(v, prec);
acb_mul(u, u, v, prec);
acb_mul_2exp_si(u, u, -1);
acb_sub(res, t, u, prec);
}
else
{
acb_sub(t, n, m, prec);
acb_add_ui(t, t, 1, prec);
acb_mul_2exp_si(u, m, 1);
acb_rising(t, t, u, prec);
acb_neg(u, m);
acb_hypgeom_legendre_p(u, n, u, z, 1, prec);
acb_mul(t, t, u, prec);
acb_hypgeom_legendre_p(u, n, m, z, 1, prec);
acb_sub(t, u, t, prec);
acb_exp_pi_i(u, m, prec);
acb_mul(t, t, u, prec);
acb_sin_pi(u, m, prec);
acb_div(t, t, u, prec);
acb_const_pi(u, prec);
acb_mul(t, t, u, prec);
acb_mul_2exp_si(t, t, -1);
acb_set(res, t);
}
acb_clear(t);
acb_clear(u);
acb_clear(v);
}
void
_acb_hypgeom_legendre_q_single(acb_t res, const acb_t n, const acb_t m,
const acb_t z, long prec)
{
acb_t a, b, c, z2, t, u;
acb_init(a);
acb_init(b);
acb_init(c);
acb_init(z2);
acb_init(t);
acb_init(u);
/* invalid in (-1,0) */
if (!_acb_hypgeom_legendre_q_single_valid(z))
{
acb_indeterminate(res);
return;
}
acb_pow_si(z2, z, -2, prec); /* z2 = 1/z^2 */
/* t = 2F1r((m+n+1)/2, (m+n)/2+1, n+3/2, 1/z^2) */
acb_add(b, m, n, prec);
acb_add_ui(a, b, 1, prec);
acb_mul_2exp_si(a, a, -1);
acb_mul_2exp_si(b, b, -1);
acb_add_ui(b, b, 1, prec);
acb_set_ui(c, 3);
acb_mul_2exp_si(c, c, -1);
acb_add(c, c, n, prec);
acb_hypgeom_2f1(t, a, b, c, z2, 1, prec);
/* prefactor sqrt(pi) 2^-n (z+1)^(m/2) (z-1)^(m/2) exp(i pi m) */
/* (1/2) gamma(m+n+1) z^(-m-n-1) */
if (!acb_is_zero(m))
{
acb_add_ui(z2, z, 1, prec);
acb_mul_2exp_si(c, m, -1);
acb_pow(z2, z2, c, prec);
acb_mul(t, t, z2, prec);
acb_sub_ui(z2, z, 1, prec);
acb_mul_2exp_si(c, m, -1);
acb_pow(z2, z2, c, prec);
acb_mul(t, t, z2, prec);
acb_exp_pi_i(z2, m, prec);
acb_mul(t, t, z2, prec);
}
acb_set_ui(z2, 2);
acb_neg(c, n);
acb_pow(z2, z2, c, prec);
acb_mul(t, t, z2, prec);
acb_add(c, m, n, prec);
acb_add_ui(c, c, 1, prec);
acb_gamma(z2, c, prec);
acb_mul(t, t, z2, prec);
acb_neg(c, c);
acb_pow(z2, z, c, prec);
acb_mul(t, t, z2, prec);
acb_mul_2exp_si(t, t, -1);
acb_const_pi(u, prec);
acb_sqrt(u, u, prec);
acb_mul(t, t, u, prec);
acb_set(res, t);
acb_clear(a);
acb_clear(b);
acb_clear(c);
acb_clear(z2);
acb_clear(t);
acb_clear(u);
}
void
acb_hypgeom_legendre_q(acb_t res, const acb_t n, const acb_t m,
const acb_t z, int type, long prec)
{
if (type == 0)
{
/* http://functions.wolfram.com/07.11.26.0033.01 */
/* todo: simplify the gamma quotients and the sqrt pi factor... */
acb_t a, b, c, z2, mn, nm, t, u;
acb_init(a);
acb_init(b);
acb_init(c);
acb_init(z2);
acb_init(mn);
acb_init(nm);
acb_init(t);
acb_init(u);
acb_add(mn, m, n, prec); /* mn = m + n */
acb_sub(nm, n, m, prec); /* nm = n - m */
acb_mul(z2, z, z, prec); /* z2 = z^2 */
/* t = 2F1((1-m-n)/2, (n-m)/2+1, 3/2, z^2) */
acb_sub_ui(a, mn, 1, prec);
acb_neg(a, a);
acb_mul_2exp_si(a, a, -1);
acb_mul_2exp_si(b, nm, -1);
acb_add_ui(b, b, 1, prec);
acb_set_ui(c, 3);
acb_mul_2exp_si(c, c, -1);
acb_hypgeom_2f1(t, a, b, c, z2, 0, prec);
/* u = 2F1(-(m+n)/2, (n-m+1)/2, 1/2, z^2) */
acb_neg(a, mn);
acb_mul_2exp_si(a, a, -1);
acb_add_ui(b, nm, 1, prec);
acb_mul_2exp_si(b, b, -1);
acb_one(c);
acb_mul_2exp_si(c, c, -1);
acb_hypgeom_2f1(u, a, b, c, z2, 0, prec);
/* a = cospi((m+n)/2) gamma((m+n)/2+1) rgamma((n-m+1)/2) z */
/* b = sinpi((m+n)/2) gamma((m+n+1)/2) rgamma((n-m)/2+1) / 2 */
acb_mul_2exp_si(a, mn, -1);
acb_sin_cos_pi(b, a, a, prec);
acb_mul_2exp_si(c, mn, -1);
acb_add_ui(c, c, 1, prec);
acb_gamma(c, c, prec);
acb_mul(a, a, c, prec);
acb_add_ui(c, nm, 1, prec);
acb_mul_2exp_si(c, c, -1);
acb_rgamma(c, c, prec);
acb_mul(a, a, c, prec);
acb_mul(a, a, z, prec);
acb_add_ui(c, mn, 1, prec);
acb_mul_2exp_si(c, c, -1);
acb_gamma(c, c, prec);
acb_mul(b, b, c, prec);
acb_mul_2exp_si(c, nm, -1);
acb_add_ui(c, c, 1, prec);
acb_rgamma(c, c, prec);
acb_mul(b, b, c, prec);
acb_mul_2exp_si(b, b, -1);
/* at - bu */
acb_mul(t, t, a, prec);
acb_mul(u, u, b, prec);
acb_sub(t, t, u, prec);
/* prefactor sqrt(pi) 2^m (1-z^2)^(-m/2) */
if (!acb_is_zero(m))
{
acb_sub_ui(u, z2, 1, prec);
acb_neg(u, u);
acb_neg(c, m);
acb_mul_2exp_si(c, c, -1);
acb_pow(u, u, c, prec);
acb_set_ui(c, 2);
acb_pow(c, c, m, prec);
acb_mul(u, u, c, prec);
acb_mul(t, t, u, prec);
}
acb_const_pi(u, prec);
acb_sqrt(u, u, prec);
acb_mul(t, t, u, prec);
acb_set(res, t);
acb_clear(a);
acb_clear(b);
acb_clear(c);
acb_clear(z2);
acb_clear(mn);
acb_clear(nm);
acb_clear(t);
acb_clear(u);
}
else if (type == 1)
{
if ((arf_cmpabs_2exp_si(arb_midref(acb_realref(z)), -2) < 0 &&
arf_cmpabs_2exp_si(arb_midref(acb_imagref(z)), -2) < 0) ||
!_acb_hypgeom_legendre_q_single_valid(z))
{
_acb_hypgeom_legendre_q_double(res, n, m, z, prec);
}
else
{
_acb_hypgeom_legendre_q_single(res, n, m, z, prec);
}
}
else
{
printf("unsupported 'type' %d for legendre q\n", type);
abort();
}
}

View file

@ -0,0 +1,184 @@
/*=============================================================================
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) 2015 Fredrik Johansson
******************************************************************************/
#include "acb_hypgeom.h"
/* these functions are not public for now */
void _acb_hypgeom_legendre_q_single(acb_t res, const acb_t n, const acb_t m,
const acb_t z, long prec);
void _acb_hypgeom_legendre_q_double(acb_t res, const acb_t n, const acb_t m,
const acb_t z, long prec);
int main()
{
long iter;
flint_rand_t state;
printf("legendre_q....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 1000; iter++)
{
acb_t n, m, z, res1, res2;
long prec1, prec2, ebits;
acb_init(n);
acb_init(m);
acb_init(z);
acb_init(res1);
acb_init(res2);
prec1 = 2 + n_randint(state, 300);
prec2 = 2 + n_randint(state, 300);
ebits = 1 + n_randint(state, 10);
if (n_randint(state, 2))
{
acb_set_si(m, n_randint(state, 20) - 10);
acb_set_si(n, n_randint(state, 20) - 10);
}
else
{
acb_randtest_param(n, state, 1 + n_randint(state, 400), ebits);
acb_randtest_param(m, state, 1 + n_randint(state, 400), ebits);
}
acb_randtest_param(z, state, 1 + n_randint(state, 400), ebits);
_acb_hypgeom_legendre_q_single(res1, n, m, z, prec1);
_acb_hypgeom_legendre_q_double(res2, n, m, z, prec2);
if (!acb_overlaps(res1, res2))
{
printf("FAIL: consistency 1\n\n");
printf("iter = %ld, prec1 = %ld, prec2 = %ld\n\n", iter, prec1, prec2);
printf("m = "); acb_printd(m, 30); printf("\n\n");
printf("n = "); acb_printd(n, 30); printf("\n\n");
printf("z = "); acb_printd(z, 30); printf("\n\n");
printf("res1 = "); acb_printd(res1, 30); printf("\n\n");
printf("res2 = "); acb_printd(res2, 30); printf("\n\n");
abort();
}
acb_clear(n);
acb_clear(m);
acb_clear(z);
acb_clear(res1);
acb_clear(res2);
}
for (iter = 0; iter < 1000; iter++)
{
acb_t n, m, z, res1, res2, t, u;
long prec1, prec2, ebits;
int type;
acb_init(n);
acb_init(m);
acb_init(z);
acb_init(res1);
acb_init(res2);
acb_init(t);
acb_init(u);
prec1 = 2 + n_randint(state, 300);
prec2 = 2 + n_randint(state, 300);
ebits = 1 + n_randint(state, 10);
acb_randtest_param(n, state, 1 + n_randint(state, 400), ebits);
acb_randtest_param(m, state, 1 + n_randint(state, 400), ebits);
acb_randtest_param(z, state, 1 + n_randint(state, 400), ebits);
type = n_randint(state, 2);
acb_hypgeom_legendre_q(res1, n, m, z, type, prec1);
acb_neg(t, m);
acb_hypgeom_legendre_p(res2, n, t, z, type, prec2);
acb_add(u, m, n, prec2);
acb_add_ui(u, u, 1, prec2);
acb_gamma(u, u, prec2);
acb_mul(res2, res2, u, prec2);
acb_sub(u, n, m, prec2);
acb_add_ui(u, u, 1, prec2);
acb_rgamma(u, u, prec2);
acb_mul(res2, res2, u, prec2);
acb_hypgeom_legendre_p(t, n, m, z, type, prec2);
if (type == 0)
{
acb_cos_pi(u, m, prec2);
acb_mul(t, t, u, prec2);
}
acb_sub(res2, t, res2, prec2);
if (type == 1)
{
acb_exp_pi_i(t, m, prec2);
acb_mul(res2, res2, t, prec2);
}
acb_sin_pi(t, m, prec2);
if (acb_contains_zero(t))
acb_indeterminate(res2);
else
acb_div(res2, res2, t, prec2);
acb_const_pi(t, prec2);
acb_mul(res2, res2, t, prec2);
acb_mul_2exp_si(res2, res2, -1);
if (!acb_overlaps(res1, res2))
{
printf("FAIL: consistency 2\n\n");
printf("iter = %ld, prec1 = %ld, prec2 = %ld\n\n", iter, prec1, prec2);
printf("type = %d\n\n", type);
printf("m = "); acb_printd(m, 30); printf("\n\n");
printf("n = "); acb_printd(n, 30); printf("\n\n");
printf("z = "); acb_printd(z, 30); printf("\n\n");
printf("res1 = "); acb_printd(res1, 30); printf("\n\n");
printf("res2 = "); acb_printd(res2, 30); printf("\n\n");
abort();
}
acb_clear(n);
acb_clear(m);
acb_clear(z);
acb_clear(res1);
acb_clear(res2);
acb_clear(t);
acb_clear(u);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -651,3 +651,67 @@ if the flag *regularized* is set.
Computes `F(z)` (or `\operatorname{\mathbf{F}}(z)` if *regularized* is set)
using an automatic algorithm choice.
Orthogonal polynomials and functions
-------------------------------------------------------------------------------
.. function:: void acb_hypgeom_legendre_p(acb_t res, const acb_t n, const acb_t m, const acb_t z, int type, long prec)
Sets *res* to the associated Legendre function of the first kind
evaluated for degree *n*, order *m*, and argument *z*.
When *m* is zero, this reduces to the Legendre polynomial `P_n(z)`.
Many different branch cut conventions appear in the literature.
If *type* is 0, the version
.. math ::
P_n^m(z) = \frac{(1+z)^{m/2}}{(1-z)^{m/2}}
\mathbf{F}\left(-n, n+1, 1-m, \frac{1-z}{2}\right)
is computed, and if *type* is 1, the alternative version
.. math ::
{\mathcal P}_n^m(z) = \frac{(z+1)^{m/2}}{(z-1)^{m/2}}
\mathbf{F}\left(-n, n+1, 1-m, \frac{1-z}{2}\right).
is computed. Type 0 and type 1 respectively correspond to
type 2 and type 3 in *Mathematica* and *mpmath*.
.. function:: void acb_hypgeom_legendre_q(acb_t res, const acb_t n, const acb_t m, const acb_t z, int type, long prec)
Sets *res* to the associated Legendre function of the second kind
evaluated for degree *n*, order *m*, and argument *z*.
When *m* is zero, this reduces to the Legendre function `Q_n(z)`.
Many different branch cut conventions appear in the literature.
If *type* is 0, the version
.. math ::
Q_n^m(z) = \frac{\pi}{2 \sin(\pi m)}
\left( \cos(\pi m) P_n^m(z) -
\frac{\Gamma(1+m+n)}{\Gamma(1-m+n)} P_n^{-m}(z)\right)
is computed, and if *type* is 1, the alternative version
.. math ::
\mathcal{Q}_n^m(z) = \frac{\pi}{2 \sin(\pi m)} e^{\pi i m}
\left( \mathcal{P}_n^m(z) -
\frac{\Gamma(1+m+n)}{\Gamma(1-m+n)} \mathcal{P}_n^{-m}(z)\right)
is computed. Type 0 and type 1 respectively correspond to
type 2 and type 3 in *Mathematica* and *mpmath*.
When *m* is an integer, either expression is interpreted as a limit.
We make use of the connection formulas [WQ3a]_, [WQ3b]_ and [WQ3c]_
to allow computing the function even in the limiting case.
(The formula [WQ3d]_ would be useful, but is incorrect in the lower
half plane.)
.. [WQ3a] http://functions.wolfram.com/07.11.26.0033.01
.. [WQ3b] http://functions.wolfram.com/07.12.27.0014.01
.. [WQ3c] http://functions.wolfram.com/07.12.26.0003.01
.. [WQ3d] http://functions.wolfram.com/07.12.26.0088.01