File: Permutation.pm.1.15

package info (click to toggle)
libmath-gsl-perl 0.45-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192,156 kB
  • sloc: ansic: 895,524; perl: 24,682; makefile: 12
file content (406 lines) | stat: -rw-r--r-- 18,624 bytes parent folder | download | duplicates (11)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# This file was automatically generated by SWIG (https://www.swig.org).
# Version 4.2.0
#
# Do not make changes to this file unless you know what you are doing - modify
# the SWIG interface file instead.

package Math::GSL::Permutation;
use base qw(Exporter);
use base qw(DynaLoader);
package Math::GSL::Permutationc;
bootstrap Math::GSL::Permutation;
package Math::GSL::Permutation;
@EXPORT = qw();

# ---------- BASE METHODS -------------

package Math::GSL::Permutation;

sub TIEHASH {
    my ($classname,$obj) = @_;
    return bless $obj, $classname;
}

sub CLEAR { }

sub FIRSTKEY { }

sub NEXTKEY { }

sub FETCH {
    my ($self,$field) = @_;
    my $member_func = "swig_${field}_get";
    $self->$member_func();
}

sub STORE {
    my ($self,$field,$newval) = @_;
    my $member_func = "swig_${field}_set";
    $self->$member_func($newval);
}

sub this {
    my $ptr = shift;
    return tied(%$ptr);
}


# ------- FUNCTION WRAPPERS --------

package Math::GSL::Permutation;

