1 /* specfunc/gegenbauer.c
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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 /* Author: G. Jungman */
21
22 #include <config.h>
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_gegenbauer.h>
26
27 #include "error.h"
28
29 /* See: [Thompson, Atlas for Computing Mathematical Functions] */
30
31
32 int
gsl_sf_gegenpoly_1_e(double lambda,double x,gsl_sf_result * result)33 gsl_sf_gegenpoly_1_e(double lambda, double x, gsl_sf_result * result)
34 {
35 /* CHECK_POINTER(result) */
36
37 if(lambda == 0.0) {
38 result->val = 2.0*x;
39 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
40 return GSL_SUCCESS;
41 }
42 else {
43 result->val = 2.0*lambda*x;
44 result->err = 4.0 * GSL_DBL_EPSILON * fabs(result->val);
45 return GSL_SUCCESS;
46 }
47 }
48
49 int
gsl_sf_gegenpoly_2_e(double lambda,double x,gsl_sf_result * result)50 gsl_sf_gegenpoly_2_e(double lambda, double x, gsl_sf_result * result)
51 {
52 /* CHECK_POINTER(result) */
53
54 if(lambda == 0.0) {
55 const double txx = 2.0*x*x;
56 result->val = -1.0 + txx;
57 result->err = 2.0 * GSL_DBL_EPSILON * fabs(txx);
58 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
59 return GSL_SUCCESS;
60 }
61 else {
62 result->val = lambda*(-1.0 + 2.0*(1.0+lambda)*x*x);
63 result->err = GSL_DBL_EPSILON * (2.0 * fabs(result->val) + fabs(lambda));
64 return GSL_SUCCESS;
65 }
66 }
67
68 int
gsl_sf_gegenpoly_3_e(double lambda,double x,gsl_sf_result * result)69 gsl_sf_gegenpoly_3_e(double lambda, double x, gsl_sf_result * result)
70 {
71 /* CHECK_POINTER(result) */
72
73 if(lambda == 0.0) {
74 result->val = x*(-2.0 + 4.0/3.0*x*x);
75 result->err = GSL_DBL_EPSILON * (2.0 * fabs(result->val) + fabs(x));
76 return GSL_SUCCESS;
77 }
78 else {
79 double c = 4.0 + lambda*(6.0 + 2.0*lambda);
80 result->val = 2.0*lambda * x * ( -1.0 - lambda + c*x*x/3.0 );
81 result->err = GSL_DBL_EPSILON * (2.0 * fabs(result->val) + fabs(lambda * x));
82 return GSL_SUCCESS;
83 }
84 }
85
86
87 int
gsl_sf_gegenpoly_n_e(int n,double lambda,double x,gsl_sf_result * result)88 gsl_sf_gegenpoly_n_e(int n, double lambda, double x, gsl_sf_result * result)
89 {
90 /* CHECK_POINTER(result) */
91
92 if(lambda <= -0.5 || n < 0) {
93 DOMAIN_ERROR(result);
94 }
95 else if(n == 0) {
96 result->val = 1.0;
97 result->err = 0.0;
98 return GSL_SUCCESS;
99 }
100 else if(n == 1) {
101 return gsl_sf_gegenpoly_1_e(lambda, x, result);
102 }
103 else if(n == 2) {
104 return gsl_sf_gegenpoly_2_e(lambda, x, result);
105 }
106 else if(n == 3) {
107 return gsl_sf_gegenpoly_3_e(lambda, x, result);
108 }
109 else {
110 if(lambda == 0.0 && (x >= -1.0 && x <= 1.0)) {
111 /* 2 T_n(x)/n */
112 const double z = n * acos(x);
113 result->val = 2.0 * cos(z) / n;
114 result->err = 2.0 * GSL_DBL_EPSILON * fabs(z * result->val);
115 return GSL_SUCCESS;
116 }
117 else {
118 int k;
119 gsl_sf_result g2;
120 gsl_sf_result g3;
121 int stat_g2 = gsl_sf_gegenpoly_2_e(lambda, x, &g2);
122 int stat_g3 = gsl_sf_gegenpoly_3_e(lambda, x, &g3);
123 int stat_g = GSL_ERROR_SELECT_2(stat_g2, stat_g3);
124 double gkm2 = g2.val;
125 double gkm1 = g3.val;
126 double gk = 0.0;
127 for(k=4; k<=n; k++) {
128 gk = (2.0*(k+lambda-1.0)*x*gkm1 - (k+2.0*lambda-2.0)*gkm2) / k;
129 gkm2 = gkm1;
130 gkm1 = gk;
131 }
132 result->val = gk;
133 result->err = 2.0 * GSL_DBL_EPSILON * 0.5 * n * fabs(gk);
134 return stat_g;
135 }
136 }
137 }
138
139
140 int
gsl_sf_gegenpoly_array(int nmax,double lambda,double x,double * result_array)141 gsl_sf_gegenpoly_array(int nmax, double lambda, double x, double * result_array)
142 {
143 int k;
144
145 /* CHECK_POINTER(result_array) */
146
147 if(lambda <= -0.5 || nmax < 0) {
148 GSL_ERROR("domain error", GSL_EDOM);
149 }
150
151 /* n == 0 */
152 result_array[0] = 1.0;
153 if(nmax == 0) return GSL_SUCCESS;
154
155 /* n == 1 */
156 if(lambda == 0.0)
157 result_array[1] = 2.0*x;
158 else
159 result_array[1] = 2.0*lambda*x;
160
161 /* n <= nmax */
162 for(k=2; k<=nmax; k++) {
163 double term1 = 2.0*(k+lambda-1.0) * x * result_array[k-1];
164 double term2 = (k+2.0*lambda-2.0) * result_array[k-2];
165 result_array[k] = (term1 - term2) / k;
166 }
167
168 return GSL_SUCCESS;
169 }
170
171
172 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
173
174 #include "eval.h"
175
gsl_sf_gegenpoly_1(double lambda,double x)176 double gsl_sf_gegenpoly_1(double lambda, double x)
177 {
178 EVAL_RESULT(gsl_sf_gegenpoly_1_e(lambda, x, &result));
179 }
180
gsl_sf_gegenpoly_2(double lambda,double x)181 double gsl_sf_gegenpoly_2(double lambda, double x)
182 {
183 EVAL_RESULT(gsl_sf_gegenpoly_2_e(lambda, x, &result));
184 }
185
gsl_sf_gegenpoly_3(double lambda,double x)186 double gsl_sf_gegenpoly_3(double lambda, double x)
187 {
188 EVAL_RESULT(gsl_sf_gegenpoly_3_e(lambda, x, &result));
189 }
190
gsl_sf_gegenpoly_n(int n,double lambda,double x)191 double gsl_sf_gegenpoly_n(int n, double lambda, double x)
192 {
193 EVAL_RESULT(gsl_sf_gegenpoly_n_e(n, lambda, x, &result));
194 }
195