File: VectorValued.pm

package info (click to toggle)
libpdl-vectorvalued-perl 1.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: perl: 377; makefile: 3
file content (388 lines) | stat: -rw-r--r-- 10,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
## $Id$
##
## File: PDL::VectorValued.pm
## Author: Bryan Jurish <moocow@cpan.org>
## Description: Vector utilities for PDL: perl side only
##======================================================================

package PDL::VectorValued;
use strict;
use warnings;

##======================================================================
## Export hacks
use PDL;
use PDL::Exporter;
use PDL::VectorValued::Utils;
our @ISA = qw(PDL::Exporter);

our (@EXPORT_OK);
BEGIN {
  ##--------------------------------------------------------------------
  ## Conditional bindings for PDL > v2.079
  ##  + see https://github.com/moocow-the-bovine/PDL-VectorValued/issues/5,
  ##    https://github.com/moocow-the-bovine/PDL-VectorValued/pull/8

  ## @VV_SYMBOLS : exportable symbols (vv_FOO)
  my @VV_SYMBOLS =
    (
     (@PDL::VectorValued::Utils::EXPORT_OK), ##-- inherited
     qw(vv_uniqvec),
     qw(vv_rleND vv_rldND),
     qw(vv_indx),
    );

  # %VV_IMPORT: import these from PDL core if available (PDL > v2.079)
  my %VV_IMPORT = (
		   vv_rlevec => {vv=>'rlevec', p=>PDL->can('rlevec')},
		   vv_rldvec => {vv=>'rldvec', p=>PDL->can('rldvec')},
		   vv_rleseq => {vv=>'rleseq', p=>PDL->can('rleseq')},
		   vv_rldseq => {vv=>'rldseq', p=>PDL->can('rldseq')},
		   vv_enumvec => {vv=>'enumvec', p=>PDL->can('enumvec')},
		   vv_enumvecg => {vv=>'enumvecg', p=>PDL->can('enumvecg')},
		   vv_vsearchvec => {vv=>'vsearchvec', p=>PDL->can('vsearchvec')},
		   vv_cmpvec => {vv=>'cmpvec', p=>PDL->can('cmpvec')},
		   vv_union => {vv=>'vv_union', p=>PDL->can('unionvec')},
		   vv_intersect => {vv=>'vv_intersect', p=>PDL->can('intersectvec')},
		   vv_setdiff => {vv=>'vv_setdiff', p=>PDL->can('setdiffvec')},
		   v_union => {vv=>'v_union', p=>PDL->can('union_sorted')},
		   v_intersect => {vv=>'v_intersect', p=>PDL->can('intersect_sorted')},
		   v_setdiff => {vv=>'v_setdiff', p=>PDL->can('setdiff_sorted')},
		   vv_rleND => {vv=>'rleND', p=>PDL->can('rleND')},
		   vv_rldND => {vv=>'rldND', p=>PDL->can('rldND')},
		   vv_vcos => {vv=>'vv_vcos', p=>PDL->can('vcos')},
		   #vv_indx => {vv=>'vv_indx', p=>PDL->can('indx')}, # DEBUG
		  );


  @EXPORT_OK = @VV_SYMBOLS;
  foreach my $vv_sym (@VV_SYMBOLS) {
    no strict 'refs';
    if ($VV_IMPORT{$vv_sym} && defined($VV_IMPORT{$vv_sym}{p})) {
      # function lives in PDL core: import it here, and clobber $vv_sym here (but not in VV::Utils)
      no warnings 'redefine';
      *$vv_sym = *{$VV_IMPORT{$vv_sym}{vv}} = $VV_IMPORT{$vv_sym}{p};
    }
    elsif ($VV_IMPORT{$vv_sym}) {
      # $sym is defined here as "vv_$sym" : bind it here & in PDL namespace
      my $sym = $VV_IMPORT{$vv_sym}{vv};
      ${PDL::}{$sym} = *$sym = *$vv_sym;

      # ... and make it exportable
      push(@EXPORT_OK, $sym);
    }
  }
}

our %EXPORT_TAGS =
  (
   Func => [@EXPORT_OK],               ##-- respect PDL conventions (hopefully)
  );