*gsl_error = *Math::GSL::Permutationc::gsl_error;
*gsl_stream_printf = *Math::GSL::Permutationc::gsl_stream_printf;
*gsl_strerror = *Math::GSL::Permutationc::gsl_strerror;
*gsl_set_error_handler = *Math::GSL::Permutationc::gsl_set_error_handler;
*gsl_set_error_handler_off = *Math::GSL::Permutationc::gsl_set_error_handler_off;
*gsl_set_stream_handler = *Math::GSL::Permutationc::gsl_set_stream_handler;
*gsl_set_stream = *Math::GSL::Permutationc::gsl_set_stream;
*gsl_permute_complex_long_double = *Math::GSL::Permutationc::gsl_permute_complex_long_double;
*gsl_permute_complex_long_double_inverse = *Math::GSL::Permutationc::gsl_permute_complex_long_double_inverse;
*gsl_permute_complex = *Math::GSL::Permutationc::gsl_permute_complex;
*gsl_permute_complex_inverse = *Math::GSL::Permutationc::gsl_permute_complex_inverse;
*gsl_permute_complex_float = *Math::GSL::Permutationc::gsl_permute_complex_float;
*gsl_permute_complex_float_inverse = *Math::GSL::Permutationc::gsl_permute_complex_float_inverse;
*gsl_permute_long_double = *Math::GSL::Permutationc::gsl_permute_long_double;
*gsl_permute_long_double_inverse = *Math::GSL::Permutationc::gsl_permute_long_double_inverse;
*gsl_permute = *Math::GSL::Permutationc::gsl_permute;
*gsl_permute_inverse = *Math::GSL::Permutationc::gsl_permute_inverse;
*gsl_permute_int = *Math::GSL::Permutationc::gsl_permute_int;
*gsl_permute_int_inverse = *Math::GSL::Permutationc::gsl_permute_int_inverse;
*gsl_permute_uint = *Math::GSL::Permutationc::gsl_permute_uint;
*gsl_permute_uint_inverse = *Math::GSL::Permutationc::gsl_permute_uint_inverse;
*gsl_permute_vector = *Math::GSL::Permutationc::gsl_permute_vector;
*gsl_permute_vector_inverse = *Math::GSL::Permutationc::gsl_permute_vector_inverse;
*gsl_permute_vector_int = *Math::GSL::Permutationc::gsl_permute_vector_int;
*gsl_permute_vector_int_inverse = *Math::GSL::Permutationc::gsl_permute_vector_int_inverse;
*gsl_permute_vector_uint = *Math::GSL::Permutationc::gsl_permute_vector_uint;
*gsl_permute_vector_uint_inverse = *Math::GSL::Permutationc::gsl_permute_vector_uint_inverse;
*gsl_permute_vector_long = *Math::GSL::Permutationc::gsl_permute_vector_long;
*gsl_permute_vector_long_inverse = *Math::GSL::Permutationc::gsl_permute_vector_long_inverse;
*gsl_permute_vector_long_double = *Math::GSL::Permutationc::gsl_permute_vector_long_double;
*gsl_permute_vector_long_double_inverse = *Math::GSL::Permutationc::gsl_permute_vector_long_double_inverse;
*gsl_permute_vector_ulong = *Math::GSL::Permutationc::gsl_permute_vector_ulong;
*gsl_permute_vector_ulong_inverse = *Math::GSL::Permutationc::gsl_permute_vector_ulong_inverse;
*gsl_permute_vector_short = *Math::GSL::Permutationc::gsl_permute_vector_short;
*gsl_permute_vector_short_inverse = *Math::GSL::Permutationc::gsl_permute_vector_short_inverse;
*gsl_permute_vector_ushort = *Math::GSL::Permutationc::gsl_permute_vector_ushort;
*gsl_permute_vector_ushort_inverse = *Math::GSL::Permutationc::gsl_permute_vector_ushort_inverse;
*gsl_permutation_alloc = *Math::GSL::Permutationc::gsl_permutation_alloc;
*gsl_permutation_calloc = *Math::GSL::Permutationc::gsl_permutation_calloc;
*gsl_permutation_init = *Math::GSL::Permutationc::gsl_permutation_init;
*gsl_permutation_free = *Math::GSL::Permutationc::gsl_permutation_free;
*gsl_permutation_memcpy = *Math::GSL::Permutationc::gsl_permutation_memcpy;
*gsl_permutation_fread = *Math::GSL::Permutationc::gsl_permutation_fread;
*gsl_permutation_fwrite = *Math::GSL::Permutationc::gsl_permutation_fwrite;
*gsl_permutation_fscanf = *Math::GSL::Permutationc::gsl_permutation_fscanf;
*gsl_permutation_fprintf = *Math::GSL::Permutationc::gsl_permutation_fprintf;
*gsl_permutation_size = *Math::GSL::Permutationc::gsl_permutation_size;
*gsl_permutation_data = *Math::GSL::Permutationc::gsl_permutation_data;
*gsl_permutation_swap = *Math::GSL::Permutationc::gsl_permutation_swap;
*gsl_permutation_valid = *Math::GSL::Permutationc::gsl_permutation_valid;
*gsl_permutation_reverse = *Math::GSL::Permutationc::gsl_permutation_reverse;
*gsl_permutation_inverse = *Math::GSL::Permutationc::gsl_permutation_inverse;
*gsl_permutation_next = *Math::GSL::Permutationc::gsl_permutation_next;
*gsl_permutation_prev = *Math::GSL::Permutationc::gsl_permutation_prev;
*gsl_permutation_mul = *Math::GSL::Permutationc::gsl_permutation_mul;
*gsl_permutation_linear_to_canonical = *Math::GSL::Permutationc::gsl_permutation_linear_to_canonical;
*gsl_permutation_canonical_to_linear = *Math::GSL::Permutationc::gsl_permutation_canonical_to_linear;
*gsl_permutation_inversions = *Math::GSL::Permutationc::gsl_permutation_inversions;
*gsl_permutation_linear_cycles = *Math::GSL::Permutationc::gsl_permutation_linear_cycles;
*gsl_permutation_canonical_cycles = *Math::GSL::Permutationc::gsl_permutation_canonical_cycles;
*gsl_permutation_get = *Math::GSL::Permutationc::gsl_permutation_get;

############# Class : Math::GSL::Permutation::gsl_permutation_struct ##############

package Math::GSL::Permutation::gsl_permutation_struct;
use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);
@ISA = qw( Math::GSL::Permutation );
%OWNER = ();
%ITERATORS = ();
*swig_size_get = *Math::GSL::Permutationc::gsl_permutation_struct_size_get;
*swig_size_set = *Math::GSL::Permutationc::gsl_permutation_struct_size_set;
*swig_data_get = *Math::GSL::Permutationc::gsl_permutation_struct_data_get;
*swig_data_set = *Math::GSL::Permutationc::gsl_permutation_struct_data_set;
sub new {
    my $pkg = shift;
    my $self = Math::GSL::Permutationc::new_gsl_permutation_struct(@_);
    bless $self, $pkg if defined($self);
}

