1Ufunc(3) User Contributed Perl Documentation Ufunc(3)
2
3
4
6 PDL::Ufunc - primitive ufunc operations for pdl
7
9 This module provides some primitive and useful functions defined using
10 PDL::PP based on functionality of what are sometimes called ufuncs (for
11 example NumPY and Mathematica talk about these). It collects all the
12 functions generally used to "reduce" or "accumulate" along a dimension.
13 These all do their job across the first dimension but by using the
14 slicing functions you can do it on any dimension.
15
16 The PDL::Reduce module provides an alternative interface to many of the
17 functions in this module.
18
20 use PDL::Ufunc;
21
23 prodover
24 Signature: (a(n); int+ [o]b())
25
26 Project via product to N-1 dimensions
27
28 This function reduces the dimensionality of a piddle by one by taking
29 the product along the 1st dimension.
30
31 By using xchg etc. it is possible to use any dimension.
32
33 $a = prodover($b);
34
35 $spectrum = prodover $image->xchg(0,1)
36
37 prodover does handle bad values. It will set the bad-value flag of all
38 output piddles if the flag is set for any of the input piddles.
39
40 dprodover
41 Signature: (a(n); double [o]b())
42
43 Project via product to N-1 dimensions
44
45 This function reduces the dimensionality of a piddle by one by taking
46 the product along the 1st dimension.
47
48 By using xchg etc. it is possible to use any dimension.
49
50 $a = dprodover($b);
51
52 $spectrum = dprodover $image->xchg(0,1)
53
54 Unlike prodover, the calculations are performed in double precision.
55
56 dprodover does handle bad values. It will set the bad-value flag of
57 all output piddles if the flag is set for any of the input piddles.
58
59 cumuprodover
60 Signature: (a(n); int+ [o]b(n))
61
62 Cumulative product
63
64 This function calculates the cumulative product along the 1st
65 dimension.
66
67 By using xchg etc. it is possible to use any dimension.
68
69 The sum is started so that the first element in the cumulative product
70 is the first element of the parameter.
71
72 $a = cumuprodover($b);
73
74 $spectrum = cumuprodover $image->xchg(0,1)
75
76 cumuprodover does handle bad values. It will set the bad-value flag of
77 all output piddles if the flag is set for any of the input piddles.
78
79 dcumuprodover
80 Signature: (a(n); double [o]b(n))
81
82 Cumulative product
83
84 This function calculates the cumulative product along the 1st
85 dimension.
86
87 By using xchg etc. it is possible to use any dimension.
88
89 The sum is started so that the first element in the cumulative product
90 is the first element of the parameter.
91
92 $a = cumuprodover($b);
93
94 $spectrum = cumuprodover $image->xchg(0,1)
95
96 Unlike cumuprodover, the calculations are performed in double
97 precision.
98
99 dcumuprodover does handle bad values. It will set the bad-value flag
100 of all output piddles if the flag is set for any of the input piddles.
101
102 sumover
103 Signature: (a(n); int+ [o]b())
104
105 Project via sum to N-1 dimensions
106
107 This function reduces the dimensionality of a piddle by one by taking
108 the sum along the 1st dimension.
109
110 By using xchg etc. it is possible to use any dimension.
111
112 $a = sumover($b);
113
114 $spectrum = sumover $image->xchg(0,1)
115
116 sumover does handle bad values. It will set the bad-value flag of all
117 output piddles if the flag is set for any of the input piddles.
118
119 dsumover
120 Signature: (a(n); double [o]b())
121
122 Project via sum to N-1 dimensions
123
124 This function reduces the dimensionality of a piddle by one by taking
125 the sum along the 1st dimension.
126
127 By using xchg etc. it is possible to use any dimension.
128
129 $a = dsumover($b);
130
131 $spectrum = dsumover $image->xchg(0,1)
132
133 Unlike sumover, the calculations are performed in double precision.
134
135 dsumover does handle bad values. It will set the bad-value flag of all
136 output piddles if the flag is set for any of the input piddles.
137
138 cumusumover
139 Signature: (a(n); int+ [o]b(n))
140
141 Cumulative sum
142
143 This function calculates the cumulative sum along the 1st dimension.
144
145 By using xchg etc. it is possible to use any dimension.
146
147 The sum is started so that the first element in the cumulative sum is
148 the first element of the parameter.
149
150 $a = cumusumover($b);
151
152 $spectrum = cumusumover $image->xchg(0,1)
153
154 cumusumover does handle bad values. It will set the bad-value flag of
155 all output piddles if the flag is set for any of the input piddles.
156
157 dcumusumover
158 Signature: (a(n); double [o]b(n))
159
160 Cumulative sum
161
162 This function calculates the cumulative sum along the 1st dimension.
163
164 By using xchg etc. it is possible to use any dimension.
165
166 The sum is started so that the first element in the cumulative sum is
167 the first element of the parameter.
168
169 $a = cumusumover($b);
170
171 $spectrum = cumusumover $image->xchg(0,1)
172
173 Unlike cumusumover, the calculations are performed in double precision.
174
175 dcumusumover does handle bad values. It will set the bad-value flag of
176 all output piddles if the flag is set for any of the input piddles.
177
178 orover
179 Signature: (a(n); int+ [o]b())
180
181 Project via or to N-1 dimensions
182
183 This function reduces the dimensionality of a piddle by one by taking
184 the or along the 1st dimension.
185
186 By using xchg etc. it is possible to use any dimension.
187
188 $a = orover($b);
189
190 $spectrum = orover $image->xchg(0,1)
191
192 If "a()" contains only bad data (and its bad flag is set), "b()" is set
193 bad. Otherwise "b()" will have its bad flag cleared, as it will not
194 contain any bad values.
195
196 bandover
197 Signature: (a(n); int+ [o]b())
198
199 Project via bitwise and to N-1 dimensions
200
201 This function reduces the dimensionality of a piddle by one by taking
202 the bitwise and along the 1st dimension.
203
204 By using xchg etc. it is possible to use any dimension.
205
206 $a = bandover($b);
207
208 $spectrum = bandover $image->xchg(0,1)
209
210 If "a()" contains only bad data (and its bad flag is set), "b()" is set
211 bad. Otherwise "b()" will have its bad flag cleared, as it will not
212 contain any bad values.
213
214 borover
215 Signature: (a(n); int+ [o]b())
216
217 Project via bitwise or to N-1 dimensions
218
219 This function reduces the dimensionality of a piddle by one by taking
220 the bitwise or along the 1st dimension.
221
222 By using xchg etc. it is possible to use any dimension.
223
224 $a = borover($b);
225
226 $spectrum = borover $image->xchg(0,1)
227
228 If "a()" contains only bad data (and its bad flag is set), "b()" is set
229 bad. Otherwise "b()" will have its bad flag cleared, as it will not
230 contain any bad values.
231
232 zcover
233 Signature: (a(n); int+ [o]b())
234
235 Project via == 0 to N-1 dimensions
236
237 This function reduces the dimensionality of a piddle by one by taking
238 the == 0 along the 1st dimension.
239
240 By using xchg etc. it is possible to use any dimension.
241
242 $a = zcover($b);
243
244 $spectrum = zcover $image->xchg(0,1)
245
246 If "a()" contains only bad data (and its bad flag is set), "b()" is set
247 bad. Otherwise "b()" will have its bad flag cleared, as it will not
248 contain any bad values.
249
250 andover
251 Signature: (a(n); int+ [o]b())
252
253 Project via and to N-1 dimensions
254
255 This function reduces the dimensionality of a piddle by one by taking
256 the and along the 1st dimension.
257
258 By using xchg etc. it is possible to use any dimension.
259
260 $a = andover($b);
261
262 $spectrum = andover $image->xchg(0,1)
263
264 If "a()" contains only bad data (and its bad flag is set), "b()" is set
265 bad. Otherwise "b()" will have its bad flag cleared, as it will not
266 contain any bad values.
267
268 intover
269 Signature: (a(n); int+ [o]b())
270
271 Project via integral to N-1 dimensions
272
273 This function reduces the dimensionality of a piddle by one by taking
274 the integral along the 1st dimension.
275
276 By using xchg etc. it is possible to use any dimension.
277
278 $a = intover($b);
279
280 $spectrum = intover $image->xchg(0,1)
281
282 Notes:
283
284 For "n > 3", these are all "O(h^4)" (like Simpson's rule), but are
285 integrals between the end points assuming the pdl gives values just at
286 these centres: for such `functions', sumover is correct to O(h), but is
287 the natural (and correct) choice for binned data, of course.
288
289 intover ignores the bad-value flag of the input piddles. It will set
290 the bad-value flag of all output piddles if the flag is set for any of
291 the input piddles.
292
293 average
294 Signature: (a(n); int+ [o]b())
295
296 Project via average to N-1 dimensions
297
298 This function reduces the dimensionality of a piddle by one by taking
299 the average along the 1st dimension.
300
301 By using xchg etc. it is possible to use any dimension.
302
303 $a = average($b);
304
305 $spectrum = average $image->xchg(0,1)
306
307 average does handle bad values. It will set the bad-value flag of all
308 output piddles if the flag is set for any of the input piddles.
309
310 daverage
311 Signature: (a(n); double [o]b())
312
313 Project via average to N-1 dimensions
314
315 This function reduces the dimensionality of a piddle by one by taking
316 the average along the 1st dimension.
317
318 By using xchg etc. it is possible to use any dimension.
319
320 $a = daverage($b);
321
322 $spectrum = daverage $image->xchg(0,1)
323
324 Unlike average, the calculation is performed in double precision.
325
326 daverage does handle bad values. It will set the bad-value flag of all
327 output piddles if the flag is set for any of the input piddles.
328
329 medover
330 Signature: (a(n); [o]b(); [t]tmp(n))
331
332 Project via median to N-1 dimensions
333
334 This function reduces the dimensionality of a piddle by one by taking
335 the median along the 1st dimension.
336
337 By using xchg etc. it is possible to use any dimension.
338
339 $a = medover($b);
340
341 $spectrum = medover $image->xchg(0,1)
342
343 medover does handle bad values. It will set the bad-value flag of all
344 output piddles if the flag is set for any of the input piddles.
345
346 oddmedover
347 Signature: (a(n); [o]b(); [t]tmp(n))
348
349 Project via oddmedian to N-1 dimensions
350
351 This function reduces the dimensionality of a piddle by one by taking
352 the oddmedian along the 1st dimension.
353
354 By using xchg etc. it is possible to use any dimension.
355
356 $a = oddmedover($b);
357
358 $spectrum = oddmedover $image->xchg(0,1)
359
360 The median is sometimes not a good choice as if the array has an even
361 number of elements it lies half-way between the two middle values -
362 thus it does not always correspond to a data value. The lower-odd
363 median is just the lower of these two values and so it ALWAYS sits on
364 an actual data value which is useful in some circumstances.
365
366 oddmedover does handle bad values. It will set the bad-value flag of
367 all output piddles if the flag is set for any of the input piddles.
368
369 pctover
370 Signature: (a(n); p(); [o]b(); [t]tmp(n))
371
372 Project via percentile to N-1 dimensions
373
374 This function reduces the dimensionality of a piddle by one by finding
375 the specified percentile (p) along the 1st dimension. The specified
376 percentile must be between 0.0 and 1.0. When the specified percentile
377 falls between data points, the result is interpolated. Values outside
378 the allowed range are clipped to 0.0 or 1.0 respectively. The
379 algorithm implemented here is based on the interpolation variant
380 described at "/en.wikipedia.org/wiki/Percentile" in http: as used by
381 Microsoft Excel and recommended by NIST.
382
383 By using xchg etc. it is possible to use any dimension.
384
385 $a = pctover($b, $p);
386
387 $spectrum = pctover $image->xchg(0,1) $p
388
389 pctover does not process bad values. It will set the bad-value flag of
390 all output piddles if the flag is set for any of the input piddles.
391
392 oddpctover
393 Signature: (a(n); p(); [o]b(); [t]tmp(n))
394
395 Project via percentile to N-1 dimensions
396
397 This function reduces the dimensionality of a piddle by one by finding
398 the specified percentile along the 1st dimension. The specified
399 percentile must be between 0.0 and 1.0. When the specified percentile
400 falls between two values, the nearest data value is the result. The
401 algorithm implemented is from the textbook version described first at
402 "/en.wikipedia.org/wiki/Percentile" in http:.
403
404 By using xchg etc. it is possible to use any dimension.
405
406 $a = oddpctover($b, $p);
407
408 $spectrum = oddpctover $image->xchg(0,1) $p
409
410 oddpctover does not process bad values. It will set the bad-value flag
411 of all output piddles if the flag is set for any of the input piddles.
412
413 pct
414 Return the specified percentile of all elements in a piddle. The
415 specified percentile (p) must be between 0.0 and 1.0. When the
416 specified percentile falls between data points, the result is
417 interpolated.
418
419 $x = pct($data, $pct);
420
421 oddpct
422 Return the specified percentile of all elements in a piddle. The
423 specified percentile must be between 0.0 and 1.0. When the specified
424 percentile falls between two values, the nearest data value is the
425 result.
426
427 $x = oddpct($data, $pct);
428
429 avg
430 Return the average of all elements in a piddle
431
432 $x = avg($data);
433
434 This routine handles bad values (see the documentation for average). I
435 still need to decide how to handle the case when the return value is a
436 bad value (eg to make sure it has the same type as the input piddle OR
437 perhaps we should die - makes sense for the conditional ops but not
438 things like sum)
439
440 sum
441 Return the sum of all elements in a piddle
442
443 $x = sum($data);
444
445 This routine handles bad values (see the documentation for sumover). I
446 still need to decide how to handle the case when the return value is a
447 bad value (eg to make sure it has the same type as the input piddle OR
448 perhaps we should die - makes sense for the conditional ops but not
449 things like sum)
450
451 prod
452 Return the product of all elements in a piddle
453
454 $x = prod($data);
455
456 This routine handles bad values (see the documentation for prodover).
457 I still need to decide how to handle the case when the return value is
458 a bad value (eg to make sure it has the same type as the input piddle
459 OR perhaps we should die - makes sense for the conditional ops but not
460 things like sum)
461
462 davg
463 Return the average (in double precision) of all elements in a piddle
464
465 $x = davg($data);
466
467 This routine handles bad values (see the documentation for daverage).
468 I still need to decide how to handle the case when the return value is
469 a bad value (eg to make sure it has the same type as the input piddle
470 OR perhaps we should die - makes sense for the conditional ops but not
471 things like sum)
472
473 dsum
474 Return the sum (in double precision) of all elements in a piddle
475
476 $x = dsum($data);
477
478 This routine handles bad values (see the documentation for dsumover).
479 I still need to decide how to handle the case when the return value is
480 a bad value (eg to make sure it has the same type as the input piddle
481 OR perhaps we should die - makes sense for the conditional ops but not
482 things like sum)
483
484 dprod
485 Return the product (in double precision) of all elements in a piddle
486
487 $x = dprod($data);
488
489 This routine handles bad values (see the documentation for dprodover).
490 I still need to decide how to handle the case when the return value is
491 a bad value (eg to make sure it has the same type as the input piddle
492 OR perhaps we should die - makes sense for the conditional ops but not
493 things like sum)
494
495 zcheck
496 Return the check for zero of all elements in a piddle
497
498 $x = zcheck($data);
499
500 This routine handles bad values (see the documentation for zcover). I
501 still need to decide how to handle the case when the return value is a
502 bad value (eg to make sure it has the same type as the input piddle OR
503 perhaps we should die - makes sense for the conditional ops but not
504 things like sum)
505
506 and
507 Return the logical and of all elements in a piddle
508
509 $x = and($data);
510
511 This routine handles bad values (see the documentation for andover). I
512 still need to decide how to handle the case when the return value is a
513 bad value (eg to make sure it has the same type as the input piddle OR
514 perhaps we should die - makes sense for the conditional ops but not
515 things like sum)
516
517 band
518 Return the bitwise and of all elements in a piddle
519
520 $x = band($data);
521
522 This routine handles bad values (see the documentation for bandover).
523 I still need to decide how to handle the case when the return value is
524 a bad value (eg to make sure it has the same type as the input piddle
525 OR perhaps we should die - makes sense for the conditional ops but not
526 things like sum)
527
528 or
529 Return the logical or of all elements in a piddle
530
531 $x = or($data);
532
533 This routine handles bad values (see the documentation for orover). I
534 still need to decide how to handle the case when the return value is a
535 bad value (eg to make sure it has the same type as the input piddle OR
536 perhaps we should die - makes sense for the conditional ops but not
537 things like sum)
538
539 bor
540 Return the bitwise or of all elements in a piddle
541
542 $x = bor($data);
543
544 This routine handles bad values (see the documentation for borover). I
545 still need to decide how to handle the case when the return value is a
546 bad value (eg to make sure it has the same type as the input piddle OR
547 perhaps we should die - makes sense for the conditional ops but not
548 things like sum)
549
550 min
551 Return the minimum of all elements in a piddle
552
553 $x = min($data);
554
555 This routine handles bad values (see the documentation for minimum). I
556 still need to decide how to handle the case when the return value is a
557 bad value (eg to make sure it has the same type as the input piddle OR
558 perhaps we should die - makes sense for the conditional ops but not
559 things like sum)
560
561 max
562 Return the maximum of all elements in a piddle
563
564 $x = max($data);
565
566 This routine handles bad values (see the documentation for maximum). I
567 still need to decide how to handle the case when the return value is a
568 bad value (eg to make sure it has the same type as the input piddle OR
569 perhaps we should die - makes sense for the conditional ops but not
570 things like sum)
571
572 median
573 Return the median of all elements in a piddle
574
575 $x = median($data);
576
577 This routine handles bad values (see the documentation for medover). I
578 still need to decide how to handle the case when the return value is a
579 bad value (eg to make sure it has the same type as the input piddle OR
580 perhaps we should die - makes sense for the conditional ops but not
581 things like sum)
582
583 oddmedian
584 Return the oddmedian of all elements in a piddle
585
586 $x = oddmedian($data);
587
588 This routine handles bad values (see the documentation for oddmedover).
589 I still need to decide how to handle the case when the return value is
590 a bad value (eg to make sure it has the same type as the input piddle
591 OR perhaps we should die - makes sense for the conditional ops but not
592 things like sum)
593
594 any
595 Return true if any element in piddle set
596
597 Useful in conditional expressions:
598
599 if (any $a>15) { print "some values are greater than 15\n" }
600
601 See or for comments on what happens when all elements in the check are
602 bad.
603
604 all
605 Return true if all elements in piddle set
606
607 Useful in conditional expressions:
608
609 if (all $a>15) { print "all values are greater than 15\n" }
610
611 See and for comments on what happens when all elements in the check are
612 bad.
613
614 minmax
615 Returns an array with minimum and maximum values of a piddle.
616
617 ($mn, $mx) = minmax($pdl);
618
619 This routine does not thread over the dimensions of $pdl; it returns
620 the minimum and maximum values of the whole array. See minmaximum if
621 this is not what is required. The two values are returned as Perl
622 scalars similar to min/max.
623
624 perldl> $x = pdl [1,-2,3,5,0]
625 perldl> ($min, $max) = minmax($x);
626 perldl> p "$min $max\n";
627 -2 5
628
629 qsort
630 Signature: (a(n); [o]b(n))
631
632 Quicksort a vector into ascending order.
633
634 print qsort random(10);
635
636 Bad values are moved to the end of the array:
637
638 perldl> p $b
639 [42 47 98 BAD 22 96 74 41 79 76 96 BAD 32 76 25 59 BAD 96 32 BAD]
640 perldl> p qsort($b)
641 [22 25 32 32 41 42 47 59 74 76 76 79 96 96 96 98 BAD BAD BAD BAD]
642
643 qsorti
644 Signature: (a(n); int [o]indx(n))
645
646 Quicksort a vector and return index of elements in ascending order.
647
648 $ix = qsorti $a;
649 print $a->index($ix); # Sorted list
650
651 Bad elements are moved to the end of the array:
652
653 perldl> p $b
654 [42 47 98 BAD 22 96 74 41 79 76 96 BAD 32 76 25 59 BAD 96 32 BAD]
655 perldl> p $b->index( qsorti($b) )
656 [22 25 32 32 41 42 47 59 74 76 76 79 96 96 96 98 BAD BAD BAD BAD]
657
658 qsortvec
659 Signature: (a(n,m); [o]b(n,m))
660
661 Sort a list of vectors lexicographically.
662
663 The 0th dimension of the source piddle is dimension in the vector; the
664 1st dimension is list order. Higher dimensions are threaded over.
665
666 print qsortvec pdl([[1,2],[0,500],[2,3],[4,2],[3,4],[3,5]]);
667 [
668 [ 0 500]
669 [ 1 2]
670 [ 2 3]
671 [ 3 4]
672 [ 3 5]
673 [ 4 2]
674 ]
675
676 Vectors with bad components should be moved to the end of the array:
677
678 qsortveci
679 Signature: (a(n,m); int [o]indx(m))
680
681 Sort a list of vectors lexicographically, returning the indices of the
682 sorted vectors rather than the sorted list itself.
683
684 As with "qsortvec", the input PDL should be an NxM array containing M
685 separate N-dimensional vectors. The return value is an integer M-PDL
686 containing the M-indices of original array rows, in sorted order.
687
688 As with "qsortvec", the zeroth element of the vectors runs slowest in
689 the sorted list.
690
691 Additional dimensions are threaded over: each plane is sorted
692 separately, so qsortveci may be thought of as a collapse operator of
693 sorts (groan).
694
695 Vectors with bad components should be moved to the end of the array:
696
697 minimum
698 Signature: (a(n); [o]c())
699
700 Project via minimum to N-1 dimensions
701
702 This function reduces the dimensionality of a piddle by one by taking
703 the minimum along the 1st dimension.
704
705 By using xchg etc. it is possible to use any dimension.
706
707 $a = minimum($b);
708
709 $spectrum = minimum $image->xchg(0,1)
710
711 Output is set bad if all elements of the input are bad, otherwise the
712 bad flag is cleared for the output piddle.
713
714 Note that "NaNs" are considered to be valid values; see isfinite and
715 badmask for ways of masking NaNs.
716
717 minimum_ind
718 Signature: (a(n); int [o] c())
719
720 Like minimum but returns the index rather than the value
721
722 Output is set bad if all elements of the input are bad, otherwise the
723 bad flag is cleared for the output piddle.
724
725 minimum_n_ind
726 Signature: (a(n); int[o]c(m))
727
728 Returns the index of "m" minimum elements
729
730 Not yet been converted to ignore bad values
731
732 maximum
733 Signature: (a(n); [o]c())
734
735 Project via maximum to N-1 dimensions
736
737 This function reduces the dimensionality of a piddle by one by taking
738 the maximum along the 1st dimension.
739
740 By using xchg etc. it is possible to use any dimension.
741
742 $a = maximum($b);
743
744 $spectrum = maximum $image->xchg(0,1)
745
746 Output is set bad if all elements of the input are bad, otherwise the
747 bad flag is cleared for the output piddle.
748
749 Note that "NaNs" are considered to be valid values; see isfinite and
750 badmask for ways of masking NaNs.
751
752 maximum_ind
753 Signature: (a(n); int [o] c())
754
755 Like maximum but returns the index rather than the value
756
757 Output is set bad if all elements of the input are bad, otherwise the
758 bad flag is cleared for the output piddle.
759
760 maximum_n_ind
761 Signature: (a(n); int[o]c(m))
762
763 Returns the index of "m" maximum elements
764
765 Not yet been converted to ignore bad values
766
767 minmaximum
768 Signature: (a(n); [o]cmin(); [o] cmax(); int [o]cmin_ind(); int [o]cmax_ind())
769
770 Find minimum and maximum and their indices for a given piddle;
771
772 perldl> $a=pdl [[-2,3,4],[1,0,3]]
773 perldl> ($min, $max, $min_ind, $max_ind)=minmaximum($a)
774 perldl> p $min, $max, $min_ind, $max_ind
775 [-2 0] [4 3] [0 1] [2 2]
776
777 See also minmax, which clumps the piddle together.
778
779 If "a()" contains only bad data, then the output piddles will be set
780 bad, along with their bad flag. Otherwise they will have their bad
781 flags cleared, since they will not contain any bad values.
782
784 Copyright (C) Tuomas J. Lukka 1997 (lukka@husc.harvard.edu).
785 Contributions by Christian Soeller (c.soeller@auckland.ac.nz) and Karl
786 Glazebrook (kgb@aaoepp.aao.gov.au). All rights reserved. There is no
787 warranty. You are allowed to redistribute this software / documentation
788 under certain conditions. For details, see the file COPYING in the PDL
789 distribution. If this file is separated from the PDL distribution, the
790 copyright notice should be included in the file.
791
792
793
794perl v5.12.3 2011-03-31 Ufunc(3)