gsl_specfunc__gegenbauer.c 4.9 KB

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