sub DESTROY {
    return unless $_[0]->isa('HASH');
    my $self = tied(%{$_[0]});
    return unless defined $self;
    delete $ITERATORS{$self};
    if (exists $OWNER{$self}) {
        Math::GSL::Permutationc::delete_gsl_permutation_struct($self);
        delete $OWNER{$self};
    }
}

sub DISOWN {
    my $self = shift;
    my $ptr = tied(%$self);
    delete $OWNER{$ptr};
}

sub ACQUIRE {
    my $self = shift;
    my $ptr = tied(%$self);
    $OWNER{$ptr} = 1;
}


# ------- VARIABLE STUBS --------

package Math::GSL::Permutation;

*GSL_VERSION = *Math::GSL::Permutationc::GSL_VERSION;
*GSL_MAJOR_VERSION = *Math::GSL::Permutationc::GSL_MAJOR_VERSION;
*GSL_MINOR_VERSION = *Math::GSL::Permutationc::GSL_MINOR_VERSION;
*GSL_POSZERO = *Math::GSL::Permutationc::GSL_POSZERO;
*GSL_NEGZERO = *Math::GSL::Permutationc::GSL_NEGZERO;
*GSL_SUCCESS = *Math::GSL::Permutationc::GSL_SUCCESS;
*GSL_FAILURE = *Math::GSL::Permutationc::GSL_FAILURE;
*GSL_CONTINUE = *Math::GSL::Permutationc::GSL_CONTINUE;
*GSL_EDOM = *Math::GSL::Permutationc::GSL_EDOM;
*GSL_ERANGE = *Math::GSL::Permutationc::GSL_ERANGE;
*GSL_EFAULT = *Math::GSL::Permutationc::GSL_EFAULT;
*GSL_EINVAL = *Math::GSL::Permutationc::GSL_EINVAL;
*GSL_EFAILED = *Math::GSL::Permutationc::GSL_EFAILED;
*GSL_EFACTOR = *Math::GSL::Permutationc::GSL_EFACTOR;
*GSL_ESANITY = *Math::GSL::Permutationc::GSL_ESANITY;
*GSL_ENOMEM = *Math::GSL::Permutationc::GSL_ENOMEM;
*GSL_EBADFUNC = *Math::GSL::Permutationc::GSL_EBADFUNC;
*GSL_ERUNAWAY = *Math::GSL::Permutationc::GSL_ERUNAWAY;
*GSL_EMAXITER = *Math::GSL::Permutationc::GSL_EMAXITER;
*GSL_EZERODIV = *Math::GSL::Permutationc::GSL_EZERODIV;
*GSL_EBADTOL = *Math::GSL::Permutationc::GSL_EBADTOL;
*GSL_ETOL = *Math::GSL::Permutationc::GSL_ETOL;
*GSL_EUNDRFLW = *Math::GSL::Permutationc::GSL_EUNDRFLW;
*GSL_EOVRFLW = *Math::GSL::Permutationc::GSL_EOVRFLW;
*GSL_ELOSS = *Math::GSL::Permutationc::GSL_ELOSS;
*GSL_EROUND = *Math::GSL::Permutationc::GSL_EROUND;
*GSL_EBADLEN = *Math::GSL::Permutationc::GSL_EBADLEN;
*GSL_ENOTSQR = *Math::GSL::Permutationc::GSL_ENOTSQR;
*GSL_ESING = *Math::GSL::Permutationc::GSL_ESING;
*GSL_EDIVERGE = *Math::GSL::Permutationc::GSL_EDIVERGE;
*GSL_EUNSUP = *Math::GSL::Permutationc::GSL_EUNSUP;
*GSL_EUNIMPL = *Math::GSL::Permutationc::GSL_EUNIMPL;
*GSL_ECACHE = *Math::GSL::Permutationc::GSL_ECACHE;
*GSL_ETABLE = *Math::GSL::Permutationc::GSL_ETABLE;
*GSL_ENOPROG = *Math::GSL::Permutationc::GSL_ENOPROG;
*GSL_ENOPROGJ = *Math::GSL::Permutationc::GSL_ENOPROGJ;
*GSL_ETOLF = *Math::GSL::Permutationc::GSL_ETOLF;
*GSL_ETOLX = *Math::GSL::Permutationc::GSL_ETOLX;
*GSL_ETOLG = *Math::GSL::Permutationc::GSL_ETOLG;
*GSL_EOF = *Math::GSL::Permutationc::GSL_EOF;

