blib/lib/PDL/FuncND.pm

filter: Statementstmt 98.6% 145 / 147 Branchbran 78.0% 64 / 82 Conditioncond 64.5% 31 / 48 MC/DCmcdc 44.4% 12 / 27 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 84.3% 280 / 332 CC 58 SCAR 32.9
CC
58
Cov
87%
_handle_options
48.9
mahalanobis
8.1
_genericND
6.3
CRAP 26.9
↑ Summary
1 package PDL::FuncND;
2  
3 # ABSTRACT: N dimensional version of functions
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use PDL::Constants qw[ PI ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use PDL::Math qw[ lgamma ];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use PDL::MatrixOps qw[ determinant identity ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use PDL::Transform qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use PDL::Options qw[ iparse ];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Scalar::Util qw[ looks_like_number ];
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use base 'PDL::Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 our @EXPORT_OK = qw[
22 cauchyND
23 gaussND
24 lorentzND
25 mahalanobis
26 moffatND
27 ];
28  
29 9 our %EXPORT_TAGS = ( Func => [@EXPORT_OK], );
30  
31 #<<< no tidy
32 9 our $VERSION = '0.13';
33 #>>>
34  
35 # keep option handling uniform. barf if there's a problem
36 53 ▶sub _handle_options { ## no critic (Subroutines::ProhibitExcessComplexity)
Subroutine _handle_options: called CC=41 SCAR=38.9
37  
38 # remove known arguments from @_ so can use new_from_specification
39 53 my $self = shift;
40 53 my $opt = shift;
41  
42 53 my ( $vectors, $output, $ndims, $center, $scale );
43  
44 53 ▶ if ( $opt->{vectors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'vectors'}) { } 18 35 53
45 croak( "first argument must be a piddle if vectors option is set\n" )
46 18 ▶ unless eval { $self->isa( 'PDL' ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("PDL") } } 0 18 18
47  
48 18 $output = shift;
49 18 $vectors = $self;
50  
51 18 ▶ croak( 'wrong dimensions for input vectors; expected <= 2, got ', $vectors->ndims )
Branch: 1 branch
BranchTrueFalseTotal
if $vectors->ndims > 2 0 18 18
52 if $vectors->ndims > 2;
53  
54 # transform 0D piddle into 1D
55 18 ▶ $vectors = $vectors->dummy( 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $vectors->ndims == 0 4 14 18
56 if $vectors->ndims == 0;
57  
58 18 ( $ndims ) = $vectors->dims;
59  
60 $vectors = $opt->{transform}->apply( $vectors )
61 18 ▶ if defined $opt->{transform};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"transform"} 1 17 18
62  
63 18 ▶ if ( defined $opt->{center} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"center"}) 5 13 18
64 croak( "cannot use center = 'auto' if vectors is set\n" )
65 5 ▶ if !ref $opt->{center} && $opt->{center} eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $opt->{"center"} and $opt->{"center"} eq "auto" 1 4 5
Condition66%2/3
not ref $opt->{"center"} and $opt->{"center"} eq "auto"
!ll&&!rl&&r
401
MC/DC50%1/2
not ref $opt->{"center"} and $opt->{"center"} eq "auto"
not ref $opt->{"center"} $opt->{"center"} eq "auto"
Truth table66%2/3
not(A) and B
A= not ref $opt->{"center"}B= $opt->{"center"} eq "auto"
ABresult
0X0
100
111
66  
67 croak( "cannot use center = [ $opt->{center}[0], ... ] if vectors is set\n" )
68 if 'ARRAY' eq ref $opt->{center}
69 && !ref $opt->{center}[0]
70 4 ▶ && !looks_like_number( $opt->{center}[0] );
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0] and not &looks_like_number($opt->{"center"}[0]) 2 2 4
Condition83%5/6
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0] and not &looks_like_number($opt->{"center"}[0])
!ll&&!rl&&r
112
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0]
!ll&&!rl&&r
103
MC/DC66%2/3
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0] and not &looks_like_number($opt->{"center"}[0])
"ARRAY" eq ref $opt->{"center"} not ref $opt->{"center"}[0] not &looks_like_number($opt->{"center"}[0])
Truth table75%3/4
A and not(B) and not(C)
A= "ARRAY" eq ref $opt->{"center"}B= not ref $opt->{"center"}[0]C= not &looks_like_number($opt->{"center"}[0])
ABCresult
0XX0
10X0
1100
1111
71  
72 2 $center = PDL::Core::topdl( $opt->{center} );
73 }
74 }
75  
76 else {
77 35 ▶ $output
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 29 35
78 = @_
79 ? $self->new_from_specification( @_ )
80 : $self->new_or_inplace;
81  
82 35 $ndims = $output->ndims;
83  
84 35 $vectors = $output->ndcoords->reshape( $ndims, $output->nelem );
85  
86 $vectors->inplace->apply( $opt->{transform} )
87 35 ▶ if defined $opt->{transform};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"transform"} 11 24 35
88  
89 35 ▶ if ( defined $opt->{center} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'center'}) { } 34 1 35
90 34 ▶ if ( !ref $opt->{center} && $opt->{center} eq 'auto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $opt->{'center'} and $opt->{'center'} eq 'auto') { } 3 31 34
elsif ('ARRAY' eq ref $opt->{'center'} and not ref $opt->{'center'}[0] and $opt->{'center'}[0] eq 'offset') { } 2 29 31
Condition100%3/3
not ref $opt->{"center"} and $opt->{"center"} eq "auto"
!ll&&!rl&&r
2563
MC/DC100%2/2
not ref $opt->{"center"} and $opt->{"center"} eq "auto"
not ref $opt->{"center"} $opt->{"center"} eq "auto"
Truth table100%3/3
not(A) and B
A= not ref $opt->{"center"}B= $opt->{"center"} eq "auto"
ABresult
0X0
100
111
91 3 $center = ( pdl( [ $output->dims ] ) - 1 ) / 2;
92 $center->inplace->apply( $opt->{transform} )
93 3 ▶ if defined $opt->{transform};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"transform"} 1 2 3
94 }
95 ▶ elsif ('ARRAY' eq ref $opt->{center}
Condition83%5/6
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0] and $opt->{"center"}[0] eq "offset"
!ll&&!rl&&r
7222
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0]
!ll&&!rl&&r
7024
MC/DC66%2/3
"ARRAY" eq ref $opt->{"center"} and not ref $opt->{"center"}[0] and $opt->{"center"}[0] eq "offset"
"ARRAY" eq ref $opt->{"center"} not ref $opt->{"center"}[0] $opt->{"center"}[0] eq "offset"
Truth table75%3/4
A and not(B) and C
A= "ARRAY" eq ref $opt->{"center"}B= not ref $opt->{"center"}[0]C= $opt->{"center"}[0] eq "offset"
ABCresult
0XX0
10X0
1100
1111
96 && !ref $opt->{center}[0]
97 && $opt->{center}[0] eq 'offset' )
98 {
99  
100 2 $center = ( pdl( [ $output->dims ] ) - 1 ) / 2;
101  
102 # inplace bug ( PDL == 2.4.11, at least) causes SEGV
103 # if this is done in place. so. don't.
104 $center = $center->apply( $opt->{transform} )
105 2 ▶ if defined $opt->{transform};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"transform"} 1 1 2
106  
107 # allow:
108 # offset => piddle
109 # offset => [ ... ]
110 # offset => ( list )
111  
112 # NOTE!!!! bug in topdl ( PDL == 2.4.11, at least )
113 # topdl only uses the first argument
114 2 my @offset = @{ $opt->{center} };
115 2 shift @offset;
116  
117 2 ▶ $center += PDL::Core::topdl( @offset > 1 ? \@offset : @offset );
Branch: 1 branch
BranchTrueFalseTotal
@offset > 1 ? : 0 2 2
118 }
119  
120 else {
121  
122 29 $center = PDL::Core::topdl( $opt->{center} );
123  
124 }
125 }
126 else {
127  
128 1 $center = zeroes( $vectors->type, $ndims );
129  
130 }
131 }
132  
133 # for 1D output $center may be a 0D PDL; this causes grief;
134 # promote it to a 1D
135 50 ▶ $center = $center->dummy( 0 ) if defined $center && $center->ndims == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $center and $center->ndims == 0 6 44 50
Condition100%3/3
defined $center and $center->ndims == 0
!ll&&!rl&&r
13316
MC/DC100%2/2
defined $center and $center->ndims == 0
defined $center $center->ndims == 0
Truth table100%3/3
A and B
A= defined $centerB= $center->ndims == 0
ABresult
0X0
100
111
136  
137 50 ▶ croak( "center vector has wrong dimensions\n" )
Branch: 1 branch
BranchTrueFalseTotal
if defined $center and $center->ndims != 1 || ($center->dims)[0] != $ndims 0 50 50
Condition50%3/6
defined $center and $center->ndims != 1 || ($center->dims)[0] != $ndims
!ll&&!rl&&r
13370
$center->ndims != 1 or ($center->dims)[0] != $ndims
l!l&&r!l&&!r
0037
MC/DC0%0/3
defined $center and $center->ndims != 1 || ($center->dims)[0] != $ndims
defined $center $center->ndims != 1 ($center->dims)[0] != $ndims
Truth table50%2/4
A and B or C
A= defined $centerB= $center->ndims != 1C= ($center->dims)[0] != $ndims
ABCresult
0XX0
1000
11X1
1011
138 if defined $center
139 && ( $center->ndims != 1
140 || ( $center->dims )[0] != $ndims );
141  
142 ## no critic( ControlStructures::ProhibitCascadingIfElse )
143  
144 # handle scale
145 # scalar -> symmetric, independent
146 50 ▶ if ( !defined $opt->{scale} || !ref $opt->{scale} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opt->{'scale'} && ref $opt->{'scale'}) { } 34 16 50
elsif ('ARRAY' eq ref $opt->{'scale'} and @{$opt->{'scale'};} == $ndims) { } 10 6 16
elsif (eval { do { $opt->{'scale'}->isa('PDL') } } and $opt->{'scale'}->ndims == 1 and ($opt->{'scale'}->dims)[0] == $ndims) { } 0 6 6
elsif (eval { do { $opt->{'scale'}->isa('PDL') } } and $opt->{'scale'}->ndims == 2 and all(pdl($opt->{'scale'}->dims) == pdl($ndims, $ndims))) { } 6 0 6
Condition100%3/3
defined $opt->{'scale'} && ref $opt->{'scale'}
!ll&&!rl&&r
92516
MC/DC100%2/2
defined $opt->{'scale'} && ref $opt->{'scale'}
defined $opt->{'scale'} ref $opt->{'scale'}
Truth table100%3/3
A && B
A= defined $opt->{'scale'}B= ref $opt->{'scale'}
ABresult
0X0
100
111
147  
148 34 ▶ $scale = identity( $ndims ) * ( defined $opt->{scale} ? $opt->{scale}**2 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'scale'} ? : 25 9 34
149 }
150  
151 # 1D piddle of length N
152 ▶ elsif ( 'ARRAY' eq ref $opt->{scale}
Condition66%2/3
"ARRAY" eq ref $opt->{"scale"} and @{$opt->{"scale"};} == $ndims
!ll&&!rl&&r
6010
MC/DC50%1/2
"ARRAY" eq ref $opt->{"scale"} and @{$opt->{"scale"};} == $ndims
"ARRAY" eq ref $opt->{"scale"} @{$opt->{"scale"};} == $ndims
Truth table66%2/3
A and B
A= "ARRAY" eq ref $opt->{"scale"}B= @{$opt->{"scale"};} == $ndims
ABresult
0X0
100
111
153 10 && @{ $opt->{scale} } == $ndims )
154 {
155 10 $scale = identity( $ndims ) * pdl( $opt->{scale} )**2;
156 }
157  
158 # 1D piddle of length N
159 6 ▶ elsif (eval { $opt->{scale}->isa( 'PDL' ) }
Condition33%2/6
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 1 and ($opt->{"scale"}->dims)[0] == $ndims
!ll&&!rl&&r
600
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 1
!ll&&!rl&&r
060
MC/DC0%0/3
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 1 and ($opt->{"scale"}->dims)[0] == $ndims
eval { do { $opt->{"scale"}->isa("PDL") } } $opt->{"scale"}->ndims == 1 ($opt->{"scale"}->dims)[0] == $ndims
Truth table25%1/4
A and B and C
A= eval { do { $opt->{"scale"}->isa("PDL") } }B= $opt->{"scale"}->ndims == 1C= ($opt->{"scale"}->dims)[0] == $ndims
ABCresult
0XX0
10X0
1100
1111
160 && $opt->{scale}->ndims == 1
161 && ( $opt->{scale}->dims )[0] == $ndims )
162 {
163 0 $scale = identity( $ndims ) * $opt->{scale}**2;
164 }
165  
166 # full matrix N^N piddle
167 6 ▶ elsif (eval { $opt->{scale}->isa( 'PDL' ) }
Condition33%2/6
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 2 and all(pdl($opt->{"scale"}->dims) == pdl($ndims, $ndims))
!ll&&!rl&&r
060
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 2
!ll&&!rl&&r
006
MC/DC0%0/3
eval { do { $opt->{"scale"}->isa("PDL") } } and $opt->{"scale"}->ndims == 2 and all(pdl($opt->{"scale"}->dims) == pdl($ndims, $ndims))
eval { do { $opt->{"scale"}->isa("PDL") } } $opt->{"scale"}->ndims == 2 all(pdl($opt->{"scale"}->dims) == pdl($ndims, $ndims))
Truth table0%0/4
A and B and C
A= eval { do { $opt->{"scale"}->isa("PDL") } }B= $opt->{"scale"}->ndims == 2C= all(pdl($opt->{"scale"}->dims) == pdl($ndims, $ndims))
ABCresult
0XX0
10X0
1100
1111
168 && $opt->{scale}->ndims == 2
169 && all( pdl( $opt->{scale}->dims ) == pdl( $ndims, $ndims ) ) )
170 {
171 6 $scale = $opt->{scale};
172 }
173  
174 else {
175 0 croak(
176 "scale argument is not a scalar, an array of length $ndims,",
177 " or a piddle of shape ($ndims) or shape ($ndims,$ndims)\n"
178 );
179 }
180  
181 # apply a rotation to the scale matrix
182 50 ▶ if ( defined $opt->{theta} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"theta"}) 1 49 50
183 1 ▶ croak( "theta may only be used for 2D PDFs\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $ndims != 2 0 1 1
184 if $ndims != 2;
185  
186 my $R = pdl( [ cos( $opt->{theta} ), -sin( $opt->{theta} ) ],
187 1 [ sin( $opt->{theta} ), cos( $opt->{theta} ) ] );
188 1 $scale = $R->transpose x $scale x $R;
189 }
190  
191 return (
192 50 vectors => $vectors,
193 output => $output,
194 ndims => $ndims,
195 center => $center,
196 ndims => $ndims,
197 scale => $scale
198 );
199  
200 }
201  
202  
203 ############################################################################
204  
205 38 ▶sub _gamma { exp( ( lgamma( @_ ) )[0] ) }
Subroutine _gamma: called CC=1 SCAR=0.0
206  
207 ############################################################################
208  
209 41 ▶sub _genericND {
Subroutine _genericND: called CC=6 SCAR=18.4
210  
211 41 my ( $xopts, $sub ) = ( shift, shift );
212  
213 # handle being called as a method or a function
214 41 ▶ my $self = eval { ref $_[0] && $_[0]->isa( 'PDL' ) } ? shift @_ : 'PDL';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { ref $_[0] and $_[0]->isa('PDL') } } ? : 41 0 41
Condition33%1/3
ref $_[0] && $_[0]->isa('PDL')
!ll&&!rl&&r
0041
MC/DC0%0/2
ref $_[0] && $_[0]->isa('PDL')
ref $_[0] $_[0]->isa('PDL')
Truth table33%1/3
A && B
A= ref $_[0]B= $_[0]->isa('PDL')
ABresult
0X0
100
111
215  
216 # handle options.
217 41 ▶ my $opt = 'HASH' eq ref $_[-1] ? pop( @_ ) : {};
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 41 0 41
218 41 my %opt = iparse( {
219 center => undef,
220 scale => 1,
221 transform => undef,
222 vectors => 0,
223 theta => undef,
224 %$xopts,
225 },
226 $opt
227 );
228  
229 41 my %par = _handle_options( $self, \%opt, @_ );
230  
231 my $d2 = mahalanobis(
232 $par{vectors},
233 $par{scale},
234 {
235 squared => 1,
236 center => $par{center},
237 41 } );
238  
239 41 my $retval = $sub->( $d2, \%opt, \%par );
240  
241 41 my $output = $par{output};
242  
243 41 ▶ if ( $opt{vectors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'vectors'}) { } 12 29 41
244 12 $output = $retval;
245 }
246  
247 else {
248 29 $output .= $retval->reshape( $output->dims );
249 }
250  
251 return wantarray
252 ? (
253 vals => $output,
254 center => $par{center},
255 scale => $par{scale},
256 transform => $par{transform} )
257 41 ▶ : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 41 41
258  
259 }
260  
261 ############################################################################
262  
263 # from http://en.wikipedia.org/wiki/Cauchy_distribution#Multivariate_Cauchy_distribution
264  
265 # 1 + k
266 # Gamma(-----)
267 # 2
268 # ------------------------------------------------------------
269 # 1 + k
270 # -----
271 # 1 k/2 1/2 T -1 2
272 # Gamma(-) pi |S| (1 + (x - mu) S (x - mu))
273 # 2
274 #
275  
276  
277 10 ▶sub _cauchyND {
Subroutine _cauchyND: called CC=2 SCAR=7.0
278  
279 10 my ( $d2, $opt, $par ) = @_;
280  
281 10 my $k = $par->{ndims};
282  
283 my $pdf
284 = _gamma( ( 1 + $k ) / 2 )
285 10 / ( _gamma( 1 / 2 ) * PI**( $k / 2 ) * sqrt( determinant( $par->{scale} ) ) * ( 1 + $d2 )
286 **( ( 1 + $k ) / 2 ) );
287  
288 10 ▶ return $opt->{log} ? log( $pdf ) : $pdf;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'log'} ? : 0 10 10
289 }
290  
291 10 ▶sub cauchyND {
Subroutine cauchyND: called CC=1 SCAR=0.0
Pod documented
292  
293 10 unshift @_, { log => 0 }, \&_cauchyND;
294 10 goto \&_genericND;
295 }
296  
297 9 *PDL::cauchyND = \&cauchyND;
298  
299 ############################################################################
300  
301 12 ▶sub _gaussND {
Subroutine _gaussND: called CC=3 SCAR=11.2
302  
303 12 my ( $d2, $opt, $par ) = @_;
304  
305 12 my $tmp = $d2;
306  
307 12 ▶ if ( $opt->{norm} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"norm"} == 1) 12 0 12
308  
309 12 $tmp += $par->{ndims} * log( 2 * PI ) + log( determinant( $par->{scale} ) );
310  
311 }
312  
313 12 my $log_pdf = -$tmp / 2;
314  
315 12 ▶ return $opt->{log} ? $log_pdf : exp( $log_pdf );
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'log'} ? : 0 12 12
316  
317 }
318  
319 12 ▶sub gaussND {
Subroutine gaussND: called CC=1 SCAR=0.0
Pod documented
320  
321 12 unshift @_,
322 {
323 log => 0,
324 norm => 1,
325 },
326 \&_gaussND;
327 12 goto \&_genericND;
328 }
329  
330  
331 9 *PDL::gaussND = \&gaussND;
332  
333 ############################################################################
334  
335  
336 10 ▶sub _lorentzND {
Subroutine _lorentzND: called CC=1 SCAR=0.0
337  
338 10 my ( $d2, $opt, undef ) = @_;
339  
340 10 return 1 / ( 1 + $d2 );
341  
342 }
343  
344 10 ▶sub lorentzND {
Subroutine lorentzND: called CC=1 SCAR=0.0
Pod documented
345  
346 10 unshift @_, {}, \&_lorentzND;
347 10 goto \&_genericND;
348 }
349  
350  
351 9 *PDL::lorentzND = \&lorentzND;
352  
353 ############################################################################
354  
355  
356 9 ▶sub _moffatND {
Subroutine _moffatND: called CC=3 SCAR=11.1
357  
358 9 my ( $d2, $opt, $par ) = @_;
359  
360 croak( "missing beta parameter\n" )
361 9 ▶ unless defined $opt->{beta};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"beta"} 0 9 9
362  
363 9 my $n = $par->{ndims};
364  
365 9 my $tmp = ( 1 + $d2 )**-$opt->{beta};
366  
367 9 ▶ if ( $opt->{norm} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"norm"} == 1) 9 0 9
368  
369 # scale is a matrix with elements ~alpha**2
370 # det(scale) ~ (alpha**2)**$n
371 # sqrt(det(scale)) ~ alpha**$n
372  
373 9 my $alpha_n = sqrt( determinant( $par->{scale} ) );
374  
375 9 $tmp *= _gamma( $opt->{beta} ) / _gamma( $opt->{beta} - $n / 2 ) / ( PI**( $n / 2 ) * $alpha_n );
376  
377 }
378  
379 9 return $tmp;
380 }
381  
382 9 ▶sub moffatND {
Subroutine moffatND: called CC=1 SCAR=0.0
Pod documented
383  
384 9 unshift @_,
385 {
386 alpha => undef,
387 beta => undef,
388 norm => 1,
389 },
390 \&_moffatND;
391 9 goto \&_genericND;
392  
393 }
394  
395 9 *PDL::moffatND = \&moffatND;
396  
397 ############################################################################
398  
399  
400  
401 50 ▶sub mahalanobis {
Subroutine mahalanobis: called CC=8 SCAR=20.9
Pod documented
402  
403 # handle options.
404 50 ▶ my $opt = 'HASH' eq ref $_[-1] ? pop( @_ ) : {};
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 50 0 50
405 50 my %opt = PDL::Options::iparse( {
406 center => undef,
407 inverted => 0,
408 squared => 0,
409 },
410 $opt
411 );
412  
413 50 my ( $x, $scale, $out ) = @_;
414  
415 50 my $xc;
416 50 ▶ if ( defined $opt{center} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'center'}) { } 33 17 50
417 33 my $c = PDL::Core::topdl( $opt{center} );
418 33 $xc = $x - $c;
419 }
420 else {
421 17 $xc = $x;
422 }
423  
424 # may be passed a single vector; be nice
425 50 ▶ my $m = $x->ndims > 1 ? ( $x->dims )[-1] : 1;
Branch: 1 branch
BranchTrueFalseTotal
$x->ndims > 1 ? : 35 15 50
426  
427 50 ▶ $out = zeroes( double, $m )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 50 0 50
428 unless defined $out;
429  
430 # if this is 1D, $scale may come in with shapes [], [1], or [1][1]
431 # we want the latter
432 50 ▶ $scale = $scale->dummy( 1 ) if $scale->dims < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $scale->dims < 2 1 49 50
433  
434 # invert the matrix if it hasn't already been inverted
435 $scale = $scale->inv
436 50 ▶ unless $opt{inverted};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"inverted"} 49 1 50
437  
438 50 inner2( $xc, $scale, $xc, $out );
439  
440 50 ▶ $out->inplace->sqrt unless $opt{squared};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"squared"} 0 50 50
441  
442 50 return $out;
443 }
444  
445 9 *PDL::mahalanobis = \&mahalanobis;
446  
447 #
448 # This file is part of PDL-FuncND
449 #
450 # This software is Copyright (c) 2025 by Smithsonian Astrophysical Observatory.
451 #
452 # This is free software, licensed under:
453 #
454 # The GNU General Public License, Version 3, June 2007
455 #
456  
457 9 1;
458  
459 __END__
↑ Summary