1 /* specfunc/mathieu_workspace.c
3 * Copyright (C) 2003 Lowell Johnson
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.
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.
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.
20 /* Author: L. Johnson */
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_sf_mathieu.h>
29 gsl_sf_mathieu_workspace *gsl_sf_mathieu_alloc(const size_t nn,
32 gsl_sf_mathieu_workspace *workspace;
33 unsigned int even_order = nn/2 + 1, odd_order = (nn + 1)/2,
36 /* Compute the maximum number of extra terms required for 10^-18 root
37 accuracy for a given value of q (contributed by Brian Gladman). */
38 extra_values = (int)(2.1*pow(fabs(qq), 0.37)) + 9;
42 GSL_ERROR_NULL("matrix dimension must be positive integer", GSL_EINVAL);
46 (gsl_sf_mathieu_workspace *)malloc(sizeof(gsl_sf_mathieu_workspace));
47 if (workspace == NULL)
49 GSL_ERROR_NULL("failed to allocate space for workspace", GSL_ENOMEM);
52 /* Extend matrices to ensure accuracy. */
53 even_order += extra_values;
54 odd_order += extra_values;
57 workspace->even_order = even_order;
58 workspace->odd_order = odd_order;
59 workspace->extra_values = extra_values;
61 /* Allocate space for the characteristic values. */
62 workspace->aa = (double *)malloc((nn+1)*sizeof(double));
63 if (workspace->aa == NULL)
66 GSL_ERROR_NULL("Error allocating memory for characteristic a values",
70 workspace->bb = (double *)malloc((nn+1)*sizeof(double));
71 if (workspace->bb == NULL)
75 GSL_ERROR_NULL("Error allocating memory for characteristic b values",
79 /* Since even_order is always >= odd_order, dimension the arrays for
82 workspace->dd = (double *)malloc(even_order*sizeof(double));
83 if (workspace->dd == NULL)
88 GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
91 workspace->ee = (double *)malloc(even_order*sizeof(double));
92 if (workspace->ee == NULL)
98 GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
101 workspace->tt = (double *)malloc(3*even_order*sizeof(double));
102 if (workspace->tt == NULL)
109 GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
112 workspace->e2 = (double *)malloc(even_order*sizeof(double));
113 if (workspace->e2 == NULL)
121 GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
124 workspace->zz = (double *)malloc(even_order*even_order*sizeof(double));
125 if (workspace->zz == NULL)
134 GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
137 workspace->eval = gsl_vector_alloc(even_order);
139 if (workspace->eval == NULL)
149 GSL_ERROR_NULL("failed to allocate space for eval", GSL_ENOMEM);
152 workspace->evec = gsl_matrix_alloc(even_order, even_order);
154 if (workspace->evec == NULL)
156 gsl_vector_free (workspace->eval);
165 GSL_ERROR_NULL("failed to allocate space for evec", GSL_ENOMEM);
168 workspace->wmat = gsl_eigen_symmv_alloc(even_order);
170 if (workspace->wmat == NULL)
172 gsl_matrix_free (workspace->evec);
173 gsl_vector_free (workspace->eval);
182 GSL_ERROR_NULL("failed to allocate space for wmat", GSL_ENOMEM);
189 void gsl_sf_mathieu_free(gsl_sf_mathieu_workspace *workspace)
191 gsl_vector_free(workspace->eval);
192 gsl_matrix_free(workspace->evec);
193 gsl_eigen_symmv_free(workspace->wmat);