Merge branch 'bleed' of http://thgate.net/git/Math-GSL into bleed
[Math-GSL.git] / Vector.i
blobb93e5b9b75a0c44bab0ed50f631604169954acab
1 %module Vector
2 %{
3 #include "/usr/include/stdio.h"
4 #include "/usr/local/include/gsl/gsl_vector.h"
5 #include "/usr/local/include/gsl/gsl_vector_char.h"
6 #include "/usr/local/include/gsl/gsl_vector_complex.h"
7 #include "/usr/local/include/gsl/gsl_vector_complex_double.h"
8 #include "/usr/local/include/gsl/gsl_vector_double.h"
9 #include "/usr/local/include/gsl/gsl_vector_float.h"
10 #include "/usr/local/include/gsl/gsl_vector_int.h"
12 %include "/usr/local/include/gsl/gsl_vector.h"
13 %include "/usr/local/include/gsl/gsl_vector_char.h"
14 %include "/usr/local/include/gsl/gsl_vector_complex.h"
15 %include "/usr/local/include/gsl/gsl_vector_complex_double.h"
16 %include "/usr/local/include/gsl/gsl_vector_double.h"
17 %include "/usr/local/include/gsl/gsl_vector_int.h"
19 FILE *fopen(char *, char *);
20 int fclose(FILE *);
22 %perlcode %{
25 @EXPORT_OK = qw/fopen fclose
26 gsl_vector_alloc gsl_vector_calloc gsl_vector_alloc_from_b gsl_vector_alloc_from_v
27 gsl_vector_free gsl_vector_view_array gsl_vector_view_array_w gsl_vector_const_view_a
28 gsl_vector_const_view_a gsl_vector_subvector gsl_vector_subvector_wi
29 gsl_vector_const_subvec gsl_vector_const_subvec gsl_vector_get gsl_vector_set
30 gsl_vector_ptr gsl_vector_const_ptr gsl_vector_set_zero gsl_vector_set_all
31 gsl_vector_set_basis gsl_vector_fread gsl_vector_fwrite gsl_vector_fscanf
32 gsl_vector_fprintf gsl_vector_memcpy gsl_vector_reverse gsl_vector_swap
33 gsl_vector_swap_element gsl_vector_max gsl_vector_min gsl_vector_minmax
34 gsl_vector_max_index gsl_vector_min_index gsl_vector_minmax_index
35 gsl_vector_add gsl_vector_sub gsl_vector_mul gsl_vector_div
36 gsl_vector_scale gsl_vector_add_constant gsl_vector_isnull
37 gsl_vector_ispos gsl_vector_isneg gsl_vector_isnonneg
39 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
41 sub new {
42 my ($class, $values) = @_;
43 my $length = $#$values;
44 my $vector;
45 die __PACKAGE__.'::new($x) - $x must be an int or nonempty array reference'
46 if( !(defined $values) || ($length == -1));
48 if ( ref $values eq 'ARRAY' ){
49 $vector = gsl_vector_alloc($length+1);
50 map { gsl_vector_set($vector, $_, $values->[$_] ) } (0 .. $length);
51 } elsif ( int $values == $values && $values > 0) {
52 $vector = gsl_vector_alloc($length);
53 } else {
54 die __PACKAGE__.'::new($x) - $x must be an int or array reference';
56 my $self = {};
57 $self->{_vector} = $vector;
58 bless $self, $class;
61 sub get {
62 my ($self, $indices) = @_;
63 return map { gsl_vector_get($self->{_vector}, $_ ) } @$indices ;
66 sub set {
67 my ($self, $indices, $values) = @_;
68 die (__PACKAGE__.'::set($indices, $values) - $indices and $values must be array references of the same length')
69 unless ( ref $indices eq 'ARRAY' && ref $values eq 'ARRAY' && $#$indices == $#$values );
70 eval {
71 map { gsl_vector_set($self->{_vector}, $indices->[$_], $values->[$_] ) } (0..$#$indices);
72 };
73 return;