@EXPORT_OK = qw/
                gsl_permutation_alloc
                gsl_permutation_calloc
                gsl_permutation_init
                gsl_permutation_free
                gsl_permutation_memcpy
                gsl_permutation_fread
                gsl_permutation_fwrite
                gsl_permutation_fscanf
                gsl_permutation_fprintf
                gsl_permutation_size
                gsl_permutation_data
                gsl_permutation_get
                gsl_permutation_swap
                gsl_permutation_valid
                gsl_permutation_reverse
                gsl_permutation_inverse
                gsl_permutation_next
                gsl_permutation_prev
                gsl_permutation_mul
                gsl_permutation_linear_to_canonical
                gsl_permutation_canonical_to_linear
                gsl_permutation_inversions
                gsl_permutation_linear_cycles
                gsl_permutation_canonical_cycles
                gsl_permute
                gsl_permute_inverse
                gsl_permute_int
                gsl_permute_int_inverse
                gsl_permute_vector
                gsl_permute_vector_inverse
                gsl_permute_vector_int
                gsl_permute_vector_int_inverse
            /;
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );

### wrapper interface ###
sub new {
    my ($class, $value) = @_;
    my $this = {};
    $this->{_length} = $value;
    $this->{_permutation} = gsl_permutation_calloc($value);
    bless $this, $class;
}

sub as_list {
    my $self=shift;
    $self->get( [ 0 .. $self->length - 1  ] );
}

sub get {
    my ($self, $indices) = @_;
    return  map {  gsl_permutation_get($self->{_permutation}, $_ ) } @$indices ;
}

sub raw { (shift)->{_permutation} }
sub length { (shift)->{_length} }




__END__

=encoding utf8

=head1 NAME

Math::GSL::Permutation - functions for creating and manipulating permutations

=head1 SYNOPSIS

 use Math::GSL::Permutation qw/:all/;
 my $permutation = Math::GSL::Permutation->new(30); # allocate and initialize a permutation of size 30
 my $length = $permutation->length; # returns the length of the permutation object, here it is 30
 gsl_permutation_swap($permutation->raw, 2,7);
 # the raw method is made to use the underlying permutation structure of the permutation object
 my $value = $permutation->get(2); # returns the third value (starting from 0) of the permutation
 my @values = $permutation->as_list; # returns all the values of the permutation
 my @set = $permutation->get([0,1,2,3]); # returns the four first values of the permutation

=head1 DESCRIPTION

Here is a list of all the functions included in this module :

=over

=item gsl_permutation_alloc($n) - return a newly allocated permutation of size $n

=item gsl_permutation_calloc($n) - return a newly allocated permutation of size $n which is initialized to the identity

=item gsl_permutation_init($p) - initialize the permutation $p to the identity i.e. (0,1,2, ..., n-1)

=item gsl_permutation_free($p) - free all the memory use by the permutaion $p

=item gsl_permutation_memcpy($dest, $src) - copy the permutation $src into the permutation $dest, the two permutations must have the same length and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_fread($stream, $p) -  This function reads into the permutation $p from the open stream $stream (opened with the gsl_fopen function from the Math::GSL module) in binary format. The permutation $p must be preallocated with the correct length since the function uses the size of $p to determine how many bytes to read. The function returns 1 if there was a problem reading from the file. The data is assumed to have been written in the native binary format on the same architecture.

=item gsl_permutation_fwrite($stream, $p) - This function writes the elements of the permutation $p to the stream $stream (opened with the gsl_fopen function from the Math::GSL module) in binary format. The function returns 1 if there was a problem writing to the file. Since the data is written in the native binary format it may not be portable between different architectures.

=item gsl_permutation_fscanf($stream, $p) - This function reads formatted data from the stream $stream (opened with the gsl_fopen function from the Math::GSL module) into the permutation $p. The permutation $p must be preallocated with the correct length since the function uses the size of $p to determine how many numbers to read. The function returns 1 if there was a problem reading from the file.