## VERSION was formerly set by PDL::VectorValued::Version, now use perl-reversion from Perl::Version instead
our $VERSION = '1.0.23';

##======================================================================
## pod: header
=pod

=head1 NAME

PDL::VectorValued - Utilities for vector-valued PDLs

=head1 SYNOPSIS

 use PDL;
 use PDL::VectorValued;

 ##---------------------------------------------------------------------
 ## ... stuff happens

=cut

##======================================================================
## Description
=pod

=head1 DESCRIPTION

PDL::VectorValued provides generalizations of some elementary PDL
functions to higher-order PDLs which treat vectors as "data values".

=cut


##======================================================================
## pod: Aliases
=pod

=head1 ALIASES

To facilitate incorporation of selected vector-valued functions into
the PDL core, the PDL:PP-, XS-, C-, and perl-level functions defined by
this module in the C<PDL::VectorValued::Utils> package namespace
all carry a C<vv_> prefix as of PDL::VectorValued v1.0.19
Prior to v1.0.19, many of these functions (e.g. C<cmpvec()>)
were defined by this module without a C<vv_> prefix.

For PDL::VectorValued E<gt>= v1.0.19 and PDL E<gt> v2.079, most vector-valued
functions are expected to be defined in the PDL core.  For such "moving" functions
C<FUNC>, the PDL core implementations will be imported into the C<PDL::VectorValued>
namespace as both C<FUNC> and C<vv_FUNC>, clobbering any local implementation
from the C<PDL::VectorValued::Utils> namespace.  Local implementations
C<vv_FUNC> which were previously defined and exported as C<FUNC>
or for which no binding for C<*PDL::FUNC> exists will be bound to
both C<*PDL::VectorValued::FUNC> and C<*PDL::FUNC>, and exported by default,
for backwards-compatibility.

Functions expected to move to the PDL core are:


=over 4

=item *

New code should use C<FUNC()> or C<PDL::FUNC()>.

=item *

Backwards-compatible code can use C<FUNC()> or C<PDL::VectorValued::FUNC()>.

=item *

Direct use of C<PDL::VectorValued::Utils::vv_FUNC()> is deprecated.

=item *

Direct use of C<PDL::VectorValued::Utils::FUNC()> is likely broken as of
PDL::VectorValued v1.0.19.


=back

=cut


##======================================================================
## pod: Functions
=pod

=head1 FUNCTIONS

=cut

##----------------------------------------------------------------------
## vv_uniqvec

=pod

=head2 vv_uniqvec

=for sig

  Signature: (v(N,M); [o]vu(N,MU))

=for ref

Drop-in replacement for broken uniqvec() which uses vv_qsortvec().
Otherwise copied from PDL::Primitive::primitive.pd.

See also: PDL::VectorValued::Utils::vv_qsortvec, PDL::Primitive::uniqvec.

=cut

*PDL::vv_uniqvec = \&vv_uniqvec;
sub vv_uniqvec {
  my($pdl) = shift;

  # slice is not cheap but uniqvec isn't either -- shouldn't cost too much.
  return $pdl if($pdl->nelem == 0 || $pdl->ndims <2 || $pdl->slice("(0)")->nelem < 2); 

  my $srt = $pdl->mv(0,-1)->
    clump($pdl->ndims - 1)->
      mv(-1,0)->vv_qsortvec-> ##-- moo: Tue, 24 Apr 2007 17:17:39 +0200: use vv_qsortvec
	mv(0,-1);

  $srt=$srt->dice($srt->mv(0,-1)->ngoodover->which) if ($PDL::Bad::Status && $srt->badflag);
  ##use dice instead of nslice since qsortvec might be packing the badvals to the front of
  #the array instead of the end like the docs say. If that is the case and it gets fixed,
  #it won't bust uniqvec. DAL 14-March 2006
  my $uniq = ($srt != $srt->rotate(-1)) -> mv(0,-1) -> orover->which;

  return $uniq->nelem==0 ? 
	$srt->slice("0,:")->mv(0,-1) :
	$srt->dice($uniq)->mv(0,-1);
}


##======================================================================
## Run-Length Encoding/Decoding: n-dimensionl
=pod

=head1 Higher-Order Run-Length Encoding and Decoding

