POD for Math::GSL::Test
[Math-GSL.git] / Vector.i
blob9e2bb8d6b3cd70766481bb758e9d22e73420d933
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 Data::Dumper;
33 use Carp qw/croak/;
34 use overload
35 '*' => \&_multiplication,
36 fallback => 1,
39 @EXPORT_all = qw/fopen fclose
40 gsl_vector_alloc gsl_vector_calloc gsl_vector_alloc_from_block gsl_vector_alloc_from_vector
41 gsl_vector_free gsl_vector_view_array gsl_vector_const_view_array gsl_vector_view_array_with_stride
42 gsl_vector_const_view_array_with_stride gsl_vector_subvector gsl_vector_subvector_wi gsl_vector_subvector_with_stride
43 gsl_vector_const_subvec gsl_vector_const_subvec gsl_vector_get gsl_vector_set
44 gsl_vector_ptr gsl_vector_const_ptr gsl_vector_set_zero gsl_vector_set_all
45 gsl_vector_set_basis gsl_vector_fread gsl_vector_fwrite gsl_vector_fscanf
46 gsl_vector_fprintf gsl_vector_memcpy gsl_vector_reverse gsl_vector_swap
47 gsl_vector_swap_elements gsl_vector_max gsl_vector_min gsl_vector_minmax
48 gsl_vector_max_index gsl_vector_min_index gsl_vector_minmax_index
49 gsl_vector_add gsl_vector_sub gsl_vector_mul gsl_vector_div
50 gsl_vector_scale gsl_vector_add_constant gsl_vector_isnull
51 gsl_vector_ispos gsl_vector_isneg gsl_vector_isnonneg
52 gsl_vector_float_alloc gsl_vector_float_calloc gsl_vector_float_alloc_from_block
53 gsl_vector_float_alloc_from_vector gsl_vector_float_free gsl_vector_float_view_array
54 gsl_vector_float_view_array_with_stride gsl_vector_float_const_view_array gsl_vector_float_const_view_array_with_stride
55 gsl_vector_float_subvector gsl_vector_float_subvector_with_stride gsl_vector_float_const_subvector
56 gsl_vector_float_const_subvector_with_stride gsl_vector_float_get gsl_vector_float_set gsl_vector_float_ptr
57 gsl_vector_float_const_ptr gsl_vector_float_set_zero gsl_vector_float_set_all gsl_vector_float_set_basis
58 gsl_vector_float_fread gsl_vector_float_fwrite gsl_vector_float_fscanf gsl_vector_float_fprintf
59 gsl_vector_float_memcpy gsl_vector_float_reverse gsl_vector_float_swap gsl_vector_float_swap_elements
60 gsl_vector_float_max gsl_vector_float_min gsl_vector_float_minmax gsl_vector_float_max_index gsl_vector_float_min_index
61 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
62 gsl_vector_float_add_constant gsl_vector_float_isnull gsl_vector_float_ispos gsl_vector_float_isneg gsl_vector_float_isnonneg
63 gsl_vector_complex_alloc gsl_vector_complex_calloc gsl_vector_complex_alloc_from_block gsl_vector_complex_alloc_from_vector
64 gsl_vector_complex_free gsl_vector_complex_view_array gsl_vector_complex_view_array_with_stride gsl_vector_complex_const_view_array
65 gsl_vector_complex_const_view_array_with_stride gsl_vector_complex_subvector gsl_vector_complex_subvector_with_stride
66 gsl_vector_complex_const_subvector gsl_vector_complex_const_subvector_with_stride gsl_vector_complex_real gsl_vector_complex_imag
67 gsl_vector_complex_const_real gsl_vector_complex_const_imag gsl_vector_complex_get gsl_vector_complex_set
68 gsl_vector_complex_ptr gsl_vector_complex_const_ptr gsl_vector_complex_set_zero gsl_vector_complex_set_all
69 gsl_vector_complex_set_basis gsl_vector_complex_fread gsl_vector_complex_fwrite gsl_vector_complex_fscanf
70 gsl_vector_complex_fprintf gsl_vector_complex_memcpy gsl_vector_complex_reverse gsl_vector_complex_swap
71 gsl_vector_complex_swap_elements gsl_vector_complex_isnull gsl_vector_complex_ispos gsl_vector_complex_isneg
73 @EXPORT_file =qw/ fopen fclose/;
74 @EXPORT_OK = (@EXPORT_all, @EXPORT_file);
75 %EXPORT_TAGS = ( file => \@EXPORT_file, all => \@EXPORT_all );
77 =head1 NAME
79 Math::GSL::Vector - Functions concerning vectors
81 =head1 SYNOPSIS
83 use Math::GSL::Vector qw/:all/;
84 my $vec1 = Math::GSL::Vector->new([1, 7, 94, 15 ]);
85 my $vec2 = $vec1 * 5;
86 my $vec3 = Math::GSL::Vector>new(10); # 10 element zero vector
88 # set the element at index 1 to 9
89 # and the element at index 3 to 8
90 $vec3->set([ 1, 3 ], [ 9, 8 ]);
92 my @vec = $vec2->as_list; # return elements as Perl list
94 my $dot_product = $vec1 * $vec2;
95 my $length = $vec2->length;
96 my $first = $vec1->get(0);
99 =cut
101 =head1 Objected Oriented Interface to GSL Math::GSL::Vector
103 =head2 Math::GSL::Vector->new()
105 Creates a new Vector of the given size.
107 my $vector = Math::GSL::Matrix->new(3);
109 You can also create and set directly the values of the vector like this :
111 my $vector = Math::GSL::Vector->new([2,4,1]);
113 =cut
114 sub new {
115 my ($class, $values) = @_;
116 my $length = $#$values;
117 my $this = {};
118 my $vector;
119 if ( ref $values eq 'ARRAY' ){
120 die __PACKAGE__.'::new($x) - $x must be a nonempty array reference' if $length == -1;
121 $vector = gsl_vector_alloc($length+1);
122 map { gsl_vector_set($vector, $_, $values->[$_] ) } (0 .. $length);
123 $this->{_length} = $length+1;
124 } elsif ( (int($values) == $values) && ($values > 0)) {
125 $vector = gsl_vector_alloc($values);
126 gsl_vector_set_zero($vector);
127 $this->{_length} = $values;
128 } else {
129 die __PACKAGE__.'::new($x) - $x must be an int or array reference';
131 $this->{_vector} = $vector;
132 bless $this, $class;
134 =head2 raw()
136 Get the underlying GSL vector object created by SWIG, useful for using gsl_vector_* functions which do not have an OO counterpart.
138 my $vector = Math::GSL::vector->new(3);
139 my $gsl_vector = $vector->raw;
140 my $stuff = gsl_vector_get($gsl_vector, 1);
142 =cut
144 sub raw {
145 my $self = shift;
146 return $self->{_vector};
149 =head2 min()
151 Returns the minimum value contained in the vector.
153 my $vector = Math::GSL::Vector->new([2,4,1]);
154 my $minimum = $vector->min;
156 =cut
158 sub min {
159 my $self=shift;
160 return gsl_vector_min($self->raw);
163 =head2 max()
165 Returns the minimum value contained in the vector.
167 my $vector = Math::GSL::Vector->new([2,4,1]);
168 my $maximum = $vector->max;
170 =cut
172 sub max {
173 my $self=shift;
174 return gsl_vector_max($self->raw);
177 =head2 length()
179 Returns the number of elements contained in the vector.
181 my $vector = Math::GSL::Vector->new([2,4,1]);
182 my $length = $vector->length;
184 =cut
186 sub length { my $self=shift; $self->{_length} }
188 =head2 as_list()
190 Gets the content of a Math::GSL::Vector object as a Perl list.
192 my $vector = Math::GSL::vector->new(3);
194 my @values = $vector->as_list;
195 =cut
197 sub as_list {
198 my $self=shift;
199 $self->get( [ 0 .. $self->length - 1 ] );
202 =head2 get()
204 Gets the value of an of a Math::GSL::Vector object.
206 my $vector = Math::GSL::vector->new(3);
208 my @values = $vector->get(2);
210 You can also enter an array of indices to receive their corresponding values:
212 my $vector = Math::GSL::vector->new(3);
214 my @values = $vector->get([0,2]);
216 =cut
218 sub get {
219 my ($self, $indices) = @_;
220 return map { gsl_vector_get($self->raw, $_ ) } @$indices ;
223 =head2 set()
225 Sets values of an of a Math::GSL::Vector object.
227 my $vector = Math::GSL::vector->new(3);
228 $vector->set([1,2], [8,23]);
230 This sets the second and third value to 8 and 23.
232 =cut
234 sub set {
235 my ($self, $indices, $values) = @_;
236 die (__PACKAGE__.'::set($indices, $values) - $indices and $values must be array references of the same length')
237 unless ( ref $indices eq 'ARRAY' && ref $values eq 'ARRAY' && $#$indices == $#$values );
238 eval {
239 map { gsl_vector_set($self->{_vector}, $indices->[$_], $values->[$_] ) } (0..$#$indices);
241 return;
244 sub _multiplication {
245 my ($left,$right) = @_;
246 if ( blessed $right && $right->isa('Math::GSL::Vector') ) {
247 return $left->dot_product($right);
248 } else {
249 gsl_vector_scale($left->raw, $right);
251 return $left;
254 sub dot_product {
255 my ($left,$right) = @_;
256 my $sum=0;
257 if ( blessed $right && $right->isa('Math::GSL::Vector') &&
258 blessed $left && $left->isa('Math::GSL::Vector') &&
259 $left->length == $right->length ) {
260 my @l = $left->as_list;
261 my @r = $right->as_list;
262 map { $sum += $l[$_] * $r[$_] } (0..$#l);
263 return $sum;
264 } else {
265 croak "dot_product() must be called with two vectors";
269 =head1 DESCRIPTION
271 Here is a list of all the functions included in this module :
273 =over 1
275 =item C<gsl_vector_alloc($x)> - create a vector of size $x
277 =item C<gsl_vector_calloc($x)> - create a vector of size $x and initializes all the elements of the vector to zero
279 =item C<gsl_vector_alloc_from_block>
281 =item C<gsl_vector_alloc_from_vector>
283 =item C<gsl_vector_free($v)> - free a previously allocated vector $v
285 =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.
287 =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.
289 =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}.
291 =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.
293 =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
295 =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
297 =item C<gsl_vector_const_subvector>
299 =item C<gsl_vector_get($v, $i)> - return the $i-th element of a vector $v
301 =item C<gsl_vector_set($v, $i, $x)> - return the vector $v with his $i-th element set to $x
303 =item C<gsl_vector_ptr>
305 =item C<gsl_vector_const_ptr>
307 =item C<gsl_vector_set_zero($v)> - set all the elements of $v to 0
309 =item C<gsl_vector_set_all($v, $x)> - set all the elements of $v to $x
311 =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.
313 =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.
315 =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.
317 =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.
319 =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.
321 =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.
323 =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
325 =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
327 =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.
329 =item C<gsl_vector_max($v)> - return the maximum value in the vector $v
331 =item C<gsl_vector_min($v)> - return the minimum value in the vector $v
333 =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.
335 =item C<gsl_vector_max_index($v)> - return the position of the maximum value in the vector $v
337 =item C<gsl_vector_min_index($v)> - return the position of the minimum value in the vector $v
339 =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.
341 =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.
343 =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.
345 =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.
347 =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.
349 =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.
351 =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.
353 =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.
355 =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.
357 =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.
359 =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.
361 =back
363 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.
366 Precision on the vector_view type : every modification you'll make on a vector_view will also modify the original vector.
367 For example, the following code will zero the even elements of the vector $v of length $size, while leaving the odd elements untouched :
369 =over 1
371 =item C<$v_even= gsl_vector_subvector_with_stride ($v, 0, 2, $size/2);>
373 =item C<gsl_vector_set_zero ($v_even-E<gt>{vector});>
375 =back
377 For more informations on the functions, we refer you to the GSL offcial documentation:
378 L<http://www.gnu.org/software/gsl/manual/html_node/>
380 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
382 =head1 EXAMPLES
384 Here is an example using both interfaces.
386 use Math::GSL::Vector qw/:all/;
388 print "We'll create this vector : [0,1,4,9,16] \n";
389 my $vector = Math::GSL::Vector->new([0,1,4,9,16]);
390 my ($min, $max) = gsl_vector_minmax_index($vector->raw);
392 print "We then check the index value of the maximum and minimum values of the vector. \n";
393 print "The index of the maximum should be 4 and we received $max \n";
394 print "The index of the minimum should be 0 and we received $min \n";
395 print "We'll then swap the first and the third elements of the vector \n";
397 gsl_vector_swap_elements($vector->raw, 0, 3);
398 my @got = $vector->as_list;
399 print "The vector should now be like this : [9,1,4,0,16] \n";
400 print "and we received : [ @got ]\n";
402 =head1 AUTHORS
404 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
406 =head1 COPYRIGHT AND LICENSE
408 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
410 This program is free software; you can redistribute it and/or modify it
411 under the same terms as Perl itself.
413 =cut