| 1 |
|
| use strict; |
| 2 |
|
| use warnings; |
| 3 |
|
| use PDL::Types qw(types ppdefs_complex ppdefs_all); |
| 4 |
|
| |
| 5 |
|
| my $T = [map $_->ppsym, grep $_->integer, types]; |
| 6 |
|
| my $A = [ppdefs_all]; |
| 7 |
|
| my $AF = [map $_->ppsym, grep !$_->integer, types]; |
| 8 |
|
| |
| 9 |
|
| pp_addpm({At=>'Top'},<<'EOD'); |
| 10 |
|
| use strict; |
| 11 |
|
| use warnings; |
| 12 |
|
| |
| 13 |
|
| =encoding utf8 |
| 14 |
|
| |
| 15 |
|
| =head1 NAME |
| 16 |
|
| |
| 17 |
|
| PDL::Ufunc - primitive ufunc operations for pdl |
| 18 |
|
| |
| 19 |
|
| =head1 DESCRIPTION |
| 20 |
|
| |
| 21 |
|
| This module provides some primitive and useful functions defined |
| 22 |
|
| using PDL::PP based on functionality of what are sometimes called |
| 23 |
|
| I<ufuncs> (for example NumPY and Mathematica talk about these). |
| 24 |
|
| It collects all the functions generally used to C<reduce> or |
| 25 |
|
| C<accumulate> along a dimension. These all do their job across the |
| 26 |
|
| first dimension but by using the slicing functions you can do it |
| 27 |
|
| on any dimension. |
| 28 |
|
| |
| 29 |
|
| The L<PDL::Reduce> module provides an alternative interface |
| 30 |
|
| to many of the functions in this module. |
| 31 |
|
| |
| 32 |
|
| =head1 SYNOPSIS |
| 33 |
|
| |
| 34 |
|
| use PDL::Ufunc; |
| 35 |
|
| |
| 36 |
|
| =cut |
| 37 |
|
| |
| 38 |
|
| use PDL::Slices; |
| 39 |
|
| use Carp; |
| 40 |
|
| EOD |
| 41 |
|
| |
| 42 |
|
| # helper functions |
| 43 |
|
| sub projectdocs { |
| 44 |
|
| my $name = shift; |
| 45 |
|
| my $op = shift; |
| 46 |
|
| my $extras = shift; |
| 47 |
|
| <<EOD; |
| 48 |
|
| |
| 49 |
|
| =for ref |
| 50 |
|
| |
| 51 |
|
| Project via $name to N-1 dimensions |
| 52 |
|
| |
| 53 |
|
| This function reduces the dimensionality of an ndarray |
| 54 |
|
| by one by taking the $name along the 1st dimension. |
| 55 |
|
| |
| 56 |
|
| By using L<xchg|PDL::Slices/xchg> etc. it is possible to use |
| 57 |
|
| I<any> dimension. |
| 58 |
|
| |
| 59 |
|
| $extras |
| 60 |
|
| EOD |
| 61 |
|
| |
| 62 |
|
| } # sub: projectdocs() |
| 63 |
|
| |
| 64 |
|
| sub cumuprojectdocs { |
| 65 |
|
| my $name = shift; |
| 66 |
|
| my $op = shift; |
| 67 |
|
| my $extras = shift; |
| 68 |
|
| <<EOD; |
| 69 |
|
| |
| 70 |
|
| =for ref |
| 71 |
|
| |
| 72 |
|
| Cumulative $name |
| 73 |
|
| |
| 74 |
|
| This function calculates the cumulative $name |
| 75 |
|
| along the 1st dimension. |
| 76 |
|
| |
| 77 |
|
| By using L<xchg|PDL::Slices/xchg> etc. it is possible to use |
| 78 |
|
| I<any> dimension. |
| 79 |
|
| |
| 80 |
|
| The sum is started so that the first element in the cumulative $name |
| 81 |
|
| is the first element of the parameter. |
| 82 |
|
| |
| 83 |
|
| $extras |
| 84 |
|
| EOD |
| 85 |
|
| |
| 86 |
|
| } # sub: cumuprojectdocs() |
| 87 |
|
| |
| 88 |
|
| my %over = |
| 89 |
|
| ( |
| 90 |
|
| sumover => { name => 'sum', op => '+=', init => 0, }, |
| 91 |
|
| prodover => { name => 'product', op => '*=', init => 1, leftzero => 'tmp == 0' }, |
| 92 |
|
| ); |
| 93 |
|
| |
| 94 |
|
| foreach my $func ( sort keys %over ) { |
| 95 |
|
| my ($name, $op, $init, $leftzero) = @{$over{$func}}{qw(name op init leftzero)}; |
| 96 |
|
| pp_def( |
| 97 |
|
| $_->[0].$func, |
| 98 |
|
| HandleBad => 1, |
| 99 |
|
| Pars => "a(n); $_->[1] [o]b();", |
| 100 |
|
| $_->[2] ? (GenericTypes=>$_->[2]) : (), |
| 101 |
|
| Code => <<EOF, |
| 102 |
|
| \$GENERIC(b) tmp = $init; |
| 103 |
|
| PDL_IF_BAD(int flag = 0;,) |
| 104 |
|
| loop(n) %{ |
| 105 |
|
| PDL_IF_BAD(if ( \$ISBAD(a()) ) continue; flag = 1;,) |
| 106 |
|
| tmp $op \$a(); |
| 107 |
|
| @{[$leftzero ? "if ($leftzero) break;" : '']} |
| 108 |
|
| %} |
| 109 |
|
| PDL_IF_BAD(if ( !flag ) \$SETBAD(b()); else,) |
| 110 |
|
| \$b() = tmp; |
| 111 |
|
| EOF |
| 112 |
|
| Doc => projectdocs( $name, $_->[0].$func, $_->[3] ), |
| 113 |
|
| ) for ( |
| 114 |
|
| ['', 'int+', $A, ''], |
| 115 |
|
| ['d', 'double', undef, |
| 116 |
|
| "Unlike L</$func>, the calculations are performed in double precision." |
| 117 |
|
| ], |
| 118 |
|
| ); |
| 119 |
|
| |
| 120 |
|
| my $cfunc = "cumu${func}"; |
| 121 |
|
| pp_def( |
| 122 |
|
| $_->[0].$cfunc, |
| 123 |
|
| HandleBad => 1, |
| 124 |
|
| Pars => "a(n); $_->[1] [o]b(n);", |
| 125 |
|
| $_->[2] ? (GenericTypes=>$_->[2]) : (), |
| 126 |
|
| Code => <<EOF, |
| 127 |
|
| \$GENERIC(b) tmp = $init; |
| 128 |
|
| loop(n) %{ |
| 129 |
|
| PDL_IF_BAD(if ( \$ISBAD(a()) ) { \$SETBAD(b()); continue; },) |
| 130 |
|
| tmp $op \$a(); |
| 131 |
|
| \$b() = tmp; |
| 132 |
|
| %} |
| 133 |
|
| EOF |
| 134 |
|
| Doc => cumuprojectdocs( $name, $_->[0].$cfunc, $_->[3] ), |
| 135 |
|
| ) for ( |
| 136 |
|
| ['', 'int+', $A, ''], |
| 137 |
|
| ['d', 'double', undef, |
| 138 |
|
| "Unlike L</$cfunc>, the calculations are performed in double precision." |
| 139 |
|
| ], |
| 140 |
|
| ); |
| 141 |
|
| } # foreach: my $func |
| 142 |
|
| |
| 143 |
|
| %over = ( |
| 144 |
|
| firstnonzeroover => { txt => 'first non-zero value', alltypes => 1, |
| 145 |
|
| op => 'tmp = $a();', leftzero => 'tmp' }, |
| 146 |
|
| zcover => { def=>'char tmp', txt => '== 0', init => 1, alltypes => 1, |
| 147 |
|
| op => 'tmp &= ($a() == 0);', leftzero => '!tmp' }, |
| 148 |
|
| andover => { def=>'char tmp', txt => 'logical and', init => 1, alltypes => 1, |
| 149 |
|
| op => 'tmp &= ($a() != 0);', leftzero => '!tmp' }, |
| 150 |
|
| orover => { def=>'char tmp', txt => 'logical or', alltypes => 1, |
| 151 |
|
| op => 'tmp |= ($a() != 0);', leftzero => 'tmp' }, |
| 152 |
|
| xorover => { def=>'char tmp', txt => 'logical xor', alltypes => 1, |
| 153 |
|
| op => 'tmp ^= ($a() != 0);', leftzero => 0 }, |
| 154 |
|
| bandover => { txt => 'bitwise and', init => '~0', |
| 155 |
|
| op => 'tmp &= $a();', leftzero => '!tmp' }, |
| 156 |
|
| borover => { txt => 'bitwise or', |
| 157 |
|
| op => 'tmp |= $a() ;', leftzero => '!~tmp' }, |
| 158 |
|
| bxorover => { txt => 'bitwise xor', |
| 159 |
|
| op => 'tmp ^= $a() ;', leftzero => 0 }, |
| 160 |
|
| ); |
| 161 |
|
| foreach my $func ( sort keys %over ) { |
| 162 |
|
| my ($def,$txt,$init,$op,$leftzero) = @{$over{$func}}{qw(def txt init op leftzero)}; |
| 163 |
|
| $def ||= '$GENERIC() tmp'; |
| 164 |
|
| $init //= '0'; |
| 165 |
|
| pp_def( |
| 166 |
|
| $func, |
| 167 |
|
| HandleBad => 1, |
| 168 |
|
| GenericTypes => $over{$func}{alltypes} ? [ppdefs_all] : $T, |
| 169 |
|
| Pars => 'a(n); [o] b()', |
| 170 |
|
| Code => pp_line_numbers(__LINE__, <<EOF), |
| 171 |
|
| $def = $init; |
| 172 |
|
| PDL_IF_BAD(int flag = 0;,) |
| 173 |
|
| loop(n) %{ |
| 174 |
|
| PDL_IF_BAD(if ( \$ISBAD(a()) ) continue; flag = 1;,) |
| 175 |
|
| $op |
| 176 |
|
| if ( $leftzero ) break; |
| 177 |
|
| %} |
| 178 |
|
| PDL_IF_BAD(if (!flag) { \$SETBAD(b()); \$PDLSTATESETBAD(b); } else,) |
| 179 |
|
| \$b() = tmp; |
| 180 |
|
| EOF |
| 181 |
|
| Doc => projectdocs( $txt, $func,''), |
| 182 |
|
| BadDoc => |
| 183 |
|
| 'If C<a()> contains only bad data (and its bad flag is set), |
| 184 |
|
| C<b()> is set bad. Otherwise C<b()> will have its bad flag cleared, |
| 185 |
|
| as it will not contain any bad values.', |
| 186 |
|
| ); |
| 187 |
|
| } # foreach: $func |
| 188 |
|
| |
| 189 |
|
| pp_def('numdiff', |
| 190 |
|
| Pars => 'x(t); [o]dx(t)', |
| 191 |
|
| Inplace => 1, |
| 192 |
|
| GenericTypes => [ppdefs_all], |
| 193 |
|
| Code => <<'EOF', |
| 194 |
|
| /* do it in reverse so inplace works */ |
| 195 |
|
| loop (t=::-1) %{ |
| 196 |
|
| $dx() = t ? $x() - $x(t=>t-1) : $x(); |
| 197 |
|
| %} |
| 198 |
|
| EOF |
| 199 |
|
| Doc => <<'EOF', |
| 200 |
|
| =for ref |
| 201 |
|
| |
| 202 |
|
| Numerical differencing. DX(t) = X(t) - X(t-1), DX(0) = X(0). |
| 203 |
|
| Combined with C<slice('-1:0')>, can be used for backward differencing. |
| 204 |
|
| |
| 205 |
|
| Unlike L</diff2>, output vector is same length. |
| 206 |
|
| Originally by Maggie J. Xiong. |
| 207 |
|
| |
| 208 |
|
| Compare to L</cumusumover>, which acts as the converse of this. |
| 209 |
|
| See also L</diff2>, L</diffcentred>, L</partial>, L<PDL::Primitive/pchip_chim>. |
| 210 |
|
| EOF |
| 211 |
|
| ); |
| 212 |
|
| |
| 213 |
|
| pp_def('diffcentred', |
| 214 |
|
| HandleBad => 1, |
| 215 |
|
| Pars => 'a(n); [o]o(n)', |
| 216 |
|
| GenericTypes => [ppdefs_all], |
| 217 |
|
| Code => <<'EOF', |
| 218 |
|
| loop(n) %{ |
| 219 |
|
| PDL_Indx left = n-1, right = n+1; |
| 220 |
|
| if (left < 0) left = $SIZE(n)-1; |
| 221 |
|
| if (right >= $SIZE(n)) right = 0; |
| 222 |
|
| PDL_IF_BAD(if ($ISBAD($a(n=>left)) || $ISBAD($a(n=>right))) { |
| 223 |
|
| $SETBAD(o()); continue; |
| 224 |
|
| },) |
| 225 |
|
| $o() = ($a(n=>right) - $a(n=>left))/2; |
| 226 |
|
| %} |
| 227 |
|
| EOF |
| 228 |
|
| Doc => <<'EOF', |
| 229 |
|
| =for ref |
| 230 |
|
| |
| 231 |
|
| Calculates centred differences along a vector's 0th dimension. |
| 232 |
|
| Always periodic on boundaries; currently to change this, you must |
| 233 |
|
| pad your data, and/or trim afterwards. This is so that when using |
| 234 |
|
| with L</partial>, the size of data stays the same and therefore |
| 235 |
|
| compatible if differentiated along different dimensions, e.g. |
| 236 |
|
| calculating "curl". |
| 237 |
|
| |
| 238 |
|
| By using L<PDL::Slices/xchg> etc. it is possible to use I<any> dimension. |
| 239 |
|
| |
| 240 |
|
| See also L</diff2>, L</partial>, L</numdiff>, L<PDL::Primitive/pchip_chim>. |
| 241 |
|
| EOF |
| 242 |
|
| BadDoc => <<'EOF', |
| 243 |
|
| A bad value at C<n> means the affected output values at C<n-2>,C<n> |
| 244 |
|
| (if in boounds) are set bad. |
| 245 |
|
| EOF |
| 246 |
|
| ); |
| 247 |
|
| |
| 248 |
|
| pp_add_exported('partial'); |
| 249 |
|
| pp_addpm(<<'EOF'); |
| 250 |
|
| =head2 partial |
| 251 |
|
| |
| 252 |
|
| =for ref |
| 253 |
|
| |
| 254 |
|
| Take a numerical partial derivative along a given dimension, either |
| 255 |
|
| forward, backward, or centred. |
| 256 |
|
| |
| 257 |
|
| See also L</numdiff>, L</diffcentred>, |
| 258 |
|
| L<PDL::Primitive/pchip_chim>, L<PDL::Primitive/pchip_chsp>, |
| 259 |
|
| and L<PDL::Slices/mv>, which are currently used to implement this. |
| 260 |
|
| |
| 261 |
|
| Can be used to implement divergence and curl calculations (adapted |
| 262 |
|
| from Luis Mochán's work at |
| 263 |
|
| https://sourceforge.net/p/pdl/mailman/message/58843767/): |
| 264 |
|
| |
| 265 |
|
| use v5.36; |
| 266 |
|
| use PDL; |
| 267 |
|
| sub curl ($f) { |
| 268 |
|
| my ($f0, $f1, $f2) = $f->using(0..2); |
| 269 |
|
| my $o = {dir=>'c'}; |
| 270 |
|
| pdl( |
| 271 |
|
| $f2->partial(1,$o) - $f1->partial(2,$o), |
| 272 |
|
| $f0->partial(2,$o) - $f2->partial(0,$o), |
| 273 |
|
| $f1->partial(0,$o) - $f0->partial(1,$o), |
| 274 |
|
| )->mv(-1,0); |
| 275 |
|
| } |
| 276 |
|
| sub div ($f) { |
| 277 |
|
| my ($f0, $f1, $f2) = $f->using(0..2); |
| 278 |
|
| my $o = {dir=>'c'}; |
| 279 |
|
| $f0->partial(0,$o) + $f1->partial(1,$o) + $f2->partial(2,$o); |
| 280 |
|
| } |
| 281 |
|
| sub trim3d ($f) { $f->slice(':,1:-2,1:-2,1:-2') } # adjust if change "dir" |
| 282 |
|
| my $z=zeroes(5,5,5); |
| 283 |
|
| my $v=pdl(-$z->yvals, $z->xvals, $z->zvals)->mv(-1,0); |
| 284 |
|
| say trim3d(curl($v)); |
| 285 |
|
| say div($v); |
| 286 |
|
| |
| 287 |
|
| =for usage |
| 288 |
|
| |
| 289 |
|
| $pdl->partial(2); # along dim 2, centred |
| 290 |
|
| $pdl->partial(2, {d=>'c'}); # along dim 2, centred |
| 291 |
|
| $pdl->partial(2, {d=>'f'}); # along dim 2, forward |
| 292 |
|
| $pdl->partial(2, {d=>'b'}); # along dim 2, backward |
| 293 |
|
| $pdl->partial(2, {d=>'p'}); # along dim 2, piecewise cubic Hermite |
| 294 |
|
| $pdl->partial(2, {d=>'s'}); # along dim 2, cubic spline |
| 295 |
|
| |
| 296 |
|
| =cut |
| 297 |
|
| |
| 298 |
|
| my %dirtype2func = ( |
| 299 |
|
| f => \&numdiff, |
| 300 |
|
| b => sub { $_[0]->slice('-1:0')->numdiff }, |
| 301 |
|
| c => \&diffcentred, |
| 302 |
|
| p => sub {(PDL::Primitive::pchip_chim($_[0]->xvals, $_[0]))[0]}, |
| 303 |
|
| s => sub {(PDL::Primitive::pchip_chsp([0,0], [0,0], $_[0]->xvals, $_[0]))[0]}, |
| 304 |
|
| ); |
| 305 |
|
| *partial = \&PDL::partial; |
| 306 |
|
| sub PDL::partial { |
| 307 |
|
| my ($f, $dim, $opts) = @_; |
| 308 |
|
| $opts ||= {}; |
| 309 |
|
| my $difftype = $opts->{dir} || $opts->{d} || 'c'; |
| 310 |
|
| my $func = $dirtype2func{$difftype} || barf "partial: unknown 'dir' option '$difftype', only know (@{[sort keys %dirtype2func]})"; |
| 311 |
|
| $f = $f->mv($dim, 0) if $dim; |
| 312 |
|
| my $ret = $f->$func; |
| 313 |
|
| $dim ? $ret->mv(0, $dim) : $ret; |
| 314 |
|
| } |
| 315 |
|
| EOF |
| 316 |
|
| |
| 317 |
|
| pp_def('diff2', |
| 318 |
|
| HandleBad => 1, |
| 319 |
|
| Pars => 'a(n); [o]o(nminus1=CALC($SIZE(n) - 1))', |
| 320 |
|
| GenericTypes => [ppdefs_all], |
| 321 |
|
| Code => <<'EOF', |
| 322 |
|
| $GENERIC() lastval = PDL_IF_BAD($ISBAD($a(n=>0)) ? 0 :,) $a(n=>0); |
| 323 |
|
| loop(n=1) %{ |
| 324 |
|
| PDL_IF_BAD(if ($ISBAD($a())) |
| 325 |
|
| $SETBAD(o(nminus1=>n-1)); |
| 326 |
|
| else,) |
| 327 |
|
| $o(nminus1=>n-1) = $a() - lastval; |
| 328 |
|
| PDL_IF_BAD(if ($ISGOOD($a())),) lastval = $a(); |
| 329 |
|
| %} |
| 330 |
|
| EOF |
| 331 |
|
| Doc => <<'EOF', |
| 332 |
|
| =for ref |
| 333 |
|
| |
| 334 |
|
| Numerically forward differentiates a vector along 0th dimension. |
| 335 |
|
| |
| 336 |
|
| By using L<PDL::Slices/xchg> etc. it is possible to use I<any> dimension. |
| 337 |
|
| Unlike L</numdiff>, output vector is one shorter. |
| 338 |
|
| Combined with C<slice('-1:0')>, can be used for backward differencing. |
| 339 |
|
| |
| 340 |
|
| See also L</numdiff>, L</diffcentred>, L</partial>, L<PDL::Primitive/pchip_chim>. |
| 341 |
|
| |
| 342 |
|
| =for example |
| 343 |
|
| |
| 344 |
|
| print pdl(q[3 4 2 3 2 3 1])->diff2; |
| 345 |
|
| # [1 -2 1 -1 1 -2] |
| 346 |
|
| EOF |
| 347 |
|
| BadDoc => <<'EOF', |
| 348 |
|
| On bad value, output value is set bad. On next good value, output value |
| 349 |
|
| is difference between that and last good value. |
| 350 |
|
| EOF |
| 351 |
|
| ); |
| 352 |
|
| |
| 353 |
|
| # this would need a lot of work to support bad values |
| 354 |
|
| # plus it gives me a chance to check out HandleBad => 0 ;) |
| 355 |
|
| # |
| 356 |
|
| pp_def('intover', |
| 357 |
|
| HandleBad => 0, |
| 358 |
|
| Pars => 'a(n); float+ [o]b();', |
| 359 |
|
| Code => |
| 360 |
|
| '/* Integration formulae from Press et al 2nd Ed S 4.1 */ |
| 361 |
|
| switch ($SIZE(n)) { |
| 362 |
|
| case 1: |
| 363 |
|
| broadcastloop %{ |
| 364 |
|
| $b() = 0.; /* not a(n=>0); as interval has zero width */ |
| 365 |
|
| %} |
| 366 |
|
| break; |
| 367 |
|
| case 2: |
| 368 |
|
| broadcastloop %{ |
| 369 |
|
| $b() = 0.5*($a(n=>0)+$a(n=>1)); |
| 370 |
|
| %} |
| 371 |
|
| break; |
| 372 |
|
| case 3: |
| 373 |
|
| broadcastloop %{ |
| 374 |
|
| $b() = ($a(n=>0)+4*$a(n=>1)+$a(n=>2))/3.; |
| 375 |
|
| %} |
| 376 |
|
| break; |
| 377 |
|
| case 4: |
| 378 |
|
| broadcastloop %{ |
| 379 |
|
| $b() = ($a(n=>0)+$a(n=>3)+3.*($a(n=>1)+$a(n=>2)))*0.375; |
| 380 |
|
| %} |
| 381 |
|
| break; |
| 382 |
|
| case 5: |
| 383 |
|
| broadcastloop %{ |
| 384 |
|
| $b() = (14.*($a(n=>0)+$a(n=>4)) |
| 385 |
|
| +64.*($a(n=>1)+$a(n=>3)) |
| 386 |
|
| +24.*$a(n=>2))/45.; |
| 387 |
|
| %} |
| 388 |
|
| break; |
| 389 |
|
| default: |
| 390 |
|
| broadcastloop %{ |
| 391 |
|
| $GENERIC(b) tmp = 0; |
| 392 |
|
| loop (n=3:-3) %{ tmp += $a(); %} |
| 393 |
|
| loop (n=-3:-2) %{ tmp += (23./24.)*($a(n=>2)+$a()); %} |
| 394 |
|
| loop (n=-2:-1) %{ tmp += (7./6.) *($a(n=>1)+$a()); %} |
| 395 |
|
| loop (n=-1:) %{ tmp += (3./8.) *($a(n=>0)+$a()); %} |
| 396 |
|
| $b() = tmp; |
| 397 |
|
| %} |
| 398 |
|
| } |
| 399 |
|
| ', |
| 400 |
|
| Doc => projectdocs('integral','intover', <<'EOF'), |
| 401 |
|
| Notes: |
| 402 |
|
| |
| 403 |
|
| C<intover> uses a point spacing of one (i.e., delta-h==1). You will |
| 404 |
|
| need to scale the result to correct for the true point delta. |
| 405 |
|
| |
| 406 |
|
| For C<n E<gt> 3>, these are all C<O(h^4)> (like Simpson's rule), but are |
| 407 |
|
| integrals between the end points assuming the pdl gives values just at |
| 408 |
|
| these centres: for such `functions', sumover is correct to C<O(h)>, but |
| 409 |
|
| is the natural (and correct) choice for binned data, of course. |
| 410 |
|
| EOF |
| 411 |
|
| ); # intover |
| 412 |
|
| |
| 413 |
|
| sub make_average { |
| 414 |
|
| my ($prefix, $outpar_type, $extra) = @_; |
| 415 |
|
| pp_def("${prefix}average", |
| 416 |
|
| HandleBad => 1, |
| 417 |
|
| Pars => "a(n); $outpar_type [o]b();", |
| 418 |
|
| Code => <<'EOF', |
| 419 |
|
| $GENERIC(b) tmp = 0; |
| 420 |
|
| PDL_Indx cnt = 0; |
| 421 |
|
| loop(n) %{ |
| 422 |
|
| PDL_IF_BAD(if ( $ISBAD(a()) ) continue;,) |
| 423 |
|
| cnt++; |
| 424 |
|
| tmp += $a(); |
| 425 |
|
| %} |
| 426 |
|
| if ( !cnt ) { |
| 427 |
|
| PDL_IF_BAD($SETBAD(b()), $b() = PDL_IF_GENTYPE_INTEGER(0,NAN)); |
| 428 |
|
| } else |
| 429 |
|
| $b() = tmp / cnt; |
| 430 |
|
| EOF |
| 431 |
|
| Synonyms => ["${prefix}avgover"], |
| 432 |
|
| Doc => projectdocs( 'average', "${prefix}average", $extra||'' ), |
| 433 |
|
| ); |
| 434 |
|
| } |
| 435 |
|
| |
| 436 |
|
| make_average('', 'int+'); |
| 437 |
|
| make_average('c', 'cdouble', |
| 438 |
|
| "Unlike L<average|/average>, the calculation is performed in complex double |
| 439 |
|
| precision." |
| 440 |
|
| ); |
| 441 |
|
| make_average('d', 'double', |
| 442 |
|
| "Unlike L<average|/average>, the calculation is performed in double |
| 443 |
|
| precision." |
| 444 |
|
| ); |
| 445 |
|
| |
| 446 |
|
| for my $which ( |
| 447 |
|
| [qw(minimum < minover)], |
| 448 |
|
| [qw(maximum > maxover)], |
| 449 |
|
| ) { |
| 450 |
|
| my ($name, $op, $synonym) = @$which; |
| 451 |
|
| |
| 452 |
|
| pp_def($name, |
| 453 |
|
| HandleBad => 1, |
| 454 |
|
| Pars => 'a(n); [o]c();', |
| 455 |
|
| Code => |
| 456 |
|
| '$GENERIC() cur = 0; |
| 457 |
|
| int flag = 0; |
| 458 |
|
| loop(n) %{ |
| 459 |
|
| PDL_IF_BAD(if ($ISBAD(a())) continue;,) |
| 460 |
|
| if ( flag && !($a() '.$op.' cur) && !PDL_ISNAN_$PPSYM()(cur) ) continue; |
| 461 |
|
| cur = $a(); flag = 1; |
| 462 |
|
| %} |
| 463 |
|
| if ( flag ) { $c() = cur; } |
| 464 |
|
| else { $SETBAD(c()); $PDLSTATESETBAD(c); }', |
| 465 |
|
| Synonyms => [$synonym], |
| 466 |
|
| Doc => projectdocs($name,$name,''), |
| 467 |
|
| BadDoc => |
| 468 |
|
| 'Output is set bad if no elements of the input are non-bad, |
| 469 |
|
| otherwise the bad flag is cleared for the output ndarray. |
| 470 |
|
| |
| 471 |
|
| Note that C<NaNs> are considered to be valid values and will "win" over non-C<NaN>; |
| 472 |
|
| see L<isfinite|PDL::Math/isfinite> and L<badmask|PDL::Bad/badmask> |
| 473 |
|
| for ways of masking NaNs. |
| 474 |
|
| ', |
| 475 |
|
| ); |
| 476 |
|
| |
| 477 |
|
| pp_def("${name}_ind", |
| 478 |
|
| HandleBad => 1, |
| 479 |
|
| Pars => 'a(n); indx [o] c();', |
| 480 |
|
| Code => |
| 481 |
|
| '$GENERIC() cur = 0; |
| 482 |
|
| PDL_Indx curind = -1; |
| 483 |
|
| loop(n) %{ |
| 484 |
|
| PDL_IF_BAD(if ($ISBAD(a())) continue;,) |
| 485 |
|
| if (curind != -1 && !($a() '.$op.' cur) && !PDL_ISNAN_$PPSYM()(cur)) continue; |
| 486 |
|
| cur = $a(); curind = n; |
| 487 |
|
| %} |
| 488 |
|
| if ( curind != -1 ) { $c() = curind; } |
| 489 |
|
| else { $SETBAD(c()); $PDLSTATESETBAD(c); }', |
| 490 |
|
| Synonyms => ["${synonym}_ind"], |
| 491 |
|
| Doc => "Like $name but returns the first matching index rather than the value", |
| 492 |
|
| BadDoc => |
| 493 |
|
| 'Output is set bad if no elements of the input are non-bad, |
| 494 |
|
| otherwise the bad flag is cleared for the output ndarray. |
| 495 |
|
| |
| 496 |
|
| Note that C<NaNs> are considered to be valid values and will "win" over non-C<NaN>; |
| 497 |
|
| see L<isfinite|PDL::Math/isfinite> and L<badmask|PDL::Bad/badmask> |
| 498 |
|
| for ways of masking NaNs. |
| 499 |
|
| ', |
| 500 |
|
| ); |
| 501 |
|
| |
| 502 |
|
| pp_def("${name}_n_ind", |
| 503 |
|
| HandleBad => 1, |
| 504 |
|
| Pars => 'a(n); indx [o]c(m);', |
| 505 |
|
| OtherPars => 'PDL_Indx m_size => m;', |
| 506 |
|
| PMCode => PDL::PP::pp_line_numbers(__LINE__, <<EOF), |
| 507 |
|
| sub PDL::${name}_n_ind { |
| 508 |
|
| my (\$a, \$c, \$m_size) = \@_; |
| 509 |
|
| \$m_size //= ref(\$c) ? \$c->dim(0) : \$c; # back-compat with pre-2.077 |
| 510 |
|
| my \$set_out = 1; |
| 511 |
|
| \$set_out = 0, \$c = null if !ref \$c; |
| 512 |
|
| \$c = \$c->indx if !\$c->isnull; |
| 513 |
|
| PDL::_${name}_n_ind_int(\$a, \$c, \$m_size); |
| 514 |
|
| \$set_out ? \$_[1] = \$c : \$c; |
| 515 |
|
| } |
| 516 |
|
| EOF |
| 517 |
|
| RedoDimsCode => 'if($SIZE(m) > $SIZE(n)) $CROAK("m_size > n_size");', |
| 518 |
|
| Code => |
| 519 |
|
| '$GENERIC() cur = 0; PDL_Indx curind; register PDL_Indx ns = $SIZE(n); |
| 520 |
|
| $PDLSTATESETGOOD(c); |
| 521 |
|
| loop(m) %{ |
| 522 |
|
| curind = ns; |
| 523 |
|
| loop(n) %{ |
| 524 |
|
| PDL_Indx outer_m = m; int flag=0; |
| 525 |
|
| loop (m=:outer_m) %{ |
| 526 |
|
| if ($c() == n) {flag=1; break;} |
| 527 |
|
| %} |
| 528 |
|
| if (!flag && |
| 529 |
|
| PDL_IF_BAD($ISGOOD(a()) &&,) |
| 530 |
|
| ((curind == ns) || $a() '.$op.' cur || PDL_ISNAN_$PPSYM()(cur))) |
| 531 |
|
| {cur = $a(); curind = n;} |
| 532 |
|
| %} |
| 533 |
|
| if (curind != ns) { $c() = curind; } |
| 534 |
|
| else { $SETBAD(c()); $PDLSTATESETBAD(c); } |
| 535 |
|
| %}', |
| 536 |
|
| Synonyms => ["${synonym}_n_ind"], |
| 537 |
|
| Doc => <<EOF, |
| 538 |
|
| =for ref |
| 539 |
|
| |
| 540 |
|
| Returns the index of first C<m_size> $name elements. As of 2.077, you can |
| 541 |
|
| specify how many by either passing in an ndarray of the given size |
| 542 |
|
| (DEPRECATED - will be converted to indx if needed and the input arg will |
| 543 |
|
| be set to that), or just the size, or a null and the size. |
| 544 |
|
| |
| 545 |
|
| =for usage |
| 546 |
|
| |
| 547 |
|
| ${name}_n_ind(\$pdl, \$out = zeroes(5)); # DEPRECATED |
| 548 |
|
| \$out = ${name}_n_ind(\$pdl, 5); |
| 549 |
|
| ${name}_n_ind(\$pdl, \$out = null, 5); |
| 550 |
|
| |
| 551 |
|
| EOF |
| 552 |
|
| BadDoc => |
| 553 |
|
| 'Output bad flag is cleared for the output ndarray if sufficient non-bad elements found, |
| 554 |
|
| else remaining slots in C<$c()> are set bad. |
| 555 |
|
| |
| 556 |
|
| Note that C<NaNs> are considered to be valid values and will "win" over non-C<NaN>; |
| 557 |
|
| see L<isfinite|PDL::Math/isfinite> and L<badmask|PDL::Bad/badmask> |
| 558 |
|
| for ways of masking NaNs. |
| 559 |
|
| ', |
| 560 |
|
| ); |
| 561 |
|
| } # foreach: $which |
| 562 |
|
| |
| 563 |
|
| pp_def('minmaximum', |
| 564 |
|
| HandleBad => 1, |
| 565 |
|
| Pars => 'a(n); [o]cmin(); [o] cmax(); indx [o]cmin_ind(); indx [o]cmax_ind();', |
| 566 |
|
| Code => <<'EOF', |
| 567 |
|
| $GENERIC() curmin = 0, curmax = 0; /* Handle null ndarray --CED */ |
| 568 |
|
| PDL_Indx curmin_ind = 0, curmax_ind = 0; int flag = 0; |
| 569 |
|
| loop(n) %{ |
| 570 |
|
| PDL_IF_BAD(if ($ISBAD(a())) continue;,) |
| 571 |
|
| if (PDL_ISNAN_$PPSYM()($a())) continue; |
| 572 |
|
| if (!flag) { |
| 573 |
|
| curmin = curmax = $a(); |
| 574 |
|
| curmin_ind = curmax_ind = n; |
| 575 |
|
| flag = 1; |
| 576 |
|
| } else { |
| 577 |
|
| if ($a() < curmin) { curmin = $a(); curmin_ind = n; } |
| 578 |
|
| if ($a() > curmax) { curmax = $a(); curmax_ind = n; } |
| 579 |
|
| } |
| 580 |
|
| %} |
| 581 |
|
| if ( !flag ) { /* Handle null ndarray */ |
| 582 |
|
| $SETBAD(cmin()); $SETBAD(cmin_ind()); |
| 583 |
|
| $SETBAD(cmax()); $SETBAD(cmax_ind()); |
| 584 |
|
| $PDLSTATESETBAD(cmin); $PDLSTATESETBAD(cmin_ind); |
| 585 |
|
| $PDLSTATESETBAD(cmax); $PDLSTATESETBAD(cmax_ind); |
| 586 |
|
| } else { |
| 587 |
|
| $cmin() = curmin; $cmin_ind() = curmin_ind; |
| 588 |
|
| $cmax() = curmax; $cmax_ind() = curmax_ind; |
| 589 |
|
| } |
| 590 |
|
| EOF |
| 591 |
|
| Synonyms => ["minmaxover"], |
| 592 |
|
| Doc => ' |
| 593 |
|
| =for ref |
| 594 |
|
| |
| 595 |
|
| Find minimum and maximum and their indices for a given ndarray; |
| 596 |
|
| |
| 597 |
|
| =for example |
| 598 |
|
| |
| 599 |
|
| pdl> $x=pdl [[-2,3,4],[1,0,3]] |
| 600 |
|
| pdl> ($min, $max, $min_ind, $max_ind)=minmaximum($x) |
| 601 |
|
| pdl> p $min, $max, $min_ind, $max_ind |
| 602 |
|
| [-2 0] [4 3] [0 1] [2 2] |
| 603 |
|
| |
| 604 |
|
| See also L</minmax>, which clumps the ndarray together. |
| 605 |
|
| |
| 606 |
|
| =cut |
| 607 |
|
| ', |
| 608 |
|
| BadDoc => |
| 609 |
|
| 'If C<a()> contains only bad data, then the output ndarrays will |
| 610 |
|
| be set bad, along with their bad flag. |
| 611 |
|
| Otherwise they will have their bad flags cleared, |
| 612 |
|
| since they will not contain any bad values.', |
| 613 |
|
| ); # pp_def minmaximum |
| 614 |
|
| |
| 615 |
|
| # Generate small ops functions to do entire array |
| 616 |
|
| # How to handle a return value of BAD - ie what |
| 617 |
|
| # datatype to use? |
| 618 |
|
| for my $op ( ['avg','average','average'], |
| 619 |
|
| ['sum','sumover','sum'], |
| 620 |
|
| ['prod','prodover','product'], |
| 621 |
|
| |
| 622 |
|
| ['davg','daverage','average (in double precision)'], |
| 623 |
|
| ['dsum','dsumover','sum (in double precision)'], |
| 624 |
|
| ['dprod','dprodover','product (in double precision)'], |
| 625 |
|
| |
| 626 |
|
| ['zcheck','zcover','check for zero'], |
| 627 |
|
| ['and','andover','logical and'], |
| 628 |
|
| ['band','bandover','bitwise and'], |
| 629 |
|
| ['or','orover','logical or'], |
| 630 |
|
| ['bor','borover','bitwise or'], |
| 631 |
|
| ['xorall','xorover','logical xor'], |
| 632 |
|
| ['bxor','bxorover','bitwise xor'], |
| 633 |
|
| ['min','minimum','minimum'], |
| 634 |
|
| ['max','maximum','maximum'], |
| 635 |
|
| ['median', 'medover', 'median'], |
| 636 |
|
| ['mode', 'modeover', 'mode'], |
| 637 |
|
| ['oddmedian','oddmedover','oddmedian']) { |
| 638 |
|
| my ($name, $func, $text) = @$op; |
| 639 |
|
| pp_add_exported('', $name); |
| 640 |
|
| pp_addpm(<<"EOD"); |
| 641 |
|
| =head2 $name |
| 642 |
|
| |
| 643 |
|
| =for ref |
| 644 |
|
| |
| 645 |
|
| Return the $text of all elements in an ndarray. |
| 646 |
|
| |
| 647 |
|
| See the documentation for L</$func> for more information. |
| 648 |
|
| |
| 649 |
|
| =for usage |
| 650 |
|
| |
| 651 |
|
| \$x = $name(\$data); |
| 652 |
|
| |
| 653 |
|
| =for bad |
| 654 |
|
| |
| 655 |
|
| This routine handles bad values. |
| 656 |
|
| |
| 657 |
|
| =cut |
| 658 |
|
| |
| 659 |
|
| *$name = \\&PDL::$name; |
| 660 |
|
| sub PDL::$name { \$_[0]->flat->${func} } |
| 661 |
|
| EOD |
| 662 |
|
| |
| 663 |
|
| } # for $op |
| 664 |
|
| |
| 665 |
|
| pp_add_exported('','any all'); |
| 666 |
|
| pp_addpm(<<'EOPM'); |
| 667 |
|
| |
| 668 |
|
| =head2 any |
| 669 |
|
| |
| 670 |
|
| =for ref |
| 671 |
|
| |
| 672 |
|
| Return true if any element in ndarray set |
| 673 |
|
| |
| 674 |
|
| Useful in conditional expressions: |
| 675 |
|
| |
| 676 |
|
| =for example |
| 677 |
|
| |
| 678 |
|
| if (any $x>15) { print "some values are greater than 15\n" } |
| 679 |
|
| |
| 680 |
|
| =for bad |
| 681 |
|
| |
| 682 |
|
| See L</or> for comments on what happens when all elements |
| 683 |
|
| in the check are bad. |
| 684 |
|
| |
| 685 |
|
| =cut |
| 686 |
|
| |
| 687 |
|
| *any = \∨ |
| 688 |
|
| *PDL::any = \&PDL::or; |
| 689 |
|
| |
| 690 |
|
| =head2 all |
| 691 |
|
| |
| 692 |
|
| =for ref |
| 693 |
|
| |
| 694 |
|
| Return true if all elements in ndarray set |
| 695 |
|
| |
| 696 |
|
| Useful in conditional expressions: |
| 697 |
|
| |
| 698 |
|
| =for example |
| 699 |
|
| |
| 700 |
|
| if (all $x>15) { print "all values are greater than 15\n" } |
| 701 |
|
| |
| 702 |
|
| =for bad |
| 703 |
|
| |
| 704 |
|
| See L</and> for comments on what happens when all elements |
| 705 |
|
| in the check are bad. |
| 706 |
|
| |
| 707 |
|
| =cut |
| 708 |
|
| |
| 709 |
|
| *all = \∧ |
| 710 |
|
| *PDL::all = \&PDL::and; |
| 711 |
|
| |
| 712 |
|
| =head2 minmax |
| 713 |
|
| |
| 714 |
|
| =for ref |
| 715 |
|
| |
| 716 |
|
| Returns a list with minimum and maximum values of an ndarray. |
| 717 |
|
| |
| 718 |
|
| =for usage |
| 719 |
|
| |
| 720 |
|
| ($mn, $mx) = minmax($pdl); |
| 721 |
|
| |
| 722 |
|
| This routine does I<not> broadcast over the dimensions of C<$pdl>; |
| 723 |
|
| it returns the minimum and maximum values of the whole ndarray. |
| 724 |
|
| See L</minmaximum> if this is not what is required. |
| 725 |
|
| The two values are returned as Perl scalars, |
| 726 |
|
| and therefore ignore whether the values are bad. |
| 727 |
|
| |
| 728 |
|
| =for example |
| 729 |
|
| |
| 730 |
|
| pdl> $x = pdl [1,-2,3,5,0] |
| 731 |
|
| pdl> ($min, $max) = minmax($x); |
| 732 |
|
| pdl> p "$min $max\n"; |
| 733 |
|
| -2 5 |
| 734 |
|
| |
| 735 |
|
| =cut |
| 736 |
|
| |
| 737 |
|
| *minmax = \&PDL::minmax; |
| 738 |
|
| sub PDL::minmax { map $_->sclr, ($_[0]->flat->minmaximum)[0,1] } |
| 739 |
|
| |
| 740 |
|
| EOPM |
| 741 |
|
| |
| 742 |
|
| pp_add_exported('', 'minmax'); |
| 743 |
|
| |
| 744 |
|
| my $chdr_qsort = 'PDL_TYPELIST_REAL(PDL_QSORT)'; |
| 745 |
|
| my $chdr_qsorti = PDL::PP::pp_line_numbers(__LINE__, <<'EOF'); |
| 746 |
|
| #define X(symbol, ctype, ppsym, ...) \ |
| 747 |
|
| static inline void qsort_ind_ ## ppsym(ctype* xx, PDL_Indx* ix, PDL_Indx a, PDL_Indx b) { \ |
| 748 |
|
| PDL_Indx i,j; \ |
| 749 |
|
| PDL_Indx t; \ |
| 750 |
|
| ctype median; \ |
| 751 |
|
| i = a; j = b; \ |
| 752 |
|
| median = xx[ix[(i+j) / 2]]; \ |
| 753 |
|
| do { \ |
| 754 |
|
| while (xx[ix[i]] < median) \ |
| 755 |
|
| i++; \ |
| 756 |
|
| while (median < xx[ix[j]]) \ |
| 757 |
|
| j--; \ |
| 758 |
|
| if (i <= j) { \ |
| 759 |
|
| t = ix[i]; ix[i] = ix[j]; ix[j] = t; \ |
| 760 |
|
| i++; j--; \ |
| 761 |
|
| } \ |
| 762 |
|
| } while (i <= j); \ |
| 763 |
|
| if (a < j) \ |
| 764 |
|
| qsort_ind_ ## ppsym(xx,ix,a,j); \ |
| 765 |
|
| if (i < b) \ |
| 766 |
|
| qsort_ind_ ## ppsym(xx,ix,i,b); \ |
| 767 |
|
| } |
| 768 |
|
| PDL_TYPELIST_REAL(X) |
| 769 |
|
| #undef X |
| 770 |
|
| EOF |
| 771 |
|
| my $chdr_qsortvec_def = PDL::PP::pp_line_numbers(__LINE__, <<'EOF'); |
| 772 |
|
| #define X(symbol, ctype, ppsym, ...) \ |
| 773 |
|
| /******* \ |
| 774 |
|
| * qsortvec helper routines \ |
| 775 |
|
| * --CED 21-Aug-2003 \ |
| 776 |
|
| */ \ |
| 777 |
|
| /* Compare a vector in lexicographic order, return equivalent of "<=>". \ |
| 778 |
|
| */ \ |
| 779 |
|
| static inline signed int pdl_cmpvec_ ## ppsym(ctype *a, ctype *b, PDL_Indx n) { \ |
| 780 |
|
| PDL_Indx i; \ |
| 781 |
|
| for(i=0; i<n; a++,b++,i++) { \ |
| 782 |
|
| if( *a < *b ) return -1; \ |
| 783 |
|
| if( *a > *b ) return 1; \ |
| 784 |
|
| } \ |
| 785 |
|
| return 0; \ |
| 786 |
|
| } |
| 787 |
0 |
▶ | PDL_TYPELIST_REAL(X) |
Branch: 36 branches
| Branch | True | False | Total |
| PDL_TYPELIST_REAL(X) |
2 |
2 |
4 |
| PDL_TYPELIST_REAL(X) |
2 |
2 |
4 |
| PDL_TYPELIST_REAL(X) |
0 |
1 |
1 |
| PDL_TYPELIST_REAL(X) |
17 |
88 |
105 |
| PDL_TYPELIST_REAL(X) |
17 |
72 |
89 |
| PDL_TYPELIST_REAL(X) |
104 |
31 |
135 |
| PDL_TYPELIST_REAL(X) |
4 |
1 |
5 |
| PDL_TYPELIST_REAL(X) |
1 |
5 |
6 |
| PDL_TYPELIST_REAL(X) |
4 |
2 |
6 |
| PDL_TYPELIST_REAL(X) |
5 |
1 |
6 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
|
| 788 |
|
| #undef X |
| 789 |
|
| #define PDL_QSORTVEC(ppsym, RECURSE, INDEXTERM, swapcode) \ |
| 790 |
|
| PDL_Indx i,j, median_ind; \ |
| 791 |
|
| i = a; \ |
| 792 |
|
| j = b; \ |
| 793 |
|
| median_ind = (i+j)/2; \ |
| 794 |
|
| do { \ |
| 795 |
|
| while( pdl_cmpvec_ ## ppsym( &(xx[n*INDEXTERM(i)]), &(xx[n*INDEXTERM(median_ind)]), n ) < 0 ) \ |
| 796 |
|
| i++; \ |
| 797 |
|
| while( pdl_cmpvec_ ## ppsym( &(xx[n*INDEXTERM(j)]), &(xx[n*INDEXTERM(median_ind)]), n ) > 0 ) \ |
| 798 |
|
| j--; \ |
| 799 |
|
| if(i<=j) { \ |
| 800 |
|
| PDL_Indx k; \ |
| 801 |
|
| swapcode \ |
| 802 |
|
| if (median_ind==i) \ |
| 803 |
|
| median_ind=j; \ |
| 804 |
|
| else if (median_ind==j) \ |
| 805 |
|
| median_ind=i; \ |
| 806 |
|
| i++; \ |
| 807 |
|
| j--; \ |
| 808 |
|
| } \ |
| 809 |
|
| } while (i <= j); \ |
| 810 |
|
| if (a < j) \ |
| 811 |
|
| RECURSE( ppsym, a, j ); \ |
| 812 |
|
| if (i < b) \ |
| 813 |
|
| RECURSE( ppsym, i, b ); |
| 814 |
|
| EOF |
| 815 |
|
| my $chdr_qsortvec = PDL::PP::pp_line_numbers(__LINE__, <<'EOF'); |
| 816 |
|
| #define PDL_QSORTVEC_INDEXTERM(indexterm) indexterm |
| 817 |
|
| #define PDL_QSORTVEC_RECURSE(ppsym, ...) pdl_qsortvec_ ## ppsym(xx, n, __VA_ARGS__) |
| 818 |
|
| #define X(symbol, ctype, ppsym, ...) \ |
| 819 |
|
| static inline void pdl_qsortvec_ ## ppsym(ctype *xx, PDL_Indx n, PDL_Indx a, PDL_Indx b) { \ |
| 820 |
|
| PDL_QSORTVEC(ppsym, PDL_QSORTVEC_RECURSE, PDL_QSORTVEC_INDEXTERM, \ |
| 821 |
|
| ctype *aa = &xx[n*i]; \ |
| 822 |
|
| ctype *bb = &xx[n*j]; \ |
| 823 |
|
| for( k=0; k<n; aa++,bb++,k++ ) { \ |
| 824 |
|
| ctype z = *aa; \ |
| 825 |
|
| *aa = *bb; \ |
| 826 |
|
| *bb = z; \ |
| 827 |
|
| } \ |
| 828 |
|
| ) \ |
| 829 |
|
| } |
| 830 |
|
| PDL_TYPELIST_REAL(X) |
| 831 |
|
| #undef X |
| 832 |
|
| #undef PDL_QSORTVEC_INDEXTERM |
| 833 |
|
| #undef PDL_QSORTVEC_RECURSE |
| 834 |
|
| EOF |
| 835 |
|
| my $chdr_qsortvec_ind = PDL::PP::pp_line_numbers(__LINE__, <<'EOF'); |
| 836 |
|
| #define PDL_QSORTVEC_INDEXTERM(indexterm) ix[indexterm] |
| 837 |
|
| #define PDL_QSORTVEC_RECURSE(ppsym, ...) pdl_qsortvec_ind_ ## ppsym(xx, ix, n, __VA_ARGS__) |
| 838 |
|
| #define X(symbol, ctype, ppsym, ...) \ |
| 839 |
|
| static inline void pdl_qsortvec_ind_ ## ppsym(ctype *xx, PDL_Indx *ix, PDL_Indx n, PDL_Indx a, PDL_Indx b) { \ |
| 840 |
|
| PDL_QSORTVEC(ppsym, PDL_QSORTVEC_RECURSE, PDL_QSORTVEC_INDEXTERM, \ |
| 841 |
|
| k = ix[i]; \ |
| 842 |
|
| ix[i] = ix[j]; \ |
| 843 |
|
| ix[j] = k; \ |
| 844 |
|
| ) \ |
| 845 |
|
| } |
| 846 |
0 |
▶ | PDL_TYPELIST_REAL(X) |
Branch: 96 branches
| Branch | True | False | Total |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
7 |
22 |
29 |
| PDL_TYPELIST_REAL(X) |
12 |
22 |
34 |
| PDL_TYPELIST_REAL(X) |
22 |
0 |
22 |
| PDL_TYPELIST_REAL(X) |
17 |
5 |
22 |
| PDL_TYPELIST_REAL(X) |
2 |
3 |
5 |
| PDL_TYPELIST_REAL(X) |
3 |
19 |
22 |
| PDL_TYPELIST_REAL(X) |
5 |
14 |
19 |
| PDL_TYPELIST_REAL(X) |
7 |
12 |
19 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
| PDL_TYPELIST_REAL(X) |
0 |
0 |
0 |
|
| 847 |
|
| #undef X |
| 848 |
|
| #undef PDL_QSORTVEC_INDEXTERM |
| 849 |
|
| #undef PDL_QSORTVEC_RECURSE |
| 850 |
|
| EOF |
| 851 |
|
| |
| 852 |
|
| # when copying the data over to the temporary array, |
| 853 |
|
| # ignore the bad values and then only send the number |
| 854 |
|
| # of good elements to the sort routines |
| 855 |
|
| # should use broadcastloop ? |
| 856 |
|
| my $copy_to_temp = pp_line_numbers(__LINE__, ' |
| 857 |
|
| if ($PDL(a)->nvals == 0) |
| 858 |
|
| $CROAK("cannot process empty ndarray"); |
| 859 |
|
| PDL_Indx nn = PDL_IF_BAD(0,$SIZE(n)-1); |
| 860 |
|
| loop(n) %{ |
| 861 |
|
| PDL_IF_BAD(if ( $ISGOOD(a()) ) { $tmp(n=>nn) = $a(); nn++; }, |
| 862 |
|
| $tmp() = $a();) |
| 863 |
|
| %} |
| 864 |
|
| PDL_IF_BAD(if ( nn == 0 ) { |
| 865 |
|
| $SETBAD(b()); |
| 866 |
|
| } else { |
| 867 |
|
| nn -= 1;,{) |
| 868 |
|
| qsort_$PPSYM() ($P(tmp), 0, nn);'); |
| 869 |
|
| |
| 870 |
|
| my $find_median_average = ' |
| 871 |
|
| PDL_Indx nn1 = nn/2, nn2 = nn1+1; |
| 872 |
|
| if (nn%2==0) { |
| 873 |
|
| $b() = $tmp(n => nn1); |
| 874 |
|
| } |
| 875 |
|
| else { |
| 876 |
|
| $b() = 0.5*( $tmp(n => nn1) + $tmp(n => nn2) ); |
| 877 |
|
| }'; |
| 878 |
|
| pp_def('medover', |
| 879 |
|
| HandleBad => 1, |
| 880 |
|
| Pars => 'a(n); [o]b(); [t]tmp(n);', |
| 881 |
|
| Doc => projectdocs('median','medover',''), |
| 882 |
|
| CHeader => 'PDL_TYPELIST_REAL(PDL_QSORT)', |
| 883 |
|
| Code => $copy_to_temp . $find_median_average . '}', |
| 884 |
|
| ); # pp_def: medover |
| 885 |
|
| |
| 886 |
|
| my $find_median_lower = ' |
| 887 |
|
| PDL_Indx nn1 = nn/2; |
| 888 |
|
| $b() = $tmp(n => nn1);'; |
| 889 |
|
| pp_def('oddmedover', |
| 890 |
|
| HandleBad => 1, |
| 891 |
|
| Pars => 'a(n); [o]b(); [t]tmp(n);', |
| 892 |
|
| Doc => projectdocs('oddmedian','oddmedover',' |
| 893 |
|
| |
| 894 |
|
| The median is sometimes not a good choice as if the array has |
| 895 |
|
| an even number of elements it lies half-way between the two |
| 896 |
|
| middle values - thus it does not always correspond to a data |
| 897 |
|
| value. The lower-odd median is just the lower of these two values |
| 898 |
|
| and so it ALWAYS sits on an actual data value which is useful in |
| 899 |
|
| some circumstances. |
| 900 |
|
| '), |
| 901 |
|
| CHeader => 'PDL_TYPELIST_REAL(PDL_QSORT)', |
| 902 |
|
| Code => $copy_to_temp . $find_median_lower . '}', |
| 903 |
|
| ); # pp_def: oddmedover |
| 904 |
|
| |
| 905 |
|
| pp_def('modeover', |
| 906 |
|
| HandleBad=>undef, |
| 907 |
|
| Pars => 'data(n); [o]out(); [t]sorted(n);', |
| 908 |
|
| GenericTypes=>$T, |
| 909 |
|
| Doc=>projectdocs('mode','modeover',' |
| 910 |
|
| The mode is the single element most frequently found in a |
| 911 |
|
| discrete data set. |
| 912 |
|
| |
| 913 |
|
| It I<only> makes sense for integer data types, since |
| 914 |
|
| floating-point types are demoted to integer before the |
| 915 |
|
| mode is calculated. |
| 916 |
|
| |
| 917 |
|
| C<modeover> treats BAD the same as any other value: if |
| 918 |
|
| BAD is the most common element, the returned value is also BAD. |
| 919 |
|
| '), |
| 920 |
|
| CHeader => 'PDL_TYPELIST_INTEGER(PDL_QSORT)', |
| 921 |
|
| Code => <<'EOCODE', |
| 922 |
|
| PDL_Indx i = 0; |
| 923 |
|
| PDL_Indx most = 0; |
| 924 |
|
| $GENERIC() curmode = 0; |
| 925 |
|
| $GENERIC() curval = 0; |
| 926 |
|
| |
| 927 |
|
| /* Copy input to buffer for sorting, and sort it */ |
| 928 |
|
| loop(n) %{ $sorted() = $data(); %} |
| 929 |
|
| qsort_$PPSYM()($P(sorted),0,$SIZE(n)-1); |
| 930 |
|
| |
| 931 |
|
| /* Walk through the sorted data and find the most common elemen */ |
| 932 |
|
| loop(n) %{ |
| 933 |
|
| if( n==0 || curval != $sorted() ) { |
| 934 |
|
| curval = $sorted(); |
| 935 |
|
| i=0; |
| 936 |
|
| } else { |
| 937 |
|
| i++; |
| 938 |
|
| if(i>most){ |
| 939 |
|
| most=i; |
| 940 |
|
| curmode = curval; |
| 941 |
|
| } |
| 942 |
|
| } |
| 943 |
|
| %} |
| 944 |
|
| $out() = curmode; |
| 945 |
|
| EOCODE |
| 946 |
|
| ); |
| 947 |
|
| |
| 948 |
|
| |
| 949 |
|
| my $find_pct_interpolate = ' |
| 950 |
|
| double np, pp1, pp2; |
| 951 |
|
| np = nn * $p(); |
| 952 |
|
| PDL_Indx nn1 = PDLMIN(nn,PDLMAX(0,np)); |
| 953 |
|
| PDL_Indx nn2 = PDLMIN(nn,PDLMAX(0,np+1)); |
| 954 |
|
| if (nn == 0) { |
| 955 |
|
| pp1 = 0; |
| 956 |
|
| pp2 = 0; |
| 957 |
|
| } else { |
| 958 |
|
| pp1 = (double)nn1/(double)(nn); |
| 959 |
|
| pp2 = (double)nn2/(double)(nn); |
| 960 |
|
| } |
| 961 |
|
| if ( np <= 0.0 ) { |
| 962 |
|
| $b() = $tmp(n => 0); |
| 963 |
|
| } else if ( np >= nn ) { |
| 964 |
|
| $b() = $tmp(n => nn); |
| 965 |
|
| } else if ($tmp(n => nn2) == $tmp(n => nn1)) { |
| 966 |
|
| $b() = $tmp(n => nn1); |
| 967 |
|
| } else if ($p() == pp1) { |
| 968 |
|
| $b() = $tmp(n => nn1); |
| 969 |
|
| } else if ($p() == pp2) { |
| 970 |
|
| $b() = $tmp(n => nn2); |
| 971 |
|
| } else { |
| 972 |
|
| $b() = (np - nn1)*($tmp(n => nn2) - $tmp(n => nn1)) + $tmp(n => nn1); |
| 973 |
|
| } |
| 974 |
|
| '; |
| 975 |
|
| pp_def('pctover', |
| 976 |
|
| HandleBad => 1, |
| 977 |
|
| Pars => 'a(n); p(); [o]b(); [t]tmp(n);', |
| 978 |
|
| Doc => projectdocs('specified percentile', 'pctover', |
| 979 |
|
| 'The specified |
| 980 |
|
| percentile must be between 0.0 and 1.0. When the specified percentile |
| 981 |
|
| falls between data points, the result is interpolated. Values outside |
| 982 |
|
| the allowed range are clipped to 0.0 or 1.0 respectively. The algorithm |
| 983 |
|
| implemented here is based on the interpolation variant described at |
| 984 |
|
| L<http://en.wikipedia.org/wiki/Percentile> as used by Microsoft Excel |
| 985 |
|
| and recommended by NIST. |
| 986 |
|
| '), |
| 987 |
|
| CHeader => 'PDL_TYPELIST_REAL(PDL_QSORT)', |
| 988 |
|
| Code => $copy_to_temp . $find_pct_interpolate . '}', |
| 989 |
|
| ); |
| 990 |
|
| |
| 991 |
|
| pp_def('oddpctover', |
| 992 |
|
| HandleBad => 1, |
| 993 |
|
| Pars => 'a(n); p(); [o]b(); [t]tmp(n);', |
| 994 |
|
| Doc => projectdocs('specified percentile', 'oddpctover', |
| 995 |
|
| 'The specified |
| 996 |
|
| percentile must be between 0.0 and 1.0. When the specified percentile |
| 997 |
|
| falls between two values, the nearest data value is the result. |
| 998 |
|
| The algorithm implemented is from the textbook version described |
| 999 |
|
| first at L<http://en.wikipedia.org/wiki/Percentile>. |
| 1000 |
|
| '), |
| 1001 |
|
| CHeader => 'PDL_TYPELIST_REAL(PDL_QSORT)', |
| 1002 |
|
| Code => |
| 1003 |
|
| $copy_to_temp . ' |
| 1004 |
|
| PDL_Indx np = PDLMAX(0,PDLMIN(nn,(nn+1)*$p())); |
| 1005 |
|
| $b() = $tmp(n => np); |
| 1006 |
|
| }', |
| 1007 |
|
| ); |
| 1008 |
|
| |
| 1009 |
|
| for ( |
| 1010 |
|
| ['','result is interpolated'], |
| 1011 |
|
| ['odd','nearest data value is the result'], |
| 1012 |
|
| ) { |
| 1013 |
|
| pp_add_exported('', $_->[0].'pct'); |
| 1014 |
|
| pp_addpm(<<EOD); |
| 1015 |
|
| =head2 $_->[0]pct |
| 1016 |
|
| |
| 1017 |
|
| =for ref |
| 1018 |
|
| |
| 1019 |
|
| Return the specified percentile of all elements in an ndarray. The |
| 1020 |
|
| specified percentile (p) must be between 0.0 and 1.0. When the |
| 1021 |
|
| specified percentile falls between data points, the $_->[1]. |
| 1022 |
|
| |
| 1023 |
|
| =for usage |
| 1024 |
|
| |
| 1025 |
|
| \$x = $_->[0]pct(\$data, \$pct); |
| 1026 |
|
| |
| 1027 |
|
| =cut |
| 1028 |
|
| |
| 1029 |
|
| *$_->[0]pct = \\&PDL::$_->[0]pct; |
| 1030 |
|
| sub PDL::$_->[0]pct { |
| 1031 |
|
| my(\$x, \$p) = \@_; |
| 1032 |
|
| \$x->flat->$_->[0]pctover(\$p, my \$tmp=PDL->nullcreate(\$x)); |
| 1033 |
|
| \$tmp; |
| 1034 |
|
| } |
| 1035 |
|
| EOD |
| 1036 |
|
| } |
| 1037 |
|
| |
| 1038 |
|
| sub qsort_returnempty { 'if ($PDL(a)->nvals == 0) return PDL_err;' } |
| 1039 |
|
| |
| 1040 |
|
| pp_def('qsort', |
| 1041 |
|
| HandleBad => 1, |
| 1042 |
|
| Inplace => 1, |
| 1043 |
|
| Pars => '!complex a(n); !complex [o]b(n);', |
| 1044 |
|
| CHeader => 'PDL_TYPELIST_REAL(PDL_QSORT)', |
| 1045 |
|
| Code => PDL::PP::pp_line_numbers(__LINE__, ' |
| 1046 |
|
| register PDL_Indx nn = PDL_IF_BAD(0,$SIZE(n)); |
| 1047 |
|
| char is_inplace = ($P(a) == $P(b)); |
| 1048 |
|
| PDL_IF_BAD(register PDL_Indx nb = $SIZE(n) - 1;,) |
| 1049 |
|
| ').qsort_returnempty().' |
| 1050 |
|
| PDL_IF_BAD( |
| 1051 |
|
| loop(n) %{ |
| 1052 |
|
| if ($ISGOOD(a())) { |
| 1053 |
|
| if (!is_inplace) $b(n=>nn) = $a(); |
| 1054 |
|
| nn++; |
| 1055 |
|
| continue; |
| 1056 |
|
| } |
| 1057 |
|
| while (is_inplace && $ISBAD(b(n=>nb)) && nb > n) nb--; |
| 1058 |
|
| if (nb > n && is_inplace) { $a() = $b(n=>nb); nn++; } |
| 1059 |
|
| if (nb > n || !is_inplace) { $SETBAD(b(n=>nb)); nb--; } |
| 1060 |
|
| if (nb < n) break; |
| 1061 |
|
| %} |
| 1062 |
|
| , |
| 1063 |
|
| if (!is_inplace) loop(n) %{ $b() = $a(); %} |
| 1064 |
|
| ) |
| 1065 |
|
| if ( nn == 0 ) continue; nn -= 1; |
| 1066 |
|
| qsort_$PPSYM() ($P(b), 0, nn);', |
| 1067 |
|
| Doc => 'Quicksort a vector into ascending order.', |
| 1068 |
|
| BadDoc => |
| 1069 |
|
| ' |
| 1070 |
|
| Bad values are moved to the end of the array: |
| 1071 |
|
| |
| 1072 |
|
| pdl> p $y |
| 1073 |
|
| [42 47 98 BAD 22 96 74 41 79 76 96 BAD 32 76 25 59 BAD 96 32 BAD] |
| 1074 |
|
| pdl> p qsort($y) |
| 1075 |
|
| [22 25 32 32 41 42 47 59 74 76 76 79 96 96 96 98 BAD BAD BAD BAD] |
| 1076 |
|
| ', |
| 1077 |
|
| ); |
| 1078 |
|
| |
| 1079 |
|
| pp_def('qsorti', |
| 1080 |
|
| HandleBad => 1, |
| 1081 |
|
| Pars => '!complex a(n); indx [o]indx(n);', |
| 1082 |
|
| CHeader => $chdr_qsorti, |
| 1083 |
|
| Code => PDL::PP::pp_line_numbers(__LINE__, ' |
| 1084 |
|
| register PDL_Indx nn = PDL_IF_BAD(0,$SIZE(n)-1), nb = $SIZE(n) - 1; (void)nb; |
| 1085 |
|
| ').qsort_returnempty().' |
| 1086 |
|
| loop(n) %{ |
| 1087 |
|
| PDL_IF_BAD(if ($ISBAD(a())) { $indx(n=>nb) = n; nb--; } |
| 1088 |
|
| else { $indx(n=>nn) = n; nn++; } /* play safe since nn used more than once */ |
| 1089 |
|
| ,$indx() = n;) |
| 1090 |
|
| %} |
| 1091 |
|
| PDL_IF_BAD(if ( nn == 0 ) continue; nn -= 1;,) |
| 1092 |
|
| qsort_ind_$PPSYM() ($P(a), $P(indx), 0, nn);', |
| 1093 |
|
| BadDoc => |
| 1094 |
|
| 'Bad elements are moved to the end of the array: |
| 1095 |
|
| |
| 1096 |
|
| pdl> p $y |
| 1097 |
|
| [42 47 98 BAD 22 96 74 41 79 76 96 BAD 32 76 25 59 BAD 96 32 BAD] |
| 1098 |
|
| pdl> p $y->index( qsorti($y) ) |
| 1099 |
|
| [22 25 32 32 41 42 47 59 74 76 76 79 96 96 96 98 BAD BAD BAD BAD] |
| 1100 |
|
| ', |
| 1101 |
|
| Doc => ' |
| 1102 |
|
| =for ref |
| 1103 |
|
| |
| 1104 |
|
| Quicksort a vector and return index of elements in ascending order. |
| 1105 |
|
| |
| 1106 |
|
| =for example |
| 1107 |
|
| |
| 1108 |
|
| $ix = qsorti $x; |
| 1109 |
|
| print $x->index($ix); # Sorted list |
| 1110 |
|
| ' |
| 1111 |
|
| ); |
| 1112 |
|
| |
| 1113 |
|
| # move all bad values to the end of the array |
| 1114 |
|
| # |
| 1115 |
|
| pp_def('qsortvec', |
| 1116 |
|
| HandleBad => 1, |
| 1117 |
|
| Inplace => 1, |
| 1118 |
|
| Pars => '!complex a(n,m); !complex [o]b(n,m);', |
| 1119 |
|
| CHeader => $chdr_qsortvec_def . $chdr_qsortvec, |
| 1120 |
|
| Code => ' |
| 1121 |
|
| register PDL_Indx nn = PDL_IF_BAD(0,$SIZE(m)-1); |
| 1122 |
|
| char is_inplace = ($P(a) == $P(b)); |
| 1123 |
|
| '.qsort_returnempty().' |
| 1124 |
|
| PDL_IF_BAD(register PDL_Indx nb = $SIZE(m) - 1; loop(m) %{ |
| 1125 |
|
| char allgood_a = 1; |
| 1126 |
|
| loop(n) %{ if ( $ISBAD(a()) ) { allgood_a = 0; break; } %} |
| 1127 |
|
| PDL_Indx copy_dest = allgood_a ? nn++ : nb--; |
| 1128 |
|
| if (is_inplace) { |
| 1129 |
|
| if (allgood_a) continue; /* nothing to do */ |
| 1130 |
|
| char anybad_b = 0; |
| 1131 |
|
| do { |
| 1132 |
|
| anybad_b = 0; |
| 1133 |
|
| loop(n) %{ if ($ISBAD(b(m=>copy_dest))) { anybad_b = 1; break; } %} |
| 1134 |
|
| if (anybad_b) copy_dest = nb--; |
| 1135 |
|
| } while (anybad_b); |
| 1136 |
|
| if (m != copy_dest) |
| 1137 |
|
| loop(n) %{ |
| 1138 |
|
| /* as in-place we know same badval source and dest */ |
| 1139 |
|
| $GENERIC() tmp = $b(m=>copy_dest); |
| 1140 |
|
| $b(m=>copy_dest) = $a(); |
| 1141 |
|
| $a() = tmp; |
| 1142 |
|
| %} |
| 1143 |
|
| if (m >= nb-1) { nn = nb+1; break; } /* run out of "good" vectors */ |
| 1144 |
|
| } else { |
| 1145 |
|
| loop(n) %{ |
| 1146 |
|
| if ($ISBAD(a())) $SETBAD(b(m=>copy_dest)); |
| 1147 |
|
| else $b(m=>copy_dest) = $a(); |
| 1148 |
|
| %} |
| 1149 |
|
| } |
| 1150 |
|
| %} |
| 1151 |
|
| if ( nn == 0 ) continue; nn -= 1;, |
| 1152 |
|
| if (!is_inplace) { loop(n,m) %{ $b() = $a(); %} } |
| 1153 |
|
| ) |
| 1154 |
|
| pdl_qsortvec_$PPSYM() ($P(b), $SIZE(n), 0, nn);', |
| 1155 |
|
| Doc => ' |
| 1156 |
|
| =for ref |
| 1157 |
|
| |
| 1158 |
|
| Sort a list of vectors lexicographically. |
| 1159 |
|
| |
| 1160 |
|
| The 0th dimension of the source ndarray is dimension in the vector; |
| 1161 |
|
| the 1st dimension is list order. Higher dimensions are broadcasted over. |
| 1162 |
|
| |
| 1163 |
|
| =for example |
| 1164 |
|
| |
| 1165 |
|
| print qsortvec pdl([[1,2],[0,500],[2,3],[4,2],[3,4],[3,5]]); |
| 1166 |
|
| [ |
| 1167 |
|
| [ 0 500] |
| 1168 |
|
| [ 1 2] |
| 1169 |
|
| [ 2 3] |
| 1170 |
|
| [ 3 4] |
| 1171 |
|
| [ 3 5] |
| 1172 |
|
| [ 4 2] |
| 1173 |
|
| ] |
| 1174 |
|
| |
| 1175 |
|
| =cut |
| 1176 |
|
| ', |
| 1177 |
|
| BadDoc => ' |
| 1178 |
|
| Vectors with bad components are moved to the end of the array: |
| 1179 |
|
| |
| 1180 |
|
| pdl> p $p = pdl("[0 0] [-100 0] [BAD 0] [100 0]")->qsortvec |
| 1181 |
|
| |
| 1182 |
|
| [ |
| 1183 |
|
| [-100 0] |
| 1184 |
|
| [ 0 0] |
| 1185 |
|
| [ 100 0] |
| 1186 |
|
| [ BAD 0] |
| 1187 |
|
| ] |
| 1188 |
|
| ', |
| 1189 |
|
| ); |
| 1190 |
|
| |
| 1191 |
|
| pp_def('qsortveci', |
| 1192 |
|
| HandleBad => 1, |
| 1193 |
|
| Pars => '!complex a(n,m); indx [o]indx(m);', |
| 1194 |
|
| CHeader => $chdr_qsortvec_def . $chdr_qsortvec_ind, |
| 1195 |
|
| Code => |
| 1196 |
|
| 'register PDL_Indx nn = PDL_IF_BAD(0,$SIZE(m)-1); |
| 1197 |
|
| PDL_IF_BAD(register PDL_Indx nb = $SIZE(m) - 1;,) |
| 1198 |
|
| '.qsort_returnempty().' |
| 1199 |
|
| loop(m) %{ |
| 1200 |
|
| PDL_IF_BAD( |
| 1201 |
|
| char allgood_a = 1; |
| 1202 |
|
| loop(n) %{ if ( $ISBAD(a()) ) { allgood_a = 0; break; } %} |
| 1203 |
|
| PDL_Indx copy_dest = allgood_a ? nn++ : nb--; |
| 1204 |
|
| $indx(m=>copy_dest) = m; |
| 1205 |
|
| , |
| 1206 |
|
| $indx()=m; |
| 1207 |
|
| ) |
| 1208 |
|
| %} |
| 1209 |
|
| PDL_IF_BAD(if ( nn == 0 ) continue; nn -= 1;,) |
| 1210 |
|
| pdl_qsortvec_ind_$PPSYM() ($P(a), $P(indx), $SIZE(n), 0, nn);', |
| 1211 |
|
| Doc => ' |
| 1212 |
|
| =for ref |
| 1213 |
|
| |
| 1214 |
|
| Sort a list of vectors lexicographically, returning the indices of the |
| 1215 |
|
| sorted vectors rather than the sorted list itself. |
| 1216 |
|
| |
| 1217 |
|
| As with C<qsortvec>, the input PDL should be an NxM array containing M |
| 1218 |
|
| separate N-dimensional vectors. The return value is an integer M-PDL |
| 1219 |
|
| containing the M-indices of original array rows, in sorted order. |
| 1220 |
|
| |
| 1221 |
|
| As with C<qsortvec>, the zeroth element of the vectors runs slowest in the |
| 1222 |
|
| sorted list. |
| 1223 |
|
| |
| 1224 |
|
| Additional dimensions are broadcasted over: each plane is sorted separately, |
| 1225 |
|
| so qsortveci may be thought of as a collapse operator of sorts (groan). |
| 1226 |
|
| |
| 1227 |
|
| =cut |
| 1228 |
|
| ', |
| 1229 |
|
| BadDoc => ' |
| 1230 |
|
| Vectors with bad components are moved to the end of the array as |
| 1231 |
|
| for L</qsortvec>. |
| 1232 |
|
| ', |
| 1233 |
|
| ); |
| 1234 |
|
| |
| 1235 |
|
| pp_def('magnover', |
| 1236 |
|
| HandleBad => 1, |
| 1237 |
|
| Pars => "a(n); real [o]b();", |
| 1238 |
|
| GenericTypes => [(grep $_ ne 'F', @$AF), 'F'], # F last for back-compat |
| 1239 |
|
| Code => <<'EOF', |
| 1240 |
|
| long double sum=0; |
| 1241 |
|
| PDL_IF_BAD(int flag = 0;,) |
| 1242 |
|
| loop(n) %{ |
| 1243 |
|
| PDL_IF_BAD(if ($ISBAD(a())) continue; flag = 1;,) |
| 1244 |
|
| sum += PDL_IF_GENTYPE_REAL( |
| 1245 |
|
| $a()*$a(), |
| 1246 |
|
| creall($a())*creall($a()) + cimagl($a())*cimagl($a()) |
| 1247 |
|
| ); |
| 1248 |
|
| %} |
| 1249 |
|
| PDL_IF_BAD(if ( !flag ) { $SETBAD(b()); continue; },) |
| 1250 |
|
| $b() = sum == 0 ? 0 : sqrtl(sum); |
| 1251 |
|
| EOF |
| 1252 |
|
| Doc => projectdocs( 'Euclidean (aka Pythagorean) distance', 'magnover', <<'EOF' ), |
| 1253 |
|
| Minimum C<float> precision output. |
| 1254 |
|
| See also L<PDL::Primitive/inner>, L<PDL::Primitive/norm>. |
| 1255 |
|
| EOF |
| 1256 |
|
| ); |
| 1257 |
|
| |
| 1258 |
|
| pp_addpm({At=>'Bot'},<<'EOD'); |
| 1259 |
|
| |
| 1260 |
|
| =head1 AUTHOR |
| 1261 |
|
| |
| 1262 |
|
| Copyright (C) Tuomas J. Lukka 1997 (lukka@husc.harvard.edu). |
| 1263 |
|
| Contributions by Christian Soeller (c.soeller@auckland.ac.nz) |
| 1264 |
|
| and Karl Glazebrook (kgb@aaoepp.aao.gov.au). All rights |
| 1265 |
|
| reserved. There is no warranty. You are allowed to redistribute this |
| 1266 |
|
| software / documentation under certain conditions. For details, see |
| 1267 |
|
| the file COPYING in the PDL distribution. If this file is separated |
| 1268 |
|
| from the PDL distribution, the copyright notice should be included in |
| 1269 |
|
| the file. |
| 1270 |
|
| |
| 1271 |
|
| =cut |
| 1272 |
|
| EOD |
| 1273 |
|
| |
| 1274 |
|
| pp_done(); |