1 /* integration/jacobi.c
2  *
3  * Copyright (C) 2017 Konrad Griessinger, Patrick Alken
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  * The code in this module is based on IQPACK, specifically the LGPL
22  * implementation found in HERMITE_RULE:
23  * https://people.sc.fsu.edu/~jburkardt/c_src/hermite_rule/hermite_rule.html
24  */
25 
26 #include <stdio.h>
27 #include <gsl/gsl_errno.h>
28 #include <gsl/gsl_math.h>
29 #include <gsl/gsl_integration.h>
30 #include <gsl/gsl_sf_gamma.h>
31 
32 static int
jacobi_check(const size_t n,const gsl_integration_fixed_params * params)33 jacobi_check(const size_t n, const gsl_integration_fixed_params * params)
34 {
35   (void) n;
36 
37   if (fabs(params->b - params->a) <= GSL_DBL_EPSILON)
38     {
39       GSL_ERROR("|b - a| too small", GSL_EDOM);
40     }
41   else if (params->a >= params->b)
42     {
43       GSL_ERROR("lower integration limit must be smaller than upper limit", GSL_EDOM);
44     }
45   else if (params->alpha <= -1.0 || params->beta <= -1.0)
46     {
47       GSL_ERROR("alpha and beta must be > -1", GSL_EDOM);
48     }
49   else
50     {
51       return GSL_SUCCESS;
52     }
53 }
54 
55 static int
jacobi_init(const size_t n,double * diag,double * subdiag,gsl_integration_fixed_params * params)56 jacobi_init(const size_t n, double * diag, double * subdiag, gsl_integration_fixed_params * params)
57 {
58   const double absum = params->beta + params->alpha;
59   const double abdiff = params->beta - params->alpha;
60   const double a2b2 = absum * abdiff; /* beta^2 - alpha^2 */
61   size_t i;
62 
63   /* construct the diagonal and subdiagonal elements of Jacobi matrix */
64   diag[0] = abdiff/(absum + 2.0);
65   subdiag[0] = 2.0*sqrt((params->alpha + 1.0)*(params->beta + 1.0)/(absum + 3.0))/(absum + 2.0);
66   for (i = 1; i < n; i++)
67     {
68       diag[i] = a2b2 / ( (absum + 2.0*i) * (absum + 2.0*i + 2.0) );
69       subdiag[i] = sqrt ( 4.0*(i + 1.0) * (params->alpha + i + 1.0) * (params->beta + i + 1.0) * (absum + i + 1.0) / ( pow((absum + 2.0*i + 2.0), 2.0) - 1.0 ) ) / ( absum + 2.0*i + 2.0 );
70     }
71 
72   params->zemu = pow(2.0, absum + 1.0) * gsl_sf_gamma(params->alpha + 1.0) * gsl_sf_gamma(params->beta + 1.0) / gsl_sf_gamma(absum + 2.0);
73   params->shft = 0.5*(params->b + params->a);
74   params->slp = 0.5*(params->b - params->a);
75   params->al = params->alpha;
76   params->be = params->beta;
77 
78   return GSL_SUCCESS;
79 }
80 
81 static const gsl_integration_fixed_type jacobi_type =
82 {
83   jacobi_check,
84   jacobi_init
85 };
86 
87 const gsl_integration_fixed_type *gsl_integration_fixed_jacobi = &jacobi_type;
88