annotate general/numerical/hzeta.c @ 6:0ce3c2070089

Removed duplicate code and fixed doc in timed_action.
author samer
date Mon, 14 Jan 2013 14:33:37 +0000
parents e44f49929e56
children
rev   line source
samer@4 1 /*=================================================================
samer@4 2 * hzeta.c
samer@4 3 *
samer@4 4 * Wrapper function for gsl_sf_hzeta_e
samer@4 5 *
samer@4 6 * This MEX function computes the Hurwitz-zeta-function by calling
samer@4 7 * the function gsl_sf_hzeta_e from the GNU Scientific Library. For
samer@4 8 * x>1 and a>=0 the Hurwitz-zeta-function is defined as
samer@4 9 *
samer@4 10 * oo
samer@4 11 * -----
samer@4 12 * \ 1
samer@4 13 * hzeta(x, a) = ) -------- .
samer@4 14 * / x
samer@4 15 * ----- (i + a)
samer@4 16 * i = 0
samer@4 17 *
samer@4 18 * The MEX function takes two arguments, x and a. If both arguments
samer@4 19 * are matrices of the same size, then the MEX function will compute
samer@4 20 * the Hurwitz-zeta-function elementwise and the result of the MEX
samer@4 21 * function will be a matrix of the same size as the inputs. If one
samer@4 22 * input argument is a scalar s, then the function behaves as if this
samer@4 23 * argument would be a matrix filled with the value s of the same
samer@4 24 * size as the other argument.
samer@4 25 *
samer@4 26 * Compile this MEX function with "mex hzeta.c -lgsl". See Matlab
samer@4 27 * documentation for additional information on compiling MEX functions.
samer@4 28 *
samer@4 29 * (C) by Heiko Bauke, 2007, heiko.bauke@physics.ox.ac.uk
samer@4 30 *
samer@4 31 * This program is free software; you can redistribute it and/or
samer@4 32 * modify it under the terms of the GNU General Public License in
samer@4 33 * version 2 as published by the Free Software Foundation.
samer@4 34 *
samer@4 35 * This program is distributed in the hope that it will be useful, but
samer@4 36 * WITHOUT ANY WARRANTY; without even the implied warranty of
samer@4 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
samer@4 38 * General Public License for more details.
samer@4 39 *
samer@4 40 * You should have received a copy of the GNU General Public License
samer@4 41 * along with this program; if not, write to the Free Software
samer@4 42 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
samer@4 43 * 02111-1307, USA.
samer@4 44 *
samer@4 45 *=================================================================*/
samer@4 46
samer@4 47 #include "mex.h"
samer@4 48 #include <gsl/gsl_errno.h>
samer@4 49 #include <gsl/gsl_math.h>
samer@4 50 #include <gsl/gsl_sf_zeta.h>
samer@4 51
samer@4 52 void mexFunction(int nlhs, mxArray *plhs[],
samer@4 53 int nrhs, const mxArray *prhs[]) {
samer@4 54 double *x, *y, *a;
samer@4 55 int nrows_x, ncols_x, i_x,
samer@4 56 nrows_a, ncols_a, i_a,
samer@4 57 nrows_y, ncols_y, i_y,
samer@4 58 status;
samer@4 59 gsl_sf_result result;
samer@4 60
samer@4 61 /* check input arguments */
samer@4 62 if (nrhs!=2)
samer@4 63 mexErrMsgTxt("Two input arguments required.\n");
samer@4 64 if (!mxIsDouble(prhs[0]))
samer@4 65 mexErrMsgTxt("1st argument not a matrix of double.");
samer@4 66 nrows_x=mxGetM(prhs[0]);
samer@4 67 ncols_x=mxGetN(prhs[0]);
samer@4 68 if (!mxIsDouble(prhs[1]))
samer@4 69 mexErrMsgTxt("2nd argument not a matrix of double.");
samer@4 70 nrows_a=mxGetM(prhs[1]);
samer@4 71 ncols_a=mxGetN(prhs[1]);
samer@4 72 if (!( (nrows_a==nrows_x && ncols_a==ncols_x) ||
samer@4 73 (nrows_x==1 && ncols_x==1) ||
samer@4 74 (nrows_a==1 && ncols_a==1) ) )
samer@4 75 mexErrMsgTxt("arguments must be matrices of the same size or scalars.");
samer@4 76 /* create matrix for the return argument */
samer@4 77 nrows_y=nrows_x>nrows_a ? nrows_x : nrows_a;
samer@4 78 ncols_y=ncols_x>ncols_a ? ncols_x : ncols_a;
samer@4 79 plhs[0]=mxCreateDoubleMatrix(nrows_y, ncols_y, mxREAL);
samer@4 80 /* assign pointers to each input and output */
samer@4 81 x=mxGetPr(prhs[0]);
samer@4 82 a=mxGetPr(prhs[1]);
samer@4 83 y=mxGetPr(plhs[0]);
samer@4 84 /* compute zeta function */
samer@4 85 gsl_set_error_handler_off();
samer@4 86 for (i_x=i_a=i_y=0; i_y<nrows_y*ncols_y; ++i_y) {
samer@4 87 if (gsl_sf_hzeta_e(x[i_x], a[i_a], &result)==GSL_SUCCESS)
samer@4 88 y[i_y]=result.val;
samer@4 89 else
samer@4 90 y[i_y]=GSL_NAN;
samer@4 91 if (!(nrows_x==1 && ncols_x==1))
samer@4 92 ++i_x;
samer@4 93 if (!(nrows_a==1 && ncols_a==1))
samer@4 94 ++i_a;
samer@4 95 }
samer@4 96 }