Finishing the Sort functions documentation and adding a Performance section with...
[Math-GSL.git] / Sort.i
blob17b5b7b39bc1455afc1d631eaa5d1641a9bd6559
1 %module "Math::GSL::Sort"
2 /* Danger Will Robinson! */
4 %include "typemaps.i"
5 %include "gsl_typemaps.i"
7 %typemap(argout) (double * data, const size_t stride, const size_t n) {
8 int i=0;
9 AV* tempav = newAV();
11 while( i < $3 ) {
12 av_push(tempav, newSVnv((double) $1[i]));
13 i++;
16 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
17 //Perl_sv_dump($result);
18 argvi++;
20 %typemap(argout) (double * dest, const size_t k, const gsl_vector * v) {
21 int i=0;
22 AV* tempav = newAV();
24 while( i < $2 ) {
25 av_push(tempav, newSVnv((double) $1[i]));
26 i++;
29 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
30 argvi++;
33 %typemap(argout) (double * dest, const size_t k, const double * src, const size_t stride, const size_t n) {
34 int i=0;
35 AV* tempav = newAV();
36 while( i < $2 ) {
37 av_push(tempav, newSVnv((double) $1[i]));
38 i++;
41 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
42 argvi++;
44 %typemap(argout) (size_t * p, const size_t k, const gsl_vector * v)
46 int i=0;
47 AV* tempav = newAV();
48 while( i < $2 ) {
49 av_push(tempav, newSVnv((double) $1[i]));
50 i++;
53 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
54 argvi++;
57 %typemap(argout) (size_t * p, const double * data, const size_t stride, const size_t n)
59 int i=0;
60 AV* tempav = newAV();
61 while( i < $4 ) {
62 av_push(tempav, newSVnv((size_t) $1[i]));
63 i++;
66 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
67 argvi++;
70 %typemap(argout) (size_t * p, const size_t k, const double * src, const size_t stride, const size_t n)
72 int i=0;
73 AV* tempav = newAV();
74 while( i < $2 ) {
75 av_push(tempav, newSVnv((size_t) $1[i]));
76 i++;
79 $result = sv_2mortal( newRV_noinc( (SV*) tempav) );
80 argvi++;
83 %apply double * { double *data, double *dest };
86 #include "gsl/gsl_nan.h"
87 #include "gsl/gsl_sort.h"
88 #include "gsl/gsl_sort_double.h"
89 #include "gsl/gsl_sort_int.h"
90 #include "gsl/gsl_sort_vector.h"
91 #include "gsl/gsl_sort_vector_double.h"
92 #include "gsl/gsl_sort_vector_int.h"
93 #include "gsl/gsl_permutation.h"
95 %include "gsl/gsl_nan.h"
96 %include "gsl/gsl_sort.h"
97 %include "gsl/gsl_sort_double.h"
98 %include "gsl/gsl_sort_int.h"
99 %include "gsl/gsl_sort_vector.h"
100 %include "gsl/gsl_sort_vector_double.h"
101 %include "gsl/gsl_sort_vector_int.h"
102 %include "gsl/gsl_permutation.h"
105 %perlcode %{
106 @EXPORT_plain = qw/
107 gsl_sort gsl_sort_index
108 gsl_sort_smallest gsl_sort_smallest_index
109 gsl_sort_largest gsl_sort_largest_index
111 @EXPORT_vector= qw/
112 gsl_sort_vector gsl_sort_vector_index
113 gsl_sort_vector_smallest gsl_sort_vector_smallest_index
114 gsl_sort_vector_largest gsl_sort_vector_largest_index
116 @EXPORT_OK = ( @EXPORT_plain, @EXPORT_vector );
117 %EXPORT_TAGS = (
118 all => [ @EXPORT_OK ],
119 plain => [ @EXPORT_plain ],
120 vector => [ @EXPORT_vector ],
122 __END__
124 =head1 NAME
126 Math::GSL::Sort - Functions for sorting data
128 =head1 SYNOPSIS
130 use Math::GSL::Sort qw/:all/;
131 my $x = [ 2**15, 1.67, 20e5,
132 -17, 6900, 1/3 , 42e-10 ];
133 my $sorted = gsl_sort($x, 1, $#$x+1 );
134 my $array = [2,3];
135 my ($status, $smallest) = gsl_sort_smallest($array, 2, $x, 1, $#$x+1);
137 =head1 PERFORMANCE
139 In the source code of Math::GSL, the file "examples/benchmark/sort" sorts a 5000 elements random array by Perl's internal sort() function and compares it to Math::GSL's gsl_sort(). This is what it outputs on a T42 IBM laptop:
141 Benchmark: timing 1000 iterations of Math::GSL sort, perl sort ...
142 Math::GSL sort: 3 wallclock secs ( 2.42 usr + 0.05 sys = 2.47 CPU)
143 @ 404.86/s (n=1000)
144 perl sort : 15 wallclock secs (10.94 usr + 0.01 sys = 10.95 CPU)
145 @ 91.32/s (n=1000)
147 So it looks like for arrays of this length, Math::GSL's gsl_sort is
148 about 4.5 times faster that sort()!
150 You can also use this command : ./examples/benchmark/sort 1000 50000
151 to sort a 50000 elements random array for 1000 iterations
153 which gave these results on the same computer:
155 Benchmark: timing 1000 iterations of Math::GSL sort, perl sort ...
156 Math::GSL sort: 39 wallclock secs (29.50 usr + 0.36 sys = 29.86 CPU)
157 @ 33.49/s (n=1000)
158 perl sort : 221 wallclock secs (163.62 usr + 0.19 sys = 163.81
159 CPU) @ 6.10/s (n=1000)
161 This performance ratio is 5.5, so at first glance it seems that gsl_sort() gets increasingly faster than sort() for larger arrays. This will of course have to be proved out with some rigorous benchmarks, that are yet to come.
163 =head1 DESCRIPTION
165 Here is a list of all the functions included in this module :
167 =over
169 =item * gsl_sort_vector($v) - This function sorts the elements of the vector $v into ascending numerical order.
171 =item * gsl_sort_vector_index($p, $v) - This function indirectly sorts the elements of the vector $v into ascending order, storing the resulting permutation in $p. The elements of $p give the index of the vector element which would have been stored in that position if the vector had been sorted in place. The first element of $p gives the index of the least element in $v, and the last element of $p gives the index of the greatest element in $v. The vector $v is not changed.
173 =item * gsl_sort_vector_smallest($array, $k, $vector) - This function outputs 0 if the operation succeeded, 1 otherwise and then the $k smallest elements of the vector $v. $k must be less than or equal to the length of the vector $v.
175 =item * gsl_sort_vector_smallest_index($p, $k, $v) - This function outputs 0 if the operation succeeded, 1 otherwise and then the indices of the $k smallest elements of the vector $v. $p must be a prealocated array reference. This should be removed in further versions. $k must be less than or equal to the length of the vector $v.
177 =item * gsl_sort_vector_largest($array, $k, $vector) - This function outputs 0 if the operation succeeded, 1 otherwise and then the $k largest elements of the vector $v. $k must be less than or equal to the length of the vector $v.
179 =item * gsl_sort_vector_largest_index($p, $k, $v) - This function outputs 0 if the operation succeeded, 1 otherwise and then the indices of the $k largest elements of the vector $v. $p must be a prealocated array reference. This should be removed in further versions. $k must be less than or equal to the length of the vector $v.
181 =item * gsl_sort($data, $stride, $n) - This function returns an array reference to the sorted $n elements of the array $data with stride $stride into ascending numerical order.
183 =item * gsl_sort_index($p, $data, $stride, $n) - This function indirectly sorts the $n elements of the array $data with stride $stride into ascending order, outputting the permutation in the foram of an array. $p must be a prealocated array reference. This should be removed in further versions. The array $data is not changed.
185 =item * gsl_sort_smallest($array, $k, $data, $stride, $n) - This function outputs 0 if the operation succeeded, 1 otherwise and then the $k smallest elements of the array $data, of size $n and stride $stride, in ascending numerical. The size $k of the subset must be less than or equal to $n. The data $src is not modified by this operation. $array must be a prealocated array reference. This should be removed in further versions.
187 =item * gsl_sort_smallest_index($p, $k, $src, $stride, $n) - This function outputs 0 if the operation succeeded, 1 otherwise and then the indices of the $k smallest elements of the array $src, of size $n and stride $stride. The indices are chosen so that the corresponding data is in ascending numerical order. $k must be less than or equal to $n. The data $src is not modified by this operation. $p must be a prealocated array reference. This should be removed in further versions.
189 =item * gsl_sort_largest($array, $k, $data, $stride, $n) - This function outputs 0 if the operation succeeded, 1 otherwise and then the $k largest elements of the array $data, of size $n and stride $stride, in ascending numerical. The size $k of the subset must be less than or equal to $n. The data $src is not modified by this operation. $array must be a prealocated array reference. This should be removed in further versions.
191 =item * gsl_sort_largest_index($p, $k, $src, $stride, $n) - This function outputs 0 if the operation succeeded, 1 otherwise and then the indices of the $k largest elements of the array $src, of size $n and stride $stride. The indices are chosen so that the corresponding data is in ascending numerical order. $k must be less than or equal to $n. The data $src is not modified by this operation. $p must be a prealocated array reference. This should be removed in further versions.
193 =back
195 You have to add the functions you want to use inside the qw /put_funtion_here /.
196 You can also write use Math::GSL::Sort qw/:all/ to use all avaible functions of the module.
197 Other tags are also avaible, here is a complete list of all tags for this module :
199 =over
201 =item all
203 =item plain
205 =item vector
207 =back
209 For more informations on the functions, we refer you to the GSL offcial documentation:
210 L<http://www.gnu.org/software/gsl/manual/html_node/>
212 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
214 =head1 AUTHORS
216 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
218 =head1 COPYRIGHT AND LICENSE
220 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
222 This program is free software; you can redistribute it and/or modify it
223 under the same terms as Perl itself.
225 =cut