version bump
[Math-GSL.git] / Permutation.i
blob6c74665b9395a439600ba7f0cafbddcb1f31d1a2
1 %module "Math::GSL::Permutation"
2 %include "typemaps.i"
3 %include "gsl_typemaps.i"
5 int fclose(FILE *);
7 %{
8 #include "gsl/gsl_permute.h"
9 #include "gsl/gsl_permute_double.h"
10 #include "gsl/gsl_permute_int.h"
11 #include "gsl/gsl_permute_vector.h"
12 #include "gsl/gsl_permute_vector_double.h"
13 #include "gsl/gsl_permute_vector_int.h"
14 #include "gsl/gsl_permutation.h"
16 %include "gsl/gsl_permute.h"
17 %include "gsl/gsl_permute_double.h"
18 %include "gsl/gsl_permute_int.h"
19 %include "gsl/gsl_permute_vector.h"
20 %include "gsl/gsl_permute_vector_double.h"
21 %include "gsl/gsl_permute_vector_int.h"
22 %include "gsl/gsl_permutation.h"
24 %perlcode %{
25 @EXPORT_OK = qw/fclose
26 gsl_permutation_alloc
27 gsl_permutation_calloc
28 gsl_permutation_init
29 gsl_permutation_free
30 gsl_permutation_memcpy
31 gsl_permutation_fread
32 gsl_permutation_fwrite
33 gsl_permutation_fscanf
34 gsl_permutation_fprintf
35 gsl_permutation_size
36 gsl_permutation_data
37 gsl_permutation_get
38 gsl_permutation_swap
39 gsl_permutation_valid
40 gsl_permutation_reverse
41 gsl_permutation_inverse
42 gsl_permutation_next
43 gsl_permutation_prev
44 gsl_permutation_mul
45 gsl_permutation_linear_to_canonical
46 gsl_permutation_canonical_to_linear
47 gsl_permutation_inversions
48 gsl_permutation_linear_cycles
49 gsl_permutation_canonical_cycles
50 gsl_permute
51 gsl_permute_inverse
52 gsl_permute_int
53 gsl_permute_int_inverse
54 gsl_permute_vector
55 gsl_permute_vector_inverse
56 gsl_permute_vector_int
57 gsl_permute_vector_int_inverse
59 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
61 ### wrapper interface ###
62 sub new {
63 my ($class, $value) = @_;
64 my $this = {};
65 $this->{_length} = $value;
66 $this->{_permutation} = gsl_permutation_calloc($value);
67 bless $this, $class;
70 sub as_list {
71 my $self=shift;
72 $self->get( [ 0 .. $self->length - 1 ] );
75 sub get {
76 my ($self, $indices) = @_;
77 return map { gsl_permutation_get($self->{_permutation}, $_ ) } @$indices ;
80 sub raw { (shift)->{_permutation} }
81 sub length { (shift)->{_length} }
86 __END__
88 =head1 NAME
90 Math::GSL::Permutation - functions for creating and manipulating permutations
92 =head1 SYNOPSIS
94 use Math::GSL::Permutation qw/:all/;
95 my $permutation = Math::GSL::Permutation->new(30); # allocate and initialize a permutation of size 30
96 my $lenght = $permutation->lenght; # returns the lenght of the permutation object, here it is 30
97 gsl_permutation_swap($permutation->raw, 2,7);
98 # the raw method is made to use the underlying permutation structure of the permutation object
99 my $value = $permutation->get(2); # returns the third value (starting from 0) of the permutation
100 my @values = $permutation->as_list; # returns all the values of the permutation
101 my @set = $permutation->get([0,1,2,3]); # returns the four first values of the permutation
103 =head1 DESCRIPTION
105 Here is a list of all the functions included in this module :
107 =over
109 =item gsl_permutation_alloc($n) - return a newly allocated permutation of size $n
111 =item gsl_permutation_calloc($n) - return a newly allocated permutation of size $n which is initialized to the identity
113 =item gsl_permutation_init($p) - initialize the permutation $p to the identity i.e. (0,1,2, ..., n-1)
115 =item gsl_permutation_free($p) - free all the memory use by the permutaion $p
117 =item gsl_permutation_memcpy($dest, $src) - copy the permutation $src into the permutation $dest, the two permutations must have the same lenght and return 0 if the operation suceeded, 1 otherwise
119 =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.
121 =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.
123 =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.
125 =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.
127 =item gsl_permutation_size($p) - return the size of the permutation $p
129 =item gsl_permutation_data
131 =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
133 =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 suceeded, 1 otherwise
135 =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
137 =item gsl_permutation_reverse($p) - reverse the elements of the permutation $p
139 =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
141 =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
143 =item gsl_permutation_prev($p) - step backward from the permutation $p to the previous permutation in lexicographic order and return 0 if the operation suceeded, 1 otherwise
145 =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 suceeded, 1 otherwise
147 =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 suceeded, 1 otherwise
149 =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 suceeded, 1 otherwise
151 =item gsl_permutation_inversions($p) - return the number of inversions in the permutation $p
153 =item gsl_permutation_linear_cycles($p) - return the number of cycles in the permutation $p, given a linear form
155 =item gsl_permute_vector_int_inversegsl_permutation_canonical_cycles($p) - return the number of cycles in the permutation $p, given a canonical form
157 =item gsl_permute
159 =item gsl_permute_inverse
161 =item gsl_permute_int
163 =item gsl_permute_int_inverse
165 =item gsl_permute_vector
167 =item gsl_permute_vector_inverse
169 =item gsl_permute_vector_int
171 =back
173 You have to add the functions you want to use inside the qw/put_funtion_here/ with spaces between each function.
174 You can also write use Math::GSL::CDF qw/:all/ to use all avaible functions of the module.
175 Other tags are also avaible, here is a complete list of all tags for this module.
176 For more informations on the functions, we refer you to the GSL offcial documentation:
177 L<http://www.gnu.org/software/gsl/manual/html_node/>
179 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
181 =head1 EXAMPLES
183 use Math::GSL::Permutation qw/:all/;
184 $p->{permutation} = gsl_permutation_calloc(5);
185 print "The permutation contains [";
186 map { print gsl_permutation_get($p->{permutation}, $_) . ", " } (0..3);
187 print gsl_permutation_get($p->{permutation}, 4);
188 print "] \n";
189 print "We'll then swap the first and last elements of the permutation...\n";
190 gsl_permutation_swap($p->{permutation}, 0, 4);
191 print "The permutation now contains [";
192 map { print gsl_permutation_get($p->{permutation},$_) . ", " } (0..3);
193 print gsl_permutation_get($p->{permutation}, 4);
194 print "] \n";
197 use Math::GSL::Permutation qw/:all/;
198 use Math::GSL::Vector qw/:all/;
199 my $p->{permutation} = gsl_permutation_calloc(6);
200 gsl_permutation_init($p->{permutation});
201 gsl_permutation_swap($p->{permutation}, 0, 1);
202 print "The permutation has his first and second elements swapped : [";
203 map { print gsl_permutation_get($p->{permutation}, $_) . "," } (0..4);
204 print gsl_permutation_get($p->{permutation}, 5) . "] \n";
205 my $vec->{vector} = gsl_vector_alloc(6);
206 map { gsl_vector_set($vec->{vector}, $_, $_) } (0..5);
208 print "We will now apply the permutation to this vector : [";
209 map { print gsl_vector_get($vec->{vector}, $_) . "," } (0..4);
210 print gsl_vector_get($vec->{vector}, 5) . "] \n";
211 gsl_permute_vector($p->{permutation}, $vec->{vector});
212 print "The vector is now : [";
213 map { print gsl_vector_get($vec->{vector}, $_) . "," } (0..4);
214 print gsl_vector_get($vec->{vector}, 5) . "] \n";
217 =head1 AUTHORS
219 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
221 =head1 COPYRIGHT AND LICENSE
223 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
225 This program is free software; you can redistribute it and/or modify it
226 under the same terms as Perl itself.
228 =cut