File: INTERP.pd

package info (click to toggle)
libpdl-gsl-perl 2.101-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 600 kB
  • sloc: perl: 1,587; ansic: 202; makefile: 9
file content (378 lines) | stat: -rw-r--r-- 8,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use strict;
use warnings;

pp_bless('PDL::GSL::INTERP');  # make the functions generated go into our namespace

pp_addpm({At=>'Top'},<<'EOD');
use strict;
use warnings;

=head1 NAME

PDL::GSL::INTERP - PDL interface to Interpolation routines in GSL

=head1 DESCRIPTION

This is an interface to the interpolation package present in the
GNU Scientific Library.

=head1 SYNOPSIS

   use PDL;
   use PDL::GSL::INTERP;

   my $x = sequence(10);
   my $y = exp($x);

   my $spl = PDL::GSL::INTERP->init('cspline',$x,$y);

   my $res = $spl->eval(4.35);
   $res = $spl->deriv(4.35);
   $res = $spl->deriv2(4.35);
   $res = $spl->integ(2.1,7.4);

=head1 NOMENCLATURE

Throughout this documentation we strive to use the same variables that
are present in the original GSL documentation (see L<See
Also|"SEE-ALSO">). Oftentimes those variables are called C<a> and
C<b>. Since good Perl coding practices discourage the use of Perl
variables C<$a> and C<$b>, here we refer to Parameters C<a> and C<b>
as C<$pa> and C<$pb>, respectively, and Limits (of domain or
integration) as C<$la> and C<$lb>.
EOD

pp_addpm({At=>'Bot'},<<'EOD');
=head1 BUGS

Feedback is welcome.

=head1 SEE ALSO

L<PDL>

The GSL documentation for interpolation is online at
L<https://www.gnu.org/software/gsl/doc/html/interp.html>

=head1 AUTHOR

This file copyright (C) 2003 Andres Jordan <andresj@physics.rutgers.edu>
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.

The GSL interpolation module was written by Gerard Jungman.

=cut
EOD

