blib/lib/PDL/Basic.pm

filter: Statementstmt 82.7% 149 / 180 Branchbran 61.4% 59 / 96 Conditioncond 58.3% 14 / 24 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 90.0% 27 / 30 Podpod 20.0% 5 / 25 totaltotal 69.5% 258 / 371 CC 58 SCAR 15.7
CC
58
Cov
74%
ins
12.0
_hist_bin_calc
11.1
rvals
8.0
CRAP 4.8
« blib/lib/PDL/Exporter.pm ↑ Summary blib/lib/PDL/Demos/TriDGallery.pm »
1  
2 =head1 NAME
3  
4 PDL::Basic -- Basic utility functions for PDL
5  
6 =head1 DESCRIPTION
7  
8 This module contains basic utility functions for
9 creating and manipulating piddles. Most of these functions
10 are simplified interfaces to the more flexible functions in
11 the modules
12 L<PDL::Primitive|PDL::Primitive>
13 and
14 L<PDL::Slices|PDL::Slices>.
15  
16 =head1 SYNOPSIS
17  
18 use PDL::Basic;
19  
20 =head1 FUNCTIONS
21  
22 =cut
23  
24 package PDL::Basic;
25 120 ▶use PDL::Core '';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 120 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 120 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 120 ▶use PDL::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 120 @ISA=qw/PDL::Exporter/;
31 120 @EXPORT_OK = qw/ ndcoords rvals axisvals allaxisvals xvals yvals zvals sec ins hist whist
32 similar_assign transpose sequence xlinvals ylinvals
33 zlinvals axislinvals/;
34 120 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
35  
36 # Exportable functions
37 120 *axisvals = \&PDL::axisvals;
38 120 *allaxisvals = \&PDL::allaxisvals;
39 120 *sec = \&PDL::sec;
40 120 *ins = \&PDL::ins;
41 120 *hist = \&PDL::hist;
42 120 *whist = \&PDL::whist;
43 120 *similar_assign = \&PDL::similar_assign;
44 120 *transpose = \&PDL::transpose;
45 120 *xlinvals = \&PDL::xlinvals;
46 120 *ylinvals = \&PDL::ylinvals;
47 120 *zlinvals = \&PDL::zlinvals;
48  
49 =head2 xvals
50  
51 =for ref
52  
53 Fills a piddle with X index values. Uses similar specifications to
54 L<zeroes|zeroes> and L<new_from_specification|new_from_specification>.
55  
56 CAVEAT:
57  
58 If you use the single argument piddle form (top row
59 in the usage table) the output will have the same type as the input;
60 this may give surprising results if, e.g., you have a byte array with
61 a dimension of size greater than 256. To force a type, use the third form.
62  
63 =for usage
64  
65 $x = xvals($somearray);
66 $x = xvals([OPTIONAL TYPE],$nx,$ny,$nz...);
67 $x = xvals([OPTIONAL TYPE], $somarray->dims);
68  
69 etc. see L<zeroes|PDL::Core/zeroes>.
70  
71 =for example
72  
73 pdl> print xvals zeroes(5,10)
74 [
75 [0 1 2 3 4]
76 [0 1 2 3 4]
77 [0 1 2 3 4]
78 [0 1 2 3 4]
79 [0 1 2 3 4]
80 [0 1 2 3 4]
81 [0 1 2 3 4]
82 [0 1 2 3 4]
83 [0 1 2 3 4]
84 [0 1 2 3 4]
85 ]
86  
87 =head2 yvals
88  
89 =for ref
90  
91 Fills a piddle with Y index values. See the CAVEAT for L<xvals|xvals>.
92  
93 =for usage
94  
95 $x = yvals($somearray); yvals(inplace($somearray));
96 $x = yvals([OPTIONAL TYPE],$nx,$ny,$nz...);
97  
98 etc. see L<zeroes|PDL::Core/zeroes>.
99  
100 =for example
101  
102 pdl> print yvals zeroes(5,10)
103 [
104 [0 0 0 0 0]
105 [1 1 1 1 1]
106 [2 2 2 2 2]
107 [3 3 3 3 3]
108 [4 4 4 4 4]
109 [5 5 5 5 5]
110 [6 6 6 6 6]
111 [7 7 7 7 7]
112 [8 8 8 8 8]
113 [9 9 9 9 9]
114 ]
115  
116 =head2 zvals
117  
118 =for ref
119  
120 Fills a piddle with Z index values. See the CAVEAT for L<xvals|xvals>.
121  
122 =for usage
123  
124 $x = zvals($somearray); zvals(inplace($somearray));
125 $x = zvals([OPTIONAL TYPE],$nx,$ny,$nz...);
126  
127 etc. see L<zeroes|PDL::Core/zeroes>.
128  
129 =for example
130  
131 pdl> print zvals zeroes(3,4,2)
132 [
133 [
134 [0 0 0]
135 [0 0 0]
136 [0 0 0]
137 [0 0 0]
138 ]
139 [
140 [1 1 1]
141 [1 1 1]
142 [1 1 1]
143 [1 1 1]
144 ]
145 ]
146  
147 =head2 xlinvals
148  
149 =for ref
150  
151 X axis values between endpoints (see L<xvals|/xvals>).
152  
153 =for usage
154  
155 $a = zeroes(100,100);
156 $x = $a->xlinvals(0.5,1.5);
157 $y = $a->ylinvals(-2,-1);
158 # calculate Z for X between 0.5 and 1.5 and
159 # Y between -2 and -1.
160 $z = f($x,$y);
161  
162 C<xlinvals>, C<ylinvals> and C<zlinvals> return a piddle with the same shape
163 as their first argument and linearly scaled values between the two other
164 arguments along the given axis.
165  
166 =head2 ylinvals
167  
168 =for ref
169  
170 Y axis values between endpoints (see L<yvals|/yvals>).
171  
172 See L<xlinvals|/xlinvals> for more information.
173  
174 =head2 zlinvals
175  
176 =for ref
177  
178 Z axis values between endpoints (see L<zvals|/zvals>).
179  
180 See L<xlinvals|/xlinvals> for more information.
181  
182 =head2 xlogvals
183  
184 =for ref
185  
186 X axis values logarithmically spaced between endpoints (see L<xvals|/xvals>).
187  
188 =for usage
189  
190 $a = zeroes(100,100);
191 $x = $a->xlogvals(1e-6,1e-3);
192 $y = $a->ylinvals(1e-4,1e3);
193 # calculate Z for X between 1e-6 and 1e-3 and
194 # Y between 1e-4 and 1e3.
195 $z = f($x,$y);
196  
197 C<xlogvals>, C<ylogvals> and C<zlogvals> return a piddle with the same shape
198 as their first argument and logarithmically scaled values between the two other
199 arguments along the given axis.
200  
201 =head2 ylogvals
202  
203 =for ref
204  
205 Y axis values logarithmically spaced between endpoints (see L<yvals|/yvals>).
206  
207 See L<xlogvals|/xlogvals> for more information.
208  
209 =head2 zlogvals
210  
211 =for ref
212  
213 Z axis values logarithmically spaced between endpoints (see L<zvals|/zvals>).
214  
215 See L<xlogvals|/xlogvals> for more information.
216  
217 =cut
218  
219 # Conveniently named interfaces to axisvals()
220  
221 116 ▶sub xvals { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? $_[0]->xvals : PDL->xvals(@_) }
Subroutine xvals: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'PDL::Type' ? : 88 28 116
Condition100%3/3
ref $_[0] and ref $_[0] ne "PDL::Type"
!ll&&!rl&&r
25388
MC/DC100%2/2
ref $_[0] and ref $_[0] ne "PDL::Type"
ref $_[0] ref $_[0] ne "PDL::Type"
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] ne "PDL::Type"
ABresult
0X0
100
111
222 23 ▶sub yvals { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? $_[0]->yvals : PDL->yvals(@_) }
Subroutine yvals: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'PDL::Type' ? : 10 13 23
Condition66%2/3
ref $_[0] and ref $_[0] ne "PDL::Type"
!ll&&!rl&&r
13010
MC/DC50%1/2
ref $_[0] and ref $_[0] ne "PDL::Type"
ref $_[0] ref $_[0] ne "PDL::Type"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "PDL::Type"
ABresult
0X0
100
111
223 3 ▶sub zvals { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? $_[0]->zvals : PDL->zvals(@_) }
Subroutine zvals: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'PDL::Type' ? : 1 2 3
Condition66%2/3
ref $_[0] and ref $_[0] ne "PDL::Type"
!ll&&!rl&&r
201
MC/DC50%1/2
ref $_[0] and ref $_[0] ne "PDL::Type"
ref $_[0] ref $_[0] ne "PDL::Type"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "PDL::Type"
ABresult
0X0
100
111
224 sub PDL::xvals {
225 307 ▶ my $class = shift;
Subroutine xvals: called CC=2 SCAR=6.9
Pod undocumented
226 307 ▶ my $pdl = scalar(@_)? $class->new_from_specification(@_) : $class->new_or_inplace;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 31 276 307
227 307 axisvals2($pdl,0);
228 307 return $pdl;
229 }
230 sub PDL::yvals {
231 30 ▶ my $class = shift;
Subroutine yvals: called CC=2 SCAR=6.9
Pod undocumented
232 30 ▶ my $pdl = scalar(@_)? $class->new_from_specification(@_) : $class->new_or_inplace;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 16 14 30
233 30 axisvals2($pdl,1);
234 30 return $pdl;
235 }
236 sub PDL::zvals {
237 5 ▶ my $class = shift;
Subroutine zvals: called CC=2 SCAR=6.9
Pod undocumented
238 5 ▶ my $pdl = scalar(@_)? $class->new_from_specification(@_) : $class->new_or_inplace;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 2 3 5
239 5 axisvals2($pdl,2);
240 5 return $pdl;
241 }
242  
243 sub PDL::xlinvals {
244 1 ▶ my $dim = $_[0]->getdim(0);
Subroutine xlinvals: called CC=2 SCAR=7.1
Pod undocumented
245 1 ▶ barf "Must have at least two elements in dimension for xlinvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
246 if $dim <= 1;
247 1 return $_[0]->xvals * (($_[2] - $_[1]) / ($dim-1)) + $_[1];
248 }
249  
250 sub PDL::ylinvals {
251 1 ▶ my $dim = $_[0]->getdim(1);
Subroutine ylinvals: called CC=2 SCAR=7.1
Pod undocumented
252 1 ▶ barf "Must have at least two elements in dimension for ylinvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
253 if $dim <= 1;
254 1 return $_[0]->yvals * (($_[2] - $_[1]) / ($dim-1)) + $_[1];
255 }
256  
257 sub PDL::zlinvals {
258 1 ▶ my $dim = $_[0]->getdim(2);
Subroutine zlinvals: called CC=2 SCAR=7.1
Pod undocumented
259 1 ▶ barf "Must have at least two elements in dimension for zlinvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
260 if $dim <= 1;
261 1 return $_[0]->zvals * (($_[2] - $_[1]) / ($dim-1)) + $_[1];
262 }
263  
264 sub PDL::xlogvals {
265 1 ▶ my $dim = $_[0]->getdim(0);
Subroutine xlogvals: called CC=4 SCAR=15.0
Pod undocumented
266 1 ▶ barf "Must have at least two elements in dimension for xlogvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
267 if $dim <= 1;
268 1 my ($xmin,$xmax) = @_[1,2];
269 1 ▶ barf "xmin and xmax must be positive"
Branch: 1 branch
BranchTrueFalseTotal
if $xmin <= 0 or $xmax <= 0 0 1 1
Condition33%1/3
$xmin <= 0 or $xmax <= 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$xmin <= 0 or $xmax <= 0
$xmin <= 0 $xmax <= 0
Truth table33%1/3
A or B
A= $xmin <= 0B= $xmax <= 0
ABresult
1X1
011
000
270 if $xmin <= 0 || $xmax <= 0;
271 1 my ($lxmin,$lxmax) = (log($xmin), log($xmax));
272 1 return exp($_[0]->xvals * (($lxmax - $lxmin) / ($dim-1)) + $lxmin);
273 }
274  
275 sub PDL::ylogvals {
276 1 ▶ my $dim = $_[0]->getdim(1);
Subroutine ylogvals: called CC=4 SCAR=15.0
Pod undocumented
277 1 ▶ barf "Must have at least two elements in dimension for xlogvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
278 if $dim <= 1;
279 1 my ($xmin,$xmax) = @_[1,2];
280 1 ▶ barf "xmin and xmax must be positive"
Branch: 1 branch
BranchTrueFalseTotal
if $xmin <= 0 or $xmax <= 0 0 1 1
Condition33%1/3
$xmin <= 0 or $xmax <= 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$xmin <= 0 or $xmax <= 0
$xmin <= 0 $xmax <= 0
Truth table33%1/3
A or B
A= $xmin <= 0B= $xmax <= 0
ABresult
1X1
011
000
281 if $xmin <= 0 || $xmax <= 0;
282 1 my ($lxmin,$lxmax) = (log($xmin), log($xmax));
283 1 return exp($_[0]->yvals * (($lxmax - $lxmin) / ($dim-1)) + $lxmin);
284 }
285  
286 sub PDL::zlogvals {
287 1 ▶ my $dim = $_[0]->getdim(2);
Subroutine zlogvals: called CC=4 SCAR=15.0
Pod undocumented
288 1 ▶ barf "Must have at least two elements in dimension for xlogvals"
Branch: 1 branch
BranchTrueFalseTotal
if $dim <= 1 0 1 1
289 if $dim <= 1;
290 1 my ($xmin,$xmax) = @_[1,2];
291 1 ▶ barf "xmin and xmax must be positive"
Branch: 1 branch
BranchTrueFalseTotal
if $xmin <= 0 or $xmax <= 0 0 1 1
Condition33%1/3
$xmin <= 0 or $xmax <= 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$xmin <= 0 or $xmax <= 0
$xmin <= 0 $xmax <= 0
Truth table33%1/3
A or B
A= $xmin <= 0B= $xmax <= 0
ABresult
1X1
011
000
292 if $xmin <= 0 || $xmax <= 0;
293 1 my ($lxmin,$lxmax) = (log($xmin), log($xmax));
294 1 return exp($_[0]->zvals * (($lxmax - $lxmin) / ($dim-1)) + $lxmin);
295 }
296  
297  
298 =head2 allaxisvals
299  
300 =for ref
301  
302 Synonym for L<ndcoords|ndcoords> - enumerates all coordinates in a
303 PDL or dim list, adding an extra dim on the front to accommodate
304 the vector coordinate index (the form expected by L<indexND|indexND>,
305 L<range|range>, and L<interpND|interpND>). See L<ndcoords|ndcoords> for more detail.
306  
307 =for usage
308  
309 $indices = allaxisvals($pdl);
310 $indices = allaxisvals(@dimlist);
311 $indices = allaxisvals($type,@dimlist);
312  
313 =cut
314  
315 =head2 ndcoords
316  
317 =for ref
318  
319 Enumerate pixel coordinates for an N-D piddle
320  
321 Returns an enumerated list of coordinates suitable for use in
322 L<indexND|PDL::Slices/indexND> or L<range|PDL::Slices/range>: you feed
323 in a dimension list and get out a piddle whose 0th dimension runs over
324 dimension index and whose 1st through Nth dimensions are the
325 dimensions given in the input. If you feed in a piddle instead of a
326 perl list, then the dimension list is used, as in L<xvals|xvals> etc.
327  
328 Unlike L<xvals|xvals> etc., if you supply a piddle input, you get
329 out a piddle of the default piddle type: double. This causes less
330 surprises than the previous default of keeping the data type of
331 the input piddle since that rarely made sense in most usages.
332  
333 =for usage
334  
335 $indices = ndcoords($pdl);
336 $indices = ndcoords(@dimlist);
337 $indices = ndcoords($type,@dimlist);
338  
339 =for example
340  
341 pdl> print ndcoords(2,3)
342  
343 [
344 [
345 [0 0]
346 [1 0]
347 ]
348 [
349 [0 1]
350 [1 1]
351 ]
352 [
353 [0 2]
354 [1 2]
355 ]
356 ]
357  
358 pdl> $a = zeroes(byte,2,3); # $a is a 2x3 byte piddle
359 pdl> $b = ndcoords($a); # $b inherits $a's type
360 pdl> $c = ndcoords(long,$a->dims); # $c is a long piddle, same dims as $b
361 pdl> help $b;
362 This variable is Byte D [2,2,3] P 0.01Kb
363 pdl> help $c;
364 This variable is Long D [2,2,3] P 0.05Kb
365  
366  
367 =cut
368  
369 sub PDL::ndcoords {
370 35 ▶ my $type;
Subroutine ndcoords: called CC=5 SCAR=16.6
Pod undocumented
371 35 ▶ if(ref $_[0] eq 'PDL::Type') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "PDL::Type") 0 35 35
372 0 $type = shift;
373 }
374
375 35 ▶ my @dims = (ref $_[0]) ? (shift)->dims : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 35 35
376 35 my @d = @dims;
377 35 unshift(@d,scalar(@dims));
378 35 ▶ unshift(@d,$type) if defined($type);
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 35 35
379  
380 35 $out = PDL->zeroes(@d);
381
382 35 for my $d(0..$#dims) {
383 70 my $a = $out->index($d)->mv($d,0);
384 70 $a .= xvals($a);
385 }
386  
387 35 $out;
388 }
389 120 *ndcoords = \&PDL::ndcoords;
390 120 *allaxisvals = \&PDL::ndcoords;
391 120 *PDL::allaxisvals = \&PDL::ndcoords;
392
393  
394 =head2 hist
395  
396 =for ref
397  
398 Create histogram of a piddle
399  
400 =for usage
401  
402 $hist = hist($data);
403 ($xvals,$hist) = hist($data);
404  
405 or
406  
407 $hist = hist($data,$min,$max,$step);
408 ($xvals,$hist) = hist($data,[$min,$max,$step]);
409  
410 If C<hist> is run in list context, C<$xvals> gives the
411 computed bin centres as double values.
412  
413 A nice idiom (with
414 L<PDL::Graphics::PGPLOT|PDL::Graphics::PGPLOT>) is
415  
416 bin hist $data; # Plot histogram
417  
418 =for example
419  
420 pdl> p $y
421 [13 10 13 10 9 13 9 12 11 10 10 13 7 6 8 10 11 7 12 9 11 11 12 6 12 7]
422 pdl> $h = hist $y,0,20,1; # hist with step 1, min 0 and 20 bins
423 pdl> p $h
424 [0 0 0 0 0 0 2 3 1 3 5 4 4 4 0 0 0 0 0 0]
425  
426 =cut
427  
428 sub PDL::hist {
429  
430 2 ▶ my $usage = "\n" . ' Usage: $hist = hist($data)' . "\n" .
Subroutine hist: called CC=3 SCAR=11.0
Pod undocumented
431 ' $hist = hist($data,$min,$max,$step)' . "\n" .
432 ' ($xvals,$hist) = hist($data)' . "\n" .
433 ' ($xvals,$hist) = hist($data,$min,$max,$step)' . "\n" ;
434 2 ▶ barf($usage) if $#_<0;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 0 2 2
435  
436 2 my($pdl,$min,$max,$step)=@_;
437 2 my $xvals;
438  
439 2 ($step, $min, $bins, $xvals) =
440 _hist_bin_calc($pdl, $min, $max, $step, wantarray());
441  
442 2 PDL::Primitive::histogram($pdl->clump(-1),(my $hist = null),
443 $step,$min,$bins);
444  
445 2 ▶ return wantarray() ? ($xvals,$hist) : $hist;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
446 }
447  
448 =head2 whist
449  
450 =for ref
451  
452 Create a weighted histogram of a piddle
453  
454 =for usage
455  
456 $hist = whist($data, $wt, [$min,$max,$step]);
457 ($xvals,$hist) = whist($data, $wt, [$min,$max,$step]);
458  
459 If requested, C<$xvals> gives the computed bin centres
460 as type double values. C<$data> and C<$wt> should have
461 the same dimensionality and extents.
462  
463 A nice idiom (with
464 L<PDL::Graphics::PGPLOT|PDL::Graphics::PGPLOT>) is
465  
466 bin whist $data, $wt; # Plot histogram
467  
468 =for example
469  
470 pdl> p $y
471 [13 10 13 10 9 13 9 12 11 10 10 13 7 6 8 10 11 7 12 9 11 11 12 6 12 7]
472 pdl> $wt = grandom($y->nelem)
473 pdl> $h = whist $y, $wt, 0, 20, 1 # hist with step 1, min 0 and 20 bins
474 pdl> p $h
475 [0 0 0 0 0 0 -0.49552342 1.7987439 0.39450696 4.0073722 -2.6255299 -2.5084501 2.6458365 4.1671676 0 0 0 0 0 0]
476  
477  
478 =cut
479  
480 sub PDL::whist {
481 1 ▶ barf('Usage: ([$xvals],$hist) = whist($data,$wt,[$min,$max,$step])')
Subroutine whist: called CC=3 SCAR=11.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 1 1
482 if @_ < 2;
483 1 my($pdl,$wt,$min,$max,$step)=@_;
484 1 my $xvals;
485  
486 1 ($step, $min, $bins, $xvals) =
487 _hist_bin_calc($pdl, $min, $max, $step, wantarray());
488  
489 1 PDL::Primitive::whistogram($pdl->clump(-1),$wt->clump(-1),
490 (my $hist = null), $step, $min, $bins);
491 1 ▶ return wantarray() ? ($xvals,$hist) : $hist;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
492 }
493  
494 sub _hist_bin_calc {
495 3 ▶ my($pdl,$min,$max,$step,$wantarray)=@_;
Subroutine _hist_bin_calc: called CC=9 SCAR=24.1
496 3 ▶ $min = $pdl->min() unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 3 3
497 3 ▶ $max = $pdl->max() unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 3 3
498 3 my $nelem = $pdl->nelem;
499 3 ▶ barf "empty piddle, no values to work with" if $nelem == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nelem == 0 0 3 3
500  
501 3 ▶ $step = ($max-$min)/(($nelem>10_000) ? 100 : sqrt($nelem)) unless defined $step;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $step 0 3 3
$nelem > 10000 ? : 0 0 0
502 3 ▶ barf "step is zero (or all data equal to one value)" if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 3 3
503  
504 3 my $bins = int(($max-$min)/$step+0.5);
505 3 ▶ print "hist with step $step, min $min and $bins bins\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 3 3
506 if $PDL::debug;
507 # Need to use double for $xvals here
508 3 ▶ my $xvals = $min + $step/2 + sequence(PDL::Core::double,$bins)*$step if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 2 1 3
509  
510 3 return ( $step, $min, $bins, $xvals );
511 }
512  
513  
514 =head2 sequence
515  
516 =for ref
517  
518 Create array filled with a sequence of values
519  
520 =for usage
521  
522 $a = sequence($b); $a = sequence [OPTIONAL TYPE], @dims;
523  
524 etc. see L<zeroes|PDL::Core/zeroes>.
525  
526 =for example
527  
528 pdl> p sequence(10)
529 [0 1 2 3 4 5 6 7 8 9]
530 pdl> p sequence(3,4)
531 [
532 [ 0 1 2]
533 [ 3 4 5]
534 [ 6 7 8]
535 [ 9 10 11]
536 ]
537  
538 =cut
539  
540 122 ▶sub sequence { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? $_[0]->sequence : PDL->sequence(@_) }
Subroutine sequence: called CC=3 SCAR=12.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'PDL::Type' ? : 0 122 122
Condition66%2/3
ref $_[0] and ref $_[0] ne "PDL::Type"
!ll&&!rl&&r
106160
MC/DC0%0/2
ref $_[0] and ref $_[0] ne "PDL::Type"
ref $_[0] ref $_[0] ne "PDL::Type"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "PDL::Type"
ABresult
0X0
100
111
541 sub PDL::sequence {
542 127 ▶ my $class = shift;
Subroutine sequence: called CC=2 SCAR=6.9
Pod undocumented
543 127 ▶ my $pdl = scalar(@_)? $class->new_from_specification(@_) : $class->new_or_inplace;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 125 2 127
544 127 my $bar = $pdl->clump(-1)->inplace;
545 127 my $foo = $bar->xvals;
546 127 return $pdl;
547 }
548  
549 =head2 rvals
550  
551 =for ref
552  
553 Fills a piddle with radial distance values from some centre.
554  
555 =for usage
556  
557 $r = rvals $piddle,{OPTIONS};
558 $r = rvals [OPTIONAL TYPE],$nx,$ny,...{OPTIONS};
559  
560 =for options
561  
562 Options:
563  
564 Centre => [$x,$y,$z...] # Specify centre
565 Center => [$x,$y.$z...] # synonym.
566  
567 Squared => 1 # return distance squared (i.e., don't take the square root)
568  
569 =for example
570  
571 pdl> print rvals long,7,7,{Centre=>[2,2]}
572 [
573 [2 2 2 2 2 3 4]
574 [2 1 1 1 2 3 4]
575 [2 1 0 1 2 3 4]
576 [2 1 1 1 2 3 4]
577 [2 2 2 2 2 3 4]
578 [3 3 3 3 3 4 5]
579 [4 4 4 4 4 5 5]
580 ]
581  
582 If C<Center> is not specified, the midpoint for a given dimension of
583 size C<N> is given by C< int(N/2) > so that the midpoint always falls
584 on an exact pixel point in the data. For dimensions of even size,
585 that means the midpoint is shifted by 1/2 pixel from the true center
586 of that dimension.
587  
588 Also note that the calculation for C<rvals> for integer values
589 does not promote the datatype so you will have wraparound when
590 the value calculated for C< r**2 > is greater than the datatype
591 can hold. If you need exact values, be sure to use large integer
592 or floating point datatypes.
593  
594 For a more general metric, one can define, e.g.,
595  
596 sub distance {
597 my ($a,$centre,$f) = @_;
598 my ($r) = $a->allaxisvals-$centre;
599 $f->($r);
600 }
601 sub l1 { sumover(abs($_[0])); }
602 sub euclid { use PDL::Math 'pow'; pow(sumover(pow($_[0],2)),0.5); }
603 sub linfty { maximum(abs($_[0])); }
604  
605 so now
606  
607 distance($a, $centre, \&euclid);
608  
609 will emulate rvals, while C<\&l1> and C<\&linfty> will generate other
610 well-known norms.
611  
612 =cut
613  
614 14 ▶sub rvals { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? $_[0]->rvals(@_[1..$#_]) : PDL->rvals(@_) }
Subroutine rvals: called CC=3 SCAR=12.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] ne 'PDL::Type' ? : 0 14 14
Condition66%2/3
ref $_[0] and ref $_[0] ne "PDL::Type"
!ll&&!rl&&r
1220
MC/DC0%0/2
ref $_[0] and ref $_[0] ne "PDL::Type"
ref $_[0] ref $_[0] ne "PDL::Type"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "PDL::Type"
ABresult
0X0
100
111
615 sub PDL::rvals { # Return radial distance from given point and offset
616 14 ▶ my $class = shift;
Subroutine rvals: called CC=8 SCAR=20.8
Pod undocumented
617 14 ▶ my $opt = pop @_ if ref($_[$#_]) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$#_] eq "HASH" 8 6 14
618 14 ▶ my %opt = defined $opt ?
Branch: 1 branch
BranchTrueFalseTotal
defined $opt ? : 8 6 14
619 iparse( {
620 CENTRE => undef, # needed, otherwise centre/center handling painful
621 Squared => 0,
622 }, $opt ) : ();
623 14 ▶ my $r = scalar(@_)? $class->new_from_specification(@_) : $class->new_or_inplace;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 14 0 14
624  
625 14 my @pos;
626 14 ▶ @pos = @{$opt{CENTRE}} if defined $opt{CENTRE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"CENTRE"} 7 7 14
627 14 my $offset;
628  
629 14 $r .= 0.0;
630 14 my $tmp = $r->copy;
631 14 my $i;
632 14 ▶ for ($i=0; $i<$r->getndims; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $r->getndims) 28 14 42
633 28 ▶ $offset = (defined $pos[$i] ? $pos[$i] : int($r->getdim($i)/2));
Branch: 1 branch
BranchTrueFalseTotal
defined $pos[$i] ? : 14 14 28
634 # Note careful coding for speed and min memory footprint
635 28 PDL::Primitive::axisvalues($tmp->xchg(0,$i));
636 28 $tmp -= $offset; $tmp *= $tmp;
637 28 $r += $tmp;
638 }
639 14 ▶ return $opt{Squared} ? $r : $r->inplace->sqrt;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'Squared'} ? : 2 12 14
640 }
641  
642 =head2 axisvals
643  
644 =for ref
645  
646 Fills a piddle with index values on Nth dimension
647  
648 =for usage
649  
650 $z = axisvals ($piddle, $nth);
651  
652 This is the routine, for which L<xvals|/xvals>, L<yvals|/yvals> etc
653 are mere shorthands. C<axisvals> can be used to fill along any dimension,
654 using a parameter.
655  
656 See also L<allaxisvals|allaxisvals>, which generates all axis values
657 simultaneously in a form useful for L<range|range>, L<interpND|interpND>,
658 L<indexND|indexND>, etc.
659  
660 Note the 'from specification' style (see L<zeroes|PDL::Core/zeroes>) is
661 not available here, for obvious reasons.
662  
663 =cut
664  
665 sub PDL::axisvals {
666 1 ▶ my($this,$nth) = @_;
Subroutine axisvals: called CC=2 SCAR=7.5
Pod undocumented
667 1 my $dummy = $this->new_or_inplace;
668 1 ▶ if($dummy->getndims() <= $nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dummy->getndims <= $nth) 0 1 1
669 # This is 'kind of' consistency...
670 0 $dummy .= 0;
671 0 return $dummy;
672 # barf("Too few dimensions given to axisvals $nth\n");
673 }
674 1 my $bar = $dummy->xchg(0,$nth);
675 1 PDL::Primitive::axisvalues($bar);
676 1 return $dummy;
677 }
678  
679 # We need this version for xvals etc to work in place
680 sub axisvals2 {
681 342 ▶ my($this,$nth) = @_;
Subroutine axisvals2: called CC=2 SCAR=7.5
Pod undocumented
682 342 my $dummy = shift;
683 342 ▶ if($dummy->getndims() <= $nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dummy->getndims <= $nth) 0 342 342
684 # This is 'kind of' consistency...
685 0 $dummy .= 0;
686 0 return $dummy;
687 # barf("Too few dimensions given to axisvals $nth\n");
688 }
689 342 my $bar = $dummy->xchg(0,$nth);
690 342 PDL::Primitive::axisvalues($bar);
691 342 return $dummy;
692 }
693 sub PDL::sec {
694 0 ▶ my($this,@coords) = @_;
Subroutine sec: not called CC=2 SCAR=17.9
Pod undocumented
695 0 my $i; my @maps;
696 0 ▶ while($#coords > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#coords > -1) 0 0 0
697 0 $i = int(shift @coords) ;
698 0 push @maps, "$i:".int(shift @coords);
699 }
700 0 my $tmp = PDL->null;
701 0 $tmp .= $this->slice(join ',',@maps);
702 0 return $tmp;
703 }
704  
705 sub PDL::ins {
706 0 ▶ my($this,$what,@coords) = @_;
Subroutine ins: not called CC=3 SCAR=24.8
Pod undocumented
707 0 my $w = PDL::Core::alltopdl($PDL::name,$what);
708 0 my $tmp;
709 0 ▶ if($this->is_inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->is_inplace) { } 0 0 0
710 0 $this->set_inplace(0);
711 } else {
712 0 $this = $this->copy;
713 }
714 ($tmp = $this->slice(
715 0 ▶ (join ',',map {int($coords[$_]).":".
Branch: 1 branch
BranchTrueFalseTotal
int($coords[$_]) + $w->getdim($_) - 1 < $this->getdim($_) ? : 0 0 0
716 ((int($coords[$_])+$w->getdim($_)-1)<$this->getdim($_) ?
717 (int($coords[$_])+$w->getdim($_)-1):$this->getdim($_))
718 }
719 0..$#coords)))
720 .= $w;
721 0 return $this;
722 }
723  
724 sub PDL::similar_assign {
725 0 ▶ my($from,$to) = @_;
Subroutine similar_assign: not called CC=2 SCAR=17.9
Pod undocumented
726 0 ▶ if((join ',',@{$from->dims}) ne (join ',',@{$to->dims})) {
Branch: 1 branch
BranchTrueFalseTotal
if (join(",", @{$from->dims;}) ne join(",", @{$to->dims;})) 0 0 0
727 barf "Similar_assign: dimensions [".
728 0 (join ',',@{$from->dims})."] and [".
729 0 (join ',',@{$to->dims})."] do not match!\n";
730 }
731 0 $to .= $from;
732 }
733  
734 =head2 transpose
735  
736 =for ref
737  
738 transpose rows and columns.
739  
740 =for usage
741  
742 $b = transpose($a);
743  
744 =for example
745  
746 pdl> $a = sequence(3,2)
747 pdl> p $a
748 [
749 [0 1 2]
750 [3 4 5]
751 ]
752 pdl> p transpose( $a )
753 [
754 [0 3]
755 [1 4]
756 [2 5]
757 ]
758  
759 =cut
760  
761 sub PDL::transpose {
762 10 ▶ my($this) = @_;
Subroutine transpose: called CC=3 SCAR=11.2
Pod undocumented
763 10 ▶ if($this->getndims <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->getndims <= 1) 4 6 10
764 4 ▶ if($this->getndims==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->getndims == 0) { } 0 4 4
765 0 return pdl $this->dummy(0)->dummy(0);
766 } else {
767 4 return pdl $this->dummy(0);
768 }
769 }
770 6 return $this->xchg(0,1);
771 }
772  
773 120 1;
774  
« blib/lib/PDL/Exporter.pm ↑ Summary blib/lib/PDL/Demos/TriDGallery.pm »