Apply nan/inf handling patch from Sisyphus
[Math-GSL.git] / Vector.i
blobcb04022d16f5b51b520aaa4ab249427617b68ef7
1 %module "Math::GSL::Vector"
3 %include "typemaps.i"
5 %apply int *OUTPUT { size_t *imin, size_t *imax };
7 %apply double *OUTPUT { double * min_out, double * max_out };
9 %typemap(in) double *v {
10 AV *tempav;
11 I32 len;
12 int i;
13 SV **tv;
14 if (!SvROK($input))
15 croak("Math::GSL : $input is not a reference!");
16 if (SvTYPE(SvRV($input)) != SVt_PVAV)
17 croak("Math::GSL : $input is not an array ref!");
19 tempav = (AV*)SvRV($input);
20 len = av_len(tempav);
21 $1 = (double *) malloc((len+1)*sizeof(double));
22 for (i = 0; i <= len; i++) {
23 tv = av_fetch(tempav, i, 0);
24 $1[i] = (double) SvNV(*tv);
28 FILE * fopen(char *, char *);
29 int fclose(FILE *);
32 #include "gsl/gsl_nan.h"
33 #include "gsl/gsl_vector.h"
34 #include "gsl/gsl_vector_char.h"
35 #include "gsl/gsl_vector_complex.h"
36 #include "gsl/gsl_vector_complex_double.h"
37 #include "gsl/gsl_vector_double.h"
38 #include "gsl/gsl_vector_float.h"
39 #include "gsl/gsl_vector_int.h"
42 %include "gsl/gsl_nan.h"
43 %include "gsl/gsl_vector.h"
44 %include "gsl/gsl_vector_char.h"
45 %include "gsl/gsl_vector_complex.h"
46 %include "gsl/gsl_vector_complex_double.h"
47 %include "gsl/gsl_vector_double.h"
48 %include "gsl/gsl_vector_int.h"
52 %perlcode %{
53 use Scalar::Util 'blessed';
54 use Carp qw/croak/;
55 use overload
56 '*' => \&_multiplication,
57 fallback => 1,
60 @EXPORT_all = qw/fopen fclose
61 gsl_vector_alloc gsl_vector_calloc gsl_vector_alloc_from_block gsl_vector_alloc_from_vector
62 gsl_vector_free gsl_vector_view_array gsl_vector_const_view_array gsl_vector_view_array_with_stride
63 gsl_vector_const_view_array_with_stride gsl_vector_subvector gsl_vector_subvector_wi gsl_vector_subvector_with_stride
64 gsl_vector_const_subvec gsl_vector_const_subvec gsl_vector_get gsl_vector_set
65 gsl_vector_ptr gsl_vector_const_ptr gsl_vector_set_zero gsl_vector_set_all
66 gsl_vector_set_basis gsl_vector_fread gsl_vector_fwrite gsl_vector_fscanf
67 gsl_vector_fprintf gsl_vector_memcpy gsl_vector_reverse gsl_vector_swap
68 gsl_vector_swap_elements gsl_vector_max gsl_vector_min gsl_vector_minmax
69 gsl_vector_max_index gsl_vector_min_index gsl_vector_minmax_index
70 gsl_vector_add gsl_vector_sub gsl_vector_mul gsl_vector_div
71 gsl_vector_scale gsl_vector_add_constant gsl_vector_isnull
72 gsl_vector_ispos gsl_vector_isneg gsl_vector_isnonneg
73 gsl_vector_float_alloc gsl_vector_float_calloc gsl_vector_float_alloc_from_block
74 gsl_vector_float_alloc_from_vector gsl_vector_float_free gsl_vector_float_view_array
75 gsl_vector_float_view_array_with_stride gsl_vector_float_const_view_array gsl_vector_float_const_view_array_with_stride
76 gsl_vector_float_subvector gsl_vector_float_subvector_with_stride gsl_vector_float_const_subvector
77 gsl_vector_float_const_subvector_with_stride gsl_vector_float_get gsl_vector_float_set gsl_vector_float_ptr
78 gsl_vector_float_const_ptr gsl_vector_float_set_zero gsl_vector_float_set_all gsl_vector_float_set_basis
79 gsl_vector_float_fread gsl_vector_float_fwrite gsl_vector_float_fscanf gsl_vector_float_fprintf
80 gsl_vector_float_memcpy gsl_vector_float_reverse gsl_vector_float_swap gsl_vector_float_swap_elements
81 gsl_vector_float_max gsl_vector_float_min gsl_vector_float_minmax gsl_vector_float_max_index gsl_vector_float_min_index
82 gsl_vector_float_minmax_index gsl_vector_float_add gsl_vector_float_sub gsl_vector_float_mul gsl_vector_float_div gsl_vector_float_scale
83 gsl_vector_float_add_constant gsl_vector_float_isnull gsl_vector_float_ispos gsl_vector_float_isneg gsl_vector_float_isnonneg
84 gsl_vector_complex_alloc gsl_vector_complex_calloc gsl_vector_complex_alloc_from_block gsl_vector_complex_alloc_from_vector
85 gsl_vector_complex_free gsl_vector_complex_view_array gsl_vector_complex_view_array_with_stride gsl_vector_complex_const_view_array
86 gsl_vector_complex_const_view_array_with_stride gsl_vector_complex_subvector gsl_vector_complex_subvector_with_stride
87 gsl_vector_complex_const_subvector gsl_vector_complex_const_subvector_with_stride gsl_vector_complex_real gsl_vector_complex_imag
88 gsl_vector_complex_const_real gsl_vector_complex_const_imag gsl_vector_complex_get gsl_vector_complex_set
89 gsl_vector_complex_ptr gsl_vector_complex_const_ptr gsl_vector_complex_set_zero gsl_vector_complex_set_all
90 gsl_vector_complex_set_basis gsl_vector_complex_fread gsl_vector_complex_fwrite gsl_vector_complex_fscanf
91 gsl_vector_complex_fprintf gsl_vector_complex_memcpy gsl_vector_complex_reverse gsl_vector_complex_swap
92 gsl_vector_complex_swap_elements gsl_vector_complex_isnull gsl_vector_complex_ispos gsl_vector_complex_isneg
94 @EXPORT_file =qw/ fopen fclose/;
95 @EXPORT_OK = (@EXPORT_all, @EXPORT_file);
96 %EXPORT_TAGS = ( file => \@EXPORT_file, all => \@EXPORT_all );
98 =head1 NAME
100 Math::GSL::Vector - Functions concerning vectors
102 =head1 SYNOPSIS
104 use Math::GSL::Vector qw/:all/;
105 my $vec1 = Math::GSL::Vector->new([1, 7, 94, 15 ]);
106 my $vec2 = $vec1 * 5;
107 my $vec3 = Math::GSL::Vector>new(10); # 10 element zero vector
109 # set the element at index 1 to 9
110 # and the element at index 3 to 8
111 $vec3->set([ 1, 3 ], [ 9, 8 ]);
113 my @vec = $vec2->as_list; # return elements as Perl list
115 my $dot_product = $vec1 * $vec2;
116 my $length = $vec2->length;
117 my $first = $vec1->get(0);
120 =cut
122 =head1 Objected Oriented Interface to GSL Math::GSL::Vector
124 =head2 Math::GSL::Vector->new()
126 Creates a new Vector of the given size.
128 my $vector = Math::GSL::Matrix->new(3);
130 You can also create and set directly the values of the vector like this :
132 my $vector = Math::GSL::Vector->new([2,4,1]);
134 =cut
135 sub new {
136 my ($class, $values) = @_;
137 my $length = $#$values;
138 my $this = {};
139 my $vector;
140 if ( ref $values eq 'ARRAY' ){
141 die __PACKAGE__.'::new($x) - $x must be a nonempty array reference' if $length == -1;
142 $vector = gsl_vector_alloc($length+1);
143 map { gsl_vector_set($vector, $_, $values->[$_] ) } (0 .. $length);
144 $this->{_length} = $length+1;
145 } elsif ( (int($values) == $values) && ($values > 0)) {
146 $vector = gsl_vector_alloc($values);
147 gsl_vector_set_zero($vector);
148 $this->{_length} = $values;
149 } else {
150 die __PACKAGE__.'::new($x) - $x must be an int or array reference';
152 $this->{_vector} = $vector;
153 bless $this, $class;
155 =head2 raw()
157 Get the underlying GSL vector object created by SWIG, useful for using gsl_vector_* functions which do not have an OO counterpart.
159 my $vector = Math::GSL::vector->new(3);
160 my $gsl_vector = $vector->raw;
161 my $stuff = gsl_vector_get($gsl_vector, 1);
163 =cut
165 sub raw { (shift)->{_vector} }
167 =head2 min()
169 Returns the minimum value contained in the vector.
171 my $vector = Math::GSL::Vector->new([2,4,1]);
172 my $minimum = $vector->min;
174 =cut
176 sub min {
177 my $self=shift;
178 return gsl_vector_min($self->raw);
181 =head2 max()
183 Returns the minimum value contained in the vector.
185 my $vector = Math::GSL::Vector->new([2,4,1]);
186 my $maximum = $vector->max;
188 =cut
190 sub max {
191 my $self=shift;
192 return gsl_vector_max($self->raw);
195 =head2 length()
197 Returns the number of elements contained in the vector.
199 my $vector = Math::GSL::Vector->new([2,4,1]);
200 my $length = $vector->length;
202 =cut
204 sub length { my $self=shift; $self->{_length} }
206 =head2 as_list()
208 Gets the content of a Math::GSL::Vector object as a Perl list.
210 my $vector = Math::GSL::vector->new(3);
212 my @values = $vector->as_list;
213 =cut
215 sub as_list {
216 my $self=shift;
217 $self->get( [ 0 .. $self->length - 1 ] );
220 =head2 get()
222 Gets the value of an of a Math::GSL::Vector object.
224 my $vector = Math::GSL::vector->new(3);
226 my @values = $vector->get(2);
228 You can also enter an array of indices to receive their corresponding values:
230 my $vector = Math::GSL::vector->new(3);
232 my @values = $vector->get([0,2]);
234 =cut
236 sub get {
237 my ($self, $indices) = @_;
238 return map { gsl_vector_get($self->{_vector}, $_ ) } @$indices ;
241 =head2 set()
243 Sets values of an of a Math::GSL::Vector object.
245 my $vector = Math::GSL::vector->new(3);
246 $vector->set([1,2], [8,23]);
248 This sets the second and third value to 8 and 23.
250 =cut
252 sub set {
253 my ($self, $indices, $values) = @_;
254 die (__PACKAGE__.'::set($indices, $values) - $indices and $values must be array references of the same length')
255 unless ( ref $indices eq 'ARRAY' && ref $values eq 'ARRAY' && $#$indices == $#$values );
256 eval {
257 map { gsl_vector_set($self->{_vector}, $indices->[$_], $values->[$_] ) } (0..$#$indices);
259 return;
262 sub _multiplication {
263 my ($left,$right) = @_;
264 if ( blessed $right && $right->isa('Math::GSL::Vector') ) {
265 return $left->dot_product($right);
266 } else {
267 gsl_vector_scale($left->raw, $right);
269 return $left;
272 sub dot_product {
273 my ($left,$right) = @_;
274 my $sum=0;
275 if ( blessed $right && $right->isa('Math::GSL::Vector') &&
276 blessed $left && $left->isa('Math::GSL::Vector') &&
277 $left->length == $right->length ) {
278 my @l = $left->as_list;
279 my @r = $right->as_list;
280 map { $sum += $l[$_] * $r[$_] } (0..$#l);
281 return $sum;
282 } else {
283 croak "dot_product() must be called with two vectors";
287 =head1 DESCRIPTION
289 Here is a list of all the functions included in this module :
291 =over 1
293 =item C<gsl_vector_alloc($x)> - create a vector of size $x
295 =item C<gsl_vector_calloc($x)> - create a vector of size $x and initializes all the elements of the vector to zero
297 =item C<gsl_vector_alloc_from_block>
299 =item C<gsl_vector_alloc_from_vector>
301 =item C<gsl_vector_free($v)> - free a previously allocated vector $v
303 =item C<gsl_vector_view_array($base, $n)> - This function returns a vector view of an array reference $base. The start of the new vector is given by $base and has $n elements. Mathematically, the i-th element of the new vector v' is given by, v'(i) = $base->[i] where the index i runs from 0 to $n-1. The array containing the elements of v is not owned by the new vector view. When the view goes out of scope the original array will continue to exist. The original memory can only be deallocated by freeing the original pointer base. Of course, the original array should not be deallocated while the view is still in use.
305 =item C<gsl_vector_const_view_array($base, $n)> - This function is equivalent to gsl_vector_view_array but can be used for arrays which are declared const.
307 =item C<gsl_vector_view_array_with_stride($base, $stride, $n)> - This function returns a vector view of an array reference $base with an additional $stride argument. The subvector is formed in the same way as for gsl_vector_view_array but the new vector has $n elements with a step-size of $stride from one element to the next in the original array. Mathematically, the i-th element of the new vector v' is given by, v'(i) = $base->[i*$stride] where the index i runs from 0 to $n-1. Note that the view gives direct access to the underlying elements of the original array. A vector view $view can be passed to any subroutine which takes a vector argument just as a directly allocated vector would be, using $view->{vector}.
309 =item C<gsl_vector_const_view_array_with_stride($base, $stride, $n)> - This function is equivalent to gsl_vector_view_array_with_stride but can be used for arrays which are declared const.
311 =item C<gsl_vector_subvector($v, $offset, $n)> - return a vector_view type which contains a subvector of $v, with a size of $size, starting from the $offset position
313 =item C<gsl_vector_subvector_with_stride($v, $offset, $stride, $size)> - return a vector_view type which contains a subvector of $v, with a size of $size, starting from the $offset position and with a $stride step between each element of $v
315 =item C<gsl_vector_const_subvector>
317 =item C<gsl_vector_get($v, $i)> - return the $i-th element of a vector $v
319 =item C<gsl_vector_set($v, $i, $x)> - return the vector $v with his $i-th element set to $x
321 =item C<gsl_vector_ptr>
323 =item C<gsl_vector_const_ptr>
325 =item C<gsl_vector_set_zero($v)> - set all the elements of $v to 0
327 =item C<gsl_vector_set_all($v, $x)> - set all the elements of $v to $x
329 =item C<gsl_vector_set_basis($v, $i)> - set all the elements of $v to 0 except for the $i-th element which is set to 1 and return 0 if the operation succeded, 1 otherwise.
331 =item C<gsl_vector_fread($file, $v)> - This function reads into the vector $v from the open stream $file opened with gsl_fopen function from the Math::GSL module in binary format. The vector $v must be preallocated with the correct length since the function uses the size of $v to determine how many bytes to read. The return value is 0 for success and 1 if there was a problem reading from the file.
333 =item C<gsl_vector_fwrite($file, $v)> - This function writes the elements of the vector $v to the stream $file opened with gsl_fopen function from the Math::GSL module in binary format. The return value is 0 for success and 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.
335 =item C<gsl_vector_fscanf($file, $v)> This function reads formatted data from the stream $file opened with gsl_fopen function from the Math::GSL module into the vector $v. The vector $v must be preallocated with the correct length since the function uses the size of $v to determine how many numbers to read. The function returns 0 for success and 1 if there was a problem reading from the file.
337 =item C<gsl_vector_fprintf($file, $v, $format)> -This function writes the elements of the vector $v line-by-line to the stream $file opened with gsl_fopen function from the Math::GSL module using the format specifier $format, which should be one of the "%g", "%e" or "%f" formats for floating point numbers and "%d" for integers. The function returns 0 for success and 1 if there was a problem writing to the file.
339 =item C<gsl_vector_memcpy($dest, $src)> - This function copies the elements of the vector $src into the vector $dest and return 0 if the opertaion succeded, 1 otherwise. The two vectors must have the same length.
341 =item C<gsl_vector_reverse($v)> - reverse the order of the elements of the vector $v and return 0 if the opertaion succeded, 1 otherwise
343 =item C<gsl_vector_swap($v, $v2)> - swap the values of the vectors $v and $v2 and return 0 if the opertaion succeded, 1 otherwise
345 =item C<gsl_vector_swap_elements($v, $i, $j)> - permute the elements at position $i and $j in the vector $v and return 0 if the operation succeded, 1 otherwise.
347 =item C<gsl_vector_max($v)> - return the maximum value in the vector $v
349 =item C<gsl_vector_min($v)> - return the minimum value in the vector $v
351 =item C<gsl_vector_minmax($v)> - return two values, the first is the minimum value in the vector $v and the second is the maximum value.
353 =item C<gsl_vector_max_index($v)> - return the position of the maximum value in the vector $v
355 =item C<gsl_vector_min_index($v)> - return the position of the minimum value in the vector $v
357 =item C<gsl_vector_minmax_index> - return two values, the first is the position of the minimum value in the vector $v and the second is the position of the maximum value.
359 =item C<gsl_vector_add($v, $v2)> - add the elements of $v2 to the elements of $v, the two vectors must have the same lenght and return 0 if the operation succeded, 1 otherwise.
361 =item C<gsl_vector_sub($v, $v2)> - substract the elements of $v2 from the elements of $v, the two vectors must have the same lenght and return 0 if the operation succeded, 1 otherwise.
363 =item C<gsl_vector_mul($v, $v2)> - multiply the elements of $v by the elements of $v2, the two vectors must have the same lenght and return 0 if the operation succeded, 1 otherwise.
365 =item C<gsl_vector_div($v, $v2)> - divides the elements of $v by the elements of $v2, the two vectors must have the same lenght and return 0 if the operation succeded, 1 otherwise.
367 =item C<gsl_vector_scale($v, $x)> - multiplty the elements of the vector $v by a constant $x and return 0 if the operation succeded, 1 otherwise.
369 =item C<gsl_vector_add_constant($v, $x)> - add a constant $x to the elements of the vector $v and return 0 if the operation succeded, 1 otherwise.
371 =item C<gsl_vector_isnull($v)> - verify if all the elements of the vector $v are null, return 0 if it's the case, 1 otherwise.
373 =item C<gsl_vector_ispos($v)> - verify if all the elements of the vector $v are positive, return 0 if it's the case, 1 otherwise.
375 =item C<gsl_vector_isneg($v)> - verify if all the elements of the vector $v are negative, return 0 if it's the case, 1 otherwise.
377 =item C<gsl_vector_isnonneg($v)> - verify if all the elements the vector $v are not negative, return 0 if it's the case, 1 otherwise.
379 =back
381 You have to add the functions you want to use inside the qw /put_funtion_here / with spaces between each function. You can also write use Math::GSL::Complex qw/:all/ to use all avaible functions of the module.
384 Precision on the vector_view type : every modification you'll make on a vector_view will also modify the original vector.
385 For example, the following code will zero the even elements of the vector $v of length $size, while leaving the odd elements untouched :
387 =over 1
389 =item C<$v_even= gsl_vector_subvector_with_stride ($v, 0, 2, $size/2);>
391 =item C<gsl_vector_set_zero ($v_even-E<gt>{vector});>
393 =back
395 For more informations on the functions, we refer you to the GSL offcial documentation:
396 L<http://www.gnu.org/software/gsl/manual/html_node/>
398 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
400 =head1 EXAMPLES
402 Here is an example using both interfaces.
404 use Math::GSL::Vector qw/:all/;
406 print "We'll create this vector : [0,1,4,9,16] \n";
407 my $vector = Math::GSL::Vector->new([0,1,4,9,16]);
408 my ($min, $max) = gsl_vector_minmax_index($vector->raw);
410 print "We then check the index value of the maximum and minimum values of the vector. \n";
411 print "The index of the maximum should be 4 and we received $max \n";
412 print "The index of the minimum should be 0 and we received $min \n";
413 print "We'll then swap the first and the third elements of the vector \n";
415 gsl_vector_swap_elements($vector->raw, 0, 3);
416 my @got = $vector->as_list;
417 print "The vector should now be like this : [9,1,4,0,16] \n";
418 print "and we received : [ @got ]\n";
420 =head1 AUTHORS
422 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
424 =head1 COPYRIGHT AND LICENSE
426 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
428 This program is free software; you can redistribute it and/or modify it
429 under the same terms as Perl itself.
431 =cut