Littles changes to the Integration doc
[Math-GSL.git] / Vector.i
blob2a67ceae5f7124e2d5b8ed1390c5bda75f9c816e
1 %module "Math::GSL::Vector"
3 %include "typemaps.i"
4 %include "gsl_typemaps.i"
6 FILE * fopen(char *, char *);
7 int fclose(FILE *);
9 %{
10 #include "gsl/gsl_nan.h"
11 #include "gsl/gsl_vector.h"
12 #include "gsl/gsl_vector_char.h"
13 #include "gsl/gsl_vector_complex.h"
14 #include "gsl/gsl_vector_complex_double.h"
15 #include "gsl/gsl_vector_double.h"
16 #include "gsl/gsl_vector_float.h"
17 #include "gsl/gsl_vector_int.h"
20 %include "gsl/gsl_nan.h"
21 %include "gsl/gsl_vector.h"
22 %include "gsl/gsl_vector_char.h"
23 %include "gsl/gsl_vector_complex.h"
24 %include "gsl/gsl_vector_complex_double.h"
25 %include "gsl/gsl_vector_double.h"
26 %include "gsl/gsl_vector_int.h"
30 %perlcode %{
31 use Scalar::Util 'blessed';
32 use Carp qw/croak/;
33 use overload
34 '*' => \&_multiplication,
35 fallback => 1,
38 @EXPORT_all = qw/fopen fclose
39 gsl_vector_alloc gsl_vector_calloc gsl_vector_alloc_from_block gsl_vector_alloc_from_vector
40 gsl_vector_free gsl_vector_view_array gsl_vector_const_view_array gsl_vector_view_array_with_stride
41 gsl_vector_const_view_array_with_stride gsl_vector_subvector gsl_vector_subvector_wi gsl_vector_subvector_with_stride
42 gsl_vector_const_subvec gsl_vector_const_subvec gsl_vector_get gsl_vector_set
43 gsl_vector_ptr gsl_vector_const_ptr gsl_vector_set_zero gsl_vector_set_all
44 gsl_vector_set_basis gsl_vector_fread gsl_vector_fwrite gsl_vector_fscanf
45 gsl_vector_fprintf gsl_vector_memcpy gsl_vector_reverse gsl_vector_swap
46 gsl_vector_swap_elements gsl_vector_max gsl_vector_min gsl_vector_minmax
47 gsl_vector_max_index gsl_vector_min_index gsl_vector_minmax_index
48 gsl_vector_add gsl_vector_sub gsl_vector_mul gsl_vector_div
49 gsl_vector_scale gsl_vector_add_constant gsl_vector_isnull
50 gsl_vector_ispos gsl_vector_isneg gsl_vector_isnonneg
51 gsl_vector_float_alloc gsl_vector_float_calloc gsl_vector_float_alloc_from_block
52 gsl_vector_float_alloc_from_vector gsl_vector_float_free gsl_vector_float_view_array
53 gsl_vector_float_view_array_with_stride gsl_vector_float_const_view_array gsl_vector_float_const_view_array_with_stride
54 gsl_vector_float_subvector gsl_vector_float_subvector_with_stride gsl_vector_float_const_subvector
55 gsl_vector_float_const_subvector_with_stride gsl_vector_float_get gsl_vector_float_set gsl_vector_float_ptr
56 gsl_vector_float_const_ptr gsl_vector_float_set_zero gsl_vector_float_set_all gsl_vector_float_set_basis
57 gsl_vector_float_fread gsl_vector_float_fwrite gsl_vector_float_fscanf gsl_vector_float_fprintf
58 gsl_vector_float_memcpy gsl_vector_float_reverse gsl_vector_float_swap gsl_vector_float_swap_elements
59 gsl_vector_float_max gsl_vector_float_min gsl_vector_float_minmax gsl_vector_float_max_index gsl_vector_float_min_index
60 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
61 gsl_vector_float_add_constant gsl_vector_float_isnull gsl_vector_float_ispos gsl_vector_float_isneg gsl_vector_float_isnonneg
62 gsl_vector_complex_alloc gsl_vector_complex_calloc gsl_vector_complex_alloc_from_block gsl_vector_complex_alloc_from_vector
63 gsl_vector_complex_free gsl_vector_complex_view_array gsl_vector_complex_view_array_with_stride gsl_vector_complex_const_view_array
64 gsl_vector_complex_const_view_array_with_stride gsl_vector_complex_subvector gsl_vector_complex_subvector_with_stride
65 gsl_vector_complex_const_subvector gsl_vector_complex_const_subvector_with_stride gsl_vector_complex_real gsl_vector_complex_imag
66 gsl_vector_complex_const_real gsl_vector_complex_const_imag gsl_vector_complex_get gsl_vector_complex_set
67 gsl_vector_complex_ptr gsl_vector_complex_const_ptr gsl_vector_complex_set_zero gsl_vector_complex_set_all
68 gsl_vector_complex_set_basis gsl_vector_complex_fread gsl_vector_complex_fwrite gsl_vector_complex_fscanf
69 gsl_vector_complex_fprintf gsl_vector_complex_memcpy gsl_vector_complex_reverse gsl_vector_complex_swap
70 gsl_vector_complex_swap_elements gsl_vector_complex_isnull gsl_vector_complex_ispos gsl_vector_complex_isneg
72 @EXPORT_file =qw/ fopen fclose/;
73 @EXPORT_OK = (@EXPORT_all, @EXPORT_file);
74 %EXPORT_TAGS = ( file => \@EXPORT_file, all => \@EXPORT_all );
76 =head1 NAME
78 Math::GSL::Vector - Functions concerning vectors
80 =head1 SYNOPSIS
82 use Math::GSL::Vector qw/:all/;
83 my $vec1 = Math::GSL::Vector->new([1, 7, 94, 15 ]);
84 my $vec2 = $vec1 * 5;
85 my $vec3 = Math::GSL::Vector>new(10); # 10 element zero vector
87 # set the element at index 1 to 9
88 # and the element at index 3 to 8
89 $vec3->set([ 1, 3 ], [ 9, 8 ]);
91 my @vec = $vec2->as_list; # return elements as Perl list
93 my $dot_product = $vec1 * $vec2;
94 my $length = $vec2->length;
95 my $first = $vec1->get(0);
98 =cut
100 =head1 Objected Oriented Interface to GSL Math::GSL::Vector
102 =head2 Math::GSL::Vector->new()
104 Creates a new Vector of the given size.
106 my $vector = Math::GSL::Matrix->new(3);
108 You can also create and set directly the values of the vector like this :
110 my $vector = Math::GSL::Vector->new([2,4,1]);
112 =cut
113 sub new {
114 my ($class, $values) = @_;
115 my $length = $#$values;
116 my $this = {};
117 my $vector;
118 if ( ref $values eq 'ARRAY' ){
119 die __PACKAGE__.'::new($x) - $x must be a nonempty array reference' if $length == -1;
120 $vector = gsl_vector_alloc($length+1);
121 map { gsl_vector_set($vector, $_, $values->[$_] ) } (0 .. $length);
122 $this->{_length} = $length+1;
123 } elsif ( (int($values) == $values) && ($values > 0)) {
124 $vector = gsl_vector_alloc($values);
125 gsl_vector_set_zero($vector);
126 $this->{_length} = $values;
127 } else {
128 die __PACKAGE__.'::new($x) - $x must be an int or array reference';
130 $this->{_vector} = $vector;
131 bless $this, $class;
133 =head2 raw()
135 Get the underlying GSL vector object created by SWIG, useful for using gsl_vector_* functions which do not have an OO counterpart.
137 my $vector = Math::GSL::vector->new(3);
138 my $gsl_vector = $vector->raw;
139 my $stuff = gsl_vector_get($gsl_vector, 1);
141 =cut
143 sub raw { (shift)->{_vector} }
145 =head2 min()
147 Returns the minimum value contained in the vector.
149 my $vector = Math::GSL::Vector->new([2,4,1]);
150 my $minimum = $vector->min;
152 =cut
154 sub min {
155 my $self=shift;
156 return gsl_vector_min($self->raw);
159 =head2 max()
161 Returns the minimum value contained in the vector.
163 my $vector = Math::GSL::Vector->new([2,4,1]);
164 my $maximum = $vector->max;
166 =cut
168 sub max {
169 my $self=shift;
170 return gsl_vector_max($self->raw);
173 =head2 length()
175 Returns the number of elements contained in the vector.
177 my $vector = Math::GSL::Vector->new([2,4,1]);
178 my $length = $vector->length;
180 =cut
182 sub length { my $self=shift; $self->{_length} }
184 =head2 as_list()
186 Gets the content of a Math::GSL::Vector object as a Perl list.
188 my $vector = Math::GSL::vector->new(3);
190 my @values = $vector->as_list;
191 =cut
193 sub as_list {
194 my $self=shift;
195 $self->get( [ 0 .. $self->length - 1 ] );
198 =head2 get()
200 Gets the value of an of a Math::GSL::Vector object.
202 my $vector = Math::GSL::vector->new(3);
204 my @values = $vector->get(2);
206 You can also enter an array of indices to receive their corresponding values:
208 my $vector = Math::GSL::vector->new(3);
210 my @values = $vector->get([0,2]);
212 =cut
214 sub get {
215 my ($self, $indices) = @_;
216 return map { gsl_vector_get($self->{_vector}, $_ ) } @$indices ;
219 =head2 set()
221 Sets values of an of a Math::GSL::Vector object.
223 my $vector = Math::GSL::vector->new(3);
224 $vector->set([1,2], [8,23]);
226 This sets the second and third value to 8 and 23.
228 =cut
230 sub set {
231 my ($self, $indices, $values) = @_;
232 die (__PACKAGE__.'::set($indices, $values) - $indices and $values must be array references of the same length')
233 unless ( ref $indices eq 'ARRAY' && ref $values eq 'ARRAY' && $#$indices == $#$values );
234 eval {
235 map { gsl_vector_set($self->{_vector}, $indices->[$_], $values->[$_] ) } (0..$#$indices);
237 return;
240 sub _multiplication {
241 my ($left,$right) = @_;
242 if ( blessed $right && $right->isa('Math::GSL::Vector') ) {
243 return $left->dot_product($right);
244 } else {
245 gsl_vector_scale($left->raw, $right);
247 return $left;
250 sub dot_product {
251 my ($left,$right) = @_;
252 my $sum=0;
253 if ( blessed $right && $right->isa('Math::GSL::Vector') &&
254 blessed $left && $left->isa('Math::GSL::Vector') &&
255 $left->length == $right->length ) {
256 my @l = $left->as_list;
257 my @r = $right->as_list;
258 map { $sum += $l[$_] * $r[$_] } (0..$#l);
259 return $sum;
260 } else {
261 croak "dot_product() must be called with two vectors";
265 =head1 DESCRIPTION
267 Here is a list of all the functions included in this module :
269 =over 1
271 =item C<gsl_vector_alloc($x)> - create a vector of size $x
273 =item C<gsl_vector_calloc($x)> - create a vector of size $x and initializes all the elements of the vector to zero
275 =item C<gsl_vector_alloc_from_block>
277 =item C<gsl_vector_alloc_from_vector>
279 =item C<gsl_vector_free($v)> - free a previously allocated vector $v
281 =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.
283 =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.
285 =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}.
287 =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.
289 =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
291 =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
293 =item C<gsl_vector_const_subvector>
295 =item C<gsl_vector_get($v, $i)> - return the $i-th element of a vector $v
297 =item C<gsl_vector_set($v, $i, $x)> - return the vector $v with his $i-th element set to $x
299 =item C<gsl_vector_ptr>
301 =item C<gsl_vector_const_ptr>
303 =item C<gsl_vector_set_zero($v)> - set all the elements of $v to 0
305 =item C<gsl_vector_set_all($v, $x)> - set all the elements of $v to $x
307 =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.
309 =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.
311 =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.
313 =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.
315 =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.
317 =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.
319 =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
321 =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
323 =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.
325 =item C<gsl_vector_max($v)> - return the maximum value in the vector $v
327 =item C<gsl_vector_min($v)> - return the minimum value in the vector $v
329 =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.
331 =item C<gsl_vector_max_index($v)> - return the position of the maximum value in the vector $v
333 =item C<gsl_vector_min_index($v)> - return the position of the minimum value in the vector $v
335 =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.
337 =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.
339 =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.
341 =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.
343 =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.
345 =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.
347 =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.
349 =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.
351 =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.
353 =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.
355 =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.
357 =back
359 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.
362 Precision on the vector_view type : every modification you'll make on a vector_view will also modify the original vector.
363 For example, the following code will zero the even elements of the vector $v of length $size, while leaving the odd elements untouched :
365 =over 1
367 =item C<$v_even= gsl_vector_subvector_with_stride ($v, 0, 2, $size/2);>
369 =item C<gsl_vector_set_zero ($v_even-E<gt>{vector});>
371 =back
373 For more informations on the functions, we refer you to the GSL offcial documentation:
374 L<http://www.gnu.org/software/gsl/manual/html_node/>
376 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
378 =head1 EXAMPLES
380 Here is an example using both interfaces.
382 use Math::GSL::Vector qw/:all/;
384 print "We'll create this vector : [0,1,4,9,16] \n";
385 my $vector = Math::GSL::Vector->new([0,1,4,9,16]);
386 my ($min, $max) = gsl_vector_minmax_index($vector->raw);
388 print "We then check the index value of the maximum and minimum values of the vector. \n";
389 print "The index of the maximum should be 4 and we received $max \n";
390 print "The index of the minimum should be 0 and we received $min \n";
391 print "We'll then swap the first and the third elements of the vector \n";
393 gsl_vector_swap_elements($vector->raw, 0, 3);
394 my @got = $vector->as_list;
395 print "The vector should now be like this : [9,1,4,0,16] \n";
396 print "and we received : [ @got ]\n";
398 =head1 AUTHORS
400 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
402 =head1 COPYRIGHT AND LICENSE
404 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
406 This program is free software; you can redistribute it and/or modify it
407 under the same terms as Perl itself.
409 =cut