gsl_specfunc__mathieu_workspace.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* specfunc/mathieu_workspace.c
  2. *
  3. * Copyright (C) 2003 Lowell Johnson
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Author: L. Johnson */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include "gsl_math.h"
  23. #include "gsl_errno.h"
  24. #include "gsl_sf_mathieu.h"
  25. gsl_sf_mathieu_workspace *gsl_sf_mathieu_alloc(const size_t nn,
  26. const double qq)
  27. {
  28. gsl_sf_mathieu_workspace *workspace;
  29. unsigned int even_order = nn/2 + 1, odd_order = (nn + 1)/2,
  30. extra_values;
  31. /* Compute the maximum number of extra terms required for 10^-18 root
  32. accuracy for a given value of q (contributed by Brian Gladman). */
  33. extra_values = (int)(2.1*pow(fabs(qq), 0.37)) + 9;
  34. if (nn + 1 == 0)
  35. {
  36. GSL_ERROR_NULL("matrix dimension must be positive integer", GSL_EINVAL);
  37. }
  38. workspace =
  39. (gsl_sf_mathieu_workspace *)malloc(sizeof(gsl_sf_mathieu_workspace));
  40. if (workspace == NULL)
  41. {
  42. GSL_ERROR_NULL("failed to allocate space for workspace", GSL_ENOMEM);
  43. }
  44. /* Extend matrices to ensure accuracy. */
  45. even_order += extra_values;
  46. odd_order += extra_values;
  47. workspace->size = nn;
  48. workspace->even_order = even_order;
  49. workspace->odd_order = odd_order;
  50. workspace->extra_values = extra_values;
  51. /* Allocate space for the characteristic values. */
  52. workspace->aa = (double *)malloc((nn+1)*sizeof(double));
  53. if (workspace->aa == NULL)
  54. {
  55. free(workspace);
  56. GSL_ERROR_NULL("Error allocating memory for characteristic a values",
  57. GSL_ENOMEM);
  58. }
  59. workspace->bb = (double *)malloc((nn+1)*sizeof(double));
  60. if (workspace->bb == NULL)
  61. {
  62. free(workspace->aa);
  63. free(workspace);
  64. GSL_ERROR_NULL("Error allocating memory for characteristic b values",
  65. GSL_ENOMEM);
  66. }
  67. /* Since even_order is always >= odd_order, dimension the arrays for
  68. even_order. */
  69. workspace->dd = (double *)malloc(even_order*sizeof(double));
  70. if (workspace->dd == NULL)
  71. {
  72. free(workspace->aa);
  73. free(workspace->bb);
  74. free(workspace);
  75. GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
  76. }
  77. workspace->ee = (double *)malloc(even_order*sizeof(double));
  78. if (workspace->ee == NULL)
  79. {
  80. free(workspace->dd);
  81. free(workspace->aa);
  82. free(workspace->bb);
  83. free(workspace);
  84. GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
  85. }
  86. workspace->tt = (double *)malloc(3*even_order*sizeof(double));
  87. if (workspace->tt == NULL)
  88. {
  89. free(workspace->ee);
  90. free(workspace->dd);
  91. free(workspace->aa);
  92. free(workspace->bb);
  93. free(workspace);
  94. GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
  95. }
  96. workspace->e2 = (double *)malloc(even_order*sizeof(double));
  97. if (workspace->e2 == NULL)
  98. {
  99. free(workspace->tt);
  100. free(workspace->ee);
  101. free(workspace->dd);
  102. free(workspace->aa);
  103. free(workspace->bb);
  104. free(workspace);
  105. GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
  106. }
  107. workspace->zz = (double *)malloc(even_order*even_order*sizeof(double));
  108. if (workspace->zz == NULL)
  109. {
  110. free(workspace->e2);
  111. free(workspace->tt);
  112. free(workspace->ee);
  113. free(workspace->dd);
  114. free(workspace->aa);
  115. free(workspace->bb);
  116. free(workspace);
  117. GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
  118. }
  119. workspace->eval = gsl_vector_alloc(even_order);
  120. if (workspace->eval == NULL)
  121. {
  122. free(workspace->zz);
  123. free(workspace->e2);
  124. free(workspace->tt);
  125. free(workspace->ee);
  126. free(workspace->dd);
  127. free(workspace->aa);
  128. free(workspace->bb);
  129. free(workspace);
  130. GSL_ERROR_NULL("failed to allocate space for eval", GSL_ENOMEM);
  131. }
  132. workspace->evec = gsl_matrix_alloc(even_order, even_order);
  133. if (workspace->evec == NULL)
  134. {
  135. gsl_vector_free (workspace->eval);
  136. free(workspace->zz);
  137. free(workspace->e2);
  138. free(workspace->tt);
  139. free(workspace->ee);
  140. free(workspace->dd);
  141. free(workspace->aa);
  142. free(workspace->bb);
  143. free(workspace);
  144. GSL_ERROR_NULL("failed to allocate space for evec", GSL_ENOMEM);
  145. }
  146. workspace->wmat = gsl_eigen_symmv_alloc(even_order);
  147. if (workspace->wmat == NULL)
  148. {
  149. gsl_matrix_free (workspace->evec);
  150. gsl_vector_free (workspace->eval);
  151. free(workspace->zz);
  152. free(workspace->e2);
  153. free(workspace->tt);
  154. free(workspace->ee);
  155. free(workspace->dd);
  156. free(workspace->aa);
  157. free(workspace->bb);
  158. free(workspace);
  159. GSL_ERROR_NULL("failed to allocate space for wmat", GSL_ENOMEM);
  160. }
  161. return workspace;
  162. }
  163. void gsl_sf_mathieu_free(gsl_sf_mathieu_workspace *workspace)
  164. {
  165. gsl_vector_free(workspace->eval);
  166. gsl_matrix_free(workspace->evec);
  167. gsl_eigen_symmv_free(workspace->wmat);
  168. free(workspace->aa);
  169. free(workspace->bb);
  170. free(workspace->dd);
  171. free(workspace->ee);
  172. free(workspace->tt);
  173. free(workspace->e2);
  174. free(workspace->zz);
  175. free(workspace);
  176. }