|
Packit |
67cb25 |
/* specfunc/mathieu_workspace.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 2003 Lowell Johnson
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
67cb25 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
67cb25 |
* the Free Software Foundation; either version 3 of the License, or (at
|
|
Packit |
67cb25 |
* your option) any later version.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit |
67cb25 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
67cb25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
67cb25 |
* General Public License for more details.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
67cb25 |
* along with this program; if not, write to the Free Software
|
|
Packit |
67cb25 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Author: L. Johnson */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <stdlib.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_math.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_errno.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_sf_mathieu.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_sf_mathieu_workspace *gsl_sf_mathieu_alloc(const size_t nn,
|
|
Packit |
67cb25 |
const double qq)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_sf_mathieu_workspace *workspace;
|
|
Packit |
67cb25 |
unsigned int even_order = nn/2 + 1, odd_order = (nn + 1)/2,
|
|
Packit |
67cb25 |
extra_values;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Compute the maximum number of extra terms required for 10^-18 root
|
|
Packit |
67cb25 |
accuracy for a given value of q (contributed by Brian Gladman). */
|
|
Packit |
67cb25 |
extra_values = (int)(2.1*pow(fabs(qq), 0.37)) + 9;
|
|
Packit |
67cb25 |
extra_values += 20; /* additional fudge */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (nn + 1 == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("matrix dimension must be positive integer", GSL_EINVAL);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace =
|
|
Packit |
67cb25 |
(gsl_sf_mathieu_workspace *)malloc(sizeof(gsl_sf_mathieu_workspace));
|
|
Packit |
67cb25 |
if (workspace == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for workspace", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Extend matrices to ensure accuracy. */
|
|
Packit |
67cb25 |
even_order += extra_values;
|
|
Packit |
67cb25 |
odd_order += extra_values;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->size = nn;
|
|
Packit |
67cb25 |
workspace->even_order = even_order;
|
|
Packit |
67cb25 |
workspace->odd_order = odd_order;
|
|
Packit |
67cb25 |
workspace->extra_values = extra_values;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Allocate space for the characteristic values. */
|
|
Packit |
67cb25 |
workspace->aa = (double *)malloc((nn+1)*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->aa == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("Error allocating memory for characteristic a values",
|
|
Packit |
67cb25 |
GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->bb = (double *)malloc((nn+1)*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->bb == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("Error allocating memory for characteristic b values",
|
|
Packit |
67cb25 |
GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Since even_order is always >= odd_order, dimension the arrays for
|
|
Packit |
67cb25 |
even_order. */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->dd = (double *)malloc(even_order*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->dd == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->ee = (double *)malloc(even_order*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->ee == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->tt = (double *)malloc(3*even_order*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->tt == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->e2 = (double *)malloc(even_order*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->e2 == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->zz = (double *)malloc(even_order*even_order*sizeof(double));
|
|
Packit |
67cb25 |
if (workspace->zz == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->e2);
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for diagonal", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->eval = gsl_vector_alloc(even_order);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (workspace->eval == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
free(workspace->zz);
|
|
Packit |
67cb25 |
free(workspace->e2);
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for eval", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->evec = gsl_matrix_alloc(even_order, even_order);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (workspace->evec == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_vector_free (workspace->eval);
|
|
Packit |
67cb25 |
free(workspace->zz);
|
|
Packit |
67cb25 |
free(workspace->e2);
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for evec", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
workspace->wmat = gsl_eigen_symmv_alloc(even_order);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (workspace->wmat == NULL)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_matrix_free (workspace->evec);
|
|
Packit |
67cb25 |
gsl_vector_free (workspace->eval);
|
|
Packit |
67cb25 |
free(workspace->zz);
|
|
Packit |
67cb25 |
free(workspace->e2);
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
GSL_ERROR_NULL("failed to allocate space for wmat", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return workspace;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void gsl_sf_mathieu_free(gsl_sf_mathieu_workspace *workspace)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
RETURN_IF_NULL (workspace);
|
|
Packit |
67cb25 |
gsl_vector_free(workspace->eval);
|
|
Packit |
67cb25 |
gsl_matrix_free(workspace->evec);
|
|
Packit |
67cb25 |
gsl_eigen_symmv_free(workspace->wmat);
|
|
Packit |
67cb25 |
free(workspace->aa);
|
|
Packit |
67cb25 |
free(workspace->bb);
|
|
Packit |
67cb25 |
free(workspace->dd);
|
|
Packit |
67cb25 |
free(workspace->ee);
|
|
Packit |
67cb25 |
free(workspace->tt);
|
|
Packit |
67cb25 |
free(workspace->e2);
|
|
Packit |
67cb25 |
free(workspace->zz);
|
|
Packit |
67cb25 |
free(workspace);
|
|
Packit |
67cb25 |
}
|