| 1 | # | ||
| 2 | # GENERATED WITH PDL::PP from ccsutils.pd! Don't modify! | ||
| 3 | # | ||
| 4 | package PDL::CCS::Utils; | ||
| 5 | |||
| 6 | 16 | our @EXPORT_OK = qw(nnz nnza ccs_encode_pointers ccs_decode_pointer ccs_xindex1d ccs_xindex2d ccs_dump_which ); | |
| 7 | 16 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); | |
| 8 | |||
| 9 | 12 | ▶ | use PDL::Core; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 12 | ▶ | use PDL::Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 12 | ▶ | use DynaLoader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | |||
| 14 | 16 | our $VERSION = '1.24.1'; | |
| 15 | 16 | our @ISA = ( 'PDL::Exporter','DynaLoader' ); | |
| 16 | 21 | push @PDL::Core::PP, __PACKAGE__; | |
| 17 | 21 | bootstrap PDL::CCS::Utils $VERSION; | |
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #line 13 "ccsutils.pd" | ||
| 27 | |||
| 28 | #use PDL::CCS::Config; | ||
| 29 | use strict; | ||
| 30 | |||
| 31 | =pod | ||
| 32 | |||
| 33 | =head1 NAME | ||
| 34 | |||
| 35 | PDL::CCS::Utils - Low-level utilities for compressed storage sparse PDLs | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | use PDL; | ||
| 40 | use PDL::CCS::Utils; | ||
| 41 | |||
| 42 | ##--------------------------------------------------------------------- | ||
| 43 | ## ... stuff happens | ||
| 44 | |||
| 45 | =cut | ||
| 46 | #line 47 "Utils.pm" | ||
| 47 | |||
| 48 | |||
| 49 | =head1 FUNCTIONS | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | #line 52 "ccsutils.pd" | ||
| 58 | |||
| 59 | *ccs_indx = \&PDL::indx; ##-- typecasting for CCS indices (deprecated) | ||
| 60 | |||
| 61 | #line 70 "ccsutils.pd" | ||
| 62 | |||
| 63 | =pod | ||
| 64 | |||
| 65 | =head1 Non-missing Value Counts | ||
| 66 | |||
| 67 | =cut | ||
| 68 | #line 69 "Utils.pm" | ||
| 69 | |||
| 70 | |||
| 71 | =head2 nnz | ||
| 72 | |||
| 73 | =for sig | ||
| 74 | |||
| 75 | Signature: (a(N); indx [o]nnz()) | ||
| 76 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 77 | float double ldouble) | ||
| 78 | |||
| 79 | =for usage | ||
| 80 | |||
| 81 | $nnz = nnz($a); | ||
| 82 | nnz($a, $nnz); # all arguments given | ||
| 83 | $nnz = $a->nnz; # method call | ||
| 84 | $a->nnz($nnz); | ||
| 85 | |||
| 86 | Get number of non-zero values in a PDL $a(); | ||
| 87 | For 1d PDLs, should be equivalent to: | ||
| 88 | |||
| 89 | $nnz = nelem(which($a!=0)); | ||
| 90 | |||
| 91 | For k>1 dimensional PDLs, projects via number of nonzero elements | ||
| 92 | to N-1 dimensions by computing the number of nonzero elements | ||
| 93 | along the the 1st dimension. | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | Broadcasts over its inputs. | ||
| 98 | |||
| 99 | =for bad | ||
| 100 | |||
| 101 | The output PDL $nnz() never contains BAD values. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | *nnz = \&PDL::nnz; | ||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | =head2 nnza | ||
| 116 | |||
| 117 | =for sig | ||
| 118 | |||
| 119 | Signature: (a(N); eps(); indx [o]nnz()) | ||
| 120 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 121 | float double ldouble) | ||
| 122 | |||
| 123 | =for usage | ||
| 124 | |||
| 125 | $nnz = nnza($a, $eps); | ||
| 126 | nnza($a, $eps, $nnz); # all arguments given | ||
| 127 | $nnz = $a->nnza($eps); # method call | ||
| 128 | $a->nnza($eps, $nnz); | ||
| 129 | |||
| 130 | Like nnz() using tolerance constant $eps(). | ||
| 131 | For 1d PDLs, should be equivalent to: | ||
| 132 | |||
| 133 | $nnz = nelem(which(!$a->approx(0,$eps))); | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | Broadcasts over its inputs. | ||
| 138 | |||
| 139 | =for bad | ||
| 140 | |||
| 141 | The output PDL $nnz() never contains BAD values. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | *nnza = \&PDL::nnza; | ||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | #line 156 "ccsutils.pd" | ||
| 155 | |||
| 156 | =pod | ||
| 157 | |||
| 158 | =head1 Encoding Utilities | ||
| 159 | |||
| 160 | =cut | ||
| 161 | #line 162 "Utils.pm" | ||
| 162 | |||
| 163 | |||
| 164 | =head2 ccs_encode_pointers | ||
| 165 | |||
| 166 | =for sig | ||
| 167 | |||
| 168 | Signature: (indx ix(Nnz); indx N(); indx [o]ptr(Nplus1); indx [o]ixix(Nnz)) | ||
| 169 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 170 | float double ldouble) | ||
| 171 | |||
| 172 | =for usage | ||
| 173 | |||
| 174 | ($ptr, $ixix) = ccs_encode_pointers($ix, $N); | ||
| 175 | ccs_encode_pointers($ix, $N, $ptr, $ixix); # all arguments given | ||
| 176 | ($ptr, $ixix) = $ix->ccs_encode_pointers($N); # method call | ||
| 177 | $ix->ccs_encode_pointers($N, $ptr, $ixix); | ||
| 178 | |||
| 179 | General CCS encoding utility. | ||
| 180 | |||
| 181 | Get a compressed storage "pointer" vector $ptr | ||
| 182 | for a dimension of size $N with non-missing values at indices $ix. Also returns a vector | ||
| 183 | $ixix() which may be used as an index for $ix() to align its elements with $ptr() | ||
| 184 | along the compressed dimension. | ||
| 185 | |||
| 186 | The induced vector $ix-E<gt>index($ixix) is | ||
| 187 | guaranteed to be stably sorted along dimension $N(): | ||
| 188 | |||
| 189 | \forall $i,$j with 1 <= $i < $j <= $Nnz : | ||
| 190 | |||
| 191 | $ix->index($ixix)->at($i) < $ix->index($ixix)->at($j) ##-- primary sort on $ix() | ||
| 192 | or | ||
| 193 | $ixix->at($i) < $ixix->at($j) ##-- ... stable | ||
| 194 | |||
| 195 | =pod | ||
| 196 | |||
| 197 | Broadcasts over its inputs. | ||
| 198 | |||
| 199 | =for bad | ||
| 200 | |||
| 201 | C<ccs_encode_pointers> does not process bad values. | ||
| 202 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | sub PDL::ccs_encode_pointers { | ||
| 211 | my ($ix,$N,$ptr,$ixix) = @_; | ||
| 212 | barf("Usage: ccs_encode_pointers(ix(Nnz), N(), [o]ptr(N+1), [o]ixix(Nnz)") if (!defined($ix)); | ||
| 213 | &PDL::_ccs_encode_pointers_int($ix, ($N // $ix->max+1), ($ptr //= null), ($ixix //= null)); | ||
| 214 | return ($ptr,$ixix); | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | *ccs_encode_pointers = \&PDL::ccs_encode_pointers; | ||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | #line 239 "ccsutils.pd" | ||
| 226 | |||
| 227 | =pod | ||
| 228 | |||
| 229 | =head1 Decoding Utilities | ||
| 230 | |||
| 231 | =cut | ||
| 232 | #line 233 "Utils.pm" | ||
| 233 | |||
| 234 | |||
| 235 | =head2 ccs_decode_pointer | ||
| 236 | |||
| 237 | =for sig | ||
| 238 | |||
| 239 | Signature: (indx ptr(Nplus1); indx proj(Nproj); indx [o]projix(NnzProj); indx [o]nzix(NnzProj); PDL_Indx nnzProj) | ||
| 240 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 241 | float double ldouble) | ||
| 242 | |||
| 243 | =for usage | ||
| 244 | |||
| 245 | ($projix, $nzix) = ccs_decode_pointer($ptr, $proj, $nnzProj); | ||
| 246 | ccs_decode_pointer($ptr, $proj, $projix, $nzix, $nnzProj); # all arguments given | ||
| 247 | ($projix, $nzix) = $ptr->ccs_decode_pointer($proj, $nnzProj); # method call | ||
| 248 | $ptr->ccs_decode_pointer($proj, $projix, $nzix, $nnzProj); | ||
| 249 | |||
| 250 | General CCS decoding utility. | ||
| 251 | |||
| 252 | Project indices $proj() from a compressed storage "pointer" vector $ptr(). | ||
| 253 | If unspecified, $proj() defaults to: | ||
| 254 | |||
| 255 | sequence($ptr->dim(0) - 1) | ||
| 256 | |||
| 257 | =pod | ||
| 258 | |||
| 259 | Broadcasts over its inputs. | ||
| 260 | |||
| 261 | =for bad | ||
| 262 | |||
| 263 | C<ccs_decode_pointer> does not process bad values. | ||
| 264 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | sub PDL::ccs_decode_pointer { | ||
| 273 | my ($ptr,$proj,$projix,$nzix,$nnzproj) = @_; | ||
| 274 | barf("Usage: ccs_decode_pointer(ptr(N+1), proj(Nproj), [o]projix(NnzProj), [o]nzix(NnzProj), NnzProj?") | ||
| 275 | if (!defined($ptr)); | ||
| 276 | if (!defined($proj)) { | ||
| 277 | $proj = PDL->sequence(ccs_indx(), $ptr->dim(0)-1); | ||
| 278 | $nnzproj //= $ptr->at(-1); | ||
| 279 | } | ||
| 280 | $projix //= null; | ||
| 281 | $nzix //= null; | ||
| 282 | $nnzproj //= ($projix->isnull && $nzix->isnull | ||
| 283 | ? ($ptr->index($proj+1)-$ptr->index($proj))->sum | ||
| 284 | : -1); | ||
| 285 | return (null,null) if (!$nnzproj); | ||
| 286 | &PDL::_ccs_decode_pointer_int($ptr,$proj, $projix,$nzix, $nnzproj); | ||
| 287 | return ($projix,$nzix); | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | *ccs_decode_pointer = \&PDL::ccs_decode_pointer; | ||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | #line 311 "ccsutils.pd" | ||
| 299 | |||
| 300 | =pod | ||
| 301 | |||
| 302 | =head1 Indexing Utilities | ||
| 303 | |||
| 304 | =cut | ||
| 305 | #line 306 "Utils.pm" | ||
| 306 | |||
| 307 | |||
| 308 | =head2 ccs_xindex1d | ||
| 309 | |||
| 310 | =for sig | ||
| 311 | |||
| 312 | Signature: (indx which(Ndims,Nnz); indx a(Na); indx [o]nzia(NnzA); indx [o]nnza(); PDL_Indx sizeNnzA) | ||
| 313 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 314 | float double ldouble) | ||
| 315 | |||
| 316 | =for usage | ||
| 317 | |||
| 318 | ($nzia, $nnza) = ccs_xindex1d($which, $a, $sizeNnzA); | ||
| 319 | ccs_xindex1d($which, $a, $nzia, $nnza, $sizeNnzA); # all arguments given | ||
| 320 | ($nzia, $nnza) = $which->ccs_xindex1d($a, $sizeNnzA); # method call | ||
| 321 | $which->ccs_xindex1d($a, $nzia, $nnza, $sizeNnzA); | ||
| 322 | |||
| 323 | Compute indices $nzai() along dimension C<Nnz> of $which() whose initial values $which(0,$nzai) | ||
| 324 | match some element of $a(). Appropriate for indexing a sparse encoded PDL | ||
| 325 | with non-missing entries at $which() | ||
| 326 | along the 0th dimension, a la L<dice_axis(0,$a)|PDL::Slices/dice_axis>. | ||
| 327 | $which((0),) and $a() must be both sorted in ascending order. | ||
| 328 | |||
| 329 | In list context, returns a list ($nzai,$nnza), where $nnza() is the number of indices found, | ||
| 330 | and $nzai are those C<Nnz> indices. In scalar context, trims the output vector $nzai() to $nnza() | ||
| 331 | elements. | ||
| 332 | |||
| 333 | =pod | ||
| 334 | |||
| 335 | Broadcasts over its inputs. | ||
| 336 | |||
| 337 | =for bad | ||
| 338 | |||
| 339 | C<ccs_xindex1d> does not process bad values. | ||
| 340 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | sub PDL::ccs_xindex1d { | ||
| 349 | my ($which,$a,$nzia,$nnza) = @_; | ||
| 350 | barf("Usage: ccs_xindex2d(which(Ndims,Nnz), a(Na), [o]nzia(NnzA), [o]nnza()") | ||
| 351 | if ((grep {!defined($_)} @_[0..1]) || $which->ndims < 2 || $which->dim(0) < 1); | ||
| 352 | $nzia //= null; | ||
| 353 | $nnza //= null; | ||
| 354 | &PDL::_ccs_xindex1d_int($which,$a,$nzia,$nnza, ($nnza ? $nnza->sclr : -1)); | ||
| 355 | return wantarray ? ($nzia,$nnza) : $nzia->reshape($nnza->sclr); | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | *ccs_xindex1d = \&PDL::ccs_xindex1d; | ||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | =head2 ccs_xindex2d | ||
| 368 | |||
| 369 | =for sig | ||
| 370 | |||
| 371 | Signature: (indx which(Ndims,Nnz); indx a(Na); indx b(Nb); indx [o]ab(Nab); indx [o]nab()) | ||
| 372 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 373 | float double ldouble) | ||
| 374 | |||
| 375 | =for usage | ||
| 376 | |||
| 377 | ($ab, $nab) = ccs_xindex2d($which, $a, $b); | ||
| 378 | ccs_xindex2d($which, $a, $b, $ab, $nab); # all arguments given | ||
| 379 | ($ab, $nab) = $which->ccs_xindex2d($a, $b); # method call | ||
| 380 | $which->ccs_xindex2d($a, $b, $ab, $nab); | ||
| 381 | |||
| 382 | Compute indices along dimension C<NNz> of $which() corresponding to any combination | ||
| 383 | of values in the Cartesian product of $a() and $b(). Appropriate for indexing a | ||
| 384 | 2d sparse encoded PDL with non-missing entries at $which() via the ND-index piddle | ||
| 385 | $a-E<gt>slice("*1,")-E<gt>cat($b)-E<gt>clump(2)-E<gt>xchg(0,1), i.e. all pairs $ai,$bi with $ai in $a() | ||
| 386 | and $bi in $b(). $a() and $b() values must be be sorted in ascending order | ||
| 387 | |||
| 388 | In list context, returns a list ($ab,$nab), where $nab() is the number of indices found, | ||
| 389 | and $ab are those C<Nnz> indices. In scalar context, trims the output vector $ab() to $nab() | ||
| 390 | elements. | ||
| 391 | |||
| 392 | =pod | ||
| 393 | |||
| 394 | Broadcasts over its inputs. | ||
| 395 | |||
| 396 | =for bad | ||
| 397 | |||
| 398 | C<ccs_xindex2d> does not process bad values. | ||
| 399 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 400 | |||
| 401 | =cut | ||
| 402 | |||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | |||
| 407 | sub PDL::ccs_xindex2d { | ||
| 408 | my ($which,$a,$b,$ab,$nab) = @_; | ||
| 409 | barf("Usage: ccs_xindex2d(which(2,Nnz), a(Na), b(Nb), [o]nab(), [o]ab(Nab)") | ||
| 410 | if ((grep {!defined($_)} @_[0..2]) || $which->ndims != 2 || $which->dim(0) < 2); | ||
| 411 | &PDL::_ccs_xindex2d_int($which,$a,$b, ($ab//=null), ($nab//=null)); | ||
| 412 | return wantarray ? ($ab,$nab) : $ab->reshape($nab->sclr); | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | *ccs_xindex2d = \&PDL::ccs_xindex2d; | ||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | |||
| 423 | #line 487 "ccsutils.pd" | ||
| 424 | |||
| 425 | =pod | ||
| 426 | |||
| 427 | =head1 Debugging Utilities | ||
| 428 | |||
| 429 | =cut | ||
| 430 | #line 431 "Utils.pm" | ||
| 431 | |||
| 432 | |||
| 433 | =head2 ccs_dump_which | ||
| 434 | |||
| 435 | =for sig | ||
| 436 | |||
| 437 | Signature: (indx which(Ndims,Nnz); SV *HANDLE; char *fmt; char *fsep; char *rsep) | ||
| 438 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 439 | float double ldouble) | ||
| 440 | |||
| 441 | =for usage | ||
| 442 | |||
| 443 | ccs_dump_which($which, $HANDLE, $fmt, $fsep, $rsep); # all arguments given | ||
| 444 | $which->ccs_dump_which($HANDLE, $fmt, $fsep, $rsep); # method call | ||
| 445 | |||
| 446 | Print a text dump of an index PDL to the filehandle C<HANDLE>, which default to C<STDUT>. | ||
| 447 | C<$fmt> is a printf() format to use for output, which defaults to "%td". | ||
| 448 | C<$fsep> and C<$rsep> are field-and record separators, which default to | ||
| 449 | a single space and C<$/>, respectively. | ||
| 450 | |||
| 451 | =pod | ||
| 452 | |||
| 453 | Broadcasts over its inputs. | ||
| 454 | |||
| 455 | =for bad | ||
| 456 | |||
| 457 | C<ccs_dump_which> does not process bad values. | ||
| 458 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | |||
| 465 | |||
| 466 | sub PDL::ccs_dump_which { | ||
| 467 | my ($which,$fh,$fmt,$fsep,$rsep) = @_; | ||
| 468 | $fmt = '%td' if (!defined($fmt) || $fmt eq ''); | ||
| 469 | $fsep = " " if (!defined($fsep) || $fsep eq ''); | ||
| 470 | $rsep = "$/" if (!defined($rsep) || $rsep eq ''); | ||
| 471 | $fh = \*STDOUT if (!defined($fh)); | ||
| 472 | &PDL::_ccs_dump_which_int($which,$fh,$fmt,$fsep,$rsep); | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | |||
| 477 | *ccs_dump_which = \&PDL::ccs_dump_which; | ||
| 478 | |||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | |||
| 483 | #line 558 "ccsutils.pd" | ||
| 484 | |||
| 485 | ##--------------------------------------------------------------------- | ||
| 486 | =pod | ||
| 487 | |||
| 488 | =head1 ACKNOWLEDGEMENTS | ||
| 489 | |||
| 490 | Perl by Larry Wall. | ||
| 491 | |||
| 492 | PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others. | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | ##---------------------------------------------------------------------- | ||
| 497 | =pod | ||
| 498 | |||
| 499 | =head1 KNOWN BUGS | ||
| 500 | |||
| 501 | Probably many. | ||
| 502 | |||
| 503 | =cut | ||
| 504 | |||
| 505 | ##--------------------------------------------------------------------- | ||
| 506 | =pod | ||
| 507 | |||
| 508 | =head1 AUTHOR | ||
| 509 | |||
| 510 | Bryan Jurish E<lt>moocow@cpan.orgE<gt> | ||
| 511 | |||
| 512 | =head2 Copyright Policy | ||
| 513 | |||
| 514 | Copyright (C) 2007-2024, Bryan Jurish. All rights reserved. | ||
| 515 | |||
| 516 | This package is free software, and entirely without warranty. | ||
| 517 | You may redistribute it and/or modify it under the same terms | ||
| 518 | as Perl itself. | ||
| 519 | |||
| 520 | =head1 SEE ALSO | ||
| 521 | |||
| 522 | perl(1), PDL(3perl) | ||
| 523 | |||
| 524 | =cut | ||
| 525 | #line 526 "Utils.pm" | ||
| 526 | |||
| 527 | # Exit with OK status | ||
| 528 | |||
| 529 | 1; | ||