use strict; use warnings; { no warnings 'once'; # pass info back to Makefile.PL $PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= join '', map " $::PDLBASE-$_\$(OBJ_EXT)", qw(FUNC); } pp_bless('PDL::GSL::MROOT'); pp_add_exported('','gslmroot_fsolver'); pp_addhdr(' #include #include #include #include #include void set_funname(SV *fn, PDL_Indx n); int my_f (const gsl_vector * v, void * params, gsl_vector * df); int fsolver (double *xfree, int nelem, double epsabs, int method); '); pp_def('gslmroot_fsolver', Pars => 'double [io]xfree(n); double epsabs(); int method();', OtherPars => 'SV* function1;', Doc => <<'EOF', =for ref Multidimensional root finder without using derivatives This function provides an interface to the multidimensional root finding algorithms in the GSL library. It takes a minimum of two arguments: an ndarray $init with an initial guess for the roots of the system and a reference to a function. The latter function must return an ndarray whose i-th element is the i-th equation evaluated at the vector x (an ndarray which is the sole input to this function). See the example in the Synopsis above for an illustration. The function returns an ndarray with the roots for the system of equations. Two optional arguments can be specified as shown below. One is B, which can take the values 0,1,2,3. They correspond to the 'hybrids', 'hybrid', 'dnewton' and 'broyden' algorithms respectively (see GSL documentation for details). The other optional argument is B, which sets the absolute accuracy to which the roots of the system of equations are required. The default value for Method is 0 ('hybrids' algorithm) and the default for Epsabs is 1e-3. =for usage $res = gslmroot_fsolver($init, $function_ref, [{Method => $method, Epsabs => $epsabs}]); EOF Code =>' set_funname($COMP(function1), $SIZE(n)); if (fsolver($P(xfree), $SIZE(n), $epsabs(), $method()) != GSL_SUCCESS) $CROAK("Something is wrong: could not assign fsolver type...\n"); ', PMCode => <<'EOF', sub gslmroot_fsolver { my ($x, $f_vect) = @_; my $opt = ref($_[-1]) eq 'HASH' ? pop @_ : {Method => 0, EpsAbs => 1e-3}; if( (ref($x) ne 'PDL')){ barf("Have to pass ndarray as first argument to fsolver\n"); } my $res = $x->copy; _gslmroot_fsolver_int($res, $$opt{'EpsAbs'}, $$opt{'Method'}, $f_vect); return $res; } EOF ); pp_addpm({At=>'Top'},<<'EOD'); use strict; use warnings; =head1 NAME PDL::GSL::MROOT - PDL interface to multidimensional root-finding routines in GSL =head1 DESCRIPTION This is an interface to the multidimensional root-finding package present in the GNU Scientific Library. At the moment there is a single function B which provides an interface to the algorithms in the GSL library that do not use derivatives. =head1 SYNOPSIS use PDL; use PDL::GSL::MROOT; my $init = pdl (-10.00, -5.0); my $epsabs = 1e-7; $res = gslmroot_fsolver($init, \&rosenbrock, {Method => 0, EpsAbs => $epsabs}); sub rosenbrock{ my ($x) = @_; my $c = 1; my $d = 10; my $y = zeroes($x); my $y0 = $y->slice(0); $y0 .= $c * (1 - $x->slice(0)); my $y1 = $y->slice(1); $y1 .= $d * ($x->slice(1) - $x->slice(0)**2); return $y; } EOD pp_addpm({At=>'Bot'},<<'EOD'); =head1 SEE ALSO L The GSL documentation is online at http://www.gnu.org/software/gsl/manual/ =head1 AUTHOR This file copyright (C) 2006 Andres Jordan and Simon Casassus All rights reserved. There is no warranty. You are allowed to redistribute this software/documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file. =cut EOD pp_add_boot('gsl_set_error_handler_off(); '); pp_done();