blib/lib/PDL/Demos/Func_demo.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« blib/lib/Test/Deep/PDL.pm ↑ Summary blib/lib/PDL/Demos/Transform_demo.pm »
1 package PDL::Demos::Func_demo;
2  
3 0 ▶use PDL::Graphics::Simple;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use PDL::Func;
5  
6 sub info {('func', 'Interpolation etc (Req.: PDL::Graphics::Simple)')}
7  
8 sub init {'
9 use PDL::Graphics::Simple;
10 '}
11  
12 my @demo = (
13 [act => q|
14 # This demo illustrates the PDL::Func module.
15 # PDL::Func objects encapsulate data to interpolate, integrate,
16 # and get gradients of (differentiate).
17  
18 use PDL::Func qw(pchip spline); # load, and import convenience functions
19 $w = pgswin(); # PDL::Graphics::Simple window
20 |],
21  
22 [act => q|
23 # set up a step function, similar to
24 # https://uk.mathworks.com/help/matlab/ref/pchip.html
25 $x = sequence(7) - 3;
26 $y = pdl q[-1 -1 -1 0 1 1 1];
27 # The convenience function "pchip" uses SLATEC's PCHIP with all
28 # the default settings
29 $xi = zeroes(100)->xlinvals(-3,3);
30 $yi = pchip($x, $y, $xi);
31 $yi_s = spline($x, $y, $xi);
32 $w->plot(with => 'line', key => 'spline', $xi, $yi_s,
33 with => 'line', key => 'pchip', $xi, $yi,
34 with => 'points', $x, $y,
35 {legend=>'tl'});
36 |],
37  
38 [act => q|
39 # Now a more undulating function, where PCHIP is less effective
40 $x2 = sequence(16);
41 $y2 = bessj1($x2);
42 $xi2 = zeroes(100)->xlinvals(0,15);
43 $yi2 = pchip($x2, $y2, $xi2);
44 $yi2_s = spline($x2, $y2, $xi2);
45 $w->plot(with => 'line', key => 'spline', $xi2, $yi2_s,
46 with => 'line', key => 'pchip', $xi2, $yi2,
47 with => 'points', $x2, $y2,
48 {legend=>'tr'});
49 |],
50  
51 [act => q|
52 # And because it's PDL, it can broadcast seamlessly
53 $y3 = cat( $x2*$x2+43.3, $x2*$x2*$x2-23 ); # dim 16,2
54 $yi3 = pchip($x2, $y3, $xi2);
55 # even though PDL::Graphics::Simple can't (yet)
56 my @y3d = $y3->dog;
57 my @yi3d = $yi3->dog;
58 $w->plot(with => 'points', $x2, $y3d[0],
59 with => 'points', $x2, $y3d[1],
60 with => 'line', $xi2, $yi3d[0],
61 with => 'line', $xi2, $yi3d[1]);
62 |],
63  
64 [comment => q|
65 This concludes the PDL::Func demo.
66  
67 Be sure to check the documentation for PDL::Func, to see further
68 possibilities.
69 |],
70 );
71  
72 sub demo { @demo }
73 sub done {'
74 undef $w;
75 '}
76  
77 1;
« blib/lib/Test/Deep/PDL.pm ↑ Summary blib/lib/PDL/Demos/Transform_demo.pm »