Refactor PowInt tests to use Test::Exceptioni's dies_ok() , get rid of _assert_dies...
[Math-GSL.git] / Vector.i
blob0763514cc0a58e9e564afe5304dd0d25774bdae7
1 %module 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 "/usr/local/include/gsl/gsl_nan.h"
33 #include "/usr/local/include/gsl/gsl_vector.h"
34 #include "/usr/local/include/gsl/gsl_vector_char.h"
35 #include "/usr/local/include/gsl/gsl_vector_complex.h"
36 #include "/usr/local/include/gsl/gsl_vector_complex_double.h"
37 #include "/usr/local/include/gsl/gsl_vector_double.h"
38 #include "/usr/local/include/gsl/gsl_vector_float.h"
39 #include "/usr/local/include/gsl/gsl_vector_int.h"
42 %include "/usr/local/include/gsl/gsl_nan.h"
43 %include "/usr/local/include/gsl/gsl_vector.h"
44 %include "/usr/local/include/gsl/gsl_vector_char.h"
45 %include "/usr/local/include/gsl/gsl_vector_complex.h"
46 %include "/usr/local/include/gsl/gsl_vector_complex_double.h"
47 %include "/usr/local/include/gsl/gsl_vector_double.h"
48 %include "/usr/local/include/gsl/gsl_vector_int.h"
52 %perlcode %{
55 @EXPORT_OK = qw/fopen fclose
56 gsl_vector_alloc gsl_vector_calloc gsl_vector_alloc_from_block gsl_vector_alloc_from_vector
57 gsl_vector_free gsl_vector_view_array gsl_vector_view_array_with_stride
58 gsl_vector_const_view_array_with_stride gsl_vector_subvector gsl_vector_subvector_wi gsl_vector_subvector_with_stride
59 gsl_vector_const_subvec gsl_vector_const_subvec gsl_vector_get gsl_vector_set
60 gsl_vector_ptr gsl_vector_const_ptr gsl_vector_set_zero gsl_vector_set_all
61 gsl_vector_set_basis gsl_vector_fread gsl_vector_fwrite gsl_vector_fscanf
62 gsl_vector_fprintf gsl_vector_memcpy gsl_vector_reverse gsl_vector_swap
63 gsl_vector_swap_elements gsl_vector_max gsl_vector_min gsl_vector_minmax
64 gsl_vector_max_index gsl_vector_min_index gsl_vector_minmax_index
65 gsl_vector_add gsl_vector_sub gsl_vector_mul gsl_vector_div
66 gsl_vector_scale gsl_vector_add_constant gsl_vector_isnull
67 gsl_vector_ispos gsl_vector_isneg gsl_vector_isnonneg
68 gsl_vector_float_alloc gsl_vector_float_calloc gsl_vector_float_alloc_from_block
69 gsl_vector_float_alloc_from_vector gsl_vector_float_free gsl_vector_float_view_array
70 gsl_vector_float_view_array_with_stride gsl_vector_float_const_view_array gsl_vector_float_const_view_array_with_stride
71 gsl_vector_float_subvector gsl_vector_float_subvector_with_stride gsl_vector_float_const_subvector
72 gsl_vector_float_const_subvector_with_stride gsl_vector_float_get gsl_vector_float_set gsl_vector_float_ptr
73 gsl_vector_float_const_ptr gsl_vector_float_set_zero gsl_vector_float_set_all gsl_vector_float_set_basis
74 gsl_vector_float_fread gsl_vector_float_fwrite gsl_vector_float_fscanf gsl_vector_float_fprintf
75 gsl_vector_float_memcpy gsl_vector_float_reverse gsl_vector_float_swap gsl_vector_float_swap_elements
76 gsl_vector_float_max gsl_vector_float_min gsl_vector_float_minmax gsl_vector_float_max_index gsl_vector_float_min_index
77 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
78 gsl_vector_float_add_constant gsl_vector_float_isnull gsl_vector_float_ispos gsl_vector_float_isneg gsl_vector_float_isnonneg
80 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
82 sub new {
83 my ($class, $values) = @_;
84 my $length = $#$values;
85 my $this = {};
86 my $vector;
87 if ( ref $values eq 'ARRAY' ){
88 die __PACKAGE__.'::new($x) - $x must a nonempty array reference' if $length == -1;
89 $vector = gsl_vector_alloc($length+1);
90 map { gsl_vector_set($vector, $_, $values->[$_] ) } (0 .. $length);
91 $this->{_length} = $length+1;
92 } elsif ( (int($values) == $values) && ($values > 0)) {
93 $vector = gsl_vector_alloc($values);
94 $this->{_length} = $values;
95 } else {
96 die __PACKAGE__.'::new($x) - $x must be an int or array reference';
98 $this->{_vector} = $vector;
99 bless $this, $class;
102 sub raw { (shift)->{_vector} }
104 sub length { my $self=shift; $self->{_length} }
106 sub get_all {
107 my $self=shift;
108 $self->get( [ 0 .. $self->length - 1 ] );
110 sub get {
111 my ($self, $indices) = @_;
112 return map { gsl_vector_get($self->{_vector}, $_ ) } @$indices ;
115 sub set {
116 my ($self, $indices, $values) = @_;
117 die (__PACKAGE__.'::set($indices, $values) - $indices and $values must be array references of the same length')
118 unless ( ref $indices eq 'ARRAY' && ref $values eq 'ARRAY' && $#$indices == $#$values );
119 eval {
120 map { gsl_vector_set($self->{_vector}, $indices->[$_], $values->[$_] ) } (0..$#$indices);
122 return;
125 __END__
127 =head1 NAME
129 Math::GSL::Vector - Functions concerning vectors
131 =head1 SYPNOPSIS
133 use Math::GSL::Vector qw/:all/;
135 =head1 DESCRIPTION
137 Here is a list of all the functions included in this module :
139 =over 1
141 =item C<gsl_vector_alloc($x)> - create a vector of size $x
143 =item C<gsl_vector_calloc($x)> - create a vector of size $x and initializes all the elements of the vector to zero
145 =item C<gsl_vector_alloc_from_block>
147 =item C<gsl_vector_alloc_from_vector>
149 =item C<gsl_vector_free($v)> - free a previously allocated vector $v
151 =item C<gsl_vector_view_array>
153 =item C<gsl_vector_view_array_with_stride>
155 =item C<gsl_vector_const_view_array_with_stride>
157 =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
159 =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
161 =item C<gsl_vector_const_subvector>
163 =item C<gsl_vector_get($v, $i)> - return the $i-th element of a vector $v
165 =item C<gsl_vector_set($v, $i, $x)> - return the vector $v with his $i-th element set to $x
167 =item C<gsl_vector_ptr>
169 =item C<gsl_vector_const_ptr>
171 =item C<gsl_vector_set_zero($v)> - set all the elements of $v to 0
173 =item C<gsl_vector_set_all($v, $x)> - set all the elements of $v to $x
175 =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.
177 =item C<gsl_vector_fread($file, $v)> - This function reads into the vector $v from the open stream $file opened with fopen 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.
179 =item C<gsl_vector_fwrite($file, $v)> - This function writes the elements of the vector $v to the stream $file opened with fopen 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.
181 =item C<gsl_vector_fscanf($file, $v)> This function reads formatted data from the stream $file opened with fopen 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.
183 =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 fopen 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.
185 =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.
187 =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
189 =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
191 =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.
193 =item C<gsl_vector_max($v)> - return the maximum value in the vector $v
195 =item C<gsl_vector_min($v)> - return the minimum value in the vector $v
197 =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.
199 =item C<gsl_vector_max_index($v)> - return the position of the maximum value in the vector $v
201 =item C<gsl_vector_min_index($v)> - return the position of the minimum value in the vector $v
203 =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.
205 =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.
207 =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.
209 =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.
211 =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.
213 =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.
215 =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.
217 =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.
219 =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.
221 =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.
223 =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.
225 =back
227 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.
230 Precision on the vector_view type : every modification you'll make on a vector_view will also modify the original vector.
231 For example, the following code will zero the even elements of the vector $v of length $size, while leaving the odd elements untouched :
233 =over 1
235 =item C<$v_even= gsl_vector_subvector_with_stride ($v, 0, 2, $size/2);>
237 =item C<gsl_vector_set_zero ($v_even-E<gt>{vector});>
239 =back
241 For more informations on the functions, we refer you to the GSL offcial documentation: L<http://www.gnu.org/software/gsl/manual/html_node/>
242 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
244 =head1 EXAMPLES
247 =head1 AUTHOR
249 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
251 =head1 COPYRIGHT AND LICENSE
253 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
255 This program is free software; you can redistribute it and/or modify it
256 under the same terms as Perl itself.
258 =cut