Refactoring row()/col() in Matrix/MatrixComplex as well trying to get set_row to...
[Math-GSL.git] / pod / MatrixComplex.pod
blob7a3efb0c4a4278a651af3345f85dfde728b9d107
1 %perlcode %{
3 use Carp qw/croak/;
4 use Math::GSL qw/:all/;
5 use Math::GSL::Complex qw/:all/;
6 use Math::GSL::Errno qw/:all/;
7 use Math::Complex;
8 use Data::Dumper;
10 @EXPORT_OK = qw/
11                 gsl_matrix_complex_alloc
12                 gsl_matrix_complex_calloc
13                 gsl_matrix_complex_alloc_from_block
14                 gsl_matrix_complex_alloc_from_matrix
15                 gsl_vector_complex_alloc_row_from_matrix
16                 gsl_vector_complex_alloc_col_from_matrix
17                 gsl_matrix_complex_free
18                 gsl_matrix_complex_submatrix
19                 gsl_matrix_complex_row
20                 gsl_matrix_complex_column
21                 gsl_matrix_complex_diagonal
22                 gsl_matrix_complex_subdiagonal
23                 gsl_matrix_complex_superdiagonal
24                 gsl_matrix_complex_subrow
25                 gsl_matrix_complex_subcolumn
26                 gsl_matrix_complex_view_array
27                 gsl_matrix_complex_view_array_with_tda
28                 gsl_matrix_complex_view_vector
29                 gsl_matrix_complex_view_vector_with_tda
30                 gsl_matrix_complex_const_submatrix
31                 gsl_matrix_complex_const_row
32                 gsl_matrix_complex_const_column
33                 gsl_matrix_complex_const_diagonal
34                 gsl_matrix_complex_const_subdiagonal
35                 gsl_matrix_complex_const_superdiagonal
36                 gsl_matrix_complex_const_subrow
37                 gsl_matrix_complex_const_subcolumn 
38                 gsl_matrix_complex_const_view_array
39                 gsl_matrix_complex_const_view_array_with_tda
40                 gsl_matrix_complex_const_view_vector
41                 gsl_matrix_complex_const_view_vector_with_tda
42                 gsl_matrix_complex_get
43                 gsl_matrix_complex_set
44                 gsl_matrix_complex_ptr
45                 gsl_matrix_complex_const_ptr
46                 gsl_matrix_complex_set_zero
47                 gsl_matrix_complex_set_identity
48                 gsl_matrix_complex_set_all
49                 gsl_matrix_complex_fread
50                 gsl_matrix_complex_fwrite
51                 gsl_matrix_complex_fscanf
52                 gsl_matrix_complex_fprintf
53                 gsl_matrix_complex_memcpy
54                 gsl_matrix_complex_swap
55                 gsl_matrix_complex_swap_rows
56                 gsl_matrix_complex_swap_columns
57                 gsl_matrix_complex_swap_rowcol
58                 gsl_matrix_complex_transpose
59                 gsl_matrix_complex_transpose_memcpy
60                 gsl_matrix_complex_isnull
61                 gsl_matrix_complex_ispos
62                 gsl_matrix_complex_isneg
63                 gsl_matrix_complex_add
64                 gsl_matrix_complex_sub
65                 gsl_matrix_complex_mul_elements
66                 gsl_matrix_complex_div_elements
67                 gsl_matrix_complex_scale
68                 gsl_matrix_complex_add_constant
69                 gsl_matrix_complex_add_diagonal
70                 gsl_matrix_complex_get_row
71                 gsl_matrix_complex_get_col
72                 gsl_matrix_complex_set_row
73                 gsl_matrix_complex_set_col
76 %EXPORT_TAGS = ( all => \@EXPORT_OK ,  );
78 =head1 NAME
80 Math::GSL::MatrixComplex - Complex Matrices
82 =head1 SYNOPSIS
84     use Math::GSL::MatrixComplex qw/:all/;
85     my $matrix1 = Math::GSL::MatrixComplex->new(5,5);  # OO interface
86     my $matrix2 = gsl_matrix_complex_alloc(5,5);        # standard interface
89 =head1 Objected Oriented Interface to GSL Math::GSL::MatrixComplex
91 =head2 new()
93 Creates a new MatrixComplex object of the given size.
95     my $matrix = Math::GSL::MatrixComplex->new(10,10);
96 =cut
98 sub new 
100     my ($class, $rows, $cols) = @_;
101     my $this = {}; 
102     my $matrix;
103     if ( defined $rows       && defined $cols && 
104         $rows > 0            && $cols > 0     && 
105         (int $rows == $rows) && (int $cols == $cols)){
107         $matrix  = gsl_matrix_complex_alloc($rows,$cols);
108     } else {
109         croak( __PACKAGE__.'::new($x,$y) - $x and $y must be positive integers');
110     }
111     gsl_matrix_complex_set_zero($matrix);
112     $this->{_matrix} = $matrix; 
113     ($this->{_rows}, $this->{_cols}) = ($rows,$cols);
114     bless $this, $class;
116 =head2 raw()
118 Get the underlying GSL matrix object created by SWIG, useful for using gsl_matrix_* functions which do not have an OO counterpart.
120     my $matrix     = Math::GSL::MatrixComplex->new(3,3);
121     my $gsl_matrix = $matrix->raw;
122     my $stuff      = gsl_matrix_complex_get($gsl_matrix, 1, 2);
124 =cut
125 sub raw  { (shift)->{_matrix} }
126 =head2 rows()
128 Returns the number of rows in the matrix.
130     my $rows = $matrix->rows;
131 =cut
133 sub rows { (shift)->{_rows}   }
135 =head2 cols()
137 Returns the number of columns in the matrix.
139     my $cols = $matrix->cols;
140 =cut
142 sub cols { (shift)->{_cols}   }
144 =head2  as_list() 
146 Get the contents of a Math::GSL::Matrix object as a Perl list.
148     my $matrix = Math::GSL::MatrixComplex->new(3,3);
149     ...
150     my @matrix = $matrix->as_list;
151 =cut
154 sub as_list 
156     my $self = shift;
157     my $line;
158     my @part;
159     my @total;
160     for($line=0; $line<$self->rows; $line++){
161        @part = map { 
162          gsl_matrix_complex_get($self->raw, $line, $_) 
163        } (0 .. $self->cols-1 );
164        push(@total, @part);
165     }
166     return @total;
169 =head2 row()
171 Returns a row matrix of the row you enter.
173     my $matrix = Math::GSL::MatrixComplex->new(3,3);
174     ...
175     my $matrix_row = $matrix->row(0);
177 =cut
179 sub row
181     my ($self, $row) = @_;
182     croak (__PACKAGE__.'::$matrix->row($row) - invalid $row value') 
183         unless (($row < $self->rows) and $row >= 0);  
185    my $rowmat = Math::GSL::MatrixComplex->new(1,$self->cols);
187    for my $n (0 .. $self->cols-1) {
188         gsl_matrix_complex_set( $rowmat->raw, 0, $n,
189             gsl_matrix_complex_get($self->raw, $row, $n)
190         );
191    }
193     return $rowmat;
196 =head2 col()
198 Returns a col matrix of the column you enter.
200     my $matrix = Math::GSL::MatrixComplex->new(3,3);
201     ...
202     my $matrix_col = $matrix->col(0);
204 =cut
206 sub col
208     my ($self, $col) = @_;
209     croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column")
210         unless ($col < $self->cols and $col >= 0);
212     my $colmat = Math::GSL::MatrixComplex->new($self->rows, 1);
214     map { gsl_matrix_complex_set($colmat->raw, $_, 0,
215             gsl_matrix_complex_get($self->raw, $_, $col),
216             )
217         } (0 .. $self->rows-1);
219     return $colmat;
222 =head2 set_row()
224 Sets a the values of a row with the elements of an array.
226     my $matrix = Math::GSL::MatrixComplex->new(3,3);
227     $matrix->set_row(0, [8, 6, 2]);
229 You can also set multiple rows at once with chained calls:
230     my $matrix = Math::GSL::MatrixComplex->new(3,3);
231     $matrix->set_row(0, [8, 6, 2])
232            ->set_row(1, [2, 4, 1]);
233     ...
235 =cut
237 sub set_row {
238     my ($self, $row, $values) = @_;
239     my $length = $#$values;
240     die __PACKAGE__.'::set_row($x, $values) - $values must be a nonempty array reference' if $length == -1;
241     die __PACKAGE__.'::set_row($x, $values) - $x must be a valid row number' if ($row < 0 || $row >= $self->rows);
242     die __PACKAGE__.'::set_row($x, $values) - $values must contains the same number of elements as there is columns in the matrix' if($length != $self->cols-1);
243     # $values may have Math::Complex objects
244     @$values = map { Math::GSL::Complex::gsl_complex_rect(Re($_), Im($_)) } @$values;
245     # warn Dumper [ @$values ];
246     map { gsl_matrix_complex_set($self->raw, $row, $_, $values->[$_]) } (0..$length);
247     return $self;
250 =head2 set_col()
252 Sets a the values of a column with the elements of an array.
254     my $matrix = Math::GSL::MatrixComplex->new(3,3);
255     $matrix->set_col(0, [8, 6, 2]);
257 You can also set multiple columns at once with chained calls:
258     my $matrix = Math::GSL::MatrixComplex->new(3,3);
259     $matrix->set_col(0, [8, 6, 2])
260            ->set_col(1, [2, 4, 1]);
261     ...
263 =cut
265 sub set_col {
266     my ($self, $col, $values) = @_;
267     my $length = $#$values;
268     die __PACKAGE__.'::set_col($x, $values) - $values must be a nonempty array reference' if $length == -1;
269     die __PACKAGE__.'::set_col($x, $values) - $x must be a valid column number' if ($col < 0 || $col >= $self->cols);
270     die __PACKAGE__.'::set_col($x, $values) - $values must contains the same number of elements as there is rowss in the matrix' if($length != $self->rows-1);
271     # $values may have Math::Complex objects
272     @$values = map { gsl_complex_rect(Re($_), Im($_)) } @$values;
273     map { gsl_matrix_complex_set($self->raw, $_, $col, $values->[$_]) } (0..$length);
274     return $self;
277 =head1 DESCRIPTION
279 =over 1
281 =item C<gsl_matrix_complex_alloc >
283 =item C<gsl_matrix_complex_calloc >
285 =item C<gsl_matrix_complex_alloc_from_block >
287 =item C<gsl_matrix_complex_alloc_from_matrix >
289 =item C<gsl_vector_complex_alloc_row_from_matrix >
291 =item C<gsl_vector_complex_alloc_col_from_matrix >
293 =item C<gsl_matrix_complex_free >
295 =item C<gsl_matrix_complex_submatrix >
297 =item C<gsl_matrix_complex_row >
299 =item C<gsl_matrix_complex_column >
301 =item C<gsl_matrix_complex_diagonal >
303 =item C<gsl_matrix_complex_subdiagonal >
305 =item C<gsl_matrix_complex_superdiagonal >
307 =item C<gsl_matrix_complex_subrow >
309 =item C<gsl_matrix_complex_subcolumn >
311 =item C<gsl_matrix_complex_view_array >
313 =item C<gsl_matrix_complex_view_array_with_tda >
315 =item C<gsl_matrix_complex_view_vector >
317 =item C<gsl_matrix_complex_view_vector_with_tda >
319 =item C<gsl_matrix_complex_const_submatrix >
321 =item C<gsl_matrix_complex_const_row >
323 =item C<gsl_matrix_complex_const_column >
325 =item C<gsl_matrix_complex_const_diagonal >
327 =item C<gsl_matrix_complex_const_subdiagonal >
329 =item C<gsl_matrix_complex_const_superdiagonal >
331 =item C<gsl_matrix_complex_const_subrow >
333 =item C<gsl_matrix_complex_const_subcolumn >
335 =item C<gsl_matrix_complex_const_view_array >
337 =item C<gsl_matrix_complex_const_view_array_with_tda >
339 =item C<gsl_matrix_complex_const_view_vector >
341 =item C<gsl_matrix_complex_const_view_vector_with_tda >
343 =item C<gsl_matrix_complex_get>
345 =item C<gsl_matrix_complex_set>
347 =item C<gsl_matrix_complex_ptr>
349 =item C<gsl_matrix_complex_const_ptr>
351 =item C<gsl_matrix_complex_set_zero >
353 =item C<gsl_matrix_complex_set_identity >
355 =item C<gsl_matrix_complex_set_all >
357 =item C<gsl_matrix_complex_fread >
359 =item C<gsl_matrix_complex_fwrite >
361 =item C<gsl_matrix_complex_fscanf >
363 =item C<gsl_matrix_complex_fprintf >
365 =item C<gsl_matrix_complex_memcpy>
367 =item C<gsl_matrix_complex_swap>
369 =item C<gsl_matrix_complex_swap_rows>
371 =item C<gsl_matrix_complex_swap_columns>
373 =item C<gsl_matrix_complex_swap_rowcol>
375 =item C<gsl_matrix_complex_transpose >
377 =item C<gsl_matrix_complex_transpose_memcpy >
379 =item C<gsl_matrix_complex_isnull >
381 =item C<gsl_matrix_complex_ispos >
383 =item C<gsl_matrix_complex_isneg >
385 =item C<gsl_matrix_complex_add >
387 =item C<gsl_matrix_complex_sub >
389 =item C<gsl_matrix_complex_mul_elements >
391 =item C<gsl_matrix_complex_div_elements >
393 =item C<gsl_matrix_complex_scale >
395 =item C<gsl_matrix_complex_add_constant >
397 =item C<gsl_matrix_complex_add_diagonal >
399 =item C<gsl_matrix_complex_get_row>
401 =item C<gsl_matrix_complex_get_col>
403 =item C<gsl_matrix_complex_set_row>
405 =item C<gsl_matrix_complex_set_col>
407 =back 
409 For more informations on the functions, we refer you to the GSL offcial documentation
410 L<http://www.gnu.org/software/gsl/manual/html_node/>
413 =head1 AUTHORS
415 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
417 =head1 COPYRIGHT AND LICENSE
419 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
421 This program is free software; you can redistribute it and/or modify it
422 under the same terms as Perl itself.
424 =cut