POD for Math::GSL::Test
[Math-GSL.git] / Deriv.i
blobf4497f084ccd5d80d09acbbf60d70ea80e94fa8a
1 %module "Math::GSL::Deriv"
2 // Danger Will Robinson, for realz!
4 %include "typemaps.i"
5 %include "gsl_typemaps.i"
6 %typemap(argout) (const gsl_function *f,
7 double x, double h,
8 double *result, double *abserr) {
9 SV ** sv;
10 //AV* av = newAV();
12 sv = hv_fetch(Callbacks, (char*)&$input, sizeof($input), FALSE );
13 if (sv == (SV**)NULL)
14 croak("Math::GSL(argout) : Missing callback!\n");
15 dSP;
17 PUSHMARK(SP);
18 // these are the arguments passed to the callback
19 XPUSHs(sv_2mortal(newSViv((int)$2)));
20 PUTBACK;
22 /* This actually calls the perl subroutine */
23 call_sv(*sv, G_SCALAR);
25 //av_push(av, newSVnv((double) *$4));
26 //av_push(av, newSVnv((double) *$5));
27 //$result = sv_2mortal( newRV_noinc( (SV*) av) );
28 $result = sv_newmortal();
29 sv_setnv($result, (double) *$4);
30 argvi++;
31 sv_setnv($result, (double) *$5);
32 argvi++;
34 if (argvi >= items) {
35 EXTEND(SP,1);
40 %typemap(in) void * {
41 $1 = (double *) $input;
44 #include "gsl/gsl_math.h"
45 #include "gsl/gsl_deriv.h"
48 %include "gsl/gsl_math.h"
49 %include "gsl/gsl_deriv.h"
51 %perlcode %{
52 @EXPORT_OK = qw/
53 gsl_deriv_central
54 gsl_deriv_backward
55 gsl_deriv_forward
57 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
59 __END__
61 =head1 NAME
63 Math::GSL::Deriv - Numerical Derivatives
65 =head1 SYNOPSIS
67 use Math::GSL::Deriv qw/:all/;
68 use Math::GSL::Errno qw/:all/;
70 my ($x, $h) = (1.5, 0.01);
71 my ($status, $val,$err) = gsl_deriv_central ( sub { sin($_[0]) }, $x, $h);
72 my $res = abs($val - cos($x));
73 if ($status == $GSL_SUCCESS) {
74 printf "deriv(sin((%g)) = %.18g, max error=%.18g\n", $x, $val, $err;
75 printf " cos(%g)) = %.18g, residue= %.18g\n" , $x, cos($x), $res;
76 } else {
77 my $gsl_error = gsl_strerror($status);
78 print "Numerical Derivative FAILED, reason:\n $gsl_error\n\n";
82 =head1 DESCRIPTION
84 This module allows you to take the numerical derivative of a Perl subroutine. To find
85 a numerical derivative you must also specify a point to evaluate the derivative and a
86 "step size". The step size is a knob that you can turn to get a more finely or coarse
87 grained approximation. As the step size $h goes to zero, the formal definition of a
88 derivative is reached, but in practive you must choose a reasonable step size to get
89 a reasonable answer. Usually something in the range of 1/10 to 1/10000 is sufficient.
91 So long as your function returns a single scalar value, you can differentiate as
92 complicated a function as your heart desires.
94 =over
96 =item * C<gsl_deriv_central($function, $x, $h)>
98 use Math::GSL::Deriv qw/gsl_deriv_central/;
99 my ($x, $h) = (1.5, 0.01);
100 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
102 my ($status, $val,$err) = gsl_deriv_central ( \&func , $x, $h);
104 This method approximates the central difference of the subroutine reference
105 $function, evaluated at $x, with "step size" $h. This means that the
106 function is evaluated at $x-$h and $x+h.
109 =item * C<gsl_deriv_backward($function, $x, $h)>
111 use Math::GSL::Deriv qw/gsl_deriv_backward/;
112 my ($x, $h) = (1.5, 0.01);
113 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
115 my ($status, $val,$err) = gsl_deriv_backward ( \&func , $x, $h);
117 This method approximates the backward difference of the subroutine
118 reference $function, evaluated at $x, with "step size" $h. This means that
119 the function is evaluated at $x-$h and $x.
121 =item * C<gsl_deriv_forward($function, $x, $h)>
123 use Math::GSL::Deriv qw/gsl_deriv_forward/;
124 my ($x, $h) = (1.5, 0.01);
125 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
127 my ($status, $val,$err) = gsl_deriv_forward ( \&func , $x, $h);
129 This method approximates the forward difference of the subroutine reference
130 $function, evaluated at $x, with "step size" $h. This means that the
131 function is evaluated at $x and $x+$h.
133 =back
135 For more informations on the functions, we refer you to the GSL offcial
136 documentation: L<http://www.gnu.org/software/gsl/manual/html_node/>
138 =head1 AUTHORS
140 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
142 =head1 COPYRIGHT AND LICENSE
144 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
146 This program is free software; you can redistribute it and/or modify it
147 under the same terms as Perl itself.
149 =cut