blib/lib/PDL/Basic.pm

filter: Statementstmt 89.0% 186 / 209 Branchbran 70.4% 62 / 88 Conditioncond 60.8% 28 / 46 MC/DCmcdc 29.6% 8 / 27 Subroutinesub 90.4% 38 / 42 Podpod 0.0% 0 / 27 totaltotal 73.3% 322 / 439 CC 119 SCAR 17.0
CC
119
Cov
80%
_hist_bin_calc
12.0
_hist_bin_calc
12.0
rvals
9.0
CRAP 5.5
« blib/lib/Inline/Pdlpp.pm ↑ Summary blib/lib/PDL/IO/Misc.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 PDL::Basic -- Basic utility functions for PDL
6  
7 =head1 DESCRIPTION
8  
9 This module contains basic utility functions for
10 creating and manipulating ndarrays. Most of these functions
11 are simplified interfaces to the more flexible functions in
12 the modules
13 L<PDL::Primitive>
14 and
15 L<PDL::Slices>.
16  
17 =head1 SYNOPSIS
18  
19 use PDL::Basic;
20  
21 =head1 FUNCTIONS
22  
23 =cut
24  
25 package PDL::Basic;
26 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 71 ▶use PDL::Core '';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 71 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 71 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 71 ▶use PDL::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 71 our @ISA=qw/PDL::Exporter/;
34 71 our @EXPORT_OK = qw(
35 sec ins hist whist similar_assign transpose
36 allaxisvals ndcoords sequence rvals
37 axisvals xvals yvals zvals
38 allaxislinvals axislinvals xlinvals ylinvals zlinvals
39 allaxislogvals axislogvals xlogvals ylogvals zlogvals
40 );
41 71 our %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
42  
43 # Exportable functions
44 71 for (@EXPORT_OK) {
45 71 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1704 *$_ = \&{ $PDL::{$_} };
47 }
48  
49 =head2 axisvals, xvals, yvals, zvals
50  
51 =for ref
52  
53 Fills an ndarray with index values on X, Y, Z, or Nth dimension
54  
55 Uses similar specifications to L<zeroes|PDL::Core/zeroes> and
56 L<new_from_specification|PDL::Core/new_from_specification>.
57 B<CAVEAT>: If you use the single argument ndarray form (top row
58 in the usage table) the output will have the same type as the input,
59 except that between 2.064 and 2.100, the returned ndarray will
60 default to at least type C<double>; as of 2.101 this upgrade is
61 relaxed to C<float>.
62 As of 2.085, this will respect a given type as in the second
63 or third form below.
64  
65 =for usage
66  
67 $x = xvals($somearray); # at least type float
68 $x = xvals([OPTIONAL TYPE],$nx,$ny,$nz...);
69 $x = xvals([OPTIONAL TYPE], $somarray->dims);
70 $y = yvals($somearray); yvals(inplace($somearray));
71 $y = yvals([OPTIONAL TYPE],$nx,$ny,$nz...);
72 $z = zvals($somearray); zvals(inplace($somearray));
73 $z = zvals([OPTIONAL TYPE],$nx,$ny,$nz...);
74 $axisv = axisvals ($ndarray, $nth);
75 $axisv = $ndarray->axisvals($nth);
76 $axisv = axisvals ([type,] $nth, $dim0, $dim1, ...); # new in PDL 2.101
77 $axisv = axisvals ($nth, [type,] $dim0, $dim1, ...); # new in PDL 2.101
78  
79 See also L</allaxisvals>, which generates all axis values
80 simultaneously in a form useful for L</range>, L</interpND>,
81 L</indexND>, etc.
82  
83 =for example
84  
85 pdl> print xvals zeroes(5,2)
86 [
87 [0 1 2 3 4]
88 [0 1 2 3 4]
89 ]
90 pdl> print yvals zeroes(5,2)
91 [
92 [0 0 0 0 0]
93 [1 1 1 1 1]
94 ]
95 pdl> print zvals zeroes(3,2,2)
96 [
97 [
98 [0 0 0]
99 [0 0 0]
100 ]
101 [
102 [1 1 1]
103 [1 1 1]
104 ]
105 ]
106  
107 =cut
108  
109 262 ▶sub PDL::axisvals {
Subroutine axisvals: called CC=2 SCAR=9.2
Pod undocumented
110 0 ▶ my $type_given = grep +(ref($_[$_])||'') eq 'PDL::Type', 0..2;
Subroutine axisvals: not called CC=2 SCAR=9.2
Pod undocumented
Condition50%2/4
ref $_[$_] || ''
l!l
180606
ref $_[$_] || ''
l!l
00
MC/DC100%1/1
ref $_[$_] || ''
ref $_[$_]
MC/DC0%0/1
ref $_[$_] || ''
ref $_[$_]
Truth table100%2/2
A
A= ref $_[$_]
Aresult
11
00
Truth table0%0/2
A
A= ref $_[$_]
Aresult
11
00
111 0 my ($first_non_ref) = grep !ref $_[$_], 0..$#_;
112 0 my ($nth) = splice @_, $first_non_ref, 1;
113 0 axisvals2(&PDL::Core::_construct,$nth,$type_given);
114 }
115  
116 # We need this version for xvals etc to work in place
117 793 ▶sub axisvals2 {
Subroutine axisvals2: called CC=5 SCAR=20.9
Pod undocumented
118 0 ▶ my($dummy,$nth,$keep_type) = @_;
Subroutine axisvals2: not called CC=5 SCAR=20.9
Pod undocumented
119 0 ▶ $dummy = PDL::Core::float($dummy)
Branch: 2 branches
BranchTrueFalseTotal
if not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum 3 790 793
if not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum 0 0 0
Condition50%3/6
not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum
!ll&&!rl&&r
3274633
not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum
!ll&&!rl&&r
000
MC/DC100%2/2
not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum
not $keep_type $dummy->get_datatype < PDL::Core::float()->enum
MC/DC0%0/2
not $keep_type and $dummy->get_datatype < PDL::Core::float()->enum
not $keep_type $dummy->get_datatype < PDL::Core::float()->enum
Truth table100%3/3
not(A) and B
A= not $keep_typeB= $dummy->get_datatype < PDL::Core::float()->enum
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $keep_typeB= $dummy->get_datatype < PDL::Core::float()->enum
ABresult
0X0
100
111
120 if !$keep_type && $dummy->get_datatype < PDL::Core::float()->enum;
121 0 ▶ return $dummy .= 0 if $dummy->getndims <= $nth; # 'kind of' consistency
Branch: 2 branches
BranchTrueFalseTotal
if $dummy->getndims <= $nth 4 789 793
if $dummy->getndims <= $nth 0 0 0
122 0 ▶ (0==$nth ? $dummy : $dummy->xchg(0,$nth))->inplace->axisvalues;
Branch: 2 branches
BranchTrueFalseTotal
0 == $nth ? : 598 191 789
0 == $nth ? : 0 0 0
123 0 $dummy;
124 }
125  
126 # Conveniently named interfaces to axisvals()
127 147 ▶sub PDL::xvals { unshift @_, 0; goto &axisvals; }
Subroutine xvals: called CC=1 SCAR=0.0
Pod undocumented
128 37 ▶sub PDL::yvals { unshift @_, 1; goto &axisvals; }
Subroutine yvals: called CC=1 SCAR=0.0
Pod undocumented
129 3 ▶sub PDL::zvals { unshift @_, 2; goto &axisvals; }
Subroutine zvals: called CC=1 SCAR=0.0
Pod undocumented
130  
131 =head2 axislinvals, xlinvals, ylinvals, zlinvals
132  
133 =for ref
134  
135 Axis values linearly spaced between endpoints (see L</xvals>).
136  
137 =for usage
138  
139 $w = zeroes(100,100);
140 $x = $w->xlinvals(0.5,1.5); # can give ndarrays as start and endpoints
141 $y = $w->ylinvals(-2,-1);
142 $z = f($x,$y); # calculate Z for X from 0.5 to 1.5, Y from -2 to -1
143 # alternatively (new in PDL 2.101):
144 $x = xlinvals(0.5,1.5,100); # can give ndarrays as start and endpoints
145 $y = xlinvals(-2,-1,100); # x = along 0-th dim
146 $z = f(meshgrid($x,$y)); # should go faster as meshgrid makes better locality
147 $pdl = xlinvals(float,0.5,1.5,100); # can specify type
148 $x = $w->axislinvals(0,0.5,1.5); # same as xlinvals
149 $x = axislinvals(0,0.5,1.5,100,100); # same as xlinvals
150 $x = axislinvals(float,0,0.5,1.5,100,100); # same as xlinvals
151 $x = axislinvals(0,float,0.5,1.5,100,100); # same as xlinvals
152  
153 C<xlinvals>, C<ylinvals> and C<zlinvals> return an ndarray with the same shape
154 as their first argument if an ndarray, and linearly scaled values between the two other
155 arguments along the given axis.
156 Works with dim-length of one as of 2.093, giving the starting point.
157 As of 2.101, instead of giving an ndarray you can give an optional
158 type at the start, and dimensions after the two mandatory arguments.
159  
160 =cut
161  
162 60 ▶sub _dimcheck {
Subroutine _dimcheck: called CC=3 SCAR=11.0
163 60 my ($pdl, $whichdim, $name) = @_;
164 60 ▶ barf "Given non-PDL '$pdl'" if !UNIVERSAL::isa($pdl, 'PDL');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($pdl, "PDL") 0 60 60
165 60 my $dimlength = $pdl->getdim($whichdim);
166 60 ▶ barf "Must have at least one element in dimension for $name" if $dimlength < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $dimlength < 1 2 58 60
167 58 $dimlength;
168 }
169 76 ▶sub _extract_endpoints {
Subroutine _extract_endpoints: called CC=6 SCAR=19.9
170 76 my ($v1, $v2);
171 76 my @pdl_inds = grep UNIVERSAL::isa($_[$_], 'PDL'), 2..$#_; # not the invocant
172 76 ▶ if (@pdl_inds < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pdl_inds < 2) { } 53 23 76
173 53 my @nonref_inds = grep !ref $_[$_], 0..$#_;
174 53 $v2 = splice @_, $nonref_inds[2], 1;
175 53 $v1 = splice @_, $nonref_inds[1], 1;
176 } else {
177 23 $v2 = splice @_, $pdl_inds[1], 1;
178 23 $v1 = splice @_, $pdl_inds[0], 1;
179 0 ▶ PDL::barf "an endpoint has >1 dim: (@{[$v1->dims]}), (@{[$v2->dims]})"
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_->ndims > 1), $v1, $v2) 0 23 23
180 if grep $_->ndims > 1, $v1, $v2;
181 0 ▶ PDL::barf "endpoints have different dims: (@{[$v1->dims]}), (@{[$v2->dims]})"
Branch: 1 branch
BranchTrueFalseTotal
if $v1->ndims != $v2->ndims or $v1->ndims and $v1->dim(0) != $v2->dim(0) 0 23 23
Condition50%3/6
$v1->ndims != $v2->ndims or $v1->ndims and $v1->dim(0) != $v2->dim(0)
l!l&&r!l&&!r
0023
$v1->ndims and $v1->dim(0) != $v2->dim(0)
!ll&&!rl&&r
1670
MC/DC0%0/3
$v1->ndims != $v2->ndims or $v1->ndims and $v1->dim(0) != $v2->dim(0)
$v1->ndims != $v2->ndims $v1->ndims $v1->dim(0) != $v2->dim(0)
Truth table50%2/4
A or B and C
A= $v1->ndims != $v2->ndimsB= $v1->ndimsC= $v1->dim(0) != $v2->dim(0)
ABCresult
1XX1
0111
00X0
0100
182 if $v1->ndims != $v2->ndims or
183 ($v1->ndims and $v1->dim(0) != $v2->dim(0));
184 }
185 76 ($v1, $v2);
186 }
187 41 ▶sub _linvals {
Subroutine _linvals: called CC=2 SCAR=6.9
188 41 my ($name) = splice @_, 0, 1;
189 41 my ($first_non_ref) = grep !ref $_[$_], 0..$#_;
190 41 my $whichdim = $_[$first_non_ref];
191 41 my ($v1, $v2) = &_extract_endpoints;
192 41 my $pdl = &axisvals;
193 41 my $dimlength = _dimcheck($pdl, $whichdim, $name);
194 40 ▶ $pdl *= (($v2 - $v1) / ($dimlength > 1 ? ($dimlength-1) : 1));
Branch: 1 branch
BranchTrueFalseTotal
$dimlength > 1 ? : 39 1 40
195 40 $pdl += $v1;
196 }
197 30 ▶sub PDL::axislinvals { unshift @_, 'axislinvals'; goto &_linvals; }
Subroutine axislinvals: called CC=1 SCAR=0.0
Pod undocumented
198 6 ▶sub PDL::xlinvals { unshift @_, 'xlinvals', 0; goto &_linvals; }
Subroutine xlinvals: called CC=1 SCAR=0.0
Pod undocumented
199 4 ▶sub PDL::ylinvals { unshift @_, 'ylinvals', 1; goto &_linvals; }
Subroutine ylinvals: called CC=1 SCAR=0.0
Pod undocumented
200 1 ▶sub PDL::zlinvals { unshift @_, 'zlinvals', 2; goto &_linvals; }
Subroutine zlinvals: called CC=1 SCAR=0.0
Pod undocumented
201  
202 =head2 axislogvals, xlogvals, ylogvals, zlogvals
203  
204 =for ref
205  
206 Axis values logarithmically spaced between endpoints (see L</xvals>).
207  
208 =for usage
209  
210 $w = zeroes(100,100);
211 $x = $w->xlogvals(1e-6,1e-3);
212 $y = $w->ylogvals(1e-4,1e3);
213 $z = f($x,$y); # calculate Z for X from 1e-6 to 1e-3, Y from 1e-4 to 1e3
214 # alternatively (new in PDL 2.101):
215 $x = xlogvals(1e-6,1e-3,100);
216 $y = xlogvals(1e-4,1e3,100); # x = along 0-th dim
217 $z = f(meshgrid($x,$y)); # should go faster as meshgrid makes better locality
218 $pdl = xlogvals(float,1e-6,1e-3,100); # can specify type
219 $x = $w->axislogvals(0,0.5,1.5); # same as xlogvals
220 $x = axislogvals(0,0.5,1.5,100,100); # same as xlogvals
221 $x = axislogvals(float,0,0.5,1.5,100,100); # same as xlogvals
222 $x = axislogvals(0,float,0.5,1.5,100,100); # same as xlogvals
223  
224 C<xlogvals>, C<ylogvals> and C<zlogvals> return an ndarray with the same shape
225 as their first argument and logarithmically scaled values between the two other
226 arguments along the given axis.
227 Works with dim-length of one as of 2.093, giving the starting point.
228 As of 2.101, instead of giving an ndarray you can give an optional
229 type at the start, and dimensions after the two mandatory arguments.
230  
231 =cut
232  
233 19 ▶sub _logvals {
Subroutine _logvals: called CC=4 SCAR=14.1
234 19 my ($name) = splice @_, 0, 1;
235 19 my ($first_non_ref) = grep !ref $_[$_], 0..$#_;
236 19 my $whichdim = $_[$first_non_ref];
237 19 my ($min, $max) = &_extract_endpoints;
238 19 ▶ barf "min and max must be positive" if $min <= 0 || $max <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min <= 0 or $max <= 0 0 19 19
Condition33%1/3
$min <= 0 or $max <= 0
l!l&&r!l&&!r
0019
MC/DC0%0/2
$min <= 0 or $max <= 0
$min <= 0 $max <= 0
Truth table33%1/3
A or B
A= $min <= 0B= $max <= 0
ABresult
1X1
011
000
239 19 my ($lmin,$lmax) = map log($_), $min, $max;
240 19 my $pdl = &axisvals;
241 19 my $dimlength = _dimcheck($pdl, $whichdim, $name);
242 18 ▶ $pdl .= exp($pdl * (($lmax - $lmin) / ($dimlength > 1 ? ($dimlength-1) : 1)) + $lmin);
Branch: 1 branch
BranchTrueFalseTotal
$dimlength > 1 ? : 17 1 18
243 }
244 10 ▶sub PDL::axislogvals { unshift @_, 'axislogvals'; goto &_logvals; }
Subroutine axislogvals: called CC=1 SCAR=0.0
Pod undocumented
245 6 ▶sub PDL::xlogvals { unshift @_, 'xlogvals', 0; goto &_logvals; }
Subroutine xlogvals: called CC=1 SCAR=0.0
Pod undocumented
246 2 ▶sub PDL::ylogvals { unshift @_, 'ylogvals', 1; goto &_logvals; }
Subroutine ylogvals: called CC=1 SCAR=0.0
Pod undocumented
247 1 ▶sub PDL::zlogvals { unshift @_, 'zlogvals', 2; goto &_logvals; }
Subroutine zlogvals: called CC=1 SCAR=0.0
Pod undocumented
248  
249 =head2 ndcoords, allaxisvals, allaxislinvals, allaxislogvals
250  
251 =for ref
252  
253 Enumerate pixel coordinates for an N-D ndarray
254  
255 C<ndcoords> and C<allaxisvals> return an enumerated list of coordinates
256 suitable for use in
257 L<indexND|PDL::Slices/indexND>, L<range|PDL::Slices/range>, or
258 L<interpND|PDL::Primitive/interpND>: you feed
259 in a dimension list and get out an ndarray whose 0th dimension runs over
260 dimension index and whose 1st through Nth dimensions are the
261 dimensions given in the input. If you feed in an ndarray instead of a
262 perl list, then its dimension list is used, as in L</xvals> etc.
263  
264 Unlike L</xvals> etc., if you supply an ndarray input, you get
265 out an ndarray of the default ndarray type: double. This causes less
266 surprises than the previous default of keeping the data type of
267 the input ndarray since that rarely made sense in most usages.
268  
269 C<allaxislinvals> and C<allaxislogvals> enumerate a list of linear-
270 or logarithm-spaced values respectively, like their non-C<all> counterparts.
271 The start- and endpoints can be 1-dimensional ndarrays, with one
272 value each per desired dimension.
273  
274 =for usage
275  
276 $indices = ndcoords($pdl); # double
277 $indices = ndcoords(@dimlist); # double
278 $indices = ndcoords($type,$pdl); # $type
279 $indices = $pdl->ndcoords($type); # $type
280 $indices = ndcoords($type,@dimlist); # $type
281 $indices = allaxisvals($pdl);
282 $indices = allaxisvals(@dimlist);
283 $indices = allaxisvals($type,$pdl);
284 $indices = allaxisvals($type,@dimlist);
285 $linvals = allaxislinvals($pdl,$start,$end); # start and end can be ndarrays
286 $linvals = allaxislinvals($type,$pdl,$start,$end); # also here
287 $linvals = allaxislinvals($type,$start,$end,@dimlist); # also here
288 $linvals = allaxislinvals($start,$end,@dimlist); # not here
289 $linvals = allaxislogvals($pdl,$start,$end);
290 $linvals = allaxislogvals($type,$pdl,$start,$end);
291 $linvals = allaxislogvals($start,$end,@dimlist);
292 $linvals = allaxislogvals($type,$start,$end,@dimlist);
293  
294 =for example
295  
296 pdl> print ndcoords(2,3)
297 [
298 [
299 [0 0]
300 [1 0]
301 ]
302 [
303 [0 1]
304 [1 1]
305 ]
306 [
307 [0 2]
308 [1 2]
309 ]
310 ]
311 pdl> $w = zeroes(byte,2,3); # $w is a 2x3 byte ndarray
312 pdl> $y = ndcoords($w); # $y is double to avoid problems
313 pdl> $c = ndcoords(long,$w->dims); # $c is a long ndarray, same dims as $y
314 pdl> $d = ndcoords(long,$w); # $d overrides the default double
315 pdl> help $y;
316 This variable is Double D [2,2,3] P 0.09KB
317 pdl> help $c;
318 This variable is Long D [2,2,3] P 0.05KB
319  
320 =cut
321  
322 107 ▶sub _allvals_construct {
Subroutine _allvals_construct: called CC=6 SCAR=18.0
323 107 my $type;
324 107 ▶ if (my ($type_ind) = grep ref $_[$_] eq 'PDL::Type', 0..$#_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($type_ind) = grep((ref $_[$_] eq "PDL::Type"), 0 .. $#_)) 13 94 107
325 13 $type = splice @_, $type_ind, 1;
326 }
327 107 ▶ PDL::barf "allaxis*vals passed reference that is not ndarray"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and not UNIVERSAL::can($_[0], "dims") 0 107 107
Condition66%2/3
ref $_[0] and not UNIVERSAL::can($_[0], "dims")
!ll&&!rl&&r
95120
MC/DC0%0/2
ref $_[0] and not UNIVERSAL::can($_[0], "dims")
ref $_[0] not UNIVERSAL::can($_[0], "dims")
Truth table66%2/3
A and not(B)
A= ref $_[0]B= not UNIVERSAL::can($_[0], "dims")
ABresult
0X0
100
111
328 if ref($_[0]) and !UNIVERSAL::can($_[0], 'dims');
329 107 ▶ my @dims = ref($_[0]) ? shift->dims : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12 95 107
330 107 ▶ PDL->zeroes(defined($type) ? $type : (), scalar(@dims), @dims);
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 13 94 107
331 }
332 91 ▶sub PDL::ndcoords {
Subroutine ndcoords: called CC=2 SCAR=6.9
Pod undocumented
333 91 my $out = &_allvals_construct;
334 91 axisvals2($out->slice("($_)"), $_, 1) for 0..$out->ndims-2;
335 91 $out;
336 }
337 71 *PDL::allaxisvals = \&PDL::ndcoords;
338 0 ▶sub _nonref_vals2 {
Subroutine _nonref_vals2: not called CC=1 SCAR=6.9
339 0 my ($first_non_ref) = grep !ref $_[$_], 0..$#_;
340 0 splice @_, $first_non_ref, 2;
341 }
342 16 ▶sub _allaxis_endpointvals {
Subroutine _allaxis_endpointvals: called CC=7 SCAR=19.6
343 16 my $method = shift;
344 16 unshift @_, 1; # dummy for _extract_endpoints "whichdim"
345 16 my ($v1, $v2) = &_extract_endpoints;
346 16 shift @_; # drop dummy
347 16 my $out = &_allvals_construct;
348 16 my ($ndims_needed, $need_slice) = $out->dim(0);
349 16 ▶ if (UNIVERSAL::isa($v1, 'PDL') and $v1->ndims) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($v1, "PDL") and $v1->ndims) 5 11 16
Condition100%3/3
UNIVERSAL::isa($v1, "PDL") and $v1->ndims
!ll&&!rl&&r
925
MC/DC100%2/2
UNIVERSAL::isa($v1, "PDL") and $v1->ndims
UNIVERSAL::isa($v1, "PDL") $v1->ndims
Truth table100%3/3
A and B
A= UNIVERSAL::isa($v1, "PDL")B= $v1->ndims
ABresult
0X0
100
111
350 0 ▶ PDL::barf "endpoints have dim other than $ndims_needed: (@{[$v1->dims]}), (@{[$v2->dims]})"
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_->dim(0) != $ndims_needed), $v1, $v2) 0 5 5
351 if grep $_->dim(0) != $ndims_needed, $v1, $v2;
352 5 $need_slice = 1;
353 }
354 16 ▶ if ($need_slice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_slice) { } 5 11 16
355 5 $out->slice("($_)")->inplace->$method($_,$v1->slice("($_)"),$v2->slice("($_)")) for 0..$ndims_needed-1;
356 } else {
357 11 $out->slice("($_)")->inplace->$method($_,$v1,$v2) for 0..$ndims_needed-1;
358 }
359 16 $out;
360 }
361 13 ▶sub PDL::allaxislinvals {
Subroutine allaxislinvals: called CC=1 SCAR=0.0
Pod undocumented
362 13 unshift @_, 'axislinvals';
363 13 goto &_allaxis_endpointvals;
364 }
365 3 ▶sub PDL::allaxislogvals {
Subroutine allaxislogvals: called CC=1 SCAR=0.0
Pod undocumented
366 3 unshift @_, 'axislogvals';
367 3 goto &_allaxis_endpointvals;
368 }
369  
370 =head2 hist, whist
371  
372 =for ref
373  
374 Create histogram, or weighted histogram, of an ndarray
375  
376 =for usage
377  
378 $hist = hist($data);
379 ($xvals,$hist) = hist($data);
380 # or:
381 $hist = hist($data,$min,$max,$step);
382 ($xvals,$hist) = hist($data,[$min,$max,$step]);
383 # weighted:
384 $whist = whist($data, $wt, [$min,$max,$step]);
385 ($xvals,$whist) = whist($data, $wt, [$min,$max,$step]);
386  
387 If requested, C<$xvals> gives the computed bin centres
388 as type double values. C<$data> and C<$wt> should have
389 the same dimensionality and extents.
390  
391 A nice idiom (with L<PDL::Graphics::Simple>) is
392  
393 bins hist($data), {yrange=>[0,$data->dim(0)]}; # Plot histogram
394 bin whist $data, $wt; # weighted histogram
395 bins whist($data, $wt), {yrange=>[0,$data->dim(0)]}; # weighted histogram
396  
397 =for example
398  
399 pdl> p $y
400 [13 10 13 10 9 13 9 12 11 10 10 13 7 6 8 10 11 7 12 9 11 11 12 6 12 7]
401 pdl> $h = hist $y,0,20,1; # hist with step 1, min 0 and 20 bins
402 pdl> p $h
403 [0 0 0 0 0 0 2 3 1 3 5 4 4 4 0 0 0 0 0 0]
404 # or, weighted:
405 pdl> $wt = grandom($y->nelem)
406 pdl> $h = whist $y, $wt, 0, 20, 1 # hist with step 1, min 0 and 20 bins
407 pdl> p $h
408 [0 0 0 0 0 0 -0.49552342 1.7987439 0.39450696 4.0073722 -2.6255299 -2.5084501 2.6458365 4.1671676 0 0 0 0 0 0]
409  
410 =cut
411  
412 4 ▶sub PDL::hist {
Subroutine hist: called CC=3 SCAR=11.0
Pod undocumented
413 4 ▶ barf(<<'EOF') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
414 Usage: $hist = hist($data)
415 $hist = hist($data,$min,$max,$step)
416 ($xvals,$hist) = hist($data)
417 ($xvals,$hist) = hist($data,$min,$max,$step)
418 EOF
419 4 my ($pdl,$min,$max,$step) = @_;
420 4 ($step, $min, my $bins, my $xvals) =
421 _hist_bin_calc($pdl, $min, $max, $step, wantarray);
422 4 my $hist = PDL::Primitive::histogram($pdl->flat, $step,$min,$bins);
423 3 ▶ wantarray ? ($xvals,$hist) : $hist;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
424 }
425  
426 1 ▶sub PDL::whist {
Subroutine whist: called CC=3 SCAR=11.3
Pod undocumented
427 1 ▶ barf('Usage: ([$xvals],$hist) = whist($data,$wt,[$min,$max,$step])')
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 1 1
428 if @_ < 2;
429 1 my ($pdl,$wt,$min,$max,$step) = @_;
430 1 ($step, $min, my $bins, my $xvals) =
431 _hist_bin_calc($pdl, $min, $max, $step, wantarray);
432 1 my $hist = PDL::Primitive::whistogram($pdl->flat,$wt->flat, $step, $min, $bins);
433 1 ▶ wantarray ? ($xvals,$hist) : $hist;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
434 }
435  
436 5 ▶sub _hist_bin_calc {
Subroutine _hist_bin_calc: called CC=9 SCAR=24.8
437 5 my ($pdl,$min,$max,$step,$wantarray) = @_;
438 5 my $nelem = $pdl->nelem;
439 5 ▶ barf "empty ndarray, no values to work with" if $nelem == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nelem == 0 0 5 5
440 5 ▶ $min //= $pdl->min;
Condition33%1/3
$min //= $pdl->min
l!l&&r!l&&!r
500
MC/DC0%0/2
$min //= $pdl->min
$min $pdl->min
Truth table33%1/3
A //= B
A= $minB= $pdl->min
ABresult
1X1
011
000
441 5 ▶ $max //= $pdl->max;
Condition33%1/3
$max //= $pdl->max
l!l&&r!l&&!r
500
MC/DC0%0/2
$max //= $pdl->max
$max $pdl->max
Truth table33%1/3
A //= B
A= $maxB= $pdl->max
ABresult
1X1
011
000
442 5 ▶ $step //= ($max-$min)/(($nelem>10_000) ? 100 : sqrt($nelem));
Branch: 1 branch
BranchTrueFalseTotal
$nelem > 10000 ? : 0 0 0
Condition50%1/2
$step //= ($max - $min) / ($nelem > 10000 ? 100 : sqrt $nelem)
l!l
50
MC/DC0%0/1
$step //= ($max - $min) / ($nelem > 10000 ? 100 : sqrt $nelem)
$step
Truth table50%1/2
A
A= $step
Aresult
11
00
443 5 ▶ barf "step is zero (or all data equal to one value)" if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 5 5
444 5 my $bins = int(($max-$min)/$step+0.5);
445 5 ▶ print "hist with step $step, min $min and $bins bins\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 5 5
446 if $PDL::debug;
447 5 ▶ return ( $step, $min, $bins ) if !$wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless $wantarray 3 2 5
448 # Need to use double for $xvals here
449 2 my $xvals = $min + $step/2 + sequence(PDL::Core::double,$bins)*$step;
450 2 ( $step, $min, $bins, $xvals );
451 }
452  
453 =head2 sequence
454  
455 =for ref
456  
457 Create array filled with a sequence of values
458  
459 =for usage
460  
461 $w = sequence($y); $w = sequence [OPTIONAL TYPE], @dims;
462  
463 etc. see L<zeroes|PDL::Core/zeroes>.
464  
465 =for example
466  
467 pdl> p sequence(10)
468 [0 1 2 3 4 5 6 7 8 9]
469 pdl> p sequence(3,4)
470 [
471 [ 0 1 2]
472 [ 3 4 5]
473 [ 6 7 8]
474 [ 9 10 11]
475 ]
476  
477 =cut
478  
479 301 ▶sub PDL::sequence {
Subroutine sequence: called CC=4 SCAR=13.9
Pod undocumented
480 301 ▶ my $type_given = grep +(ref($_[$_])||'') eq 'PDL::Type', 0..1;
Condition100%2/2
ref $_[$_] || ''
l!l
73529
MC/DC100%1/1
ref $_[$_] || ''
ref $_[$_]
Truth table100%2/2
A
A= ref $_[$_]
Aresult
11
00
481 301 ▶ $type_given ||= ref($_[0]) && UNIVERSAL::isa($_[0], 'PDL'); # instance method
Condition83%5/6
$type_given ||= ref $_[0] && UNIVERSAL::isa($_[0], "PDL")
l!l&&r!l&&!r
721228
ref $_[0] && UNIVERSAL::isa($_[0], 'PDL')
!ll&&!rl&&r
22801
MC/DC0%0/3
$type_given ||= ref $_[0] && UNIVERSAL::isa($_[0], "PDL")
$type_given ref $_[0] UNIVERSAL::isa($_[0], 'PDL')
Truth table0%0/4
A ||= B && C
A= $type_givenB= ref $_[0]C= UNIVERSAL::isa($_[0], 'PDL')
ABCresult
1XX1
0111
00X0
0100
482 301 my $pdl = &PDL::Core::_construct;
483 301 axisvals2($pdl->flat->inplace,0,$type_given);
484 301 return $pdl;
485 }
486  
487 =head2 rvals
488  
489 =for ref
490  
491 Fills an ndarray with radial distance values from some centre.
492  
493 =for usage
494  
495 $r = rvals $ndarray,{OPTIONS};
496 $r = rvals [OPTIONAL TYPE],$nx,$ny,...{OPTIONS};
497  
498 =for options
499  
500 Options:
501  
502 Centre => [$x,$y,$z...] # Specify centre
503 Center => [$x,$y.$z...] # synonym.
504 Center => $c # as 1d array
505 Squared => 1 # return distance squared (i.e., don't take the square root)
506  
507 =for example
508  
509 pdl> print rvals long,7,7,{Centre=>[2,2]}
510 [
511 [2 2 2 2 2 3 4]
512 [2 1 1 1 2 3 4]
513 [2 1 0 1 2 3 4]
514 [2 1 1 1 2 3 4]
515 [2 2 2 2 2 3 4]
516 [3 3 3 3 3 4 5]
517 [4 4 4 4 4 5 5]
518 ]
519  
520 If C<Center> is not specified, the midpoint for a given dimension of
521 size C<N> is given by C< int(N/2) > so that the midpoint always falls
522 on an exact pixel point in the data. For dimensions of even size,
523 that means the midpoint is shifted by 1/2 pixel from the true center
524 of that dimension.
525  
526 If C<Center> has less components than the number of dimensions of the
527 array, its remaining components are computed as above. If it has more,
528 a warning is issued.
529  
530 Also note that the calculation for C<rvals> for integer values
531 does not promote the datatype so you will have wraparound when
532 the value calculated for C< r**2 > is greater than the datatype
533 can hold. If you need exact values, be sure to use large integer
534 or floating point datatypes.
535  
536 For a more general metric, one can define, e.g.,
537  
538 sub distance {
539 my ($w,$centre,$f) = @_;
540 my ($r) = $w->allaxisvals-$centre;
541 $f->($r);
542 }
543 sub l1 { sumover(abs($_[0])); }
544 sub euclid { use PDL::Math 'pow'; pow(sumover(pow($_[0],2)),0.5); }
545 sub linfty { maximum(abs($_[0])); }
546  
547 so now
548  
549 distance($w, $centre, \&euclid);
550  
551 will emulate rvals, while C<\&l1> and C<\&linfty> will generate other
552 well-known norms.
553  
554 =cut
555  
556 19 ▶sub PDL::rvals { # Return radial distance from given point and offset
Subroutine rvals: called CC=9 SCAR=22.0
Pod undocumented
557 19 ▶ my $opt = ref($_[-1]) eq "HASH" ? pop @_ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 9 10 19
558 19 ▶ my %opt = defined $opt ?
Branch: 1 branch
BranchTrueFalseTotal
defined $opt ? : 9 10 19
559 iparse( {
560 CENTRE => undef, # needed, otherwise centre/center handling painful
561 Squared => 0,
562 }, $opt ) : ();
563 19 my $r = &PDL::Core::_construct;
564 19 my @pos;
565 19 ▶ if (defined $opt{CENTRE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"CENTRE"}) 8 11 19
566 8 my $pos = PDL->topdl($opt{CENTRE});
567 8 ▶ barf "Center should be a 1D vector" unless $pos->getndims==1;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos->getndims == 1 0 8 8
568 8 ▶ barf "Center has more coordinates than dimensions of ndarray" if $pos->dim(0) > $r->getndims;
Branch: 1 branch
BranchTrueFalseTotal
if $pos->dim(0) > $r->getndims 0 8 8
569 8 @pos = $pos->list;
570 }
571 19 my $offset;
572 19 $r .= 0.0;
573 19 my $tmp = $r->copy;
574 19 my $i;
575 19 ▶ for ($i=0; $i<$r->getndims; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $r->getndims) 38 19 57
576 38 ▶ $offset = $pos[$i] // int($r->getdim($i)/2);
Condition100%2/2
$pos[$i] // int $r->getdim($i) / 2
l!l
1622
MC/DC100%1/1
$pos[$i] // int $r->getdim($i) / 2
$pos[$i]
Truth table100%2/2
A
A= $pos[$i]
Aresult
11
00
577 # Note careful coding for speed and min memory footprint
578 38 axisvals2($tmp, $i, 1);
579 38 $tmp -= $offset; $tmp *= $tmp;
580 38 $r += $tmp;
581 }
582 19 ▶ return $opt{Squared} ? $r : $r->inplace->sqrt;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'Squared'} ? : 2 17 19
583 }
584  
585 =head2 sec
586  
587 =for ref
588  
589 Take a subsection of an ndarray with given coordinates.
590  
591 =for usage
592  
593 $new = sec($input, $x1, $x2, $y1, $y2, $z1, $z2, ... ) # Take subsection
594  
595 =cut
596  
597 1 ▶sub PDL::sec {
Subroutine sec: called CC=2 SCAR=6.9
Pod undocumented
598 1 my ($this,@coords) = @_;
599 1 @coords = map int, @coords;
600 1 my @maps;
601 1 ▶ push @maps, shift(@coords).":".shift(@coords) while @coords;
Branch: 1 branch
BranchTrueFalseTotal
@coords and do { push @maps, shift(@coords) . ':' . shift(@coords) } 2 1 3
602 1 $this->slice(join ',',@maps)->sever;
603 }
604  
605 =head2 ins
606  
607 =for ref
608  
609 Insert one ndarray into another at given coordinates.
610  
611 =for usage
612  
613 $newimage = ins($bigimage,$smallimage,$x,$y,$z...) # Insert at x,y,z
614  
615 =cut
616  
617 2 ▶sub PDL::ins {
Subroutine ins: called CC=2 SCAR=7.0
Pod undocumented
618 2 my ($this,$what,@coords) = @_;
619 2 my $w = PDL->topdl($what);
620 2 $this = $this->new_or_inplace;
621 2 my @thisdims = $this->dims;
622 2 my @wdims_m1 = map $_-1, $w->dims;
623 2 @coords = map int, @coords;
624 2 ▶ $this->slice(
Branch: 1 branch
BranchTrueFalseTotal
$coords[$_] + $wdims_m1[$_] < $thisdims[$_] ? : 4 0 4
625 join ',',map $coords[$_].":".
626 (($coords[$_]+$wdims_m1[$_])<$thisdims[$_] ?
627 ($coords[$_]+$wdims_m1[$_]):$thisdims[$_]),
628 0..$#coords)
629 .= $w;
630 2 $this;
631 }
632  
633 0 ▶sub PDL::similar_assign {
Subroutine similar_assign: not called CC=2 SCAR=17.9
Pod undocumented
634 0 my ($from,$to) = @_;
635 0 ▶ if ((my $fd = join ',',@{$from->dims}) ne (my $td = join ',',@{$to->dims})) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $fd = join(",", @{$from->dims;})) ne (my $td = join(",", @{$to->dims;}))) 0 0 0
636 0 barf "Similar_assign: dimensions [$fd] and [$td] do not match!\n";
637 }
638 0 $to .= $from;
639 }
640  
641 =head2 transpose
642  
643 =for ref
644  
645 transpose rows and columns.
646  
647 =for usage
648  
649 $y = transpose($w);
650  
651 =for example
652  
653 pdl> $w = sequence(3,2)
654 pdl> p $w
655 [
656 [0 1 2]
657 [3 4 5]
658 ]
659 pdl> p transpose( $w )
660 [
661 [0 3]
662 [1 4]
663 [2 5]
664 ]
665  
666 =cut
667  
668 265 ▶sub PDL::transpose {
Subroutine transpose: called CC=4 SCAR=13.9
Pod undocumented
669 265 my ($this) = @_;
670 265 ▶ barf "\$pdl->inplace->transpose is meaningless" if $this->is_inplace;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_inplace 0 265 265
671 265 my $ndims = $this->dims;
672 265 ▶ $ndims > 1 ? $this->xchg(0,1) :
Branch: 2 branches
BranchTrueFalseTotal
$ndims > 1 ? : 251 14 265
$ndims > 0 ? : 13 1 14
673 $ndims > 0 ? $this->dummy(0) :
674 $this->dummy(0)->dummy(0);
675 }
676  
677 =head2 t
678  
679 =for usage
680  
681 $pdl = $pdl->t(SCALAR(conj))
682 conj : Conjugate Transpose = 1 | Transpose = 0, default = 0;
683  
684 =for ref
685  
686 Convenient function for transposing real or complex 2D array(s).
687 For complex data, if conj is true returns conjugate transposed array(s).
688 Supports broadcasting. Not exported.
689  
690 Originally by Grégory Vanuxem.
691  
692 =cut
693  
694 130 ▶sub PDL::t {
Subroutine t: called CC=3 SCAR=11.0
Pod undocumented
695 130 my ($m, $conj) = @_;
696 130 my $r = $m->transpose;
697 130 ▶ ($conj && !$r->type->real) ? $r->conj : $r;
Branch: 1 branch
BranchTrueFalseTotal
$conj && !$r->type->real ? : 1 129 130
Condition66%2/3
$conj and not $r->type->real
!ll&&!rl&&r
12901
MC/DC50%1/2
$conj and not $r->type->real
$conj not $r->type->real
Truth table66%2/3
A and not(B)
A= $conjB= not $r->type->real
ABresult
0X0
100
111
698 }
699  
700 71 1;
« blib/lib/Inline/Pdlpp.pm ↑ Summary blib/lib/PDL/IO/Misc.pm »