1 %module
"Math::GSL::Combination"
3 %include
"gsl_typemaps.i"
5 #include
"gsl/gsl_types.h"
6 #include
"gsl/gsl_combination.h"
9 %include
"gsl/gsl_types.h"
10 %include
"gsl/gsl_combination.h"
15 gsl_combination_calloc
16 gsl_combination_init_first
17 gsl_combination_init_last
19 gsl_combination_memcpy
21 gsl_combination_fwrite
22 gsl_combination_fscanf
23 gsl_combination_fprintf
32 %EXPORT_TAGS
= ( all
=> [ @EXPORT_OK
] );
36 Math
::GSL
::Combination
- Functions for creating and manipulating combinations
40 use Math
::GSL qw
/:all
/;
41 use Math
::GSL
::Combination qw
/:all
/;
43 my $c
= Math
::GSL
::Combination-
>new
(6,3);
44 print join
(" ", $c-
>as_list
) .
"\n";
46 print join
(" ", $c-
>as_list
) .
"\n";
48 my $fd
= gsl_fopen
('combination.dat'
, 'w'
);
49 gsl_combination_fwrite
($fd
, $c-
>raw
);
54 Here is a list of all the functions in this module
:
58 =item
* C
<gsl_combination_alloc
($n
, $k
)>
60 This function allocates memory for a new combination with parameters $n
, $k.
61 The combination is not initialized and its elements are undefined. Use the
62 function gsl_combination_calloc if you want to create a combination which is
63 initialized to the lexicographically first combination.
65 =item
* C
<gsl_combination_calloc
($n
, $k
)>
67 This function allocates memory for a new combination with parameters $n
, $k and
68 initializes it to the lexicographically first combination.
70 =item
* C
<gsl_combination_init_first
($c
)>
72 This function initializes the combination $c to the lexicographically first
73 combination
, i.e.
(0,1,2,...
,k-1
).
75 =item
* C
<gsl_combination_init_last
($c
)>
77 This function initializes the combination $c to the lexicographically last
78 combination
, i.e.
(n-k
,n-k
+1,...
,n-1
).
80 =item
* C
<gsl_combination_free
($c
)>
82 This function frees all the memory used by the combination $c.
84 =item
* C
<gsl_combination_memcpy
($dest
, $src
)>
86 This function copies the elements of the combination $src into the combination
87 $dest. The two combinations must have the same size.
89 =item
* C
<gsl_combination_get
($c
, $i
)>
91 This function returns the value of the i-th element of the combination $c. If
92 $i lies outside the allowed range of
0 to k-1 then the error handler is invoked
95 =item
* C
<gsl_combination_fwrite
($stream
, $c
)>
97 This function writes the elements of the combination $c to the stream $stream
,
98 opened with the gsl_fopen function from the Math
::GSL module
, in binary format.
99 The function returns $GSL_EFAILED if there was a problem writing to the file.
100 Since the data is written in the native binary format it may not be portable
101 between different architectures.
103 =item
* C
<gsl_combination_fread
($stream
, $c
)>
105 This function reads elements from the open stream $stream
, opened with the
106 gsl_fopen function from the Math
::GSL module
, into the combination $c in binary
107 format. The combination $c must be preallocated with correct values of n and k
108 since the function uses the size of $c to determine how many bytes to read. The
109 function returns $GSL_EFAILED if there was a problem reading from the file. The
110 data is assumed to have been written in the native binary format on the same
113 =item
* C
<gsl_combination_fprintf
($stream
, $c
, $format
)>
115 This function writes the elements of the combination $c line-by-line to the
116 stream $stream
, opened with the gsl_fopen function from the Math
::GSL module
,
117 using the format specifier $format
, which should be suitable for a type of
118 size_t. In ISO C99 the type modifier z represents size_t
, so
"%zu\n" is a
119 suitable format. The function returns $GSL_EFAILED if there was a problem
122 =item
* C
<gsl_combination_fscanf
($stream
, $c
)>
124 This function reads formatted data from the stream $stream into the combination
125 $c. The combination $c must be preallocated with correct values of n and k
126 since the function uses the size of $c to determine how many numbers to read.
127 The function returns $GSL_EFAILED if there was a problem reading from the file.
129 =item
* C
<gsl_combination_n
($c
)>
131 This function returns the range
(n
) of the combination $c.
133 =item
* C
<gsl_combination_k
($c
)>
135 This function returns the number of elements
(k
) in the combination $c.
137 =item
* C
<gsl_combination_data
($c
)>
139 This function returns a pointer to the array of elements in the combination $c.
141 =item
* C
<gsl_combination_valid
($c
)>
143 This function checks that the combination $c is valid. The k elements should
144 lie in the range
0 to n-1
, with each value occurring once at most and in
147 =item
* C
<gsl_combination_next
($c
)>
149 This function advances the combination $c to the next combination in
150 lexicographic order and returns $GSL_SUCCESS. If no further combinations are
151 available it returns $GSL_FAILURE and leaves $c unmodified. Starting with the
152 first combination and repeatedly applying this function will iterate through
153 all possible combinations of a given order.
155 =item
* C
<gsl_combination_prev
($c
)>
157 This function steps backwards from the combination $c to the previous
158 combination in lexicographic order
, returning $GSL_SUCCESS. If no previous
159 combination is available it returns $GSL_FAILURE and leaves $c unmodified.
166 my
($class
, $n
, $k
) = @_
;
168 $this-
>{_length
} = $n
;
169 $this-
>{_combination
} = gsl_combination_calloc
($n
, $k
);
175 $self-
>get
( [ 0 .. $self-
>elements
- 1 ] );
179 my
($self
, $indices
) = @_
;
180 return map
{ gsl_combination_get
($self-
>{_combination
}, $_
) } @$indices
;
183 sub raw
{ (shift
)->{_combination
} }
184 sub length
{ (shift
)->{_length
} }
188 return gsl_combination_k
($self-
>{_combination
});
192 my
($self
,$status
) = @_
;
193 if
(defined $status
) {
194 $self-
>{status
} = $status
;
197 return $self-
>{status
};
203 my $status
= gsl_combination_next
($self-
>{_combination
});
204 $self-
>status
($status
);
210 my $status
= gsl_combination_prev
($self-
>{_combination
});
211 $self-
>status
($status
);
217 For more informations on the functions
, we refer you to the GSL offcial
218 documentation
: L
<http
://www.gnu.org
/software
/gsl
/manual
/html_node
/>
220 Tip
: search on google
: site
:http
://www.gnu.org
/software
/gsl
/manual
/html_node
/ name_of_the_function_you_want
225 Jonathan Leto
<jonathan@leto.net
> and Thierry Moisan
<thierry.moisan@gmail.com
>
227 =head1 COPYRIGHT
AND LICENSE
229 Copyright
(C
) 2008 Jonathan Leto and Thierry Moisan
231 This program is free software
; you can redistribute it and
/or modify it
232 under the same terms as Perl itself.