| 1 | # | ||
| 2 | # GENERATED WITH PDL::PP from utils.pd! Don't modify! | ||
| 3 | # | ||
| 4 | package PDL::VectorValued::Utils; | ||
| 5 | |||
| 6 | 6 | our @EXPORT_OK = qw(vv_rlevec vv_rldvec vv_enumvec vv_enumvecg vv_rleseq vv_rldseq vv_vsearchvec vv_cmpvec vv_qsortvec vv_qsortveci vv_union vv_intersect vv_setdiff v_union v_intersect v_setdiff vv_vcos ); | |
| 7 | 6 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); | |
| 8 | |||
| 9 | 6 | ▶ | use PDL::Core; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 6 | ▶ | use PDL::Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 6 | ▶ | use DynaLoader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | |||
| 14 | 6 | our $VERSION = '1.0.23'; | |
| 15 | 6 | our @ISA = ( 'PDL::Exporter','DynaLoader' ); | |
| 16 | 6 | push @PDL::Core::PP, __PACKAGE__; | |
| 17 | 6 | bootstrap PDL::VectorValued::Utils $VERSION; | |
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #line 21 "utils.pd" | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | |||
| 30 | =pod | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | PDL::VectorValued::Utils - Low-level utilities for vector-valued PDLs | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use PDL; | ||
| 39 | use PDL::VectorValued::Utils; | ||
| 40 | |||
| 41 | ##--------------------------------------------------------------------- | ||
| 42 | ## ... stuff happens | ||
| 43 | |||
| 44 | =cut | ||
| 45 | #line 46 "Utils.pm" | ||
| 46 | |||
| 47 | |||
| 48 | =head1 FUNCTIONS | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | #line 67 "utils.pd" | ||
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | =head1 Vector-Based Run-Length Encoding and Decoding | ||
| 61 | |||
| 62 | =cut | ||
| 63 | #line 64 "Utils.pm" | ||
| 64 | |||
| 65 | |||
| 66 | =head2 vv_rlevec | ||
| 67 | |||
| 68 | =for sig | ||
| 69 | |||
| 70 | Signature: (c(M,N); indx [o]a(N); [o]b(M,N)) | ||
| 71 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 72 | float double ldouble) | ||
| 73 | |||
| 74 | =for usage | ||
| 75 | |||
| 76 | ($a, $b) = vv_rlevec($c); | ||
| 77 | vv_rlevec($c, $a, $b); # all arguments given | ||
| 78 | ($a, $b) = $c->vv_rlevec; # method call | ||
| 79 | $c->vv_rlevec($a, $b); | ||
| 80 | |||
| 81 | Run-length encode a set of vectors. | ||
| 82 | |||
| 83 | Higher-order rle(), for use with qsortvec(). | ||
| 84 | |||
| 85 | Given set of vectors $c, generate a vector $a with the number of occurrences of each element | ||
| 86 | (where an "element" is a vector of length $M ocurring in $c), | ||
| 87 | and a set of vectors $b containing the unique values. | ||
| 88 | As for rle(), only the elements up to the first instance of 0 in $a should be considered. | ||
| 89 | |||
| 90 | Can be used together with clump() to run-length encode "values" of arbitrary dimensions. | ||
| 91 | Can be used together with rotate(), cat(), append(), and qsortvec() to count N-grams | ||
| 92 | over a 1d PDL. | ||
| 93 | |||
| 94 | See also: PDL::Slices::rle, PDL::Ufunc::qsortvec, PDL::Primitive::uniqvec | ||
| 95 | |||
| 96 | =pod | ||
| 97 | |||
| 98 | Broadcasts over its inputs. | ||
| 99 | |||
| 100 | =for bad | ||
| 101 | |||
| 102 | C<vv_rlevec> does not process bad values. | ||
| 103 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | *vv_rlevec = \&PDL::vv_rlevec; | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | =head2 vv_rldvec | ||
| 118 | |||
| 119 | =for sig | ||
| 120 | |||
| 121 | Signature: (int a(N); b(M,N); [o]c(M,N)) | ||
| 122 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 123 | float double ldouble) | ||
| 124 | |||
| 125 | =for usage | ||
| 126 | |||
| 127 | $c = vv_rldvec($a, $b); | ||
| 128 | vv_rldvec($a, $b, $c); # all arguments given | ||
| 129 | $c = $a->vv_rldvec($b); # method call | ||
| 130 | $a->vv_rldvec($b, $c); | ||
| 131 | |||
| 132 | Run-length decode a set of vectors, akin to a higher-order rld(). | ||
| 133 | |||
| 134 | Given a vector $a() of the number of occurrences of each row, and a set $c() | ||
| 135 | of row-vectors each of length $M, run-length decode to $c(). | ||
| 136 | |||
| 137 | Can be used together with clump() to run-length decode "values" of arbitrary dimensions. | ||
| 138 | |||
| 139 | See also: PDL::Slices::rld. | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | Broadcasts over its inputs. | ||
| 144 | |||
| 145 | =for bad | ||
| 146 | |||
| 147 | C<vv_rldvec> does not process bad values. | ||
| 148 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | sub PDL::vv_rldvec { | ||
| 156 | my ($a,$b,$c) = @_; | ||
| 157 | if (!defined($c)) { | ||
| 158 | # XXX Need to improve emulation of broadcasting in auto-generating c | ||
| 159 | my ($rowlen) = $b->dim(0); | ||
| 160 | my ($size) = $a->sumover->max; | ||
| 161 | my (@dims) = $a->dims; | ||
| 162 | shift(@dims); | ||
| 163 | $c = $b->zeroes($b->type,$rowlen,$size,@dims); | ||
| 164 | } | ||
| 165 | &PDL::_vv_rldvec_int($a,$b,$c); | ||
| 166 | return $c; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | *vv_rldvec = \&PDL::vv_rldvec; | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | =head2 vv_enumvec | ||
| 179 | |||
| 180 | =for sig | ||
| 181 | |||
| 182 | Signature: (v(M,N); int [o]k(N)) | ||
| 183 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 184 | float double ldouble) | ||
| 185 | |||
| 186 | =for usage | ||
| 187 | |||
| 188 | $k = vv_enumvec($v); | ||
| 189 | vv_enumvec($v, $k); # all arguments given | ||
| 190 | $k = $v->vv_enumvec; # method call | ||
| 191 | $v->vv_enumvec($k); | ||
| 192 | |||
| 193 | Enumerate a list of vectors with locally unique keys. | ||
| 194 | |||
| 195 | Given a sorted list of vectors $v, generate a vector $k containing locally unique keys for the elements of $v | ||
| 196 | (where an "element" is a vector of length $M ocurring in $v). | ||
| 197 | |||
| 198 | Note that the keys returned in $k are only unique over a run of a single vector in $v, | ||
| 199 | so that each unique vector in $v has at least one 0 (zero) index in $k associated with it. | ||
| 200 | If you need global keys, see enumvecg(). | ||
| 201 | |||
| 202 | =pod | ||
| 203 | |||
| 204 | Broadcasts over its inputs. | ||
| 205 | |||
| 206 | =for bad | ||
| 207 | |||
| 208 | C<vv_enumvec> does not process bad values. | ||
| 209 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | *vv_enumvec = \&PDL::vv_enumvec; | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | =head2 vv_enumvecg | ||
| 224 | |||
| 225 | =for sig | ||
| 226 | |||
| 227 | Signature: (v(M,N); int [o]k(N)) | ||
| 228 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 229 | float double ldouble) | ||
| 230 | |||
| 231 | =for usage | ||
| 232 | |||
| 233 | $k = vv_enumvecg($v); | ||
| 234 | vv_enumvecg($v, $k); # all arguments given | ||
| 235 | $k = $v->vv_enumvecg; # method call | ||
| 236 | $v->vv_enumvecg($k); | ||
| 237 | |||
| 238 | Enumerate a list of vectors with globally unique keys. | ||
| 239 | |||
| 240 | Given a sorted list of vectors $v, generate a vector $k containing globally unique keys for the elements of $v | ||
| 241 | (where an "element" is a vector of length $M ocurring in $v). | ||
| 242 | Basically does the same thing as: | ||
| 243 | |||
| 244 | $k = $v->vsearchvec($v->uniqvec); | ||
| 245 | |||
| 246 | ... but somewhat more efficiently. | ||
| 247 | |||
| 248 | =pod | ||
| 249 | |||
| 250 | Broadcasts over its inputs. | ||
| 251 | |||
| 252 | =for bad | ||
| 253 | |||
| 254 | C<vv_enumvecg> does not process bad values. | ||
| 255 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | *vv_enumvecg = \&PDL::vv_enumvecg; | ||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | =head2 vv_rleseq | ||
| 270 | |||
| 271 | =for sig | ||
| 272 | |||
| 273 | Signature: (c(N); indx [o]a(N); [o]b(N)) | ||
| 274 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 275 | float double ldouble) | ||
| 276 | |||
| 277 | =for usage | ||
| 278 | |||
| 279 | ($a, $b) = vv_rleseq($c); | ||
| 280 | vv_rleseq($c, $a, $b); # all arguments given | ||
| 281 | ($a, $b) = $c->vv_rleseq; # method call | ||
| 282 | $c->vv_rleseq($a, $b); | ||
| 283 | |||
| 284 | Run-length encode a vector of subsequences. | ||
| 285 | |||
| 286 | Given a vector of $c() of concatenated variable-length, variable-offset subsequences, | ||
| 287 | generate a vector $a containing the length of each subsequence | ||
| 288 | and a vector $b containing the subsequence offsets. | ||
| 289 | As for rle(), only the elements up to the first instance of 0 in $a should be considered. | ||
| 290 | |||
| 291 | See also PDL::Slices::rle. | ||
| 292 | |||
| 293 | =pod | ||
| 294 | |||
| 295 | Broadcasts over its inputs. | ||
| 296 | |||
| 297 | =for bad | ||
| 298 | |||
| 299 | C<vv_rleseq> does not process bad values. | ||
| 300 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 | *vv_rleseq = \&PDL::vv_rleseq; | ||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | =head2 vv_rldseq | ||
| 315 | |||
| 316 | =for sig | ||
| 317 | |||
| 318 | Signature: (int a(N); b(N); [o]c(M)) | ||
| 319 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 320 | float double ldouble) | ||
| 321 | |||
| 322 | =for usage | ||
| 323 | |||
| 324 | $c = vv_rldseq($a, $b); | ||
| 325 | vv_rldseq($a, $b, $c); # all arguments given | ||
| 326 | $c = $a->vv_rldseq($b); # method call | ||
| 327 | $a->vv_rldseq($b, $c); | ||
| 328 | |||
| 329 | Run-length decode a subsequence vector. | ||
| 330 | |||
| 331 | Given a vector $a() of sequence lengths | ||
| 332 | and a vector $b() of corresponding offsets, | ||
| 333 | decode concatenation of subsequences to $c(), | ||
| 334 | as for: | ||
| 335 | |||
| 336 | $c = null; | ||
| 337 | $c = $c->append($b($_)+sequence($a->type,$a($_))) foreach (0..($N-1)); | ||
| 338 | |||
| 339 | See also: PDL::Slices::rld. | ||
| 340 | |||
| 341 | =pod | ||
| 342 | |||
| 343 | Broadcasts over its inputs. | ||
| 344 | |||
| 345 | =for bad | ||
| 346 | |||
| 347 | C<vv_rldseq> does not process bad values. | ||
| 348 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | sub PDL::vv_rldseq { | ||
| 356 | my ($a,$b,$c) = @_; | ||
| 357 | if (!defined($c)) { | ||
| 358 | my $size = $a->sumover->max; | ||
| 359 | my (@dims) = $a->dims; | ||
| 360 | shift(@dims); | ||
| 361 | $c = $b->zeroes($b->type,$size,@dims); | ||
| 362 | } | ||
| 363 | &PDL::_vv_rldseq_int($a,$b,$c); | ||
| 364 | return $c; | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | *vv_rldseq = \&PDL::vv_rldseq; | ||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | =head2 vv_vsearchvec | ||
| 377 | |||
| 378 | =for sig | ||
| 379 | |||
| 380 | Signature: (find(M); which(M,N); int [o]found()) | ||
| 381 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 382 | float double ldouble) | ||
| 383 | |||
| 384 | =for ref | ||
| 385 | |||
| 386 | Routine for searching N-dimensional values - akin to vsearch() for vectors. | ||
| 387 | |||
| 388 | =for usage | ||
| 389 | |||
| 390 | $found = vsearchvec($find, $which); | ||
| 391 | $nearest = $which->dice_axis(1,$found); | ||
| 392 | |||
| 393 | Returns for each row-vector in C<$find> the index along dimension N | ||
| 394 | of the least row vector of C<$which> | ||
| 395 | greater or equal to it. | ||
| 396 | C<$which> should be sorted in increasing order. | ||
| 397 | If the value of C<$find> is larger | ||
| 398 | than any member of C<$which>, the index to the last element of C<$which> is | ||
| 399 | returned. | ||
| 400 | |||
| 401 | See also: PDL::Primitive::vsearch(). | ||
| 402 | |||
| 403 | =pod | ||
| 404 | |||
| 405 | Broadcasts over its inputs. | ||
| 406 | |||
| 407 | =for bad | ||
| 408 | |||
| 409 | C<vv_vsearchvec> does not process bad values. | ||
| 410 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 411 | |||
| 412 | =cut | ||
| 413 | |||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | *vv_vsearchvec = \&PDL::vv_vsearchvec; | ||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | |||
| 423 | #line 392 "utils.pd" | ||
| 424 | |||
| 425 | =pod | ||
| 426 | |||
| 427 | =head1 Vector-Valued Sorting and Comparison | ||
| 428 | |||
| 429 | The following functions are provided for lexicographic sorting of | ||
| 430 | vectors, rsp. axis indices. As of PDL::VectorValued v1.0.12, vv_qsortvec() and | ||
| 431 | vv_qsortveci() are just deprecated aliases for the builtin PDL functions of the same names. | ||
| 432 | Older versions of this module used a dedicated implementation as a workaround | ||
| 433 | for a bug in PDL-2.4.3, which has long since been fixed. | ||
| 434 | |||
| 435 | =cut | ||
| 436 | #line 437 "Utils.pm" | ||
| 437 | |||
| 438 | |||
| 439 | =head2 vv_cmpvec | ||
| 440 | |||
| 441 | =for sig | ||
| 442 | |||
| 443 | Signature: (a(N); b(N); int [o]cmp()) | ||
| 444 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 445 | float double ldouble) | ||
| 446 | |||
| 447 | =for usage | ||
| 448 | |||
| 449 | $cmp = vv_cmpvec($a, $b); | ||
| 450 | vv_cmpvec($a, $b, $cmp); # all arguments given | ||
| 451 | $cmp = $a->vv_cmpvec($b); # method call | ||
| 452 | $a->vv_cmpvec($b, $cmp); | ||
| 453 | |||
| 454 | Lexicographically compare a pair of vectors. | ||
| 455 | |||
| 456 | =pod | ||
| 457 | |||
| 458 | Broadcasts over its inputs. | ||
| 459 | |||
| 460 | =for bad | ||
| 461 | |||
| 462 | C<vv_cmpvec> does not process bad values. | ||
| 463 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 464 | |||
| 465 | =cut | ||
| 466 | |||
| 467 | |||
| 468 | |||
| 469 | |||
| 470 | *vv_cmpvec = \&PDL::vv_cmpvec; | ||
| 471 | |||
| 472 | |||
| 473 | |||
| 474 | |||
| 475 | |||
| 476 | #line 422 "utils.pd" | ||
| 477 | |||
| 478 | =head2 vv_qsortvec | ||
| 479 | |||
| 480 | =for sig | ||
| 481 | |||
| 482 | Signature: (a(n,m); [o]b(n,m)) | ||
| 483 | |||
| 484 | =for ref | ||
| 485 | |||
| 486 | Deprecated alias for L<PDL::Ufunc::qsortvec()|PDL::Ufunc/qsortvec>, | ||
| 487 | which see for details. | ||
| 488 | |||
| 489 | =head2 vv_qsortveci | ||
| 490 | |||
| 491 | =for sig | ||
| 492 | |||
| 493 | Signature: (a(n,m); indx [o]ix(m)) | ||
| 494 | |||
| 495 | =for ref | ||
| 496 | |||
| 497 | Deprecated alias for L<PDL::Ufunc::qsortveci()|PDL::Ufunc/qsortveci>, | ||
| 498 | which see for details. | ||
| 499 | |||
| 500 | =cut | ||
| 501 | |||
| 502 | BEGIN { | ||
| 503 | *vv_qsortvec = *PDL::vv_qsortvec = *PDL::qsortvec; | ||
| 504 | *vv_qsortveci = *PDL::vv_qsortveci = *PDL::qsortveci; | ||
| 505 | } | ||
| 506 | |||
| 507 | #line 463 "utils.pd" | ||
| 508 | |||
| 509 | =pod | ||
| 510 | |||
| 511 | =head1 Vector-Valued Set Operations | ||
| 512 | |||
| 513 | The following functions are provided for set operations on | ||
| 514 | sorted vector-valued PDLs. | ||
| 515 | |||
| 516 | =cut | ||
| 517 | #line 518 "Utils.pm" | ||
| 518 | |||
| 519 | |||
| 520 | =head2 vv_union | ||
| 521 | |||
| 522 | =for sig | ||
| 523 | |||
| 524 | Signature: (a(M,NA); b(M,NB); [o]c(M,NC); int [o]nc()) | ||
| 525 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 526 | float double ldouble) | ||
| 527 | |||
| 528 | =for usage | ||
| 529 | |||
| 530 | ($c, $nc) = vv_union($a, $b); | ||
| 531 | vv_union($a, $b, $c, $nc); # all arguments given | ||
| 532 | ($c, $nc) = $a->vv_union($b); # method call | ||
| 533 | $a->vv_union($b, $c, $nc); | ||
| 534 | |||
| 535 | Union of two vector-valued PDLs. Input PDLs $a() and $b() B<MUST> be | ||
| 536 | sorted in lexicographic order. | ||
| 537 | On return, $nc() holds the actual number of vector-values in the union. | ||
| 538 | |||
| 539 | In scalar context, slices $c() to the actual number of elements in the union | ||
| 540 | and returns the sliced PDL. | ||
| 541 | |||
| 542 | =pod | ||
| 543 | |||
| 544 | Broadcasts over its inputs. | ||
| 545 | |||
| 546 | =for bad | ||
| 547 | |||
| 548 | C<vv_union> does not process bad values. | ||
| 549 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 550 | |||
| 551 | =cut | ||
| 552 | |||
| 553 | |||
| 554 | |||
| 555 | |||
| 556 | |||
| 557 | sub PDL::vv_union { | ||
| 558 | my ($a,$b,$c,$nc) = @_; | ||
| 559 | $c = PDL->null if (!defined($nc)); | ||
| 560 | $nc = PDL->null if (!defined($nc)); | ||
| 561 | &PDL::_vv_union_int($a,$b,$c,$nc); | ||
| 562 | return ($c,$nc) if (wantarray); | ||
| 563 | return $c->slice(",0:".($nc->max-1)); | ||
| 564 | } | ||
| 565 | |||
| 566 | |||
| 567 | |||
| 568 | *vv_union = \&PDL::vv_union; | ||
| 569 | |||
| 570 | |||
| 571 | |||
| 572 | |||
| 573 | |||
| 574 | |||
| 575 | =head2 vv_intersect | ||
| 576 | |||
| 577 | =for sig | ||
| 578 | |||
| 579 | Signature: (a(M,NA); b(M,NB); [o]c(M,NC); int [o]nc()) | ||
| 580 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 581 | float double ldouble) | ||
| 582 | |||
| 583 | =for usage | ||
| 584 | |||
| 585 | ($c, $nc) = vv_intersect($a, $b); | ||
| 586 | vv_intersect($a, $b, $c, $nc); # all arguments given | ||
| 587 | ($c, $nc) = $a->vv_intersect($b); # method call | ||
| 588 | $a->vv_intersect($b, $c, $nc); | ||
| 589 | |||
| 590 | Intersection of two vector-valued PDLs. | ||
| 591 | Input PDLs $a() and $b() B<MUST> be sorted in lexicographic order. | ||
| 592 | On return, $nc() holds the actual number of vector-values in the intersection. | ||
| 593 | |||
| 594 | In scalar context, slices $c() to the actual number of elements in the intersection | ||
| 595 | and returns the sliced PDL. | ||
| 596 | |||
| 597 | =pod | ||
| 598 | |||
| 599 | Broadcasts over its inputs. | ||
| 600 | |||
| 601 | =for bad | ||
| 602 | |||
| 603 | C<vv_intersect> does not process bad values. | ||
| 604 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 605 | |||
| 606 | =cut | ||
| 607 | |||
| 608 | |||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | sub PDL::vv_intersect { | ||
| 613 | my ($a,$b,$c,$nc) = @_; | ||
| 614 | $c = PDL->null if (!defined($c)); | ||
| 615 | $nc = PDL->null if (!defined($nc)); | ||
| 616 | &PDL::_vv_intersect_int($a,$b,$c,$nc); | ||
| 617 | return ($c,$nc) if (wantarray); | ||
| 618 | my $nc_max = $nc->max; | ||
| 619 | return ($nc_max > 0 | ||
| 620 | ? $c->slice(",0:".($nc_max-1)) | ||
| 621 | : $c->reshape($c->dim(0), 0, ($c->dims)[2..($c->ndims-1)])); | ||
| 622 | } | ||
| 623 | |||
| 624 | |||
| 625 | |||
| 626 | *vv_intersect = \&PDL::vv_intersect; | ||
| 627 | |||
| 628 | |||
| 629 | |||
| 630 | |||
| 631 | |||
| 632 | |||
| 633 | =head2 vv_setdiff | ||
| 634 | |||
| 635 | =for sig | ||
| 636 | |||
| 637 | Signature: (a(M,NA); b(M,NB); [o]c(M,NC); int [o]nc()) | ||
| 638 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 639 | float double ldouble) | ||
| 640 | |||
| 641 | =for usage | ||
| 642 | |||
| 643 | ($c, $nc) = vv_setdiff($a, $b); | ||
| 644 | vv_setdiff($a, $b, $c, $nc); # all arguments given | ||
| 645 | ($c, $nc) = $a->vv_setdiff($b); # method call | ||
| 646 | $a->vv_setdiff($b, $c, $nc); | ||
| 647 | |||
| 648 | Set-difference ($a() \ $b()) of two vector-valued PDLs. | ||
| 649 | Input PDLs $a() and $b() B<MUST> be sorted in lexicographic order. | ||
| 650 | On return, $nc() holds the actual number of vector-values in the computed vector set. | ||
| 651 | |||
| 652 | In scalar context, slices $c() to the actual number of elements in the output vector set | ||
| 653 | and returns the sliced PDL. | ||
| 654 | |||
| 655 | =pod | ||
| 656 | |||
| 657 | Broadcasts over its inputs. | ||
| 658 | |||
| 659 | =for bad | ||
| 660 | |||
| 661 | C<vv_setdiff> does not process bad values. | ||
| 662 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 663 | |||
| 664 | =cut | ||
| 665 | |||
| 666 | |||
| 667 | |||
| 668 | |||
| 669 | |||
| 670 | sub PDL::vv_setdiff { | ||
| 671 | my ($a,$b,$c,$nc) = @_; | ||
| 672 | $c = PDL->null if (!defined($c)); | ||
| 673 | $nc = PDL->null if (!defined($nc)); | ||
| 674 | &PDL::_vv_setdiff_int($a,$b,$c,$nc); | ||
| 675 | return ($c,$nc) if (wantarray); | ||
| 676 | my $nc_max = $nc->max; | ||
| 677 | return ($nc_max > 0 | ||
| 678 | ? $c->slice(",0:".($nc_max-1)) | ||
| 679 | : $c->reshape($c->dim(0), 0, ($c->dims)[2..($c->ndims-1)])); | ||
| 680 | } | ||
| 681 | |||
| 682 | |||
| 683 | |||
| 684 | *vv_setdiff = \&PDL::vv_setdiff; | ||
| 685 | |||
| 686 | |||
| 687 | |||
| 688 | |||
| 689 | |||
| 690 | #line 697 "utils.pd" | ||
| 691 | |||
| 692 | =pod | ||
| 693 | |||
| 694 | =head1 Sorted Vector Set Operations | ||
| 695 | |||
| 696 | The following functions are provided for set operations on | ||
| 697 | flat sorted PDLs with unique values. They may be more efficient to compute | ||
| 698 | than the corresponding implementations via PDL::Primitive::setops(). | ||
| 699 | |||
| 700 | =cut | ||
| 701 | #line 702 "Utils.pm" | ||
| 702 | |||
| 703 | |||
| 704 | =head2 v_union | ||
| 705 | |||
| 706 | =for sig | ||
| 707 | |||
| 708 | Signature: (a(NA); b(NB); [o]c(NC); int [o]nc()) | ||
| 709 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 710 | float double ldouble) | ||
| 711 | |||
| 712 | =for usage | ||
| 713 | |||
| 714 | ($c, $nc) = v_union($a, $b); | ||
| 715 | v_union($a, $b, $c, $nc); # all arguments given | ||
| 716 | ($c, $nc) = $a->v_union($b); # method call | ||
| 717 | $a->v_union($b, $c, $nc); | ||
| 718 | |||
| 719 | Union of two flat sorted unique-valued PDLs. | ||
| 720 | Input PDLs $a() and $b() B<MUST> be sorted in lexicographic order and contain no duplicates. | ||
| 721 | On return, $nc() holds the actual number of values in the union. | ||
| 722 | |||
| 723 | In scalar context, reshapes $c() to the actual number of elements in the union and returns it. | ||
| 724 | |||
| 725 | =pod | ||
| 726 | |||
| 727 | Broadcasts over its inputs. | ||
| 728 | |||
| 729 | =for bad | ||
| 730 | |||
| 731 | C<v_union> does not process bad values. | ||
| 732 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 733 | |||
| 734 | =cut | ||
| 735 | |||
| 736 | |||
| 737 | |||
| 738 | |||
| 739 | |||
| 740 | sub PDL::v_union { | ||
| 741 | my ($a,$b,$c,$nc) = @_; | ||
| 742 | $c = PDL->null if (!defined($c)); | ||
| 743 | $nc = PDL->null if (!defined($nc)); | ||
| 744 | &PDL::_v_union_int($a,$b,$c,$nc); | ||
| 745 | return ($c,$nc) if (wantarray); | ||
| 746 | return $c->slice("0:".($nc->max-1)); | ||
| 747 | } | ||
| 748 | |||
| 749 | |||
| 750 | |||
| 751 | *v_union = \&PDL::v_union; | ||
| 752 | |||
| 753 | |||
| 754 | |||
| 755 | |||
| 756 | |||
| 757 | |||
| 758 | =head2 v_intersect | ||
| 759 | |||
| 760 | =for sig | ||
| 761 | |||
| 762 | Signature: (a(NA); b(NB); [o]c(NC); int [o]nc()) | ||
| 763 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 764 | float double ldouble) | ||
| 765 | |||
| 766 | =for usage | ||
| 767 | |||
| 768 | ($c, $nc) = v_intersect($a, $b); | ||
| 769 | v_intersect($a, $b, $c, $nc); # all arguments given | ||
| 770 | ($c, $nc) = $a->v_intersect($b); # method call | ||
| 771 | $a->v_intersect($b, $c, $nc); | ||
| 772 | |||
| 773 | Intersection of two flat sorted unique-valued PDLs. | ||
| 774 | Input PDLs $a() and $b() B<MUST> be sorted in lexicographic order and contain no duplicates. | ||
| 775 | On return, $nc() holds the actual number of values in the intersection. | ||
| 776 | |||
| 777 | In scalar context, reshapes $c() to the actual number of elements in the intersection and returns it. | ||
| 778 | |||
| 779 | =pod | ||
| 780 | |||
| 781 | Broadcasts over its inputs. | ||
| 782 | |||
| 783 | =for bad | ||
| 784 | |||
| 785 | C<v_intersect> does not process bad values. | ||
| 786 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 787 | |||
| 788 | =cut | ||
| 789 | |||
| 790 | |||
| 791 | |||
| 792 | |||
| 793 | |||
| 794 | sub PDL::v_intersect { | ||
| 795 | my ($a,$b,$c,$nc) = @_; | ||
| 796 | $c = PDL->null if (!defined($c)); | ||
| 797 | $nc = PDL->null if (!defined($nc)); | ||
| 798 | &PDL::_v_intersect_int($a,$b,$c,$nc); | ||
| 799 | return ($c,$nc) if (wantarray); | ||
| 800 | my $nc_max = $nc->max; | ||
| 801 | return ($nc_max > 0 | ||
| 802 | ? $c->slice("0:".($nc_max-1)) | ||
| 803 | : $c->reshape(0, ($c->dims)[1..($c->ndims-1)])); | ||
| 804 | } | ||
| 805 | |||
| 806 | |||
| 807 | |||
| 808 | *v_intersect = \&PDL::v_intersect; | ||
| 809 | |||
| 810 | |||
| 811 | |||
| 812 | |||
| 813 | |||
| 814 | |||
| 815 | =head2 v_setdiff | ||
| 816 | |||
| 817 | =for sig | ||
| 818 | |||
| 819 | Signature: (a(NA); b(NB); [o]c(NC); int [o]nc()) | ||
| 820 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 821 | float double ldouble) | ||
| 822 | |||
| 823 | =for usage | ||
| 824 | |||
| 825 | ($c, $nc) = v_setdiff($a, $b); | ||
| 826 | v_setdiff($a, $b, $c, $nc); # all arguments given | ||
| 827 | ($c, $nc) = $a->v_setdiff($b); # method call | ||
| 828 | $a->v_setdiff($b, $c, $nc); | ||
| 829 | |||
| 830 | Set-difference ($a() \ $b()) of two flat sorted unique-valued PDLs. | ||
| 831 | Input PDLs $a() and $b() B<MUST> be sorted in lexicographic order and contain no duplicate values. | ||
| 832 | On return, $nc() holds the actual number of values in the computed vector set. | ||
| 833 | |||
| 834 | In scalar context, reshapes $c() to the actual number of elements in the difference set and returns it. | ||
| 835 | |||
| 836 | =pod | ||
| 837 | |||
| 838 | Broadcasts over its inputs. | ||
| 839 | |||
| 840 | =for bad | ||
| 841 | |||
| 842 | C<v_setdiff> does not process bad values. | ||
| 843 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. | ||
| 844 | |||
| 845 | =cut | ||
| 846 | |||
| 847 | |||
| 848 | |||
| 849 | |||
| 850 | |||
| 851 | sub PDL::v_setdiff { | ||
| 852 | my ($a,$b,$c,$nc) = @_; | ||
| 853 | $c = PDL->null if (!defined($c)); | ||
| 854 | $nc = PDL->null if (!defined($nc)); | ||
| 855 | &PDL::_v_setdiff_int($a,$b,$c,$nc); | ||
| 856 | return ($c,$nc) if (wantarray); | ||
| 857 | my $nc_max = $nc->max; | ||
| 858 | return ($nc_max > 0 | ||
| 859 | ? $c->slice("0:".($nc_max-1)) | ||
| 860 | : $c->reshape(0, ($c->dims)[1..($c->ndims-1)])); | ||
| 861 | } | ||
| 862 | |||
| 863 | |||
| 864 | |||
| 865 | *v_setdiff = \&PDL::v_setdiff; | ||
| 866 | |||
| 867 | |||
| 868 | |||
| 869 | |||
| 870 | |||
| 871 | #line 923 "utils.pd" | ||
| 872 | |||
| 873 | =pod | ||
| 874 | |||
| 875 | =head1 Miscellaneous Vector-Valued Operations | ||
| 876 | |||
| 877 | =cut | ||
| 878 | #line 879 "Utils.pm" | ||
| 879 | |||
| 880 | |||
| 881 | =head2 vv_vcos | ||
| 882 | |||
| 883 | =for sig | ||
| 884 | |||
| 885 | Signature: (a(M,N);b(M);float+ [o]vcos(N)) | ||
| 886 | Types: (sbyte byte short ushort long ulong indx ulonglong longlong | ||
| 887 | float double ldouble) | ||
| 888 | |||
| 889 | =for usage | ||
| 890 | |||
| 891 | $vcos = vv_vcos($a, $b); | ||
| 892 | vv_vcos($a, $b, $vcos); # all arguments given | ||
| 893 | $vcos = $a->vv_vcos($b); # method call | ||
| 894 | $a->vv_vcos($b, $vcos); | ||
| 895 | |||
| 896 | Computes the vector cosine similarity of a dense vector $b() with respect to each row $a(*,i) | ||
| 897 | of a dense PDL $a(). This is basically the same thing as: | ||
| 898 | |||
| 899 | ($a * $b)->sumover / ($a->pow(2)->sumover->sqrt * $b->pow(2)->sumover->sqrt) | ||
| 900 | |||
| 901 | ... but should be much faster to compute, and avoids allocating potentially large temporaries for | ||
| 902 | the vector magnitudes. Output values in $vcos() are cosine similarities in the range [-1,1], | ||
| 903 | except for zero-magnitude vectors which will result in NaN values in $vcos(). | ||
| 904 | |||
| 905 | You can use PDL broadcasting to batch-compute distances for multiple $b() vectors simultaneously: | ||
| 906 | |||
| 907 | $bx = random($M, $NB); ##-- get $NB random vectors of size $M | ||
| 908 | $vcos = vv_vcos($a,$bx); ##-- $vcos(i,j) ~ sim($a(,i),$b(,j)) | ||
| 909 | |||
| 910 | =pod | ||
| 911 | |||
| 912 | Broadcasts over its inputs. | ||
| 913 | |||
| 914 | =for bad | ||
| 915 | |||
| 916 | vv_vcos() will set the bad status flag on the output piddle $vcos() if it is set on either of the input | ||
| 917 | piddles $a() or $b(), but BAD values will otherwise be ignored for computing the cosine similarity. | ||
| 918 | |||
| 919 | =cut | ||
| 920 | |||
| 921 | |||
| 922 | |||
| 923 | |||
| 924 | *vv_vcos = \&PDL::vv_vcos; | ||
| 925 | |||
| 926 | |||
| 927 | |||
| 928 | |||
| 929 | |||
| 930 | #line 1033 "utils.pd" | ||
| 931 | |||
| 932 | ##--------------------------------------------------------------------- | ||
| 933 | =pod | ||
| 934 | |||
| 935 | =head1 ACKNOWLEDGEMENTS | ||
| 936 | |||
| 937 | =over 4 | ||
| 938 | |||
| 939 | =item * | ||
| 940 | |||
| 941 | Perl by Larry Wall | ||
| 942 | |||
| 943 | =item * | ||
| 944 | |||
| 945 | PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others. | ||
| 946 | |||
| 947 | =item * | ||
| 948 | |||
| 949 | Code for rlevec() and rldvec() derived from the PDL builtin functions | ||
| 950 | rle() and rld() in $PDL_SRC_ROOT/Basic/Slices/slices.pd | ||
| 951 | |||
| 952 | =back | ||
| 953 | |||
| 954 | =cut | ||
| 955 | |||
| 956 | ##---------------------------------------------------------------------- | ||
| 957 | =pod | ||
| 958 | |||
| 959 | =head1 KNOWN BUGS | ||
| 960 | |||
| 961 | Probably many. | ||
| 962 | |||
| 963 | =cut | ||
| 964 | |||
| 965 | ##--------------------------------------------------------------------- | ||
| 966 | =pod | ||
| 967 | |||
| 968 | =head1 AUTHOR | ||
| 969 | |||
| 970 | Bryan Jurish E<lt>moocow@cpan.orgE<gt> | ||
| 971 | |||
| 972 | =head1 COPYRIGHT | ||
| 973 | |||
| 974 | =over 4 | ||
| 975 | |||
| 976 | =item * | ||
| 977 | |||
| 978 | Code for qsortvec() copyright (C) Tuomas J. Lukka 1997. | ||
| 979 | Contributions by Christian Soeller (c.soeller@auckland.ac.nz) | ||
| 980 | and Karl Glazebrook (kgb@aaoepp.aao.gov.au). All rights | ||
| 981 | reserved. There is no warranty. You are allowed to redistribute this | ||
| 982 | software / documentation under certain conditions. For details, see | ||
| 983 | the file COPYING in the PDL distribution. If this file is separated | ||
| 984 | from the PDL distribution, the copyright notice should be included in | ||
| 985 | the file. | ||
| 986 | |||
| 987 | =item * | ||
| 988 | |||
| 989 | All other parts copyright (c) 2007-2022, Bryan Jurish. All rights reserved. | ||
| 990 | |||
| 991 | This package is free software, and entirely without warranty. | ||
| 992 | You may redistribute it and/or modify it under the same terms | ||
| 993 | as Perl itself. | ||
| 994 | |||
| 995 | =back | ||
| 996 | |||
| 997 | =head1 SEE ALSO | ||
| 998 | |||
| 999 | perl(1), PDL(3perl) | ||
| 1000 | |||
| 1001 | =cut | ||
| 1002 | #line 1003 "Utils.pm" | ||
| 1003 | |||
| 1004 | # Exit with OK status | ||
| 1005 | |||
| 1006 | 1; | ||