The following functions generalize the builtin PDL functions rle() and rld()
for higher-order "values".

See also:
PDL::VectorValued::Utils::vv_rlevec(), PDL::VectorValued::Utils::vv_rldvec().

=cut

##----------------------------------------------------------------------
## rleND()
=pod

=head2 vv_rleND

=for sig

  Signature: (data(@vdims,N); int [o]counts(N); [o]elts(@vdims,N))

=for ref

Run-length encode a set of (sorted) n-dimensional values.

Generalization of rle() and vv_rlevec():
given set of values $data, generate a vector $counts with the number of occurrences of each element
(where an "element" is a matrix of dimensions @vdims ocurring as a sequential run over the
final dimension in $data), and a set of vectors $elts containing the elements which begin a run.
Really just a wrapper for clump() and rlevec().

See also: PDL::Slices::rle, PDL::Ngrams::VectorValued::Utils::vv_rlevec.

=cut

*PDL::vv_rleND = \&vv_rleND if !defined &PDL::vv_rleND;
*rleND = sub {
  my $data   = shift;
  my @vdimsN = $data->dims;

  ##-- construct output pdls
  my $counts = $#_ >= 0 ? $_[0] : zeroes(long, $vdimsN[$#vdimsN]);
  my $elts   = $#_ >= 1 ? $_[1] : zeroes($data->type, @vdimsN);

  ##-- guts: call rlevec()
  vv_rlevec($data->clump($#vdimsN), $counts, $elts->clump($#vdimsN));

  return ($counts,$elts);
} if !defined &rleND;

##----------------------------------------------------------------------
## rldND()
=pod

=head2 vv_rldND

=for sig

  Signature: (int counts(N); elts(@vdims,N); [o]data(@vdims,N);)

=for ref

Run-length decode a set of (sorted) n-dimensional values.

Generalization of rld() and rldvec():
given a vector $counts() of the number of occurrences of each @vdims-dimensioned element,
and a set $elts() of @vdims-dimensioned elements, run-length decode to $data().

Really just a wrapper for clump() and rldvec().

See also: PDL::Slices::rld, PDL::VectorValued::Utils::rldvec

=cut

*PDL::vv_rldND = \&vv_rldND if !defined &PDL::vv_rldND;
*rldND = sub {
  my ($counts,$elts) = (shift,shift);
  my @vdimsN        = $elts->dims;

  ##-- construct output pdl
  my ($data);
  if ($#_ >= 0) { $data = $_[0]; }
  else {
    my $size      = $counts->sumover->max; ##-- get maximum size for Nth-dimension for small encodings
    my @countdims = $counts->dims;
    shift(@countdims);
    $data         = zeroes($elts->type, @vdimsN, @countdims);
  }

  ##-- guts: call rldvec()
  vv_rldvec($counts, $elts->clump($#vdimsN), $data->clump($#vdimsN));

  return $data;
} if !defined &rldND;

##======================================================================
## pod: Functions: datatype utilities
=pod

=head1 Datatype Utilities

=cut

##----------------------------------------------------------------------
## vv_indx()
=pod

=head2 vv_indx

=for sig

  Signature: vv_indx()

=for ref

Returns PDL::Type subclass used for indices.
If built with PDL E<lt> v2.007, this should return C<PDL::long>, otherwise C<PDL::indx>.

=cut

sub vv_indx {
  return defined(&PDL::indx) ? PDL::indx(@_) : PDL::long(@_);
}


##======================================================================
## pod: Functions: low-level
=pod

=head2 Low-Level Functions

Some additional low-level functions are provided in the
PDL::VectorValued::Utils
package.
See L<PDL::VectorValued::Utils> for details.

=cut



1; ##-- make perl happy

##======================================================================
## pod: Footer
=pod

=head1 ACKNOWLEDGEMENTS

perl by Larry Wall.

=head1 AUTHOR

Bryan Jurish E<lt>moocow@cpan.orgE<gt>

PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.

=head1 COPYRIGHT

Copyright (c) 2007-2022, Bryan Jurish.  All rights reserved.

This package is free software.  You may redistribute it
and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

perl(1), PDL(3perl), PDL::VectorValued::Utils(3perl)

=cut