blib/lib/PDL/Reduce.pm

filter: Statementstmt 95.1% 39 / 41 Branchbran 77.2% 17 / 22 Conditioncond 50.0% 6 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 2 totaltotal 74.4% 67 / 90 CC 17 SCAR 27.5
CC
17
Cov
83%
reduce
19.1
canreduce
2.0
BEGIN
1.0
CRAP 15.7
« blib/lib/PDL/Opt/Simplex.pm ↑ Summary blib/lib/PDL/IO/Storable.pm »
1 =head1 NAME
2  
3 PDL::Reduce -- a C<reduce> function for PDL
4  
5 =head1 DESCRIPTION
6  
7 Many languages have a C<reduce> function used to reduce
8 the rank of an N-D array by one. It works by applying a selected
9 operation along a specified dimension. This module implements
10 such a function for PDL by providing a simplified interface
11 to the existing projection functions (e.g. C<sumover>,
12 C<maximum>, C<average>, etc).
13  
14 =head1 SYNOPSIS
15  
16 use PDL::Reduce;
17 $a = sequence 5,5;
18 # reduce by adding all
19 # elements along 2nd dimension
20 $b = $a->reduce('add',1);
21 @ops = $a->canreduce; # return a list of all allowed operations
22  
23 =head1 FUNCTIONS
24  
25 =cut
26  
27 # in a very similar vein we want the following methods
28 # (1) accumulate
29 # (2) outer
30 # what's reduceat ??
31  
32 # TODO
33 # - aliases (e.g. plus -> add)
34 # - other binary ops?
35 # - allow general subs?
36  
37 package PDL::Reduce;
38 1 ▶use PDL::Core ''; # barf
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 @PDL::Reduce::ISA = qw/PDL::Exporter/;
43 1 @PDL::Reduce::EXPORT_OK = qw/reduce canreduce/;
44 1 %PDL::Reduce::EXPORT_TAGS = (Func=>[@PDL::Reduce::EXPORT_OK]);
45  
46 # maps operations onto underlying PDL primitives
47 1 my %reduce = (
48 add => 'sumover',
49 '+' => 'sumover',
50 plus => 'sumover',
51 mult => 'prodover',
52 '*' => 'prodover',
53 dadd => 'dsumover',
54 dmult => 'dprodover',
55 avg => 'average',
56 davg => 'daverage',
57 and => 'andover',
58 band => 'bandover',
59 bor => 'borover',
60 or => 'orover',
61 median => 'medover',
62 integral => 'intover',
63 max => 'maximum',
64 min => 'minimum',
65 oddmedian => 'oddmedover',
66 iszero => 'zcover',
67 );
68  
69 =head2 reduce
70  
71 =for ref
72  
73 reduce dimension of piddle by one by applying an operation
74 along the specified dimension
75  
76 =for example
77  
78 $a = sequence 5,5;
79 # reduce by adding all
80 # elements along 2nd dimension
81 $b = $a->reduce('add',1);
82 $b = $a->reduce('plus',1);
83 $b = $a->reduce('+',1); # three ways to do the same thing
84  
85 [ As an aside: if you are familiar with threading you will see that
86 this is actually the same as
87  
88 $b = $a->mv(1,0)->sumover
89  
90 ]
91  
92 NOTE: You should quote the name of the operation (1st arg) that
93 you want C<reduce> to perform. This is important since some of the
94 names are identical to the names of the actual PDL functions
95 which might be imported into your namespace. And you definitely
96 want a string as argument, not a function invocation! For example,
97 this will probably fail:
98  
99 $b = $a->reduce(avg,1); # gives an error from invocation of 'avg'
100  
101 Rather use
102  
103 $b = $a->reduce('avg',1);
104  
105 C<reduce> provides a simple and unified interface to the
106 I<projection> functions and makes people coming from other
107 data/array languages hopefully feel more at home.
108  
109 =for usage
110  
111 $result = $pdl->reduce($operation [,@dims]);
112  
113 C<reduce> applies the named operation along the specified
114 dimension(s) reducing the input piddle dimension by as many
115 dimensions as supplied as arguments. If the
116 dimension(s) argument is omitted the operation is applied along the first
117 dimension. To get a list of valid operations see L<canreduce>.
118  
119 NOTE - new power user feature: you can now supply a code
120 reference as operation to reduce with.
121  
122 =for example
123  
124 # reduce by summing over dims 0 and 2
125 $result = $pdl->reduce(\&sumover, 0, 2);
126  
127 It is your responsibility to ensure that this is indeed a
128 PDL projection operation that turns vectors into scalars!
129 You have been warned.
130  
131 =cut
132  
133 1 *reduce = \&PDL::reduce;
134 sub PDL::reduce ($$;$) {
135 5 ▶ my ($pdl, $op, @dims) = @_;
Subroutine reduce: called CC=17 SCAR=29.5
Pod undocumented
136 barf "trying to reduce using unknown operation"
137 5 ▶ unless exists $reduce{$op} || ref $op eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $reduce{$op} or ref $op eq "CODE" 0 5 5
Condition66%2/3
exists $reduce{$op} or ref $op eq "CODE"
l!l&&r!l&&!r
410
MC/DC0%0/2
exists $reduce{$op} or ref $op eq "CODE"
exists $reduce{$op} ref $op eq "CODE"
Truth table66%2/3
A or B
A= exists $reduce{$op}B= ref $op eq "CODE"
ABresult
1X1
011
000
138 5 my $dim;
139 5 ▶ if (@dims > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dims > 1) { } 1 4 5
140 1 my $n = $pdl->getndims;
141 1 ▶ @dims = map { $_ < 0 ? $_ + $n : $_ } @dims;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 ? : 0 2 2
142 1 my $min = $n;
143 1 my $max = 0;
144 1 ▶ for (@dims) { $min = $_ if $_ < $min; $max = $_ if $_ > $max }
Branch: 2 branches
BranchTrueFalseTotal
if $_ < $min 1 1 2
if $_ > $max 1 1 2
145 1 ▶ barf "dimension out of bounds (one of @dims >= $n)"
Branch: 1 branch
BranchTrueFalseTotal
if $min >= $n or $max >= $n 0 1 1
Condition33%1/3
$min >= $n or $max >= $n
l!l&&r!l&&!r
001
MC/DC0%0/2
$min >= $n or $max >= $n
$min >= $n $max >= $n
Truth table33%1/3
A or B
A= $min >= $nB= $max >= $n
ABresult
1X1
011
000
146 if $min >= $n || $max >= $n;
147 1 $dim = $min; # this will be the resulting dim of the clumped piddle
148 1 $pdl = $pdl->clump(@dims);
149 } else {
150 4 ▶ $dim = @dims > 0 ? $dims[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
@dims > 0 ? : 3 1 4
151 }
152 5 ▶ if (defined $dim && $dim != 0) { # move the target dim to the front
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dim and $dim != 0) 2 3 5
Condition66%2/3
defined $dim and $dim != 0
!ll&&!rl&&r
032
MC/DC50%1/2
defined $dim and $dim != 0
defined $dim $dim != 0
Truth table66%2/3
A and B
A= defined $dimB= $dim != 0
ABresult
0X0
100
111
153 2 my $n = $pdl->getndims;
154 2 ▶ $dim += $n if $dim < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dim < 0 0 2 2
155 2 ▶ barf "dimension out of bounds" if $dim <0 || $dim >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $dim < 0 or $dim >= $n 0 2 2
Condition33%1/3
$dim < 0 or $dim >= $n
l!l&&r!l&&!r
002
MC/DC0%0/2
$dim < 0 or $dim >= $n
$dim < 0 $dim >= $n
Truth table33%1/3
A or B
A= $dim < 0B= $dim >= $n
ABresult
1X1
011
000
156 2 $pdl = $pdl->mv($dim,0);
157 }
158 5 ▶ my $method = ref $op eq 'CODE' ? $op : $reduce{$op};
Branch: 1 branch
BranchTrueFalseTotal
ref $op eq 'CODE' ? : 1 4 5
159 5 return $pdl->$method();
160 }
161  
162 =head2 canreduce
163  
164 =for ref
165  
166 return list of valid named C<reduce> operations
167 Some common operations can be accessed using a
168 number of names, e.g. C<'+'>, C<add> and C<plus>
169 all sum the elements along the chosen dimension.
170  
171 =for example
172  
173 @ops = PDL->canreduce;
174  
175 This list is useful if you want to make sure which
176 operations can be used with C<reduce>.
177  
178 =cut
179  
180 1 *canreduce = \&PDL::canreduce;
181 sub PDL::canreduce {
182 0 ▶ my ($this) = @_;
Subroutine canreduce: not called CC=1 SCAR=6.9
Pod undocumented
183 0 return keys %reduce;
184 }
185  
186 =head1 AUTHOR
187  
188 Copyright (C) 2000 Christian Soeller (c.soeller@auckland.ac.nz). All
189 rights reserved. There is no warranty. You are allowed to redistribute
190 this software / documentation under certain conditions. For details,
191 see the file COPYING in the PDL distribution. If this file is
192 separated from the PDL distribution, the copyright notice should be
193 included in the file.
194  
195 =cut
196  
197 1 1;
« blib/lib/PDL/Opt/Simplex.pm ↑ Summary blib/lib/PDL/IO/Storable.pm »