Merge branch 'bleed'
[Math-GSL.git] / Matrix.i
blob81446e7d6e3dcca8d656dc5247c21eb49d31a8d2
1 %module "Math::GSL::Matrix"
3 %include "typemaps.i"
4 %include "gsl_typemaps.i"
5 %apply int *OUTPUT { size_t *imin, size_t *imax, size_t *jmin, size_t *jmax };
6 %apply double *OUTPUT { double * min_out, double * max_out };
8 FILE * fopen(char *, char *);
9 int fclose(FILE *);
12 #include "gsl/gsl_matrix.h"
13 #include "gsl/gsl_complex.h"
14 #include "gsl/gsl_vector_double.h"
15 #include "gsl/gsl_matrix_double.h"
16 #include "gsl/gsl_matrix_int.h"
17 #include "gsl/gsl_matrix_complex_double.h"
18 #include "gsl/gsl_matrix_char.h"
21 %include "gsl/gsl_matrix.h"
22 %include "gsl/gsl_complex.h"
23 %include "gsl/gsl_vector_double.h"
24 %include "gsl/gsl_matrix_double.h"
25 %include "gsl/gsl_matrix_int.h"
26 %include "gsl/gsl_matrix_complex_double.h"
27 %include "gsl/gsl_matrix_char.h"
30 %perlcode %{
32 no warnings 'redefine';
33 use Carp qw/croak/;
34 use Math::GSL qw/:all/;
35 use Math::GSL::Errno qw/:all/;
37 @EXPORT_OK = qw/fopen fclose
38 gsl_matrix_alloc gsl_matrix_calloc gsl_matrix_alloc_from_block
39 gsl_matrix_alloc_from_matrix gsl_vector_alloc_row_from_matrix
40 gsl_vector_alloc_col_from_matrix gsl_matrix_free gsl_matrix_submatrix
41 gsl_matrix_row gsl_matrix_column gsl_matrix_diagonal
42 gsl_matrix_subdiagonal gsl_matrix_superdiagonal gsl_matrix_subrow
43 gsl_matrix_subcolumn gsl_matrix_view_array
44 gsl_matrix_view_array_with_tda gsl_matrix_view_vector
45 gsl_matrix_view_vector_with_tda gsl_matrix_const_submatrix
46 gsl_matrix_const_row gsl_matrix_const_column gsl_matrix_const_diagonal
47 gsl_matrix_const_subdiagonal gsl_matrix_const_superdiagonal
48 gsl_matrix_const_subrow gsl_matrix_const_subcolumn
49 gsl_matrix_const_view_array gsl_matrix_const_view_array_with_tda
50 gsl_matrix_const_view_vector gsl_matrix_const_view_vector_with_tda
51 gsl_matrix_get gsl_matrix_set gsl_matrix_ptr gsl_matrix_const_ptr
52 gsl_matrix_set_zero gsl_matrix_set_identity gsl_matrix_set_all
53 gsl_matrix_fread gsl_matrix_fwrite gsl_matrix_fscanf gsl_matrix_fprintf
54 gsl_matrix_memcpy gsl_matrix_swap gsl_matrix_swap_rows
55 gsl_matrix_swap_columns gsl_matrix_swap_rowcol gsl_matrix_transpose
56 gsl_matrix_transpose_memcpy gsl_matrix_max gsl_matrix_min
57 gsl_matrix_minmax gsl_matrix_max_index gsl_matrix_min_index
58 gsl_matrix_minmax_index gsl_matrix_isnull gsl_matrix_ispos
59 gsl_matrix_isneg gsl_matrix_isnonneg gsl_matrix_add gsl_matrix_sub
60 gsl_matrix_mul_elements gsl_matrix_div_elements gsl_matrix_scale
61 gsl_matrix_add_constant gsl_matrix_add_diagonal
62 gsl_matrix_char_alloc gsl_matrix_char_calloc gsl_matrix_char_alloc_from_block
63 gsl_matrix_char_alloc_from_matrix gsl_vector_char_alloc_row_from_matrix gsl_vector_char_alloc_col_from_matrix
64 gsl_matrix_char_free gsl_matrix_char_submatrix
65 gsl_matrix_char_row gsl_matrix_char_column
66 gsl_matrix_char_diagonal gsl_matrix_char_subdiagonal gsl_matrix_char_superdiagonal
67 gsl_matrix_char_subrow gsl_matrix_char_subcolumn gsl_matrix_char_view_array
68 gsl_matrix_char_view_array_with_tda gsl_matrix_char_view_vector gsl_matrix_char_view_vector_with_tda
69 gsl_matrix_char_const_submatrix gsl_matrix_char_const_row gsl_matrix_char_const_column
70 gsl_matrix_char_const_diagonal gsl_matrix_char_const_subdiagonal gsl_matrix_char_const_superdiagonal
71 gsl_matrix_char_const_subrow gsl_matrix_char_const_subcolumn gsl_matrix_char_const_view_array
72 gsl_matrix_char_const_view_array_with_tda gsl_matrix_char_const_view_vector gsl_matrix_char_const_view_vector_with_tda
73 gsl_matrix_char_get gsl_matrix_char_set gsl_matrix_char_ptr gsl_matrix_char_const_ptr
74 gsl_matrix_char_set_zero gsl_matrix_char_set_identity
75 gsl_matrix_char_set_all gsl_matrix_char_fread
76 gsl_matrix_char_fwrite gsl_matrix_char_fscanf gsl_matrix_char_fprintf
77 gsl_matrix_char_memcpy gsl_matrix_char_swap
78 gsl_matrix_char_swap_rows gsl_matrix_char_swap_columns
79 gsl_matrix_char_swap_rowcol gsl_matrix_char_transpose gsl_matrix_char_transpose_memcpy
80 gsl_matrix_char_max gsl_matrix_char_min
81 gsl_matrix_char_minmax gsl_matrix_char_max_index
82 gsl_matrix_char_min_index gsl_matrix_char_minmax_index
83 gsl_matrix_char_isnull gsl_matrix_char_ispos gsl_matrix_char_isneg
84 gsl_matrix_char_isnonneg gsl_matrix_char_add
85 gsl_matrix_char_sub gsl_matrix_char_mul_elements gsl_matrix_char_div_elements
86 gsl_matrix_char_scale gsl_matrix_char_add_constant gsl_matrix_char_add_diagonal
87 gsl_matrix_int_alloc gsl_matrix_int_calloc gsl_matrix_int_alloc_from_block
88 gsl_matrix_int_alloc_from_matrix gsl_vector_int_alloc_row_from_matrix gsl_vector_int_alloc_col_from_matrix
89 gsl_matrix_int_free gsl_matrix_int_submatrix gsl_matrix_int_row
90 gsl_matrix_int_column gsl_matrix_int_diagonal gsl_matrix_int_subdiagonal
91 gsl_matrix_int_superdiagonal gsl_matrix_int_subrow gsl_matrix_int_subcolumn gsl_matrix_int_view_array
92 gsl_matrix_int_view_array_with_tda gsl_matrix_int_view_vector gsl_matrix_int_view_vector_with_tda
93 gsl_matrix_int_const_submatrix gsl_matrix_int_const_row gsl_matrix_int_const_column
94 gsl_matrix_int_const_diagonal gsl_matrix_int_const_subdiagonal gsl_matrix_int_const_superdiagonal
95 gsl_matrix_int_const_subrow gsl_matrix_int_const_subcolumn gsl_matrix_int_const_view_array
96 gsl_matrix_int_const_view_array_with_tda gsl_matrix_int_const_view_vector gsl_matrix_int_const_view_vector_with_tda
97 gsl_matrix_int_get gsl_matrix_int_set
98 gsl_matrix_int_ptr gsl_matrix_int_const_ptr
99 gsl_matrix_int_set_zero gsl_matrix_int_set_identity gsl_matrix_int_set_all
100 gsl_matrix_int_fread gsl_matrix_int_fwrite
101 gsl_matrix_int_fscanf gsl_matrix_int_fprintf
102 gsl_matrix_int_memcpy gsl_matrix_int_swap
103 gsl_matrix_int_swap_rows gsl_matrix_int_swap_columns gsl_matrix_int_swap_rowcol
104 gsl_matrix_int_transpose gsl_matrix_int_transpose_memcpy
105 gsl_matrix_int_max gsl_matrix_int_min gsl_matrix_int_minmax
106 gsl_matrix_int_max_index gsl_matrix_int_min_index
107 gsl_matrix_int_minmax_index gsl_matrix_int_isnull
108 gsl_matrix_int_ispos gsl_matrix_int_isneg gsl_matrix_int_isnonneg
109 gsl_matrix_int_add gsl_matrix_int_sub
110 gsl_matrix_int_mul_elements gsl_matrix_int_div_elements gsl_matrix_int_scale
111 gsl_matrix_int_add_constant gsl_matrix_int_add_diagonal
112 gsl_matrix_get_row gsl_matrix_get_col gsl_matrix_set_row gsl_matrix_set_col
113 gsl_matrix_complex_alloc gsl_matrix_complex_calloc gsl_matrix_complex_alloc_from_block
114 gsl_matrix_complex_alloc_from_matrix gsl_vector_complex_alloc_row_from_matrix gsl_vector_complex_alloc_col_from_matrix
115 gsl_matrix_complex_free gsl_matrix_complex_submatrix gsl_matrix_complex_row
116 gsl_matrix_complex_column gsl_matrix_complex_diagonal gsl_matrix_complex_subdiagonal
117 gsl_matrix_complex_superdiagonal gsl_matrix_complex_subrow gsl_matrix_complex_subcolumn
118 gsl_matrix_complex_view_array gsl_matrix_complex_view_array_with_tda gsl_matrix_complex_view_vector
119 gsl_matrix_complex_view_vector_with_tda gsl_matrix_complex_const_submatrix gsl_matrix_complex_const_row
120 gsl_matrix_complex_const_column gsl_matrix_complex_const_diagonal gsl_matrix_complex_const_subdiagonal
121 gsl_matrix_complex_const_superdiagonal gsl_matrix_complex_const_subrow gsl_matrix_complex_const_subcolumn
122 gsl_matrix_complex_const_view_array gsl_matrix_complex_const_view_array_with_tda gsl_matrix_complex_const_view_vector
123 gsl_matrix_complex_const_view_vector_with_tda gsl_matrix_complex_get gsl_matrix_complex_set
124 gsl_matrix_complex_ptr gsl_matrix_complex_const_ptr gsl_matrix_complex_set_zero
125 gsl_matrix_complex_set_identity gsl_matrix_complex_set_all gsl_matrix_complex_fread
126 gsl_matrix_complex_fwrite gsl_matrix_complex_fscanf gsl_matrix_complex_fprintf
127 gsl_matrix_complex_memcpy gsl_matrix_complex_swap gsl_matrix_complex_swap_rows
128 gsl_matrix_complex_swap_columns gsl_matrix_complex_swap_rowcol gsl_matrix_complex_transpose
129 gsl_matrix_complex_transpose_memcpy gsl_matrix_complex_isnull gsl_matrix_complex_ispos
130 gsl_matrix_complex_isneg gsl_matrix_complex_add gsl_matrix_complex_sub
131 gsl_matrix_complex_mul_elements gsl_matrix_complex_div_elements gsl_matrix_complex_scale
132 gsl_matrix_complex_add_constant gsl_matrix_complex_add_diagonal gsl_matrix_complex_get_row
133 gsl_matrix_complex_get_col gsl_matrix_complex_set_row gsl_matrix_complex_set_col /;
137 %EXPORT_TAGS = ( all => [ @EXPORT_OK ],
138 char => [ qw/
139 gsl_matrix_char_alloc
140 gsl_matrix_char_calloc
141 gsl_matrix_char_alloc_from_block
142 gsl_matrix_char_alloc_from_matrix
143 gsl_vector_char_alloc_row_from_matrix
144 gsl_vector_char_alloc_col_from_matrix
145 gsl_matrix_char_free
146 gsl_matrix_char_submatrix
147 gsl_matrix_char_row
148 gsl_matrix_char_column
149 gsl_matrix_char_diagonal
150 gsl_matrix_char_subdiagonal
151 gsl_matrix_char_superdiagonal
152 gsl_matrix_char_subrow
153 gsl_matrix_char_subcolumn
154 gsl_matrix_char_view_array
155 gsl_matrix_char_view_array_with_tda
156 gsl_matrix_char_view_vector
157 gsl_matrix_char_view_vector_with_tda
158 gsl_matrix_char_const_submatrix
159 gsl_matrix_char_const_row
160 gsl_matrix_char_const_column
161 gsl_matrix_char_const_diagonal
162 gsl_matrix_char_const_subdiagonal
163 gsl_matrix_char_const_superdiagonal
164 gsl_matrix_char_const_subrow
165 gsl_matrix_char_const_subcolumn
166 gsl_matrix_char_const_view_array
167 gsl_matrix_char_const_view_array_with_tda
168 gsl_matrix_char_const_view_vector
169 gsl_matrix_char_const_view_vector_with_tda
170 gsl_matrix_char_get
171 gsl_matrix_char_set
172 gsl_matrix_char_ptr
173 gsl_matrix_char_const_ptr
174 gsl_matrix_char_set_zero
175 gsl_matrix_char_set_identity
176 gsl_matrix_char_set_all
177 gsl_matrix_char_fread
178 gsl_matrix_char_fwrite
179 gsl_matrix_char_fscanf
180 gsl_matrix_char_fprintf
181 gsl_matrix_char_memcpy
182 gsl_matrix_char_swap
183 gsl_matrix_char_swap_rows
184 gsl_matrix_char_swap_columns
185 gsl_matrix_char_swap_rowcol
186 gsl_matrix_char_transpose
187 gsl_matrix_char_transpose_memcpy
188 gsl_matrix_char_max
189 gsl_matrix_char_min
190 gsl_matrix_char_minmax
191 gsl_matrix_char_max_index
192 gsl_matrix_char_min_index
193 gsl_matrix_char_minmax_index
194 gsl_matrix_char_isnull
195 gsl_matrix_char_ispos
196 gsl_matrix_char_isneg
197 gsl_matrix_char_isnonneg
198 gsl_matrix_char_add
199 gsl_matrix_char_sub
200 gsl_matrix_char_mul_elements
201 gsl_matrix_char_div_elements
202 gsl_matrix_char_scale
203 gsl_matrix_char_add_constant
204 gsl_matrix_char_add_diagonal
207 double => [ qw/
208 gsl_matrix_alloc
209 gsl_matrix_calloc
210 gsl_matrix_alloc_from_block
211 gsl_matrix_alloc_from_matrix
212 gsl_vector_alloc_row_from_matrix
213 gsl_vector_alloc_col_from_matrix
214 gsl_matrix_free
215 gsl_matrix_submatrix
216 gsl_matrix_row
217 gsl_matrix_column
218 gsl_matrix_diagonal
219 gsl_matrix_subdiagonal
220 gsl_matrix_superdiagonal
221 gsl_matrix_subrow
222 gsl_matrix_subcolumn
223 gsl_matrix_view_array
224 gsl_matrix_view_array_with_tda
225 gsl_matrix_view_vector
226 gsl_matrix_view_vector_with_tda
227 gsl_matrix_const_submatrix
228 gsl_matrix_const_row
229 gsl_matrix_const_column
230 gsl_matrix_const_diagonal
231 gsl_matrix_const_subdiagonal
232 gsl_matrix_const_superdiagonal
233 gsl_matrix_const_subrow
234 gsl_matrix_const_subcolumn
235 gsl_matrix_const_view_array
236 gsl_matrix_const_view_array_with_tda
237 gsl_matrix_const_view_vector
238 gsl_matrix_const_view_vector_with_tda
239 gsl_matrix_get
240 gsl_matrix_set
241 gsl_matrix_ptr
242 gsl_matrix_const_ptr
243 gsl_matrix_set_zero
244 gsl_matrix_set_identity
245 gsl_matrix_set_all
246 gsl_matrix_fread
247 gsl_matrix_fwrite
248 gsl_matrix_fscanf
249 gsl_matrix_fprintf
250 gsl_matrix_memcpy
251 gsl_matrix_swap
252 gsl_matrix_swap_rows
253 gsl_matrix_swap_columns
254 gsl_matrix_swap_rowcol
255 gsl_matrix_transpose
256 gsl_matrix_transpose_memcpy
257 gsl_matrix_max
258 gsl_matrix_minmax
259 gsl_matrix_max_index
260 gsl_matrix_min_index
261 gsl_matrix_minmax_index
262 gsl_matrix_isnull
263 gsl_matrix_ispos
264 gsl_matrix_isneg
265 gsl_matrix_isnonneg
266 gsl_matrix_add
267 gsl_matrix_mul_elements
268 gsl_matrix_div_elements
269 gsl_matrix_scale
270 gsl_matrix_add_constant
271 gsl_matrix_add_diagonal
273 int => [ qw/
274 gsl_matrix_int_alloc
275 gsl_matrix_int_alloc_from_matrix
276 gsl_matrix_int_free
277 gsl_matrix_int_column
278 gsl_matrix_int_superdiagonal
279 gsl_matrix_int_view_array_with_tda
280 gsl_matrix_int_const_submatrix
281 gsl_matrix_int_const_diagonal
282 gsl_matrix_int_const_subrow
283 gsl_matrix_int_const_view_array_with_tda
284 gsl_matrix_int_get
285 gsl_matrix_int_ptr
286 gsl_matrix_int_set_zero
287 gsl_matrix_int_fread
288 gsl_matrix_int_fscanf
289 gsl_matrix_int_memcpy
290 gsl_matrix_int_swap_rows
291 gsl_matrix_int_transpose
292 gsl_matrix_int_max
293 gsl_matrix_int_max_index
294 gsl_matrix_int_minmax_index
295 gsl_matrix_int_ispos
296 gsl_matrix_int_add
297 gsl_matrix_int_mul_elements
298 gsl_matrix_int_add_constant
300 complex => [ qw/
301 gsl_matrix_complex_alloc
302 gsl_matrix_complex_calloc
303 gsl_matrix_complex_alloc_from_block
304 gsl_matrix_complex_alloc_from_matrix
305 gsl_vector_complex_alloc_row_from_matrix
306 gsl_vector_complex_alloc_col_from_matrix
307 gsl_matrix_complex_free
308 gsl_matrix_complex_submatrix
309 gsl_matrix_complex_row
310 gsl_matrix_complex_column
311 gsl_matrix_complex_diagonal
312 gsl_matrix_complex_subdiagonal
313 gsl_matrix_complex_superdiagonal
314 gsl_matrix_complex_subrow
315 gsl_matrix_complex_subcolumn
316 gsl_matrix_complex_view_array
317 gsl_matrix_complex_view_array_with_tda
318 gsl_matrix_complex_view_vector
319 gsl_matrix_complex_view_vector_with_tda
320 gsl_matrix_complex_const_submatrix
321 gsl_matrix_complex_const_row
322 gsl_matrix_complex_const_column
323 gsl_matrix_complex_const_diagonal
324 gsl_matrix_complex_const_subdiagonal
325 gsl_matrix_complex_const_superdiagonal
326 gsl_matrix_complex_const_subrow
327 gsl_matrix_complex_const_subcolumn
328 gsl_matrix_complex_const_view_array
329 gsl_matrix_complex_const_view_array_with_tda
330 gsl_matrix_complex_const_view_vector
331 gsl_matrix_complex_const_view_vector_with_tda
332 gsl_matrix_complex_get
333 gsl_matrix_complex_set
334 gsl_matrix_complex_ptr
335 gsl_matrix_complex_const_ptr
336 gsl_matrix_complex_set_zero gsl_matrix_complex_set_identity
337 gsl_matrix_complex_set_all
338 gsl_matrix_complex_fread
339 gsl_matrix_complex_fwrite
340 gsl_matrix_complex_fscanf
341 gsl_matrix_complex_fprintf
342 gsl_matrix_complex_memcpygsl_matrix_complex_swap
343 gsl_matrix_complex_swap_rows
344 gsl_matrix_complex_swap_columns
345 gsl_matrix_complex_swap_rowcol
346 gsl_matrix_complex_transpose
347 gsl_matrix_complex_transpose_memcpy
348 gsl_matrix_complex_isnull
349 gsl_matrix_complex_ispos
350 gsl_matrix_complex_isneg
351 gsl_matrix_complex_add
352 gsl_matrix_complex_sub
353 gsl_matrix_complex_mul_elements
354 gsl_matrix_complex_div_elements
355 gsl_matrix_complex_scale
356 gsl_matrix_complex_add_constant
357 gsl_matrix_complex_add_diagonal
358 gsl_matrix_complex_get_row
359 gsl_matrix_complex_get_col
360 gsl_matrix_complex_set_row
361 gsl_matrix_complex_set_col
362 /]);
364 =head1 NAME
366 Math::GSL::Matrix - Mathematical functions concerning Matrices
368 =head1 SYNOPSIS
370 use Math::GSL::Matrix qw/:all/;
371 my $matrix1 = Math::GSL::Matrix->new(5,5); # OO interface
372 my $matrix2 = gsl_matrix_alloc(5,5); # standard interface
375 =head1 Objected Oriented Interface to GSL Math::GSL::Matrix
377 =head2 Math::GSL::Matrix->new()
379 Creates a new Matrix of the given size.
381 my $matrix = Math::GSL::Matrix->new(10,10);
382 =cut
384 sub new
386 my ($class, $rows, $cols) = @_;
387 my $this = {};
388 my $matrix;
389 if ( defined $rows && defined $cols &&
390 $rows > 0 && $cols > 0 &&
391 (int $rows == $rows) && (int $cols == $cols)){
393 $matrix = gsl_matrix_alloc($rows,$cols);
394 } else {
395 croak( __PACKAGE__.'::new($x,$y) - $x and $y must be positive integers');
397 gsl_matrix_set_zero($matrix);
398 $this->{_matrix} = $matrix;
399 ($this->{_rows}, $this->{_cols}) = ($rows,$cols);
400 bless $this, $class;
402 =head2 raw()
404 Get the underlying GSL matrix object created by SWIG, useful for using gsl_matrix_* functions which do not have an OO counterpart.
406 my $matrix = Math::GSL::Matrix->new(3,3);
407 my $gsl_matrix = $matrix->raw;
408 my $stuff = gsl_matrix_get($gsl_matrix, 1, 2);
410 =cut
411 sub raw { (shift)->{_matrix} }
412 =head2 rows()
414 Returns the number of rows in the matrix.
416 my $rows = $matrix->rows;
417 =cut
419 sub rows { (shift)->{_rows} }
421 =head2 cols()
423 Returns the number of columns in the matrix.
425 my $cols = $matrix->cols;
426 =cut
428 sub cols { (shift)->{_cols} }
430 =head2 as_list()
432 Get the contents of a Math::GSL::Matrix object as a Perl list.
434 my $matrix = Math::GSL::Matrix->new(3,3);
436 my @matrix = $matrix->as_list;
437 =cut
440 sub as_list
442 my $self = shift;
443 my $line;
444 my @part;
445 my @total;
446 for($line=0; $line<$self->rows; $line++){
447 @part = map {
448 gsl_matrix_get($self->raw, $line, $_)
449 } (0 .. $self->cols-1 );
450 push(@total, @part);
452 return @total;
455 =head2 row()
457 Returns a row matrix of the row you enter.
459 my $matrix = Math::GSL::Matrix->new(3,3);
461 my $matrix_row = $matrix->row(0);
463 =cut
465 sub row
467 my ($self, $row) = @_;
468 croak (__PACKAGE__.'::$matrix->row($row) - invalid $row value')
469 unless (($row < $self->rows) and $row >= 0);
471 my $rowvec = Math::GSL::Vector->new($self->cols);
472 my $rowmat = Math::GSL::Matrix->new(1,$self->cols);
474 my $status = gsl_matrix_get_row($rowvec->raw, $self->raw, $row);
475 croak (__PACKAGE__.'::gsl_matrix_get_row - ' . gsl_strerror($status) )
476 unless ( $status == $GSL_SUCCESS );
478 $status = gsl_matrix_set_row($rowmat->raw, 0, $rowvec->raw);
480 croak (__PACKAGE__.'::gsl_matrix_set_row - ' . gsl_strerror($status) )
481 unless ( $status == $GSL_SUCCESS );
483 return $rowmat;
486 =head2 col()
488 Returns a col matrix of the column you enter.
490 my $matrix = Math::GSL::Matrix->new(3,3);
492 my $matrix_col = $matrix->col(0);
494 =cut
496 sub col
498 my ($self, $col) = @_;
499 croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column")
500 unless ($col < $self->cols and $col >= 0);
502 my $colvec = Math::GSL::Vector->new($self->cols);
503 my $colmat = Math::GSL::Matrix->new($self->rows, 1);
505 my $status = gsl_matrix_get_col($colvec->raw, $self->raw, $col);
506 # return $colvec;
507 $status = gsl_matrix_set_col($colmat->raw, 0, $colvec->raw);
508 return $colmat;
511 =head2 set_row()
513 Sets a the values of a row with the elements of an array.
515 my $matrix = Math::GSL::Matrix->new(3,3);
516 $matrix->set_row(0, [8, 6, 2]);
518 You can also set multiple rows at once with chained calls:
519 my $matrix = Math::GSL::Matrix->new(3,3);
520 $matrix->set_row(0, [8, 6, 2]);
521 ->set_row(1, [2, 4, 1]);
524 =cut
526 sub set_row {
527 my ($self, $row, $values) = @_;
528 my $length = $#$values;
529 die __PACKAGE__.'::new($x, $values) - $values must be a nonempty array reference' if $length == -1;
530 die __PACKAGE__.'::set_row($x, $values) - $x must be a valid row number' if ($row < 0 || $row >= $self->rows);
531 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);
532 map { gsl_matrix_set($self->raw, $row, $_, $values->[$_]) } (0..$length);
533 return $self;
536 =head2 set_col()
538 Sets a the values of a column with the elements of an array.
540 my $matrix = Math::GSL::Matrix->new(3,3);
541 $matrix->set_col(0, [8, 6, 2]);
543 You can also set multiple columns at once with chained calls:
544 my $matrix = Math::GSL::Matrix->new(3,3);
545 $matrix->set_col(0, [8, 6, 2]);
546 ->set_col(1, [2, 4, 1]);
549 =cut
551 sub set_col {
552 my ($self, $col, $values) = @_;
553 my $length = $#$values;
554 die __PACKAGE__.'::new($x, $values) - $values must be a nonempty array reference' if $length == -1;
555 die __PACKAGE__.'::set_row($x, $values) - $x must be a valid column number' if ($col < 0 || $col >= $self->cols);
556 die __PACKAGE__.'::set_row($x, $values) - $values must contains the same number of elements as there is rowss in the matrix' if($length != $self->rows-1);
557 map { gsl_matrix_set($self->raw, $_, $col, $values->[$_]) } (0..$length);
558 return $self;
561 =head1 DESCRIPTION
563 Here is a list of all the functions included in this module :
565 =over 1
567 =item C<gsl_matrix_alloc($i, $j)> - Return a gsl_matrix of $i rows and $j columns
569 =item C<gsl_matrix_calloc($i, $j)> - Return a gsl_matrix of $i rows and $j columns and initialize all of the elements of the matrix to zero
571 =item C<gsl_matrix_alloc_from_block> -
573 =item C<gsl_matrix_free> -
575 =item C<gsl_matrix_alloc_from_matrix > -
577 =item C<gsl_vector_alloc_row_from_matrix> -
579 =item C<gsl_vector_alloc_col_from_matrix > -
581 =item C<gsl_matrix_submatrix($m, $k1, $k2, $n1, $n2)> - Return a matrix view of the matrix $m. The upper-left element of the submatrix is the element ($k1,$k2) of the original matrix. The submatrix has $n1 rows and $n2 columns.
583 =item C<gsl_matrix_row($m , $i)> - Return a vector view of the $i-th row of the matrix $m
585 =item C<gsl_matrix_column($m, $j)> - Return a vector view of the $j-th column of the matrix $m
587 =item C<gsl_matrix_diagonal($m)> - Return a vector view of the diagonal of the vector. The matrix doesn't have to be square.
589 =item C<gsl_matrix_subdiagonal($m, $k)> - Return a vector view of the $k-th subdiagonal of the matrix $m. The diagonal of the matrix corresponds to k=0.
591 =item C<gsl_matrix_superdiagonal($m, $k)> - Return a vector view of the $k-th superdiagonal of the matrix $m. The matrix doesn't have to be square.
593 =item C<gsl_matrix_subrow($m, $i, $offset, $n)> - Return a vector view of the $i-th row of the matrix $m beginning at offset elements and containing n elements.
595 =item C<gsl_matrix_subcolumn($m, $j, $offset, $n)> - Return a vector view of the $j-th column of the matrix $m beginning at offset elements and containing n elements.
597 =item C<gsl_matrix_view_array($base, $n1, $n2)> - This function returns a matrix view of the array reference $base. The matrix has $n1 rows and $n2 columns. The physical number of columns in memory is also given by $n2. Mathematically, the (i,j)-th element of the new matrix is given by, m'(i,j) = $base->[i*$n2 + j] where the index i runs from 0 to $n1-1 and the index j runs from 0 to $n2-1. The new matrix is only a view of the array reference $base. When the view goes out of scope the original array reference $base will continue to exist. The original memory can only be deallocated by freeing the original array. Of course, the original array should not be deallocated while the view is still in use.
599 =item C<gsl_matrix_view_array_with_tda($base, $n1, $n2, $tda)> - This function returns a matrix view of the array reference $base with a physical number of columns $tda which may differ from the corresponding dimension of the matrix. The matrix has $n1 rows and $n2 columns, and the physical number of columns in memory is given by $tda. Mathematically, the (i,j)-th element of the new matrix is given by, m'(i,j) = $base->[i*$tda + j] where the index i runs from 0 to $n1-1 and the index j runs from 0 to $n2-1. The new matrix is only a view of the array reference $base. When the view goes out of scope the original array reference $base will continue to exist. The original memory can only be deallocated by freeing the original array. Of course, the original array should not be deallocated while the view is still in use.
601 =item C<gsl_matrix_view_vector> -
603 =item C<gsl_matrix_view_vector_with_tda> -
605 =item C<gsl_matrix_const_submatrix> -
607 =item C<gsl_matrix_get($m, $i, $j)> - Return the (i,j)-th element of the matrix $m
609 =item C<gsl_matrix_set($m, $i, $j, $x)> - Set the value of the (i,j)-th element of the matrix $m to $x
611 =item C<gsl_matrix_ptr> -
613 =item C<gsl_matrix_const_ptr> -
615 =item C<gsl_matrix_set_zero($m)> - Set all the elements of the matrix $m to zero
617 =item C<gsl_matrix_set_identity($m)> - Set the elements of the matrix $m to the corresponding elements of the identity matrix
619 =item C<gsl_matrix_set_all($m, $x)> - Set all the elements of the matrix $m to the value $x
621 =item C<gsl_matrix_fread($fh, $m)> - Read a file which has been written with gsl_matrix_fwrite from the stream $fh opened with the fopen function and stores the data inside the matrix $m
623 =item C<gsl_matrix_fwrite($fh, $m)> - Write the elements of the matrix $m in binary format to a stream $fh opened with the fopen function
625 =item C<gsl_matrix_fscanf($fh, $m)> - Read a file which has been written with gsl_matrix_fprintf from the stream $fh opened with the fopen function and stores the data inside the matrix $m
627 =item C<gsl_matrix_fprintf($fh, $m, $format)> - Write the elements of the matrix $m in the format $format (for example "%f" is the format for double) to a stream $fh opened with the fopen function
629 =item C<gsl_matrix_memcpy($dest, $src)> - Copy the elements of the matrix $src to the matrix $dest. The two matrices must have the same size.
631 =item C<gsl_matrix_swap($m1, $m2)> - Exchange the elements of the matrices $m1 and $m2 by copying. The two matrices must have the same size.
633 =item C<gsl_matrix_swap_rows($m, $i, $j)> - Exchange the $i-th and $j-th row of the matrix $m. The function returns 0 if the operation suceeded, 1 otherwise.
635 =item C<gsl_matrix_swap_columns($m, $i, $j)> - Exchange the $i-th and $j-th column of the matrix $m. The function returns 0 if the operation suceeded, 1 otherwise.
637 =item C<gsl_matrix_swap_rowcol($m, $i, $j)> - Exchange the $i-th row and the $j-th column of the matrix $m. The matrix must be square. The function returns 0 if the operation suceeded, 1 otherwise.
639 =item C<gsl_matrix_transpose($m)> - This function replaces the matrix m by its transpose by copying the elements of the matrix in-place. The matrix must be square for this operation to be possible.
641 =item C<gsl_matrix_transpose_memcpy($dest, $src)> - Make the matrix $dest the transpose of the matrix $src. This function works for all matrices provided that the dimensions of the matrix dest match the transposed dimensions of the matrix src.
643 =item C<gsl_matrix_max($m)> - Return the maximum value in the matrix $m
645 =item C<gsl_matrix_min($m)> - Return the minimum value in the matrix $m
647 =item C<gsl_matrix_minmax($m)> - Return two values, the first is the minimum value of the Matrix $m and the second is the maximum of the same the same matrix.
649 =item C<gsl_matrix_max_index($m)> - Return two values, the first is the the i indice of the maximum value of the matrix $m and the second is the j indice of the same value.
651 =item C<gsl_matrix_min_index($m)> - Return two values, the first is the the i indice of the minimum value of the matrix $m and the second is the j indice of the same value.
653 =item C<gsl_matrix_minmax_index($m)> - Return four values, the first is the i indice of the minimum of the matrix $m, the second is the j indice of the same value, the third is the i indice of the maximum of the matrix $m and the fourth is the j indice of the same value
655 =item C<gsl_matrix_isnull($m)> - Return 1 if all the elements of the matrix $m are zero, 0 otherwise
657 =item C<gsl_matrix_ispos($m)> - Return 1 if all the elements of the matrix $m are strictly positve, 0 otherwise
659 =item C<gsl_matrix_isneg($m)> - Return 1 if all the elements of the matrix $m are strictly negative, 0 otherwise
661 =item C<gsl_matrix_isnonneg($m)> - Return 1 if all the elements of the matrix $m are non-negatuive, 0 otherwise
663 =item C<gsl_matrix_add($a, $b)> - Add the elements of matrix $b to the elements of matrix $a
665 =item C<gsl_matrix_sub($a, $b)> - Subtract the elements of matrix $b from the elements of matrix $a
667 =item C<gsl_matrix_mul_elements($a, $b)> - Multiplie the elements of matrix $a by the elements of matrix $b
669 =item C<gsl_matrix_div_elements($a, $b)> - Divide the elements of matrix $a by the elements of matrix $b
671 =item C<gsl_matrix_scale($a, $x)> - Multiplie the elements of matrix $a by the constant factor $x
673 =item C<gsl_matrix_add_constant($a, $x)> - Add the constant value $x to the elements of the matrix $a
675 =item C<gsl_matrix_add_diagonal($a, $x)> - Add the constant value $x to the elements of the diagonal of the matrix $a
677 =item C<gsl_matrix_get_row($v, $m, $i)> - Copy the elements of the $i-th row of the matrix $m into the vector $v. The lenght of the vector must be of the same as the lenght of the row. The function returns 0 if it succeded, 1 otherwise.
679 =item C<gsl_matrix_get_col($v, $m, $i)> - Copy the elements of the $j-th column of the matrix $m into the vector $v. The lenght of the vector must be of the same as the lenght of the column. The function returns 0 if it succeded, 1 otherwise.
681 =item C<gsl_matrix_set_row($m, $i, $v)> - Copy the elements of vector $v into the $i-th row of the matrix $m The lenght of the vector must be of the same as the lenght of the row. The function returns 0 if it succeded, 1 otherwise.
683 =item C<gsl_matrix_set_col($m, $j, $v)> - Copy the elements of vector $v into the $j-th row of the matrix $m The lenght of the vector must be of the same as the lenght of the column. The function returns 0 if it succeded, 1 otherwise.
685 =back
687 The following functions are specific to matrices containing complex numbers :
689 =over 1
691 =item C<gsl_matrix_complex_alloc >
693 =item C<gsl_matrix_complex_calloc >
695 =item C<gsl_matrix_complex_alloc_from_block >
697 =item C<gsl_matrix_complex_alloc_from_matrix >
699 =item C<gsl_vector_complex_alloc_row_from_matrix >
701 =item C<gsl_vector_complex_alloc_col_from_matrix >
703 =item C<gsl_matrix_complex_free >
705 =item C<gsl_matrix_complex_submatrix >
707 =item C<gsl_matrix_complex_row >
709 =item C<gsl_matrix_complex_column >
711 =item C<gsl_matrix_complex_diagonal >
713 =item C<gsl_matrix_complex_subdiagonal >
715 =item C<gsl_matrix_complex_superdiagonal >
717 =item C<gsl_matrix_complex_subrow >
719 =item C<gsl_matrix_complex_subcolumn >
721 =item C<gsl_matrix_complex_view_array >
723 =item C<gsl_matrix_complex_view_array_with_tda >
725 =item C<gsl_matrix_complex_view_vector >
727 =item C<gsl_matrix_complex_view_vector_with_tda >
729 =item C<gsl_matrix_complex_const_submatrix >
731 =item C<gsl_matrix_complex_const_row >
733 =item C<gsl_matrix_complex_const_column >
735 =item C<gsl_matrix_complex_const_diagonal >
737 =item C<gsl_matrix_complex_const_subdiagonal >
739 =item C<gsl_matrix_complex_const_superdiagonal >
741 =item C<gsl_matrix_complex_const_subrow >
743 =item C<gsl_matrix_complex_const_subcolumn >
745 =item C<gsl_matrix_complex_const_view_array >
747 =item C<gsl_matrix_complex_const_view_array_with_tda >
749 =item C<gsl_matrix_complex_const_view_vector >
751 =item C<gsl_matrix_complex_const_view_vector_with_tda >
753 =item C<gsl_matrix_complex_get>
755 =item C<gsl_matrix_complex_set>
757 =item C<gsl_matrix_complex_ptr>
759 =item C<gsl_matrix_complex_const_ptr>
761 =item C<gsl_matrix_complex_set_zero >
763 =item C<gsl_matrix_complex_set_identity >
765 =item C<gsl_matrix_complex_set_all >
767 =item C<gsl_matrix_complex_fread >
769 =item C<gsl_matrix_complex_fwrite >
771 =item C<gsl_matrix_complex_fscanf >
773 =item C<gsl_matrix_complex_fprintf >
775 =item C<gsl_matrix_complex_memcpy>
777 =item C<gsl_matrix_complex_swap>
779 =item C<gsl_matrix_complex_swap_rows>
781 =item C<gsl_matrix_complex_swap_columns>
783 =item C<gsl_matrix_complex_swap_rowcol>
785 =item C<gsl_matrix_complex_transpose >
787 =item C<gsl_matrix_complex_transpose_memcpy >
789 =item C<gsl_matrix_complex_isnull >
791 =item C<gsl_matrix_complex_ispos >
793 =item C<gsl_matrix_complex_isneg >
795 =item C<gsl_matrix_complex_add >
797 =item C<gsl_matrix_complex_sub >
799 =item C<gsl_matrix_complex_mul_elements >
801 =item C<gsl_matrix_complex_div_elements >
803 =item C<gsl_matrix_complex_scale >
805 =item C<gsl_matrix_complex_add_constant >
807 =item C<gsl_matrix_complex_add_diagonal >
809 =item C<gsl_matrix_complex_get_row>
811 =item C<gsl_matrix_complex_get_col>
813 =item C<gsl_matrix_complex_set_row>
815 =item C<gsl_matrix_complex_set_col>
817 =back
819 These are related to constant views of a matrix.
821 =over 1
824 =item C<gsl_matrix_const_row>
826 =item C<gsl_matrix_const_colum>
828 =item C<gsl_matrix_const_diagonal>
830 =item C<gsl_matrix_const_subdiagonal>
832 =item C<gsl_matrix_const_superdiagonal>
834 =item C<gsl_matrix_const_subrow>
836 =item C<gsl_matrix_const_subcolumn>
838 =item C<gsl_matrix_const_view_array>
840 =item C<gsl_matrix_const_view_array_with_tda>
842 =back
845 The following functions are similar to those above but work with C<char>'s and C<int>'s. We are not quite
846 sure if anyone wants these. Please speak up if you do and/or submit some patches to this documentation, please!
848 =over 1
851 =item gsl_matrix_const_view_vector
853 =item gsl_matrix_const_view_vector_with_tda
855 =item gsl_matrix_char_alloc
857 =item gsl_matrix_char_calloc
859 =item gsl_matrix_char_alloc_from_block
861 =item gsl_matrix_char_alloc_from_matrix
863 =item gsl_vector_char_alloc_row_from_matrix
865 =item gsl_vector_char_alloc_col_from_matrix
867 =item gsl_matrix_char_free
869 =item gsl_matrix_char_submatrix
871 =item gsl_matrix_char_row
873 =item gsl_matrix_char_column
875 =item gsl_matrix_char_diagonal
877 =item gsl_matrix_char_subdiagonal
879 =item gsl_matrix_char_superdiagonal
881 =item gsl_matrix_char_subrow
883 =item gsl_matrix_char_subcolumn
885 =item gsl_matrix_char_view_array
887 =item gsl_matrix_char_view_array_with_tda
889 =item gsl_matrix_char_view_vector
891 =item gsl_matrix_char_view_vector_with_tda
893 =item gsl_matrix_char_const_submatrix
895 =item gsl_matrix_char_const_row
897 =item gsl_matrix_char_const_column
899 =item gsl_matrix_char_const_diagonal
901 =item gsl_matrix_char_const_subdiagonal
903 =item gsl_matrix_char_const_superdiagonal
905 =item gsl_matrix_char_const_subrow
907 =item gsl_matrix_char_const_subcolumn
909 =item gsl_matrix_char_const_view_array
911 =item gsl_matrix_char_const_view_array_with_tda
913 =item gsl_matrix_char_const_view_vector
915 =item gsl_matrix_char_const_view_vector_with_tda
917 =item gsl_matrix_char_get
919 =item gsl_matrix_char_set
921 =item gsl_matrix_char_ptr
923 =item gsl_matrix_char_const_ptr
925 =item gsl_matrix_char_set_zero
927 =item gsl_matrix_char_set_identity
929 =item gsl_matrix_char_set_all
931 =item gsl_matrix_char_fread
933 =item gsl_matrix_char_fwrite
935 =item gsl_matrix_char_fscanf
937 =item gsl_matrix_char_fprintf
939 =item gsl_matrix_char_memcpy
941 =item gsl_matrix_char_swap
943 =item gsl_matrix_char_swap_rows
945 =item gsl_matrix_char_swap_columns
947 =item gsl_matrix_char_swap_rowcol
949 =item gsl_matrix_char_transpose
951 =item gsl_matrix_char_transpose_memcpy
953 =item gsl_matrix_char_max
955 =item gsl_matrix_char_min
957 =item gsl_matrix_char_minmax
959 =item gsl_matrix_char_max_index
961 =item gsl_matrix_char_min_index
963 =item gsl_matrix_char_minmax_index
965 =item gsl_matrix_char_isnull
967 =item gsl_matrix_char_ispos
969 =item gsl_matrix_char_isneg
971 =item gsl_matrix_char_isnonneg
973 =item gsl_matrix_char_add
975 =item gsl_matrix_char_sub
977 =item gsl_matrix_char_mul_elements
979 =item gsl_matrix_char_div_elements
981 =item gsl_matrix_char_scale
983 =item gsl_matrix_char_add_constant
985 =item gsl_matrix_char_add_diagonal
987 =item gsl_matrix_int_alloc
989 =item gsl_matrix_int_calloc
991 =item gsl_matrix_int_alloc_from_block
993 =item gsl_matrix_int_alloc_from_matrix
995 =item gsl_vector_int_alloc_row_from_matrix
997 =item gsl_vector_int_alloc_col_from_matrix
999 =item gsl_matrix_int_free
1001 =item gsl_matrix_int_submatrix
1003 =item gsl_matrix_int_row
1005 =item gsl_matrix_int_column
1007 =item gsl_matrix_int_diagonal
1009 =item gsl_matrix_int_subdiagonal
1011 =item gsl_matrix_int_superdiagonal
1013 =item gsl_matrix_int_subrow
1015 =item gsl_matrix_int_subcolumn
1017 =item gsl_matrix_int_view_array
1019 =item gsl_matrix_int_view_array_with_tda
1021 =item gsl_matrix_int_view_vector
1023 =item gsl_matrix_int_view_vector_with_tda
1025 =item gsl_matrix_int_const_submatrix
1027 =item gsl_matrix_int_const_row
1029 =item gsl_matrix_int_const_column
1031 =item gsl_matrix_int_ptr
1033 =item gsl_matrix_int_const_ptr
1035 =item gsl_matrix_int_set_zero
1037 =item gsl_matrix_int_set_identity
1039 =item gsl_matrix_int_set_all
1041 =item gsl_matrix_int_fread
1043 =item gsl_matrix_int_fwrite
1045 =item gsl_matrix_int_fscanf
1047 =item gsl_matrix_int_fprintf
1049 =item gsl_matrix_int_memcpy
1051 =item gsl_matrix_int_swap
1053 =item gsl_matrix_int_swap_rows
1055 =item gsl_matrix_int_swap_columns
1057 =item gsl_matrix_int_swap_rowcol
1059 =item gsl_matrix_int_transpose
1061 =item gsl_matrix_int_transpose_memcpy
1063 =item gsl_matrix_int_max
1065 =item gsl_matrix_int_min
1067 =item gsl_matrix_int_minmax
1069 =item gsl_matrix_int_max_index
1071 =item gsl_matrix_int_min_index
1073 =item gsl_matrix_int_minmax_index
1075 =item gsl_matrix_int_isnull
1077 =item gsl_matrix_int_ispos
1079 =item gsl_matrix_int_isneg
1081 =item gsl_matrix_int_isnonneg
1083 =item gsl_matrix_int_add
1085 =item gsl_matrix_int_sub
1087 =item gsl_matrix_int_mul_elements
1089 =item gsl_matrix_int_div_elements
1091 =item gsl_matrix_int_scale
1093 =item gsl_matrix_int_add_constant
1095 =item gsl_matrix_int_add_diagonal
1097 =back
1099 You have to add the functions you want to use inside the qw /put_funtion_here /.
1100 You can also write use Math::GSL::Matrix qw/:all/ to use all avaible functions of the module.
1101 Other tags are also avaible, here is a complete list of all tags for this module :
1103 =over 1
1105 =item C<all>
1107 =item C<int>
1109 =item C<double>
1111 =item C<char>
1113 =item C<complex>
1115 =back
1117 For more informations on the functions, we refer you to the GSL offcial documentation
1118 L<http://www.gnu.org/software/gsl/manual/html_node/>
1120 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/name_of_the_function_you_want
1122 =head1 EXAMPLES
1124 Most of the examples from this section are perl versions of the examples at L<http://www.gnu.org/software/gsl/manual/html_node/Example-programs-for-matrices.html>
1126 The program below shows how to allocate, initialize and read from a matrix using the functions gsl_matrix_alloc, gsl_matrix_set and gsl_matrix_get.
1128 use Math::GSL::Matrix qw/:all/;
1129 my $m = gsl_matrix_alloc (10,3);
1130 for my $i (0..9){
1131 for my $j (0..2){
1132 gsl_matrix_set($m, $i, $j, 0.23 + 100*$i + $j);
1136 for my $i (0..99){ # OUT OF RANGE ERROR
1137 for my $j (0..2){
1138 print "m($i, $j) = " . gsl_matrix_get ($m, $i, $j) . "\n";
1141 gsl_matrix_free ($m);
1144 use Math::GSL::Matrix qw/:all/;
1146 my $m = gsl_matrix_alloc (100, 100);
1147 my $a = gsl_matrix_alloc (100, 100);
1149 for my $i (0..99){
1150 for my $j (0..99){
1151 gsl_matrix_set ($m, $i, $j, 0.23 + $i + $j);
1155 The next program shows how to write a matrix to a file.
1157 my $out = fopen("test.dat", "wb");
1158 gsl_matrix_fwrite ($out, $m);
1159 fclose ($out);
1161 my $in = fopen("test.dat", "rb");
1162 gsl_matrix_fread ($in, $a);
1163 fclose($in);
1165 my $k=0;
1166 for my $i (0..99){
1167 for my $j (0..99){
1168 $mij = gsl_matrix_get ($m, $i, $j);
1169 $aij = gsl_matrix_get ($a, $i, $j);
1170 $k++ if ($mij != $aij);
1174 gsl_matrix_free($m);
1175 gsl_matrix_free($a);
1177 print "differences = $k (should be zero)\n";
1179 =head1 AUTHORS
1181 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
1183 =head1 COPYRIGHT AND LICENSE
1185 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
1187 This program is free software; you can redistribute it and/or modify it
1188 under the same terms as Perl itself.
1190 =cut