blib/lib/Math/SparseMatrix/Operations.pm untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 242 CC 12 SCAR 20.9
CC
12
Cov
0%
op_subtraction
20.0
op_addition
12.0
op_dot_product
12.0
CRAP 8.1
1 package Math::SparseMatrix::Operations;
2  
3 use v5.10;
4  
5 use parent qw( Exporter );
6 require Exporter;
7 use Math::SparseMatrix;
8  
9 @ISA = ("Exporter");
10 @EXPORT = qw(&op_fill_matrix
11 &op_print_matrix
12 &op_transpose
13 &op_multiply
14 &op_identity
15 &op_subtraction
16 &op_addition
17 &op_dot_product
18 &op_get_col
19 &op_get_row
20 );
21  
22 use warnings;
23 use strict;
24  
25 =head1 NAME
26  
27 Math::SparseMatrix::Operations - Mathematical operations with matrices
28  
29 =head1 VERSION
30  
31 Version 0.05
32  
33 =cut
34  
35 our $VERSION = '0.06';
36  
37  
38 # returns the position and the content of each cell.
39 sub op_print_matrix {
40 my $matrix = shift;
41 my $rs = $matrix->{_rows};
42 my $cs = $matrix->{_cols};
43  
44 for (my $i = 1; $i <= $rs; $i++) {
45 for (my $j = 1; $j <= $cs; $j++) {
46 print "[$i,$j]" . $matrix->get($i, $j) . "\t";
47 }
48 print "\n";
49 }
50 }
51  
52 # each matrix cell recieves a '1' as a value.
53 sub op_fill_matrix {
54 my $matrix = shift;
55 my $rs = $matrix->{_rows};
56 my $cs = $matrix->{_cols};
57  
58 for (my $i = 1; $i <= $rs; $i++) {
59 for (my $j = 1; $j <= $cs; $j++) {
60 $matrix->set($i, $j, 1);
61 }
62 }
63 }
64  
65  
66 # standard matrix transposition.
67 sub op_transpose {
68 my $matrix = shift;
69 my $rows = $matrix->{_rows};
70 my $cols = $matrix->{_cols};
71  
72 my $inverse = Math::SparseMatrix->new($cols, $rows);
73
74 for (my $r = 1; $r <= $rows; $r++) {
75 for (my $c = 1; $c <= $cols; $c++) {
76 my $value = $matrix->get($r, $c);
77 $inverse->set($c, $r, $value);
78 }
79 }
80 return $inverse;
81 }
82  
83 # standard matrix multiplication
84 sub op_multiply {
85 my $matrix_a = shift;
86 my $matrix_b = shift;
87  
88 my $a_rows = $matrix_a->{_rows};
89 my $a_cols = $matrix_a->{_cols};
90  
91 my $b_rows = $matrix_b->{_rows};
92 my $b_cols = $matrix_b->{_cols};
93  
94 my $result = Math::SparseMatrix->new($a_rows, $b_cols);
95  
96 if ($matrix_a->{_cols} != $matrix_b->{_rows}) {
97 die "To use ordinary matrix multiplication the number of columns on the first matrix must mat the number of rows on the second";
98 }
99  
100 for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
101 for(my $result_col = 1; $result_col <= $b_cols; $result_col++) {
102 my $value = 0;
103 for (my $i = 1; $i <= $a_cols; $i++) {
104 $value += ($matrix_a->get($result_row, $i)) * ($matrix_b->get($i, $result_col));
105 }
106 $result->set($result_row, $result_col, $value);
107 }
108 }
109 return $result;
110 }
111  
112 # standard matrix identity
113 sub op_identity {
114 my $size = shift;
115  
116 if ($size < 1) {
117 die "Identity matrix must be at least of size 1.";
118 }
119
120 my $result = Math::SparseMatrix->new ($size, $size);
121  
122 for (my $i = 1; $i <= $size; $i++) {
123 $result->set($i, $i, 1);
124 }
125 return $result;
126 }
127  
128 # standard matrix identity
129 sub op_subtraction {
130 my $matrix_a = shift;
131 my $matrix_b = shift;
132  
133 my $a_rows = $matrix_a->{_rows};
134 my $a_cols = $matrix_a->{_cols};
135  
136 my $b_rows = $matrix_b->{_rows};
137 my $b_cols = $matrix_b->{_cols};
138  
139 if ($a_rows != $b_rows) {
140 die "To subtract the matrixes they must have the same number of rows and columns.";
141 }
142  
143 if ($a_cols != $b_cols) {
144 die "To subtract the matrixes they must have the same number of rows and columns. Matrix a has ";
145 }
146  
147 my $result = Math::SparseMatrix->new($a_rows, $a_cols);
148  
149 for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
150 for (my $result_col = 1; $result_col <= $a_cols; $result_col++) {
151 my $value = ( $matrix_a->get($result_row, $result_col) ) - ( $matrix_b->get($result_row, $result_col));
152
153 if ($value == 0) {
154 $value += 2;
155 }
156 $result->set($result_row, $result_col, $value);
157 }
158 }
159 return $result;
160 }
161  
162 # standard matrix addition.
163 sub op_addition {
164 #weight matrix.
165 my $matrix_a = shift;
166 #identity matrix.
167 my $matrix_b = shift;
168  
169 my $a_rows = $matrix_a->{_rows};
170 my $a_cols = $matrix_a->{_cols};
171  
172 my $b_rows = $matrix_b->{_rows};
173 my $b_cols = $matrix_b->{_cols};
174
175 if ($a_rows != $b_rows) {
176 die "To add the matrixes they must have the same number of rows and columns.";
177 }
178  
179 if ($a_cols != $b_cols) {
180 die "To add the matrixes they must have the same number of rows and columns.";
181 }
182  
183 my $result = Math::SparseMatrix->new($a_rows, $a_cols);
184  
185 for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
186 for (my $result_col = 1; $result_col <= $a_cols; $result_col++) {
187 my $value = $matrix_b->get($result_row, $result_col);
188 $result->set($result_row, $result_col, $matrix_a->get($result_row, $result_col) + $value )
189 }
190 }
191 return $result;
192 }
193  
194 # standard matrix dot product.
195 sub op_dot_product {
196 my $matrix_a = shift;
197 my $matrix_b = shift;
198
199 my $a_rows = $matrix_a->{_rows};
200 my $a_cols = $matrix_a->{_cols};
201
202 my $b_rows = $matrix_b->{_rows};
203 my $b_cols = $matrix_b->{_cols};
204  
205 my @array_a = &packed_array($matrix_a);
206 my @array_b = &packed_array($matrix_b);
207  
208 for (my $n = 0; $n <= $#array_b; $n++) {
209 if ($array_b[$n] == 2) {
210 $array_b[$n] = 0;
211 }
212 }
213
214 if ($#array_a != $#array_b) {
215 die "To take the dot product, both matrixes must be of the same length.";
216 }
217  
218 my $result = 0;
219 my $length = $#array_a + 1;
220  
221 for (my $i = 0; $i < $length; $i++) {
222 $result += $array_a[$i] * $array_b[$i];
223 }
224 return $result;
225 }
226  
227 # returns a specific column from the matrix.
228 sub op_get_col {
229 my $self = shift;
230 my $col = shift;
231  
232 my $matrix = $self->matrix();
233
234 my $matrix_rows = $self->matrix_rows();
235  
236 if ($col > $matrix_rows) {
237 die "Can't get column";
238 }
239  
240 my $new_matrix = Math::SparseMatrix->new($matrix_rows, 1);
241  
242 for (my $row = 1; $row <= $matrix_rows; $row++) {
243 my $value = $matrix->get($row, $col);
244 $new_matrix->set($row, 1, $value);
245 }
246 return $new_matrix;
247 }
248  
249 #returns a specific cow from the matrix.
250 sub op_get_row {
251 my $self = shift;
252 my $row = shift;
253  
254 my $matrix = $self->matrix();
255  
256 my $matrix_cols = $self->matrix_cols();
257  
258 if ($row > $matrix_cols) {
259 die "Can't get row";
260 }
261  
262 my $new_matrix = Math::SparseMatrix->new(1, $matrix_cols);
263  
264 for (my $col = 1; $col <= $matrix_cols; $col++) {
265 my $value = $matrix->get($row, $col);
266 $new_matrix->set(1, $col, $value);
267 }
268  
269 return $new_matrix;
270 }
271  
272 1;
273 =head1 SYNOPSIS
274  
275 This module introduces new utilities and mathematical functions for matrices from the module Math::SparseMatrix. This is not a class module, it contains only
276 the subroutines for the operations. All the subroutines described below will only function with Math::SparseMatrix objects.
277  
278 Please consider the subroutines names before using the module, the module usage will bring to your namespace all the subroutines described below.
279  
280 =head1 SUBROUTINES
281  
282 =head2 Print
283  
284 This subroutines prints the value of each cell.
285  
286 # Input: $matrix_a.
287 # Output: matrix content.
288 &op_print_matrix($matrix_a);
289  
290 =cut
291  
292 =head2 Fill Matrix
293  
294 This subroutines puts a number 1 inside each matrix cell.
295  
296 # Input: $matrix_a.
297 # Output: none.
298 &op_fill_matrix($matrix_a);
299  
300 =cut
301  
302 =head2 Get Column
303  
304 Returns a new Math::SparseMatrix object with the selected column. (Not tested yet!)
305  
306 # Input: $matrix_a, $column_number.
307 # Output: $matrix_b.
308 my $matrix_b = &get_col($matrix_a, $column_number);
309  
310 =cut
311  
312 =head2 Get Row
313  
314 Returns a new Math::SparseMatrix object with the selected row. (Not Tested Yest!)
315  
316 # Input: $matrix_a, $row_number.
317 # Output: $matrix_b.
318 my $matrix_b = &get_col($matrix_a, $column_number);
319  
320 =cut
321  
322 =head2 Addition
323  
324 Basic matrix addition, returns a new Math::SparseMatrix object.
325  
326 # Input: $matrix_a, $matrix_b.
327 # Output: $matrix_c.
328 my $matrix_c = &op_addition($matrix_a, $matrix_b);
329  
330 =cut
331  
332 =head2 Subtraction
333  
334 Basic matrix subtraction, returns a new Math::SparseMatrix object.
335  
336 # Input: $matrix_a, $matrix_b.
337 # Output: $matrix_c.
338 my $matrix_c = &op_subtraction($matrix_a, $matrix_b);
339  
340 =cut
341  
342 =head2 Multiplication
343  
344 Basic matrix multiplication, returns a new Math::SparseMatrix object.
345  
346 # Input: $matrix_a, $matrix_b.
347 # Output: $matrix_c.
348 my $matrix_c = &op_multiply($matrix_a, $matrix_b);
349  
350 =cut
351  
352 =head2 Identity
353  
354 Returns the identity matrix for the given input matrix. Returns a new Math::SparseMatrix object.
355  
356 # Input: $size.
357 # Output: $matrix_a.
358 my $matrix_a = &op_identity($size);
359  
360 =cut
361  
362 =head2 Transposition
363  
364 Returns the transposed matrix for the given input matrix. Returns a new Math::SparseMatrix object.
365  
366 # Input: $matrix_a.
367 # Output: $matrix_b.
368 my $matrix_b = &op_transpose($matrix_a);
369  
370 =cut
371  
372 =head2 Dot Product
373  
374 Returns the dot product for the given input matrix.
375  
376 # Input: $matrix_a.
377 # Output: $dot_product.
378 my $dot_product = &op_dot_product($matrix_a);
379  
380 =cut
381  
382  
383  
384 =head1 AUTHOR
385  
386 Felipe da V. Leprevost, C<< <leprevost at cpan.org> >>
387  
388 =head1 BUGS
389  
390 Please report any bugs or feature requests to C<bug-math-sparsematrix-operations at rt.cpan.org>, or through
391 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-SparseMatrix-Operations>. I will be notified, and then you'll
392 automatically be notified of progress on your bug as I make changes.
393  
394  
395 =head1 SUPPORT
396  
397 You can find documentation for this module with the perldoc command.
398  
399 perldoc Math::SparseMatrix::Operations
400  
401  
402 You can also look for information at:
403  
404 =over 4
405  
406 =item * RT: CPAN's request tracker (report bugs here)
407  
408 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-SparseMatrix-Operations>
409  
410 =item * AnnoCPAN: Annotated CPAN documentation
411  
412 L<http://annocpan.org/dist/Math-SparseMatrix-Operations>
413  
414 =item * CPAN Ratings
415  
416 L<http://cpanratings.perl.org/d/Math-SparseMatrix-Operations>
417  
418 =item * Search CPAN
419  
420 L<http://search.cpan.org/dist/Math-SparseMatrix-Operations/>
421  
422 =back
423  
424  
425 =head1 ACKNOWLEDGEMENTS
426  
427  
428 =head1 LICENSE AND COPYRIGHT
429  
430 Copyright 2013 Felipe da V. Leprevost.
431  
432 This program is free software; you can redistribute it and/or modify it
433 under the terms of either: the GNU General Public License as published
434 by the Free Software Foundation; or the Artistic License.
435  
436 See L<http://dev.perl.org/licenses/> for more information.
437  
438  
439 =cut
440  
441 # End of Math::SparseMatrix::Operations