=item gsl_permutation_fprintf($stream, $p, $format) - This function writes the elements of the permutation $p line-by-line to the stream $stream (opened with the gsl_fopen function from the Math::GSL module) using the format specifier $format, which should be suitable. "%zu\n" is a suitable format. The function returns 1 if there was a problem writing to the file.

=item gsl_permutation_size($p) - return the size of the permutation $p

=item gsl_permutation_data

=item gsl_permutation_get($p, $i) - return the $i-th element of the permutation $p, return 0 if $i is outside the range of 0 to n-1

=item gsl_permutation_swap($p, $i, $j) - exchange the $i-th position and the $j-th position of the permutation $p and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_valid($p) - return 0 if the permutation $p is valid (if the n elements contain each of the numbers 0 to n-1 once and only once), 1 otherwise

=item gsl_permutation_reverse($p) - reverse the elements of the permutation $p

=item gsl_permutation_inverse($inv, $p) - compute the inverse of the permutation $p, storing it in $inv and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_next($p) - advance the permutation $p to the next permutation in lexicographic order and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_prev($p) - step backward from the permutation $p to the previous permutation in lexicographic order and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_mul($p, $pa, $pb) - combine the two permutation $pa and $pb into a single permutation $p and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_linear_to_canonical($q, $p) - compute the canonical form the permutation $p and store it in $q and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_canonical_to_linear($p, $q) - convert a canonical permutation $q back into linear form and store it in $p and return 0 if the operation succeeded, 1 otherwise

=item gsl_permutation_inversions($p) - return the number of inversions in the permutation $p

=item gsl_permutation_linear_cycles($p) - return the number of cycles in the permutation $p, given a linear form

=item gsl_permute_vector_int_inversegsl_permutation_canonical_cycles($p) - return the number of cycles in the permutation $p, given a canonical form

=item gsl_permute

=item gsl_permute_inverse

=item gsl_permute_int

=item gsl_permute_int_inverse

=item gsl_permute_vector

=item gsl_permute_vector_inverse

=item gsl_permute_vector_int

=back

 You have to add the functions you want to use inside the qw/put_function_here/ with spaces between each function.
 You can also write use Math::GSL::CDF qw/:all/ to use all available functions of the module.
 Other tags are also available, here is a complete list of all tags for this module.
For more information on the functions, we refer you to the GSL official documentation:
L<http://www.gnu.org/software/gsl/manual/html_node/>



=head1 EXAMPLES

 use Math::GSL::Permutation qw/:all/;
 $p->{permutation} = gsl_permutation_calloc(5);
 print "The permutation contains [";
 map { print gsl_permutation_get($p->{permutation}, $_) . ", " } (0..3);
 print gsl_permutation_get($p->{permutation}, 4);
 print "] \n";
 print "We'll then swap the first and last elements of the permutation...\n";
 gsl_permutation_swap($p->{permutation}, 0, 4);
 print "The permutation now contains [";
 map { print gsl_permutation_get($p->{permutation},$_) . ", " } (0..3);
 print gsl_permutation_get($p->{permutation}, 4);
 print "] \n";


 use Math::GSL::Permutation qw/:all/;
 use Math::GSL::Vector qw/:all/;
 my $p->{permutation} = gsl_permutation_calloc(6);
 gsl_permutation_init($p->{permutation});
 gsl_permutation_swap($p->{permutation}, 0, 1);
 print "The permutation has his first and second elements swapped : [";
 map { print gsl_permutation_get($p->{permutation}, $_) . "," } (0..4);
 print gsl_permutation_get($p->{permutation}, 5) . "] \n";
 my $vec->{vector} = gsl_vector_alloc(6);
 map { gsl_vector_set($vec->{vector}, $_, $_) } (0..5);

 print "We will now apply the permutation to this vector : [";
 map { print gsl_vector_get($vec->{vector}, $_) . "," } (0..4);
 print gsl_vector_get($vec->{vector}, 5) . "] \n";
 gsl_permute_vector($p->{permutation}, $vec->{vector});
 print "The vector is now : [";
 map { print gsl_vector_get($vec->{vector}, $_) . "," } (0..4);
 print gsl_vector_get($vec->{vector}, 5) . "] \n";


=head1 AUTHORS

Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut
1;