pp_addhdr('
#include<string.h>
#include<math.h>
#include<gsl/gsl_errno.h>
#include<gsl/gsl_spline.h>
#include "gslerr.h"

typedef gsl_spline GslSpline;
typedef gsl_interp_accel GslAccel;

');

pp_def('init',
        Pars => 'double x(n); double y(n);',
        OtherPars => 'gsl_spline *spl',
        Doc => <<'EOF',
=for ref

The init method initializes a new instance of INTERP. It needs as
input an interpolation type and two ndarrays holding the x and y
values to be interpolated. The GSL routines require that x be
monotonically increasing and a quicksort is performed by default to
ensure that. You can skip the quicksort by passing the option
{Sort => 0}.

The available interpolation types are :

=over 2

=item linear

=item polynomial

=item cspline (natural cubic spline)

=item cspline_periodic  (periodic cubic spline)

=item akima (natural akima spline)

=item akima_periodic  (periodic akima spline)

=back

Please check the GSL documentation for more information.

=for usage

Usage:

    $blessed_ref = PDL::GSL::INTERP->init($interp_method,$x,$y,$opt);

=for example

Example:

    $x = sequence(10);
    $y = exp($x);

    $spl = PDL::GSL::INTERP->init('cspline',$x,$y)
    $spl = PDL::GSL::INTERP->init('cspline',$x,$y,{Sort => 1}) #same as above

    # no sorting done on x, user is certain that x is monotonically increasing
    $spl = PDL::GSL::INTERP->init('cspline',$x,$y,{Sort => 0});
=cut
EOF
        Code =>'GSLERR(gsl_spline_init,($COMP(spl),$P(x),$P(y),$SIZE(n)));',
        PMCode => <<'EOF',
sub init {
  my $opt;
  if (ref($_[$#_]) eq 'HASH'){ $opt = pop @_; }
  else{ $opt = {Sort => 1}; }
  my ($class,$type,$x,$y) = @_;
  if( (ref($x) ne 'PDL') || (ref($y) ne 'PDL') ){
    barf("Have to pass ndarrays as arguments to init method\n");
  }
  if($$opt{Sort} != 0){
    my $idx = PDL::Ufunc::qsorti($x);
    $x = $x->index($idx);
    $y = $y->index($idx);
  }
  my $ene = nelem($x);
  my $obj1 = new_spline($type,$ene);
  my $obj2 = new_accel();
  _init_int($x,$y,$$obj1);
  my @ret_a = ($obj1,$obj2);
  return bless(\@ret_a, $class);
}
EOF
);

pp_def('eval',
        Pars => 'double x(); double [o] out();',
        OtherPars => 'gsl_spline *spl;gsl_interp_accel *acc;',
        Doc => <<'EOF',
=for ref

The function eval returns the interpolating function at a given point.
It will barf with an "input domain error" if you try to extrapolate.

=for usage

Usage:

    $result = $spl->eval($points);

=for example

Example:

    my $res = $spl->eval($x)

=cut
EOF
        PMCode => <<'EOF',
sub eval {
  my $opt;
  my ($obj,$x) = @_;
  my $s_obj = $$obj[0];
  my $a_obj = $$obj[1];
  _eval_int($x,my $o=PDL->null,$$s_obj,$$a_obj);
  $o;
}
EOF
	HandleBad => 1,
        Code => '
        PDL_IF_BAD(if ($ISBAD($x())) {
          $out() = $x();
	} else,) {
	  GSLERR(gsl_spline_eval_e,($COMP(spl), $x(), $COMP(acc), $P(out)));
        }
        ',
);

pp_def('deriv',
        Pars => 'double x(); double [o] out();',
        OtherPars => 'gsl_spline *spl;gsl_interp_accel *acc;',
        PMCode => <<'EOF',
sub deriv {
  my ($obj,$x) = @_;
  my $s_obj = $$obj[0];
  my $a_obj = $$obj[1];
  _deriv_int($x,my $o=PDL->null,$$s_obj,$$a_obj);
  $o;
}
EOF
        Doc => <<'EOF',
=for ref

The deriv function returns the derivative of the
interpolating function at a given point.
It will barf with an "input domain error" if you try to extrapolate.

=for usage

Usage:

    $result = $spl->deriv($points);

=for example

Example:

    my $res = $spl->deriv($x)

=cut
EOF
	Code =>'
GSLERR(gsl_spline_eval_deriv_e,($COMP(spl), $x(), $COMP(acc), $P(out)));
');

pp_def('deriv2',
        Pars => 'double x(); double [o] out();',
        OtherPars => 'gsl_spline *spl;gsl_interp_accel *acc;',
        Doc => <<'EOF',
=for ref

The deriv2 function returns the second derivative
of the interpolating function at a given point.
It will barf with an "input domain error" if you try to extrapolate.

=for usage

Usage:

    $result = $spl->deriv2($points);

=for example

Example:

    my $res = $spl->deriv2($x)

=cut
EOF
        PMCode => <<'EOF',
sub deriv2 {
  my ($obj,$x) = @_;
  my $s_obj = $$obj[0];
  my $a_obj = $$obj[1];
  _deriv2_int($x,my $o=PDL->null,$$s_obj,$$a_obj);
  $o;
}
EOF
	Code =>'
GSLERR(gsl_spline_eval_deriv2_e,($COMP(spl), $x(), $COMP(acc), $P(out)));
');

pp_def('integ',
        Pars => 'double a(); double b(); double [o] out();',
        OtherPars => 'gsl_spline *spl;gsl_interp_accel *acc;',
        Doc => <<'EOF',
=for ref

The integ function returns the integral
of the interpolating function between two points.
It will barf with an "input domain error" if you try to extrapolate.

=for usage

Usage:

    $result = $spl->integ($la,$lb);

=for example

Example:

    my $res = $spl->integ($la,$lb)

=cut
EOF
        PMCode => <<'EOF',
sub integ {
  my ($obj,$la,$lb) = @_;
  my $s_obj = $$obj[0];
  my $a_obj = $$obj[1];
  _integ_int($la,$lb,my $o=PDL->null,$$s_obj,$$a_obj);
  $o;
}
EOF
	Code =>'
GSLERR(gsl_spline_eval_integ_e,($COMP(spl), $a(), $b(), $COMP(acc),$P(out)));
');

# XS functions for the INTERP objects

pp_addxs('','
MODULE = PDL::GSL::INTERP PACKAGE = PDL::GSL::INTERP

#define DEF_INTERP(X) if (!strcmp(TYPE,#X)) spline=gsl_spline_alloc( gsl_interp_ ## X , ene); strcat(ula,#X ", ");

GslSpline *
new_spline (TYPE,ene)
  char *TYPE
  int ene
 CODE:
  GslSpline * spline = NULL;
  char ula[100];
strcpy(ula,"");
DEF_INTERP(linear);
DEF_INTERP(polynomial);
DEF_INTERP(cspline);
DEF_INTERP(cspline_periodic);
DEF_INTERP(akima);
DEF_INTERP(akima_periodic);
  if (spline==NULL) {
    barf("Unknown interpolation type, please use one of the following: %s", ula);
  }
  else
  RETVAL = spline;
 OUTPUT:
  RETVAL

GslAccel *
new_accel ()
 CODE:
  GslAccel * accel = NULL;
  accel = gsl_interp_accel_alloc();
  if (accel == NULL){
    barf("Problem allocating accelerator object\n");
  }
  RETVAL = accel;
 OUTPUT:
  RETVAL

MODULE = PDL::GSL::INTERP PACKAGE = GslSplinePtr PREFIX = spl_

void
spl_DESTROY(spline)
  GslSpline * spline
 CODE:
  gsl_spline_free(spline);

MODULE = PDL::GSL::INTERP PACKAGE = GslAccelPtr PREFIX = acc_

void
acc_DESTROY(accel)
  GslAccel * accel
 CODE:
  gsl_interp_accel_free(accel);

');

pp_export_nothing;

pp_add_boot('gsl_set_error_handler_off();
');

pp_done();