blib/lib/PDL/Slices.pm

filter: Statementstmt 91.1% 124 / 136 Branchbran 75.0% 33 / 44 Conditioncond 53.3% 8 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 9 totaltotal 79.9% 183 / 229 CC 33 SCAR 19.9
CC
33
Cov
85%
using
20.0
range
11.1
reorder
7.7
CRAP 7.3
« blib/lib/PDL/NiceSlice/FilterUtilCall.pm ↑ Summary blib/lib/PDL/Graphics/LUT.pm »
1  
2 #
3 # GENERATED WITH PDL::PP! Don't modify!
4 #
5 package PDL::Slices;
6  
7 120 @EXPORT_OK = qw( PDL::PP affineinternal PDL::PP s_identity PDL::PP index PDL::PP index1d PDL::PP index2d indexND indexNDb PDL::PP rangeb PDL::PP rld PDL::PP rle PDL::PP flowconvert PDL::PP converttypei PDL::PP _clump_int PDL::PP xchg PDL::PP mv PDL::PP oslice using PDL::PP affine PDL::PP diagonalI PDL::PP lags PDL::PP splitdim PDL::PP rotate PDL::PP threadI PDL::PP identvaff PDL::PP unthread dice dice_axis slice PDL::PP sliceb );
8 120 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9  
10 120 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 120 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 120 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16
17 120 @ISA = ( 'PDL::Exporter','DynaLoader' );
18 120 push @PDL::Core::PP, __PACKAGE__;
19 120 bootstrap PDL::Slices ;
20  
21  
22  
23  
24  
25 =head1 NAME
26  
27 PDL::Slices -- Indexing, slicing, and dicing
28  
29 =head1 SYNOPSIS
30  
31 use PDL;
32 $a = ones(3,3);
33 $b = $a->slice('-1:0,(1)');
34 $c = $a->dummy(2);
35  
36  
37 =head1 DESCRIPTION
38  
39 This package provides many of the powerful PerlDL core index
40 manipulation routines. These routines mostly allow two-way data flow,
41 so you can modify your data in the most convenient representation.
42 For example, you can make a 1000x1000 unit matrix with
43  
44 $a = zeroes(1000,1000);
45 $a->diagonal(0,1) ++;
46  
47 which is quite efficient. See L<PDL::Indexing> and L<PDL::Tips> for
48 more examples.
49  
50 Slicing is so central to the PDL language that a special compile-time
51 syntax has been introduced to handle it compactly; see L<PDL::NiceSlice>
52 for details.
53  
54 PDL indexing and slicing functions usually include two-way data flow,
55 so that you can separate the actions of reshaping your data structures
56 and modifying the data themselves. Two special methods, L<copy|copy> and
57 L<sever|sever>, help you control the data flow connection between related
58 variables.
59  
60 $b = $a->slice("1:3"); # Slice maintains a link between $a and $b.
61 $b += 5; # $a is changed!
62  
63 If you want to force a physical copy and no data flow, you can copy or
64 sever the slice expression:
65  
66 $b = $a->slice("1:3")->copy;
67 $b += 5; # $a is not changed.
68  
69 $b = $a->slice("1:3")->sever;
70 $b += 5; # $a is not changed.
71  
72 The difference between C<sever> and C<copy> is that sever acts on (and
73 returns) its argument, while copy produces a disconnected copy. If you
74 say
75  
76 $b = $a->slice("1:3");
77 $c = $b->sever;
78  
79 then the variables C<$b> and C<$c> point to the same object but with
80 C<-E<gt>copy> they would not.
81  
82 =cut
83  
84 120 ▶use PDL::Core ':Internal';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 120 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87  
88  
89  
90  
91  
92  
93 =head1 FUNCTIONS
94  
95  
96  
97 =cut
98  
99  
100  
101  
102  
103  
104 120 *affineinternal = \&PDL::affineinternal;
105  
106  
107  
108  
109  
110 =head2 s_identity
111  
112 =for sig
113  
114 Signature: (P(); C())
115  
116 =for ref
117  
118 Internal vaffine identity function.
119  
120  
121  
122 =for bad
123  
124 s_identity processes bad values.
125 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
126  
127  
128 =cut
129  
130  
131  
132  
133  
134  
135 120 *s_identity = \&PDL::s_identity;
136  
137  
138  
139  
140  
141 =head2 index
142  
143 =for sig
144  
145 Signature: (a(n); indx ind(); [oca] c())
146  
147 =for ref
148  
149 C<index>, C<index1d>, and C<index2d> provide rudimentary index indirection.
150  
151 =for example
152  
153 $c = index($source,$ind);
154 $c = index1d($source,$ind);
155 $c = index2d($source2,$ind1,$ind2);
156  
157 use the C<$ind> variables as indices to look up values in C<$source>.
158 The three routines thread slightly differently.
159  
160 =over 3
161  
162 =item *
163  
164 C<index> uses direct threading for 1-D indexing across the 0 dim
165 of C<$source>. It can thread over source thread dims or index thread
166 dims, but not (easily) both: If C<$source> has more than 1
167 dimension and C<$ind> has more than 0 dimensions, they must agree in
168 a threading sense.
169  
170 =item *
171  
172 C<index1d> uses a single active dim in C<$ind> to produce a list of
173 indexed values in the 0 dim of the output - it is useful for
174 collapsing C<$source> by indexing with a single row of values along
175 C<$source>'s 0 dimension. The output has the same number of dims as
176 C<$source>. The 0 dim of the output has size 1 if C<$ind> is a
177 scalar, and the same size as the 0 dim of C<$ind> if it is not. If
178 C<$ind> and C<$source> both have more than 1 dim, then all dims higher
179 than 0 must agree in a threading sense.
180  
181 =item *
182  
183 C<index2d> works like C<index> but uses separate piddles for X and Y
184 coordinates. For more general N-dimensional indexing, see the
185 L<PDL::NiceSlice|PDL::NiceSlice> syntax or L<PDL::Slices|PDL::Slices> (in particular C<slice>,
186 C<indexND>, and C<range>).
187  
188 =back
189  
190 These functions are two-way, i.e. after
191  
192 $c = $a->index(pdl[0,5,8]);
193 $c .= pdl [0,2,4];
194  
195 the changes in C<$c> will flow back to C<$a>.
196  
197 C<index> provids simple threading: multiple-dimensioned arrays are treated
198 as collections of 1-D arrays, so that
199  
200 $a = xvals(10,10)+10*yvals(10,10);
201 $b = $a->index(3);
202 $c = $a->index(9-xvals(10));
203  
204 puts a single column from C<$a> into C<$b>, and puts a single element
205 from each column of C<$a> into C<$c>. If you want to extract multiple
206 columns from an array in one operation, see L<dice|/dice> or
207 L<indexND|/indexND>.
208  
209  
210  
211 =for bad
212  
213 index barfs if any of the index values are bad.
214  
215 =cut
216  
217  
218  
219  
220  
221  
222 120 *index = \&PDL::index;
223  
224  
225  
226  
227  
228 =head2 index1d
229  
230 =for sig
231  
232 Signature: (a(n); indx ind(m); [oca] c(m))
233  
234 =for ref
235  
236 C<index>, C<index1d>, and C<index2d> provide rudimentary index indirection.
237  
238 =for example
239  
240 $c = index($source,$ind);
241 $c = index1d($source,$ind);
242 $c = index2d($source2,$ind1,$ind2);
243  
244 use the C<$ind> variables as indices to look up values in C<$source>.
245 The three routines thread slightly differently.
246  
247 =over 3
248  
249 =item *
250  
251 C<index> uses direct threading for 1-D indexing across the 0 dim
252 of C<$source>. It can thread over source thread dims or index thread
253 dims, but not (easily) both: If C<$source> has more than 1
254 dimension and C<$ind> has more than 0 dimensions, they must agree in
255 a threading sense.
256  
257 =item *
258  
259 C<index1d> uses a single active dim in C<$ind> to produce a list of
260 indexed values in the 0 dim of the output - it is useful for
261 collapsing C<$source> by indexing with a single row of values along
262 C<$source>'s 0 dimension. The output has the same number of dims as
263 C<$source>. The 0 dim of the output has size 1 if C<$ind> is a
264 scalar, and the same size as the 0 dim of C<$ind> if it is not. If
265 C<$ind> and C<$source> both have more than 1 dim, then all dims higher
266 than 0 must agree in a threading sense.
267  
268 =item *
269  
270 C<index2d> works like C<index> but uses separate piddles for X and Y
271 coordinates. For more general N-dimensional indexing, see the
272 L<PDL::NiceSlice|PDL::NiceSlice> syntax or L<PDL::Slices|PDL::Slices> (in particular C<slice>,
273 C<indexND>, and C<range>).
274  
275 =back
276  
277 These functions are two-way, i.e. after
278  
279 $c = $a->index(pdl[0,5,8]);
280 $c .= pdl [0,2,4];
281  
282 the changes in C<$c> will flow back to C<$a>.
283  
284 C<index> provids simple threading: multiple-dimensioned arrays are treated
285 as collections of 1-D arrays, so that
286  
287 $a = xvals(10,10)+10*yvals(10,10);
288 $b = $a->index(3);
289 $c = $a->index(9-xvals(10));
290  
291 puts a single column from C<$a> into C<$b>, and puts a single element
292 from each column of C<$a> into C<$c>. If you want to extract multiple
293 columns from an array in one operation, see L<dice|/dice> or
294 L<indexND|/indexND>.
295  
296  
297  
298 =for bad
299  
300 index1d propagates BAD index elements to the output variable.
301  
302 =cut
303  
304  
305  
306  
307  
308  
309 120 *index1d = \&PDL::index1d;
310  
311  
312  
313  
314  
315 =head2 index2d
316  
317 =for sig
318  
319 Signature: (a(na,nb); indx inda(); indx indb(); [oca] c())
320  
321 =for ref
322  
323 C<index>, C<index1d>, and C<index2d> provide rudimentary index indirection.
324  
325 =for example
326  
327 $c = index($source,$ind);
328 $c = index1d($source,$ind);
329 $c = index2d($source2,$ind1,$ind2);
330  
331 use the C<$ind> variables as indices to look up values in C<$source>.
332 The three routines thread slightly differently.
333  
334 =over 3
335  
336 =item *
337  
338 C<index> uses direct threading for 1-D indexing across the 0 dim
339 of C<$source>. It can thread over source thread dims or index thread
340 dims, but not (easily) both: If C<$source> has more than 1
341 dimension and C<$ind> has more than 0 dimensions, they must agree in
342 a threading sense.
343  
344 =item *
345  
346 C<index1d> uses a single active dim in C<$ind> to produce a list of
347 indexed values in the 0 dim of the output - it is useful for
348 collapsing C<$source> by indexing with a single row of values along
349 C<$source>'s 0 dimension. The output has the same number of dims as
350 C<$source>. The 0 dim of the output has size 1 if C<$ind> is a
351 scalar, and the same size as the 0 dim of C<$ind> if it is not. If
352 C<$ind> and C<$source> both have more than 1 dim, then all dims higher
353 than 0 must agree in a threading sense.
354  
355 =item *
356  
357 C<index2d> works like C<index> but uses separate piddles for X and Y
358 coordinates. For more general N-dimensional indexing, see the
359 L<PDL::NiceSlice|PDL::NiceSlice> syntax or L<PDL::Slices|PDL::Slices> (in particular C<slice>,
360 C<indexND>, and C<range>).
361  
362 =back
363  
364 These functions are two-way, i.e. after
365  
366 $c = $a->index(pdl[0,5,8]);
367 $c .= pdl [0,2,4];
368  
369 the changes in C<$c> will flow back to C<$a>.
370  
371 C<index> provids simple threading: multiple-dimensioned arrays are treated
372 as collections of 1-D arrays, so that
373  
374 $a = xvals(10,10)+10*yvals(10,10);
375 $b = $a->index(3);
376 $c = $a->index(9-xvals(10));
377  
378 puts a single column from C<$a> into C<$b>, and puts a single element
379 from each column of C<$a> into C<$c>. If you want to extract multiple
380 columns from an array in one operation, see L<dice|/dice> or
381 L<indexND|/indexND>.
382  
383  
384  
385 =for bad
386  
387 index2d barfs if either of the index values are bad.
388  
389 =cut
390  
391  
392  
393  
394  
395  
396 120 *index2d = \&PDL::index2d;
397  
398  
399  
400  
401 =head2 indexNDb
402  
403 =for ref
404  
405 Backwards-compatibility alias for indexND
406  
407 =head2 indexND
408  
409 =for ref
410  
411 Find selected elements in an N-D piddle, with optional boundary handling
412  
413 =for example
414  
415 $out = $source->indexND( $index, [$method] )
416  
417 $source = 10*xvals(10,10) + yvals(10,10);
418 $index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
419 print $source->indexND( $index );
420  
421 [
422 [23 45]
423 [67 89]
424 ]
425  
426 IndexND collapses C<$index> by lookup into C<$source>. The
427 0th dimension of C<$index> is treated as coordinates in C<$source>, and
428 the return value has the same dimensions as the rest of C<$index>.
429 The returned elements are looked up from C<$source>. Dataflow
430 works -- propagated assignment flows back into C<$source>.
431  
432 IndexND and IndexNDb were originally separate routines but they are both
433 now implemented as a call to L<range|/range>, and have identical syntax to
434 one another.
435  
436 =cut
437  
438 sub PDL::indexND {
439 5 ▶ my($source,$index, $boundary) = @_;
Subroutine indexND: called CC=1 SCAR=0.0
Pod undocumented
440 5 return PDL::range($source,$index,undef,$boundary);
441 }
442  
443 120 *PDL::indexNDb = \&PDL::indexND;
444  
445  
446  
447  
448 sub PDL::range {
449 49 ▶ my($source,$ind,$sz,$bound) = @_;
Subroutine range: called CC=11 SCAR=24.0
Pod undocumented
450 49 my $index = PDL->pdl($ind);
451  
452 49 ▶ my $size = defined($sz) ? PDL->pdl($sz) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sz ? : 30 19 49
453  
454  
455 # Handle empty PDL case: return a properly constructed Empty.
456 49 ▶ if($index->isempty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index->isempty) 4 45 49
457 4 my @sdims= $source->dims;
458 4 splice(@sdims, 0, $index->dim(0) + ($index->dim(0)==0)); # added term is to treat Empty[0] like a single empty coordinate
459 4 ▶ unshift(@sdims, $size->list) if(defined($size));
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 0 4 4
460 4 return PDL->new_from_specification(0 x ($index->ndims-1), @sdims);
461 }
462  
463  
464 45 ▶ $index = $index->dummy(0,1) unless $index->ndims;
Branch: 1 branch
BranchTrueFalseTotal
unless $index->ndims 0 45 45
465  
466  
467 # Pack boundary string if necessary
468 45 ▶ if(defined $bound) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bound) 27 18 45
469 27 ▶ if(ref $bound eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bound eq 'ARRAY') { } 1 26 27
elsif (not $bound =~ /^[0123ftepx]+$/ and $bound =~ /^([0123ftepx])/i) { } 1 49 50
470 1 my ($s,$el);
471 1 foreach $el(@$bound) {
472 2 ▶ barf "Illegal boundary value '$el' in range"
Branch: 1 branch
BranchTrueFalseTotal
unless $el =~ /^([0123fFtTeEpPmM])/ 0 2 2
473 unless( $el =~ m/^([0123fFtTeEpPmM])/ );
474 2 $s .= $1;
475 }
476 1 $bound = $s;
477 }
478 ▶ elsif($bound !~ m/^[0123ftepx]+$/ && $bound =~ m/^([0123ftepx])/i ) {
Condition100%3/3
not $bound =~ /^[0123ftepx]+$/ and $bound =~ /^([0123ftepx])/i
!ll&&!rl&&r
2411
MC/DC100%2/2
not $bound =~ /^[0123ftepx]+$/ and $bound =~ /^([0123ftepx])/i
not $bound =~ /^[0123ftepx]+$/ $bound =~ /^([0123ftepx])/i
Truth table100%3/3
not(A) and B
A= not $bound =~ /^[0123ftepx]+$/B= $bound =~ /^([0123ftepx])/i
ABresult
0X0
100
111
479 1 $bound = $1;
480 }
481 }
482  
483 120 ▶ no warnings; # shut up about passing undef into rangeb
Subroutine BEGIN: called CC=1 SCAR=0.0
484 45 $source->rangeb($index,$size,$bound);
485 }
486  
487  
488  
489  
490 =head2 rangeb
491  
492 =for sig
493  
494 Signature: (P(); C(); SV *index; SV *size; SV *boundary)
495  
496 =for ref
497  
498 Engine for L<range|/range>
499  
500 =for example
501  
502 Same calling convention as L<range|/range>, but you must supply all
503 parameters. C<rangeb> is marginally faster as it makes a direct PP call,
504 avoiding the perl argument-parsing step.
505  
506  
507 =head2 range
508  
509 =for ref
510  
511 Extract selected chunks from a source piddle, with boundary conditions
512  
513 =for example
514  
515 $out = $source->range($index,[$size,[$boundary]])
516  
517 Returns elements or rectangular slices of the original piddle, indexed by
518 the C<$index> piddle. C<$source> is an N-dimensional piddle, and C<$index> is
519 a piddle whose first dimension has size up to N. Each row of C<$index> is
520 treated as coordinates of a single value or chunk from C<$source>, specifying
521 the location(s) to extract.
522  
523 If you specify a single index location, then range is essentially an expensive
524 slice, with controllable boundary conditions.
525  
526 B<INPUTS>
527  
528 C<$index> and C<$size> can be piddles or array refs such as you would
529 feed to L<zeroes|PDL::Core/zeroes> and its ilk. If C<$index>'s 0th dimension
530 has size higher than the number of dimensions in C<$source>, then
531 C<$source> is treated as though it had trivial dummy dimensions of
532 size 1, up to the required size to be indexed by C<$index> -- so if
533 your source array is 1-D and your index array is a list of 3-vectors,
534 you get two dummy dimensions of size 1 on the end of your source array.
535  
536 You can extract single elements or N-D rectangular ranges from C<$source>,
537 by setting C<$size>. If C<$size> is undef or zero, then you get a single
538 sample for each row of C<$index>. This behavior is similar to
539 L<indexNDb|/indexNDb>, which is in fact implemented as a call to L<range|/range>.
540  
541 If C<$size> is positive then you get a range of values from C<$source> at
542 each location, and the output has extra dimensions allocated for them.
543 C<$size> can be a scalar, in which case it applies to all dimensions, or an
544 N-vector, in which case each element is applied independently to the
545 corresponding dimension in C<$source>. See below for details.
546  
547 C<$boundary> is a number, string, or list ref indicating the type of
548 boundary conditions to use when ranges reach the edge of C<$source>. If you
549 specify no boundary conditions the default is to forbid boundary violations
550 on all axes. If you specify exactly one boundary condition, it applies to
551 all axes. If you specify more (as elements of a list ref, or as a packed
552 string, see below), then they apply to dimensions in the order in which they
553 appear, and the last one applies to all subsequent dimensions. (This is
554 less difficult than it sounds; see the examples below).
555  
556 =over 3
557  
558 =item 0 (synonyms: 'f','forbid') B<(default)>
559  
560 Ranges are not allowed to cross the boundary of the original PDL. Disallowed
561 ranges throw an error. The errors are thrown at evaluation time, not
562 at the time of the range call (this is the same behavior as L<slice|/slice>).
563  
564 =item 1 (synonyms: 't','truncate')
565  
566 Values outside the original piddle get BAD if you've got bad value
567 support compiled into your PDL and set the badflag for the source PDL;
568 or 0 if you haven't (you must set the badflag if you want BADs for out
569 of bound values, otherwise you get 0). Reverse dataflow works OK for
570 the portion of the child that is in-bounds. The out-of-bounds part of
571 the child is reset to (BAD|0) during each dataflow operation, but
572 execution continues.
573  
574 =item 2 (synonyms: 'e','x','extend')
575  
576 Values that would be outside the original piddle point instead to the
577 nearest allowed value within the piddle. See the CAVEAT below on
578 mappings that are not single valued.
579  
580 =item 3 (synonyms: 'p','periodic')
581  
582 Periodic boundary conditions apply: the numbers in $index are applied,
583 strict-modulo the corresponding dimensions of $source. This is equivalent to
584 duplicating the $source piddle throughout N-D space. See the CAVEAT below
585 about mappings that are not single valued.
586  
587 =item 4 (synonyms: 'm','mirror')
588  
589 Mirror-reflection periodic boundary conditions apply. See the CAVEAT
590 below about mappings that are not single valued.
591  
592 =back
593  
594 The boundary condition identifiers all begin with unique characters, so
595 you can feed in multiple boundary conditions as either a list ref or a
596 packed string. (The packed string is marginally faster to run). For
597 example, the four expressions [0,1], ['forbid','truncate'], ['f','t'],
598 and 'ft' all specify that violating the boundary in the 0th dimension
599 throws an error, and all other dimensions get truncated.
600  
601 If you feed in a single string, it is interpreted as a packed boundary
602 array if all of its characters are valid boundary specifiers (e.g. 'pet'),
603 but as a single word-style specifier if they are not (e.g. 'forbid').
604  
605 B<OUTPUT>
606  
607 The output threads over both C<$index> and C<$source>. Because implicit
608 threading can happen in a couple of ways, a little thought is needed. The
609 returned dimension list is stacked up like this:
610  
611 (index thread dims), (index dims (size)), (source thread dims)
612  
613 The first few dims of the output correspond to the extra dims of
614 C<$index> (beyond the 0 dim). They allow you to pick out individual
615 ranges from a large, threaded collection.
616  
617 The middle few dims of the output correspond to the size dims
618 specified in C<$size>, and contain the range of values that is extracted
619 at each location in C<$source>. Every nonzero element of C<$size> is copied to
620 the dimension list here, so that if you feed in (for example) C<$size
621 = [2,0,1]> you get an index dim list of C<(2,1)>.
622  
623 The last few dims of the output correspond to extra dims of C<$source> beyond
624 the number of dims indexed by C<$index>. These dims act like ordinary
625 thread dims, because adding more dims to C<$source> just tacks extra dims
626 on the end of the output. Each source thread dim ranges over the entire
627 corresponding dim of C<$source>.
628  
629 B<Dataflow>: Dataflow is bidirectional.
630  
631 B<Examples>:
632 Here are basic examples of C<range> operation, showing how to get
633 ranges out of a small matrix. The first few examples show extraction
634 and selection of individual chunks. The last example shows
635 how to mark loci in the original matrix (using dataflow).
636  
637 pdl> $src = 10*xvals(10,5)+yvals(10,5)
638 pdl> print $src->range([2,3]) # Cut out a single element
639 23
640 pdl> print $src->range([2,3],1) # Cut out a single 1x1 block
641 [
642 [23]
643 ]
644 pdl> print $src->range([2,3], [2,1]) # Cut a 2x1 chunk
645 [
646 [23 33]
647 ]
648 pdl> print $src->range([[2,3]],[2,1]) # Trivial list of 1 chunk
649 [
650 [
651 [23]
652 [33]
653 ]
654 ]
655 pdl> print $src->range([[2,3],[0,1]], [2,1]) # two 2x1 chunks
656 [
657 [
658 [23 1]
659 [33 11]
660 ]
661 ]
662 pdl> # A 2x2 collection of 2x1 chunks
663 pdl> print $src->range([[[1,1],[2,2]],[[2,3],[0,1]]],[2,1])
664 [
665 [
666 [
667 [11 22]
668 [23 1]
669 ]
670 [
671 [21 32]
672 [33 11]
673 ]
674 ]
675 ]
676 pdl> $src = xvals(5,3)*10+yvals(5,3)
677 pdl> print $src->range(3,1) # Thread over y dimension in $src
678 [
679 [30]
680 [31]
681 [32]
682 ]
683  
684 pdl> $src = zeroes(5,4);
685 pdl> $src->range(pdl([2,3],[0,1]),pdl(2,1)) .= xvals(2,2,1) + 1
686 pdl> print $src
687 [
688 [0 0 0 0 0]
689 [2 2 0 0 0]
690 [0 0 0 0 0]
691 [0 0 1 1 0]
692 ]
693  
694 B<CAVEAT>: It's quite possible to select multiple ranges that
695 intersect. In that case, modifying the ranges doesn't have a
696 guaranteed result in the original PDL -- the result is an arbitrary
697 choice among the valid values. For some things that's OK; but for
698 others it's not. In particular, this doesn't work:
699  
700 pdl> $photon_list = new PDL::RandVar->sample(500)->reshape(2,250)*10
701 pdl> histogram = zeroes(10,10)
702 pdl> histogram->range($photon_list,1)++; #not what you wanted
703  
704 The reason is that if two photons land in the same bin, then that bin
705 doesn't get incremented twice. (That may get fixed in a later version...)
706  
707 B<PERMISSIVE RANGING>: If C<$index> has too many dimensions compared
708 to C<$source>, then $source is treated as though it had dummy
709 dimensions of size 1, up to the required number of dimensions. These
710 virtual dummy dimensions have the usual boundary conditions applied to
711 them.
712  
713 If the 0 dimension of C<$index> is ludicrously large (if its size is
714 more than 5 greater than the number of dims in the source PDL) then
715 range will insist that you specify a size in every dimension, to make
716 sure that you know what you're doing. That catches a common error with
717 range usage: confusing the initial dim (which is usually small) with another
718 index dim (perhaps of size 1000).
719  
720 If the index variable is Empty, then range() always returns the Empty PDL.
721 If the index variable is not Empty, indexing it always yields a boundary
722 violation. All non-barfing conditions are treated as truncation, since
723 there are no actual data to return.
724  
725 B<EFFICIENCY>: Because C<range> isn't an affine transformation (it
726 involves lookup into a list of N-D indices), it is somewhat
727 memory-inefficient for long lists of ranges, and keeping dataflow open
728 is much slower than for affine transformations (which don't have to copy
729 data around).
730  
731 Doing operations on small subfields of a large range is inefficient
732 because the engine must flow the entire range back into the original
733 PDL with every atomic perl operation, even if you only touch a single element.
734 One way to speed up such code is to sever your range, so that PDL
735 doesn't have to copy the data with each operation, then copy the
736 elements explicitly at the end of your loop. Here's an example that
737 labels each region in a range sequentially, using many small
738 operations rather than a single xvals assignment:
739  
740 ### How to make a collection of small ops run fast with range...
741 $a = $data->range($index, $sizes, $bound)->sever;
742 $aa = $data->range($index, $sizes, $bound);
743 map { $a($_ - 1) .= $_; } (1..$a->nelem); # Lots of little ops
744 $aa .= $a;
745  
746 C<range> is a perl front-end to a PP function, C<rangeb>. Calling
747 C<rangeb> is marginally faster but requires that you include all arguments.
748  
749 DEVEL NOTES
750  
751 * index thread dimensions are effectively clumped internally. This
752 makes it easier to loop over the index array but a little more brain-bending
753 to tease out the algorithm.
754  
755 =cut
756  
757  
758  
759 =for bad
760  
761 rangeb processes bad values.
762 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
763  
764  
765 =cut
766  
767  
768  
769  
770  
771  
772 120 *rangeb = \&PDL::rangeb;
773  
774  
775  
776  
777  
778 =head2 rld
779  
780 =for sig
781  
782 Signature: (indx a(n); b(n); [o]c(m))
783  
784 =for ref
785  
786 Run-length decode a vector
787  
788 Given a vector C<$a> of the numbers of instances of values C<$b>, run-length
789 decode to C<$c>.
790  
791 =for example
792  
793 rld($a,$b,$c=null);
794  
795  
796  
797 =for bad
798  
799 rld does not process bad values.
800 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
801  
802  
803 =cut
804  
805  
806  
807  
808 sub PDL::rld {
809 4 ▶ my ($a,$b) = @_;
Subroutine rld: called CC=2 SCAR=7.0
Pod undocumented
810 4 my ($c);
811 4 ▶ if ($#_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 2) { } 0 4 4
812 0 $c = $_[2];
813 } else {
814 # XXX Need to improve emulation of threading in auto-generating c
815 4 my ($size) = $a->sumover->max;
816 4 my (@dims) = $a->dims;
817 4 shift @dims;
818 4 $c = $b->zeroes($size,@dims);
819 }
820 4 &PDL::_rld_int($a,$b,$c);
821 4 $c;
822 }
823  
824  
825 120 *rld = \&PDL::rld;
826  
827  
828  
829  
830  
831 =head2 rle
832  
833 =for sig
834  
835 Signature: (c(n); indx [o]a(m); [o]b(m))
836  
837 =for ref
838  
839 Run-length encode a vector
840  
841 Given vector C<$c>, generate a vector C<$a> with the number of each
842 element, and a vector C<$b> of the unique values. New in PDL 2.017,
843 only the elements up to the first instance of C<0> in C<$a> are
844 returned, which makes the common use case of a 1-dimensional C<$c> simpler.
845 For threaded operation, C<$a> and C<$b> will be large enough
846 to hold the largest row of C<$a>, and only the elements up to the
847 first instance of C<0> in each row of C<$a> should be considered.
848  
849 =for example
850  
851 $c = floor(4*random(10));
852 rle($c,$a=null,$b=null);
853 #or
854 ($a,$b) = rle($c);
855  
856 #for $c of shape [10, 4]:
857 $c = floor(4*random(10,4));
858 ($a,$b) = rle($c);
859  
860 #to see the results of each row one at a time:
861 foreach (0..$c->dim(1)-1){
862 my ($as,$bs) = ($a(:,($_)),$b(:,($_)));
863 my ($ta,$tb) = where($as,$bs,$as!=0); #only the non-zero elements of $a
864 print $c(:,($_)) . " rle==> " , ($ta,$tb) , "\trld==> " . rld($ta,$tb) . "\n";
865 }
866  
867  
868  
869 =for bad
870  
871 rle does not process bad values.
872 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
873  
874  
875 =cut
876  
877  
878  
879  
880 sub PDL::rle {
881 5 ▶ my $c = shift;
Subroutine rle: called CC=3 SCAR=11.0
Pod undocumented
882 5 ▶ my ($a,$b) = @_==2 ? @_ : (null,null);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 2 3 5
883 5 &PDL::_rle_int($c,$a,$b);
884 5 ▶ my $max_ind = ($c->ndims<2) ? ($a!=0)->sumover-1 :
Branch: 1 branch
BranchTrueFalseTotal
$c->ndims < 2 ? : 3 2 5
885 ($a!=0)->clump(1..$a->ndims-1)->sumover->max-1;
886 5 return ($a->slice("0:$max_ind"),$b->slice("0:$max_ind"));
887 }
888  
889  
890 120 *rle = \&PDL::rle;
891  
892  
893  
894  
895  
896 120 *flowconvert = \&PDL::flowconvert;
897  
898  
899  
900  
901  
902 120 *converttypei = \&PDL::converttypei;
903  
904  
905  
906  
907  
908 120 *_clump_int = \&PDL::_clump_int;
909  
910  
911  
912  
913  
914 =head2 xchg
915  
916 =for sig
917  
918 Signature: (P(); C(); int n1; int n2)
919  
920 =for ref
921  
922 exchange two dimensions
923  
924 Negative dimension indices count from the end.
925  
926 The command
927  
928 =for example
929  
930 $b = $a->xchg(2,3);
931  
932 creates C<$b> to be like C<$a> except that the dimensions 2 and 3
933 are exchanged with each other i.e.
934  
935 $b->at(5,3,2,8) == $a->at(5,3,8,2)
936  
937  
938  
939 =for bad
940  
941 xchg does not process bad values.
942 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
943  
944  
945 =cut
946  
947  
948  
949  
950  
951  
952 120 *xchg = \&PDL::xchg;
953  
954  
955  
956  
957 =head2 reorder
958  
959 =for ref
960  
961 Re-orders the dimensions of a PDL based on the supplied list.
962  
963 Similar to the L<xchg|/xchg> method, this method re-orders the dimensions
964 of a PDL. While the L<xchg|/xchg> method swaps the position of two dimensions,
965 the reorder method can change the positions of many dimensions at
966 once.
967  
968 =for usage
969  
970 # Completely reverse the dimension order of a 6-Dim array.
971 $reOrderedPDL = $pdl->reorder(5,4,3,2,1,0);
972  
973 The argument to reorder is an array representing where the current dimensions
974 should go in the new array. In the above usage, the argument to reorder
975 C<(5,4,3,2,1,0)>
976 indicates that the old dimensions (C<$pdl>'s dims) should be re-arranged to make the
977 new pdl (C<$reOrderPDL>) according to the following:
978  
979 Old Position New Position
980 ------------ ------------
981 5 0
982 4 1
983 3 2
984 2 3
985 1 4
986 0 5
987  
988 You do not need to specify all dimensions, only a complete set
989 starting at position 0. (Extra dimensions are left where they are).
990 This means, for example, that you can reorder() the X and Y dimensions of
991 an image, and not care whether it is an RGB image with a third dimension running
992 across color plane.
993  
994 =for example
995  
996 Example:
997  
998 pdl> $a = sequence(5,3,2); # Create a 3-d Array
999 pdl> p $a
1000 [
1001 [
1002 [ 0 1 2 3 4]
1003 [ 5 6 7 8 9]
1004 [10 11 12 13 14]
1005 ]
1006 [
1007 [15 16 17 18 19]
1008 [20 21 22 23 24]
1009 [25 26 27 28 29]
1010 ]
1011 ]
1012 pdl> p $a->reorder(2,1,0); # Reverse the order of the 3-D PDL
1013 [
1014 [
1015 [ 0 15]
1016 [ 5 20]
1017 [10 25]
1018 ]
1019 [
1020 [ 1 16]
1021 [ 6 21]
1022 [11 26]
1023 ]
1024 [
1025 [ 2 17]
1026 [ 7 22]
1027 [12 27]
1028 ]
1029 [
1030 [ 3 18]
1031 [ 8 23]
1032 [13 28]
1033 ]
1034 [
1035 [ 4 19]
1036 [ 9 24]
1037 [14 29]
1038 ]
1039 ]
1040  
1041 The above is a simple example that could be duplicated by calling
1042 C<$a-E<gt>xchg(0,2)>, but it demonstrates the basic functionality of reorder.
1043  
1044 As this is an index function, any modifications to the
1045 result PDL will change the parent.
1046  
1047 =cut
1048  
1049 sub PDL::reorder {
1050 11 ▶ my ($pdl,@newDimOrder) = @_;
Subroutine reorder: called CC=5 SCAR=20.4
Pod undocumented
1051  
1052 11 my $arrayMax = $#newDimOrder;
1053  
1054 #Error Checking:
1055 11 ▶ if( $pdl->getndims < scalar(@newDimOrder) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->getndims < scalar @newDimOrder) 0 11 11
1056 0 my $errString = "PDL::reorder: Number of elements (".scalar(@newDimOrder).") in newDimOrder array exceeds\n";
1057 0 $errString .= "the number of dims in the supplied PDL (".$pdl->getndims.")";
1058 0 barf($errString);
1059 }
1060  
1061 # Check to make sure all the dims are within bounds
1062 11 for my $i(0..$#newDimOrder) {
1063 40 my $dim = $newDimOrder[$i];
1064 40 ▶ if($dim < 0 || $dim > $#newDimOrder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim < 0 or $dim > $#newDimOrder) 0 40 40
Condition33%1/3
$dim < 0 or $dim > $#newDimOrder
l!l&&r!l&&!r
0040
MC/DC0%0/2
$dim < 0 or $dim > $#newDimOrder
$dim < 0 $dim > $#newDimOrder
Truth table33%1/3
A or B
A= $dim < 0B= $dim > $#newDimOrder
ABresult
1X1
011
000
1065 0 my $errString = "PDL::reorder: Dim index $newDimOrder[$i] out of range in position $i\n(range is 0-$#newDimOrder)";
1066 0 barf($errString);
1067 }
1068 }
1069  
1070 # Checking that they are all present and also not duplicated is done by thread() [I think]
1071  
1072 # a quicker way to do the reorder
1073 11 return $pdl->thread(@newDimOrder)->unthread(0);
1074 }
1075  
1076  
1077  
1078  
1079  
1080 =head2 mv
1081  
1082 =for sig
1083  
1084 Signature: (P(); C(); int n1; int n2)
1085  
1086 =for ref
1087  
1088 move a dimension to another position
1089  
1090 The command
1091  
1092 =for example
1093  
1094 $b = $a->mv(4,1);
1095  
1096 creates C<$b> to be like C<$a> except that the dimension 4 is moved to the
1097 place 1, so:
1098  
1099 $b->at(1,2,3,4,5,6) == $a->at(1,5,2,3,4,6);
1100  
1101 The other dimensions are moved accordingly.
1102 Negative dimension indices count from the end.
1103  
1104  
1105 =for bad
1106  
1107 mv does not process bad values.
1108 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1109  
1110  
1111 =cut
1112  
1113  
1114  
1115  
1116  
1117  
1118 120 *mv = \&PDL::mv;
1119  
1120  
1121  
1122  
1123  
1124 =head2 oslice
1125  
1126 =for sig
1127  
1128 Signature: (P(); C(); char* str)
1129  
1130 =for ref
1131  
1132 DEPRECATED: 'oslice' is the original 'slice' routine in pre-2.006_006
1133 versions of PDL. It is left here for reference but will disappear in
1134 PDL 3.000
1135  
1136 Extract a rectangular slice of a piddle, from a string specifier.
1137  
1138 C<slice> was the original Swiss-army-knife PDL indexing routine, but is
1139 largely superseded by the L<NiceSlice|PDL::NiceSlice> source prefilter
1140 and its associated L<nslice|PDL::Core/nslice> method. It is still used as the
1141 basic underlying slicing engine for L<nslice|PDL::Core/nslice>,
1142 and is especially useful in particular niche applications.
1143  
1144 =for example
1145  
1146 $a->slice('1:3'); # return the second to fourth elements of $a
1147 $a->slice('3:1'); # reverse the above
1148 $a->slice('-2:1'); # return last-but-one to second elements of $a
1149  
1150 The argument string is a comma-separated list of what to do
1151 for each dimension. The current formats include
1152 the following, where I<a>, I<b> and I<c> are integers and can
1153 take legal array index values (including -1 etc):
1154  
1155 =over 8
1156  
1157 =item :
1158  
1159 takes the whole dimension intact.
1160  
1161 =item ''
1162  
1163 (nothing) is a synonym for ":"
1164 (This means that C<$a-E<gt>slice(':,3')> is equal to C<$a-E<gt>slice(',3')>).
1165  
1166 =item a
1167  
1168 slices only this value out of the corresponding dimension.
1169  
1170 =item (a)
1171  
1172 means the same as "a" by itself except that the resulting
1173 dimension of length one is deleted (so if C<$a> has dims C<(3,4,5)> then
1174 C<$a-E<gt>slice(':,(2),:')> has dimensions C<(3,5)> whereas
1175 C<$a-E<gt>slice(':,2,:')> has dimensions C<(3,1,5))>.
1176  
1177 =item a:b
1178  
1179 slices the range I<a> to I<b> inclusive out of the dimension.
1180  
1181 =item a:b:c
1182  
1183 slices the range I<a> to I<b>, with step I<c> (i.e. C<3:7:2> gives the indices
1184 C<(3,5,7)>). This may be confusing to Matlab users but several other
1185 packages already use this syntax.
1186  
1187  
1188 =item '*'
1189  
1190 inserts an extra dimension of width 1 and
1191  
1192 =item '*a'
1193  
1194 inserts an extra (dummy) dimension of width I<a>.
1195  
1196 =back
1197  
1198 An extension is planned for a later stage allowing
1199 C<$a-E<gt>slice('(=1),(=1|5:8),3:6(=1),4:6')>
1200 to express a multidimensional diagonal of C<$a>.
1201  
1202 Trivial out-of-bounds slicing is allowed: if you slice a source
1203 dimension that doesn't exist, but only index the 0th element, then
1204 C<slice> treats the source as if there were a dummy dimension there.
1205 The following are all equivalent:
1206  
1207 xvals(5)->dummy(1,1)->slice('(2),0') # Add dummy dim, then slice
1208 xvals(5)->slice('(2),0') # Out-of-bounds slice adds dim.
1209 xvals(5)->slice((2),0) # NiceSlice syntax
1210 xvals(5)->((2))->dummy(0,1) # NiceSlice syntax
1211  
1212 This is an error:
1213  
1214 xvals(5)->slice('(2),1') # nontrivial out-of-bounds slice dies
1215  
1216 Because slicing doesn't directly manipulate the source and destination
1217 pdl -- it just sets up a transformation between them -- indexing errors
1218 often aren't reported until later. This is either a bug or a feature,
1219 depending on whether you prefer error-reporting clarity or speed of execution.
1220  
1221  
1222  
1223 =for bad
1224  
1225 oslice does not process bad values.
1226 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1227  
1228  
1229 =cut
1230  
1231  
1232  
1233  
1234  
1235  
1236 120 *oslice = \&PDL::oslice;
1237  
1238  
1239  
1240  
1241 =head2 using
1242  
1243 =for ref
1244  
1245 Returns array of column numbers requested
1246  
1247 =for usage
1248  
1249 line $pdl->using(1,2);
1250  
1251 Plot, as a line, column 1 of C<$pdl> vs. column 2
1252  
1253 =for example
1254  
1255 pdl> $pdl = rcols("file");
1256 pdl> line $pdl->using(1,2);
1257  
1258 =cut
1259  
1260 120 *using = \&PDL::using;
1261 sub PDL::using {
1262 0 ▶ my ($x,@ind)=@_;
Subroutine using: not called CC=4 SCAR=30.0
Pod undocumented
1263 0 ▶ @ind = list $ind[0] if (blessed($ind[0]) && $ind[0]->isa('PDL'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($ind[0]) and $ind[0]->isa("PDL") 0 0 0
Condition0%0/3
builtin::blessed($ind[0]) and $ind[0]->isa("PDL")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ind[0]) and $ind[0]->isa("PDL")
builtin::blessed($ind[0]) $ind[0]->isa("PDL")
Truth table0%0/3
A and B
A= builtin::blessed($ind[0])B= $ind[0]->isa("PDL")
ABresult
0X0
100
111
1264 0 foreach (@ind) {
1265 0 $_ = $x->slice("($_)");
1266 }
1267 0 @ind;
1268 }
1269  
1270  
1271  
1272  
1273  
1274 120 *affine = \&PDL::affine;
1275  
1276  
1277  
1278  
1279  
1280 =head2 diagonalI
1281  
1282 =for sig
1283  
1284 Signature: (P(); C(); SV *list)
1285  
1286 =for ref
1287  
1288 Returns the multidimensional diagonal over the specified dimensions.
1289  
1290 The diagonal is placed at the first (by number) dimension that is
1291 diagonalized.
1292 The other diagonalized dimensions are removed. So if C<$a> has dimensions
1293 C<(5,3,5,4,6,5)> then after
1294  
1295 =for example
1296  
1297 $b = $a->diagonal(0,2,5);
1298  
1299 the piddle C<$b> has dimensions C<(5,3,4,6)> and
1300 C<$b-E<gt>at(2,1,0,1)> refers
1301 to C<$a-E<gt>at(2,1,2,0,1,2)>.
1302  
1303 NOTE: diagonal doesn't handle threadids correctly. XXX FIX
1304  
1305  
1306 =for bad
1307  
1308 diagonalI does not process bad values.
1309 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1310  
1311  
1312 =cut
1313  
1314  
1315  
1316  
1317  
1318  
1319 120 *diagonalI = \&PDL::diagonalI;
1320  
1321  
1322  
1323  
1324  
1325 =head2 lags
1326  
1327 =for sig
1328  
1329 Signature: (P(); C(); int nthdim; int step; int n)
1330  
1331 =for ref
1332  
1333 Returns a piddle of lags to parent.
1334  
1335 Usage:
1336  
1337 =for usage
1338  
1339 $lags = $a->lags($nthdim,$step,$nlags);
1340  
1341 I.e. if C<$a> contains
1342  
1343 [0,1,2,3,4,5,6,7]
1344  
1345 then
1346  
1347 =for example
1348  
1349 $b = $a->lags(0,2,2);
1350  
1351 is a (5,2) matrix
1352  
1353 [2,3,4,5,6,7]
1354 [0,1,2,3,4,5]
1355  
1356 This order of returned indices is kept because the function is
1357 called "lags" i.e. the nth lag is n steps behind the original.
1358  
1359 C<$step> and C<$nlags> must be positive. C<$nthdim> can be
1360 negative and will then be counted from the last dim backwards
1361 in the usual way (-1 = last dim).
1362  
1363  
1364 =for bad
1365  
1366 lags does not process bad values.
1367 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1368  
1369  
1370 =cut
1371  
1372  
1373  
1374  
1375  
1376  
1377 120 *lags = \&PDL::lags;
1378  
1379  
1380  
1381  
1382  
1383 =head2 splitdim
1384  
1385 =for sig
1386  
1387 Signature: (P(); C(); int nthdim; int nsp)
1388  
1389 =for ref
1390  
1391 Splits a dimension in the parent piddle (opposite of L<clump|PDL::Core/clump>)
1392  
1393 After
1394  
1395 =for example
1396  
1397 $b = $a->splitdim(2,3);
1398  
1399 the expression
1400  
1401 $b->at(6,4,x,y,3,6) == $a->at(6,4,x+3*y)
1402  
1403 is always true (C<x> has to be less than 3).
1404  
1405  
1406 =for bad
1407  
1408 splitdim does not process bad values.
1409 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1410  
1411  
1412 =cut
1413  
1414  
1415  
1416  
1417  
1418  
1419 120 *splitdim = \&PDL::splitdim;
1420  
1421  
1422  
1423  
1424  
1425 =head2 rotate
1426  
1427 =for sig
1428  
1429 Signature: (x(n); indx shift(); [oca]y(n))
1430  
1431 =for ref
1432  
1433 Shift vector elements along with wrap. Flows data back&forth.
1434  
1435  
1436 =for bad
1437  
1438 rotate does not process bad values.
1439 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1440  
1441  
1442 =cut
1443  
1444  
1445  
1446  
1447  
1448  
1449 120 *rotate = \&PDL::rotate;
1450  
1451  
1452  
1453  
1454  
1455 =head2 threadI
1456  
1457 =for sig
1458  
1459 Signature: (P(); C(); int id; SV *list)
1460  
1461 =for ref
1462  
1463 internal
1464  
1465 Put some dimensions to a threadid.
1466  
1467 =for example
1468  
1469 $b = $a->threadI(0,1,5); # thread over dims 1,5 in id 1
1470  
1471  
1472  
1473 =for bad
1474  
1475 threadI does not process bad values.
1476 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1477  
1478  
1479 =cut
1480  
1481  
1482  
1483  
1484  
1485  
1486 120 *threadI = \&PDL::threadI;
1487  
1488  
1489  
1490  
1491  
1492 =head2 identvaff
1493  
1494 =for sig
1495  
1496 Signature: (P(); C())
1497  
1498 =for ref
1499  
1500 A vaffine identity transformation (includes thread_id copying).
1501  
1502 Mainly for internal use.
1503  
1504  
1505 =for bad
1506  
1507 identvaff does not process bad values.
1508 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1509  
1510  
1511 =cut
1512  
1513  
1514  
1515  
1516  
1517  
1518 120 *identvaff = \&PDL::identvaff;
1519  
1520  
1521  
1522  
1523  
1524 =head2 unthread
1525  
1526 =for sig
1527  
1528 Signature: (P(); C(); int atind)
1529  
1530 =for ref
1531  
1532 All threaded dimensions are made real again.
1533  
1534 See [TBD Doc] for details and examples.
1535  
1536  
1537 =for bad
1538  
1539 unthread does not process bad values.
1540 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1541  
1542  
1543 =cut
1544  
1545  
1546  
1547  
1548  
1549  
1550 120 *unthread = \&PDL::unthread;
1551  
1552  
1553  
1554  
1555 =head2 dice
1556  
1557 =for ref
1558  
1559 Dice rows/columns/planes out of a PDL using indexes for
1560 each dimension.
1561  
1562 This function can be used to extract irregular subsets
1563 along many dimension of a PDL, e.g. only certain rows in an image,
1564 or planes in a cube. This can of course be done with
1565 the usual dimension tricks but this saves having to
1566 figure it out each time!
1567  
1568 This method is similar in functionality to the L<slice|/slice>
1569 method, but L<slice|/slice> requires that contiguous ranges or ranges
1570 with constant offset be extracted. ( i.e. L<slice|/slice> requires
1571 ranges of the form C<1,2,3,4,5> or C<2,4,6,8,10>). Because of this
1572 restriction, L<slice|/slice> is more memory efficient and slightly faster
1573 than dice
1574  
1575 =for usage
1576  
1577 $slice = $data->dice([0,2,6],[2,1,6]); # Dicing a 2-D array
1578  
1579 The arguments to dice are arrays (or 1D PDLs) for each dimension
1580 in the PDL. These arrays are used as indexes to which rows/columns/cubes,etc
1581 to dice-out (or extract) from the C<$data> PDL.
1582  
1583 Use C<X> to select all indices along a given dimension (compare also
1584 L<mslice|PDL::Core/mslice>). As usual (in slicing methods) trailing
1585 dimensions can be omitted implying C<X>'es for those.
1586  
1587 =for example
1588  
1589 pdl> $a = sequence(10,4)
1590 pdl> p $a
1591 [
1592 [ 0 1 2 3 4 5 6 7 8 9]
1593 [10 11 12 13 14 15 16 17 18 19]
1594 [20 21 22 23 24 25 26 27 28 29]
1595 [30 31 32 33 34 35 36 37 38 39]
1596 ]
1597 pdl> p $a->dice([1,2],[0,3]) # Select columns 1,2 and rows 0,3
1598 [
1599 [ 1 2]
1600 [31 32]
1601 ]
1602 pdl> p $a->dice(X,[0,3])
1603 [
1604 [ 0 1 2 3 4 5 6 7 8 9]
1605 [30 31 32 33 34 35 36 37 38 39]
1606 ]
1607 pdl> p $a->dice([0,2,5])
1608 [
1609 [ 0 2 5]
1610 [10 12 15]
1611 [20 22 25]
1612 [30 32 35]
1613 ]
1614  
1615 As this is an index function, any modifications to the
1616 slice change the parent (use the C<.=> operator).
1617  
1618 =cut
1619  
1620 sub PDL::dice {
1621  
1622 28 ▶ my $self = shift;
Subroutine dice: called CC=5 SCAR=16.2
Pod undocumented
1623 28 my @dim_indexes = @_; # array of dimension indexes
1624  
1625 # Check that the number of dim indexes <=
1626 # number of dimensions in the PDL
1627 28 my $no_indexes = scalar(@dim_indexes);
1628 28 my $noDims = $self->getndims;
1629 28 ▶ barf("PDL::dice: Number of index arrays ($no_indexes) not equal to the dimensions of the PDL ($noDims")
Branch: 1 branch
BranchTrueFalseTotal
if $no_indexes > $noDims 0 28 28
1630 if $no_indexes > $noDims;
1631 28 my $index;
1632 my $pdlIndex;
1633 28 my $outputPDL=$self;
1634 28 my $indexNo = 0;
1635  
1636 # Go thru each index array and dice the input PDL:
1637 28 foreach $index(@dim_indexes){
1638 32 ▶ $outputPDL = $outputPDL->dice_axis($indexNo,$index)
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $index and $index eq "X" 29 3 32
Condition66%2/3
not ref $index and $index eq "X"
!ll&&!rl&&r
2903
MC/DC50%1/2
not ref $index and $index eq "X"
not ref $index $index eq "X"
Truth table66%2/3
not(A) and B
A= not ref $indexB= $index eq "X"
ABresult
0X0
100
111
1639 unless !ref $index && $index eq 'X';
1640  
1641 32 $indexNo++;
1642 }
1643  
1644 28 return $outputPDL;
1645 }
1646 120 *dice = \&PDL::dice;
1647  
1648  
1649 =head2 dice_axis
1650  
1651 =for ref
1652  
1653 Dice rows/columns/planes from a single PDL axis (dimension)
1654 using index along a specified axis
1655  
1656 This function can be used to extract irregular subsets
1657 along any dimension, e.g. only certain rows in an image,
1658 or planes in a cube. This can of course be done with
1659 the usual dimension tricks but this saves having to
1660 figure it out each time!
1661  
1662 =for usage
1663  
1664 $slice = $data->dice_axis($axis,$index);
1665  
1666 =for example
1667  
1668 pdl> $a = sequence(10,4)
1669 pdl> $idx = pdl(1,2)
1670 pdl> p $a->dice_axis(0,$idx) # Select columns
1671 [
1672 [ 1 2]
1673 [11 12]
1674 [21 22]
1675 [31 32]
1676 ]
1677 pdl> $t = $a->dice_axis(1,$idx) # Select rows
1678 pdl> $t.=0
1679 pdl> p $a
1680 [
1681 [ 0 1 2 3 4 5 6 7 8 9]
1682 [ 0 0 0 0 0 0 0 0 0 0]
1683 [ 0 0 0 0 0 0 0 0 0 0]
1684 [30 31 32 33 34 35 36 37 38 39]
1685 ]
1686  
1687 The trick to using this is that the index selects
1688 elements along the dimensions specified, so if you
1689 have a 2D image C<axis=0> will select certain C<X> values
1690 - i.e. extract columns
1691  
1692 As this is an index function, any modifications to the
1693 slice change the parent.
1694  
1695 =cut
1696  
1697 sub PDL::dice_axis {
1698 30 ▶ my($self,$axis,$idx) = @_;
Subroutine dice_axis: called CC=3 SCAR=11.0
Pod undocumented
1699  
1700 # Convert to PDLs: array refs using new, otherwise use topdl:
1701 30 ▶ my $ix = (ref($idx) eq 'ARRAY') ? ref($self)->new($idx) : ref($self)->topdl($idx);
Branch: 1 branch
BranchTrueFalseTotal
ref $idx eq 'ARRAY' ? : 4 26 30
1702 30 my $n = $self->getndims;
1703 30 my $a = $ix->getndims;
1704 30 ▶ barf("index_axis: index must be <=1D") if $a>1;
Branch: 1 branch
BranchTrueFalseTotal
if $a > 1 0 30 30
1705 30 return $self->mv($axis,0)->index1d($ix)->mv(0,$axis);
1706 }
1707 120 *dice_axis = \&PDL::dice_axis;
1708  
1709  
1710  
1711  
1712  
1713 =head2 slice
1714  
1715 =for usage
1716  
1717 $slice = $data->slice([2,3],'x',[2,2,0],"-1:1:-1", "*3");
1718  
1719 =for ref
1720  
1721 Extract rectangular slices of a piddle, from a string specifier,
1722 an array ref specifier, or a combination.
1723  
1724 C<slice> is the main method for extracting regions of PDLs and
1725 manipulating their dimensionality. You can call it directly or
1726 via he L<NiceSlice|PDL::NiceSlice> source prefilter that extends
1727 Perl syntax o include array slicing.
1728  
1729 C<slice> can extract regions along each dimension of a source PDL,
1730 subsample or reverse those regions, dice each dimension by selecting a
1731 list of locations along it, or basic PDL indexing routine. The
1732 selected subfield remains connected to the original PDL via dataflow.
1733 In most cases this neither allocates more memory nor slows down
1734 subsequent operations on either of the two connected PDLs.
1735  
1736 You pass in a list of arguments. Each term in the list controls
1737 the disposition of one axis of the source PDL and/or returned PDL.
1738 Each term can be a string-format cut specifier, a list ref that
1739 gives the same information without recourse to string manipulation,
1740 or a PDL with up to 1 dimension giving indices along that axis that
1741 should be selected.
1742  
1743 If you want to pass in a single string specifier for the entire
1744 operation, you can pass in a comma-delimited list as the first
1745 argument. C<slice> detects this condition and splits the string
1746 into a regular argument list. This calling style is fully
1747 backwards compatible with C<slice> calls from before PDL 2.006.
1748  
1749 B<STRING SYNTAX>
1750  
1751 If a particular argument to C<slice> is a string, it is parsed as a
1752 selection, an affine slice, or a dummy dimension depending on the
1753 form. Leading or trailing whitespace in any part of each specifier is
1754 ignored (though it is not ignored within numbers).
1755  
1756 =over 3
1757  
1758 =item C<< '' >>, C<< : >>, or C<< X >> -- keep
1759  
1760 The empty string, C<:>, or C<X> cause the entire corresponding
1761 dimension to be kept unchanged.
1762  
1763  
1764 =item C<< <n> >> -- selection
1765  
1766 A single number alone causes a single index to be selected from the
1767 corresponding dimension. The dimension is kept (and reduced to size
1768 1) in the output.
1769  
1770 =item C<< (<n>) >> -- selection and collapse
1771  
1772 A single number in parenthesis causes a single index to be selected
1773 from the corresponding dimension. The dimension is discarded
1774 (completely eliminated) in the output.
1775  
1776 =item C<< <n>:<m> >> -- select an inclusive range
1777  
1778 Two numbers separated by a colon selects a range of values from the
1779 corresponding axis, e.g. C<< 3:4 >> selects elements 3 and 4 along the
1780 corresponding axis, and reduces that axis to size 2 in the output.
1781 Both numbers are regularized so that you can address the last element
1782 of the axis with an index of C< -1 >. If, after regularization, the
1783 two numbers are the same, then exactly one element gets selected (just
1784 like the C<< <n> >> case). If, after regulariation, the second number
1785 is lower than the first, then the resulting slice counts down rather
1786 than up -- e.g. C<-1:0> will return the entire axis, in reversed
1787 order.
1788  
1789 =item C<< <n>:<m>:<s> >> -- select a range with explicit step
1790  
1791 If you include a third parameter, it is the stride of the extracted
1792 range. For example, C<< 0:-1:2 >> will sample every other element
1793 across the complete dimension. Specifying a stride of 1 prevents
1794 autoreversal -- so to ensure that your slice is *always* forward
1795 you can specify, e.g., C<< 2:$n:1 >>. In that case, an "impossible"
1796 slice gets an Empty PDL (with 0 elements along the corresponding
1797 dimension), so you can generate an Empty PDL with a slice of the
1798 form C<< 2:1:1 >>.
1799  
1800 =item C<< *<n> >> -- insert a dummy dimension
1801  
1802 Dummy dimensions aren't present in the original source and are
1803 "mocked up" to match dimensional slots, by repeating the data
1804 in the original PDL some number of times. An asterisk followed
1805 by a number produces a dummy dimension in the output, for
1806 example C<< *2 >> will generate a dimension of size 2 at
1807 the corresponding location in the output dim list. Omitting
1808 the numeber (and using just an asterisk) inserts a dummy dimension
1809 of size 1.
1810  
1811 =back
1812  
1813 B<ARRAY REF SYNTAX>
1814  
1815 If you feed in an ARRAY ref as a slice term, then it can have
1816 0-3 elements. The first element is the start of the slice along
1817 the corresponding dim; the second is the end; and the third is
1818 the stepsize. Different combinations of inputs give the same
1819 flexibility as the string syntax.
1820  
1821 =over 3
1822  
1823 =item C<< [] >> - keep dim intact
1824  
1825 An empty ARRAY ref keeps the entire corresponding dim
1826  
1827 =item C<< [ 'X' ] >> - keep dim intact
1828  
1829 =item C<< [ '*',$n ] >> - generate a dummy dim of size $n
1830  
1831 If $n is missing, you get a dummy dim of size 1.
1832  
1833 =item C<< [ $dex, , 0 ] >> - collapse and discard dim
1834  
1835 C<$dex> must be a single value. It is used to index
1836 the source, and the corresponding dimension is discarded.
1837  
1838 =item C<< [ $start, $end ] >> - collect inclusive slice
1839  
1840 In the simple two-number case, you get a slice that runs
1841 up or down (as appropriate) to connect $start and $end.
1842  
1843 =item C<< [ $start, $end, $inc ] >> - collect inclusive slice
1844  
1845 The three-number case works exactly like the three-number
1846 string case above.
1847  
1848 =back
1849  
1850 B<PDL args for dicing>
1851  
1852 If you pass in a 0- or 1-D PDL as a slicing argument, the
1853 corresponding dimension is "diced" -- you get one position
1854 along the corresponding dim, per element of the indexing PDL,
1855 e.g. C<< $a->slice( pdl(3,4,9)) >> gives you elements 3, 4, and
1856 9 along the 0 dim of C<< $a >>.
1857  
1858 Because dicing is not an affine transformation, it is slower than
1859 direct slicing even though the syntax is convenient.
1860  
1861  
1862 =for example
1863  
1864 $a->slice('1:3'); # return the second to fourth elements of $a
1865 $a->slice('3:1'); # reverse the above
1866 $a->slice('-2:1'); # return last-but-one to second elements of $a
1867  
1868 $a->slice([1,3]); # Same as above three calls, but using array ref syntax
1869 $a->slice([3,1]);
1870 $a->slice([-2,1]);
1871  
1872 =cut
1873  
1874  
1875 ##############################
1876 # 'slice' is now implemented as a small Perl wrapper around
1877 # a PP call. This permits unification of the former slice,
1878 # dice, and nslice into a single call. At the moment, dicing
1879 # is implemented a bit kludgily (it is detected in the Perl
1880 # front-end), but it is serviceable.
1881 # --CED 12-Sep-2013
1882  
1883 120 *slice = \&PDL::slice;
1884 sub PDL::slice (;@) {
1885 2150 ▶ my ($source, @others) = @_;
Subroutine slice: called CC=7 SCAR=19.6
Pod undocumented
1886  
1887 # Deal with dicing. This is lame and slow compared to the
1888 # faster slicing, but works okay. We loop over each argument,
1889 # and if it's a PDL we dispatch it in the most straightforward
1890 # way. Single-element and zero-element PDLs are trivial and get
1891 # converted into slices for faster handling later.
1892  
1893 2150 for my $i(0..$#others) {
1894 2541 ▶ if( blessed($others[$i]) && $others[$i]->isa('PDL') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($others[$i]) and $others[$i]->isa("PDL")) 5 2536 2541
Condition66%2/3
builtin::blessed($others[$i]) and $others[$i]->isa("PDL")
!ll&&!rl&&r
253605
MC/DC50%1/2
builtin::blessed($others[$i]) and $others[$i]->isa("PDL")
builtin::blessed($others[$i]) $others[$i]->isa("PDL")
Truth table66%2/3
A and B
A= builtin::blessed($others[$i])B= $others[$i]->isa("PDL")
ABresult
0X0
100
111
1895 5 my $idx = $others[$i];
1896 5 ▶ if($idx->ndims > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->ndims > 1) 0 5 5
1897 0 barf("slice: dicing parameters must be at most 1D (arg $i)\n");
1898 }
1899 5 my $nlm = $idx->nelem;
1900  
1901 5 ▶ if($nlm > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nlm > 1) { } 3 2 5
elsif ($nlm) { } 1 1 2
1902  
1903 #### More than one element - we have to dice (darn it).
1904 3 my $n = $source->getndims;
1905 3 $source = $source->mv($i,0)->index1d($idx)->mv(0,$i);
1906 3 $others[$i] = '';
1907  
1908 }
1909 elsif($nlm) {
1910  
1911 #### One element - convert to a regular slice.
1912 1 $others[$i] = $idx->flat->at(0);
1913  
1914 }
1915 else {
1916
1917 #### Zero elements -- force an extended empty.
1918 1 $others[$i] = "1:0:1";
1919 }
1920 }
1921 }
1922  
1923 2150 PDL::sliceb($source,\@others);
1924 }
1925  
1926  
1927  
1928  
1929  
1930 =head2 sliceb
1931  
1932 =for sig
1933  
1934 Signature: (P(); C(); SV *args)
1935  
1936  
1937 =for ref
1938  
1939 info not available
1940  
1941  
1942 =for bad
1943  
1944 sliceb does not process bad values.
1945 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1946  
1947  
1948 =cut
1949  
1950  
1951  
1952  
1953  
1954  
1955 120 *sliceb = \&PDL::sliceb;
1956  
1957  
1958  
1959 ;
1960  
1961  
1962 =head1 BUGS
1963  
1964 For the moment, you can't slice one of the zero-length dims of an
1965 empty piddle. It is not clear how to implement this in a way that makes
1966 sense.
1967  
1968 Many types of index errors are reported far from the indexing
1969 operation that caused them. This is caused by the underlying architecture:
1970 slice() sets up a mapping between variables, but that mapping isn't
1971 tested for correctness until it is used (potentially much later).
1972  
1973 =head1 AUTHOR
1974  
1975 Copyright (C) 1997 Tuomas J. Lukka. Contributions by
1976 Craig DeForest, deforest@boulder.swri.edu.
1977 Documentation contributions by David Mertens.
1978 All rights reserved. There is no warranty. You are allowed
1979 to redistribute this software / documentation under certain
1980 conditions. For details, see the file COPYING in the PDL
1981 distribution. If this file is separated from the PDL distribution,
1982 the copyright notice should be included in the file.
1983  
1984 =cut
1985  
1986  
1987  
1988  
1989  
1990 # Exit with OK status
1991  
1992 120 1;
1993  
1994
« blib/lib/PDL/NiceSlice/FilterUtilCall.pm ↑ Summary blib/lib/PDL/Graphics/LUT.pm »