blib/lib/Math/SparseMatrix.pm

filter: Statementstmt 80.9% 115 / 142 Branchbran 56.6% 34 / 60 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 90.9% 10 / 11 Podpod 14.2% 1 / 7 totaltotal 64.8% 168 / 259 CC 40 SCAR 23.8
CC
40
Cov
69%
set
16.9
get
16.0
createFromFile
10.0
CRAP 10.8
↑ Summary
1 package Math::SparseMatrix;
2  
3 =head1 NAME
4  
5 Math::SparseMatrix - Provides basic sparse matrix operations such as creation, reading from file, reading transpose from file and writing to file.
6  
7 =cut
8  
9 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 require Exporter;
14 1 require Math::SparseVector;
15  
16 1 ▶use Math::SparseVector;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 our @ISA = qw(Exporter);
20  
21 # Items to export into callers namespace by default. Note: do not export
22 # names by default without a very good reason. Use EXPORT_OK instead.
23 # Do not simply export all your public functions/methods/constants.
24  
25 # This allows declaration use Math::SparseMatrix ':all';
26 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27 # will save memory.
28 1 our %EXPORT_TAGS = ( 'all' => [ qw(
29
30 ) ] );
31  
32 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
33  
34 1 our @EXPORT = qw(
35
36 );
37  
38 1 our $VERSION = '0.03';
39  
40 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
41 3 my $class = shift;
42 3 my $rows = shift;
43 3 my $cols = shift;
44  
45 3 ▶ if (!defined $rows || !defined $cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rows and defined $cols) 0 3 3
Condition33%1/3
defined $rows and defined $cols
!ll&&!rl&&r
003
MC/DC0%0/2
defined $rows and defined $cols
defined $rows defined $cols
Truth table33%1/3
A and B
A= defined $rowsB= defined $cols
ABresult
0X0
100
111
46 0 die "Math::SparseMatrix->new error.\n" .
47 "USAGE: my \$spmatrix = Math::SparseMatrix->new(\$num_rows, \$num_cols);\n";
48 }
49  
50 3 my $self = {
51 _rows => $rows,
52 _cols => $cols,
53 _nnz => 0,
54 _data => {}
55 };
56  
57 3 bless $self, $class;
58 3 return $self;
59 }
60  
61 21 ▶sub set {
Subroutine set: called CC=10 SCAR=28.3
Pod undocumented
62 21 my $self = shift;
63 21 my ($row, $col, $val) = @_;
64  
65 21 ▶ if (!defined $row || !defined $col || !defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row and defined $col and defined $val) 0 21 21
Condition33%2/6
defined $row and defined $col and defined $val
!ll&&!rl&&r
0021
defined $row and defined $col
!ll&&!rl&&r
0021
MC/DC0%0/3
defined $row and defined $col and defined $val
defined $row defined $col defined $val
Truth table25%1/4
A and B and C
A= defined $rowB= defined $colC= defined $val
ABCresult
0XX0
10X0
1100
1111
66 0 die "Math::SparseMatrix->set error.\n" .
67 "USAGE: \$spmatrix->set(\$row, \$col, \$val);\n";
68 }
69  
70 21 ▶ if ($row < 1 || $row > $self->{_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < 1 or $row > $self->{"_rows"}) 0 21 21
Condition33%1/3
$row < 1 or $row > $self->{"_rows"}
l!l&&r!l&&!r
0021
MC/DC0%0/2
$row < 1 or $row > $self->{"_rows"}
$row < 1 $row > $self->{"_rows"}
Truth table33%1/3
A or B
A= $row < 1B= $row > $self->{"_rows"}
ABresult
1X1
011
000
71 die "Math::SparseMatrix->set error.\n" .
72 "Row index out of bounds, must be between 1 and " .
73 0 $self->{_rows} . " inclusive.\n";
74 }
75
76 21 ▶ if ($col < 1 || $col > $self->{_cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 1 or $col > $self->{"_cols"}) 0 21 21
Condition33%1/3
$col < 1 or $col > $self->{"_cols"}
l!l&&r!l&&!r
0021
MC/DC0%0/2
$col < 1 or $col > $self->{"_cols"}
$col < 1 $col > $self->{"_cols"}
Truth table33%1/3
A or B
A= $col < 1B= $col > $self->{"_cols"}
ABresult
1X1
011
000
77 die "Math::SparseMatrix->set error.\n" .
78 "Column index out of bounds, must be between 1 and " .
79 0 $self->{_cols} . " inclusive.\n";
80 }
81  
82 21 ▶ if ($val == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val == 0) 0 21 21
83 0 die "Math::SparseMatrix->set error.\n" .
84 "Cannot store zero value in a sparse matrix.\n";
85 }
86  
87 21 ▶ if (exists $self->{_data}->{$row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_data'}{$row}) { } 2 19 21
88 # update the number of non-zero elements in the matrix
89 2 $self->{_nnz} -= scalar($self->{_data}->{$row}->keys);
90 2 $self->{_data}{$row}->set($col,$val);
91 2 $self->{_nnz} += scalar($self->{_data}->{$row}->keys);
92 } else {
93 19 $self->{_data}->{$row} = new Math::SparseVector();
94 19 $self->{_data}->{$row}->set($col,$val);
95 19 $self->{_nnz}++;
96 }
97 }
98  
99 81 ▶sub get {
Subroutine get: called CC=8 SCAR=27.7
Pod undocumented
100 81 my $self = shift;
101 81 my ($row, $col) = @_;
102  
103 81 ▶ if (!defined $row || !defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row and defined $col) 0 81 81
Condition33%1/3
defined $row and defined $col
!ll&&!rl&&r
0081
MC/DC0%0/2
defined $row and defined $col
defined $row defined $col
Truth table33%1/3
A and B
A= defined $rowB= defined $col
ABresult
0X0
100
111
104 0 die "Math::SparseMatrix->get error.\n" .
105 "USAGE: \$val = \$spmatrix->get(\$row, \$col);\n";
106 }
107  
108 81 ▶ if ($row < 1 || $row > $self->{_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < 1 or $row > $self->{"_rows"}) 0 81 81
Condition33%1/3
$row < 1 or $row > $self->{"_rows"}
l!l&&r!l&&!r
0081
MC/DC0%0/2
$row < 1 or $row > $self->{"_rows"}
$row < 1 $row > $self->{"_rows"}
Truth table33%1/3
A or B
A= $row < 1B= $row > $self->{"_rows"}
ABresult
1X1
011
000
109 die "Math::SparseMatrix->get error.\n" .
110 "Row index out of bounds, must be between 1 and " .
111 0 $self->{_rows} . " inclusive.\n";
112 }
113
114 81 ▶ if ($col < 1 || $col > $self->{_cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 1 or $col > $self->{"_cols"}) 0 81 81
Condition33%1/3
$col < 1 or $col > $self->{"_cols"}
l!l&&r!l&&!r
0081
MC/DC0%0/2
$col < 1 or $col > $self->{"_cols"}
$col < 1 $col > $self->{"_cols"}
Truth table33%1/3
A or B
A= $col < 1B= $col > $self->{"_cols"}
ABresult
1X1
011
000
115 die "Math::SparseMatrix->get error.\n" .
116 "Column index out of bounds, must be between 1 and " .
117 0 $self->{_cols} . " inclusive.\n";
118 }
119  
120 81 ▶ if (exists $self->{_data}->{$row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_data'}{$row}) { } 81 0 81
121 81 return $self->{_data}->{$row}->get($col);
122 } else {
123 0 return 0;
124 }
125 }
126  
127 1 ▶sub createFromFile {
Subroutine createFromFile: called CC=9 SCAR=23.1
Pod undocumented
128  
129 1 my $class = shift;
130 1 my $infile = shift;
131  
132 1 ▶ if (!defined $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $infile) 0 1 1
133 0 die "Math::SparseMatrix->createFromFile error.\n" .
134 "USAGE: my \$spmatrix = Math::SparseMatrix->createFromFile(\$input_file);\n";
135 }
136  
137 1 ▶ if (!-f $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $infile) 0 1 1
138 0 die "Math::SparseMatrix->createFromFile error.\n" .
139 "Cannot find file $infile.\n";
140 }
141  
142 1 my $self = {
143 _rows => 0,
144 _cols => 0,
145 _nnz => 0,
146 _data => {}
147 };
148  
149 1 ▶ open(INPUT, "< $infile") or
Branch: 1 branch
BranchTrueFalseTotal
open INPUT, "< $infile" or die "Math::SparseMatrix->createFromFile error.\n" . "Failed to open sparse matrix input file $infile.\n" 1 0 1
150 die "Math::SparseMatrix->createFromFile error.\n" .
151 "Failed to open sparse matrix input file $infile.\n";
152  
153 # read the number of rows, columns and the non-zero elements in the
154 # file
155 1 my $line = <INPUT>;
156 1 chomp $line;
157 1 my ($rows, $cols, $nnz) = split / +/, $line;
158 1 $self->{_rows} = $rows;
159 1 $self->{_cols} = $cols;
160 # DO NOT SET THE NUMBER OF NON-ZEROS, THAT IS MANAGED BY THE set(...)
161 # function, which is called below
162  
163 # create a Math::SparseMatrix object
164 1 bless $self, $class;
165  
166 # also do error checks on file integrity
167 1 my $lineNum = 0;
168 1 my $nnzFound = 0;
169 1 ▶ while ($line = <INPUT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline INPUT)) 5 1 6
170 5 chomp $line;
171 5 $lineNum++;
172 5 ▶ if ($lineNum > $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNum > $rows) 0 5 5
173 0 die "Math::SparseMatrix->createFromFile error.\n".
174 "Number of rows in $infile is greater than that " .
175 "mentioned in the file header ($rows).";
176 }
177  
178 5 my @tokens = split / +/, $line;
179 5 my $index;
180 # process the <column, value> pairs in each line
181 5 ▶ for ($index = 0; $index < @tokens; $index += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @tokens) 5 5 10
182 # the column number of the current pair is the column
183 # number, and the current line number is the row number
184 5 $self->set($lineNum, $tokens[$index], $tokens[$index + 1]);
185 5 $nnzFound++;
186 }
187 }
188  
189 1 ▶ if ($nnzFound < $nnz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nnzFound < $nnz) 0 1 1
190 0 die "Math::SparseMatrix->createFromFile error.\n".
191 "Number of non-zero elements found in $infile is less than that " .
192 "mentioned in the file header -- $nnzFound < $nnz.\n";
193 }
194  
195 1 ▶ if ($nnzFound > $nnz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nnzFound > $nnz) 0 1 1
196 0 die "Math::SparseMatrix->createFromFile error.\n".
197 "Number of non-zero elements found in $infile is greater than that " .
198 "mentioned in the file header -- $nnzFound > $nnz.\n";
199 }
200  
201 # now the transposed matrix is loaded and ready to be returned
202 1 return $self;
203 }
204  
205 1 ▶sub createTransposeFromFile {
Subroutine createTransposeFromFile: called CC=9 SCAR=23.1
Pod undocumented
206  
207 1 my $class = shift;
208 1 my $infile = shift;
209  
210 1 ▶ if (!defined $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $infile) 0 1 1
211 0 die "Math::SparseMatrix->createTransposeFromFile error.\n" .
212 "USAGE: \$my \$spmatrix = Math::SparseMatrix->createTransposeFromFile(\$input_file)\n";
213 }
214  
215 1 ▶ if (!-f $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $infile) 0 1 1
216 0 die "Math::SparseMatrix->createTransposeFromFile error.\n" .
217 "Cannot find file $infile.\n";
218 }
219  
220 1 my $self = {
221 _rows => 0,
222 _cols => 0,
223 _nnz => 0,
224 _data => {}
225 };
226  
227 1 ▶ open(INPUT, "< $infile") or
Branch: 1 branch
BranchTrueFalseTotal
open INPUT, "< $infile" or die "Math::SparseMatrix->createTransposeFromFile error.\n" . "Failed to open sparse matrix input file $infile.\n" 1 0 1
228 die "Math::SparseMatrix->createTransposeFromFile error.\n" .
229 "Failed to open sparse matrix input file $infile.\n";
230  
231 # read the number of rows, columns and the non-zero elements in the
232 # file
233 1 my $line = <INPUT>;
234 1 chomp $line;
235 1 my ($rows, $cols, $nnz) = split / +/, $line;
236 # swap number of rows and columns
237 1 $self->{_rows} = $cols;
238 1 $self->{_cols} = $rows;
239 # DO NOT SET THE NUMBER OF NON-ZEROS, THAT IS MANAGED BY THE set(...)
240 # function, which is called below
241  
242 # create a Math::SparseMatrix object
243 1 bless $self, $class;
244  
245 # also do error checks on file integrity
246 1 my $lineNum = 0;
247 1 my $nnzFound = 0;
248 1 ▶ while ($line = <INPUT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline INPUT)) 5 1 6
249 5 chomp $line;
250 5 $lineNum++;
251 5 ▶ if ($lineNum > $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNum > $rows) 0 5 5
252 0 die "Math::SparseMatrix->createTransposeFromFile error.\n".
253 "Number of columns in $infile is greater than that " .
254 "mentioned in the file header ($rows).";
255 }
256  
257 5 my @tokens = split / +/, $line;
258 5 my $index;
259 # process the <column, value> pairs in each line
260 5 ▶ for ($index = 0; $index < @tokens; $index += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @tokens) 5 5 10
261 # the column number of the current pair is the row
262 # number in the transpose, the current line number is
263 # the column number in the transpose
264 5 $self->set($tokens[$index], $lineNum, $tokens[$index + 1]);
265 5 $nnzFound++;
266 }
267 }
268  
269 1 ▶ if ($nnzFound < $nnz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nnzFound < $nnz) 0 1 1
270 0 die "Math::SparseMatrix->createTransposeFromFile error.\n".
271 "Number of non-zero elements found in $infile is less than that " .
272 "mentioned in the file header -- $nnzFound < $nnz.\n";
273 }
274  
275 1 ▶ if ($nnzFound > $nnz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nnzFound > $nnz) 0 1 1
276 0 die "Math::SparseMatrix->createTransposeFromFile error.\n".
277 "Number of non-zero elements found in $infile is greater than that " .
278 "mentioned in the file header -- $nnzFound > $nnz.\n";
279 }
280  
281  
282 # now the transposed matrix is loaded and ready to be returned
283 1 return $self;
284 }
285  
286 1 ▶sub writeToFile {
Subroutine writeToFile: called CC=6 SCAR=20.5
Pod undocumented
287  
288 1 my $self = shift;
289 1 my $outfile = shift;
290  
291 1 ▶ if (!defined $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outfile) 0 1 1
292 0 die "Math::SparseMatrix->writeToFile error.\n" .
293 "USAGE: \$spmatrix->writeToFile (\$output_file);\n";
294 }
295  
296 # write it to the output file
297 1 ▶ open(OUTPUT, "> $outfile") or
Branch: 1 branch
BranchTrueFalseTotal
open OUTPUT, "> $outfile" or die "Math::SparseMatrix->writeToFile error.\n" . "Failed to create output file $outfile.\n" 1 0 1
298 die "Math::SparseMatrix->writeToFile error.\n" .
299 "Failed to create output file $outfile.\n";
300 print OUTPUT $self->{_rows} . " " . $self->{_cols} . " " .
301 1 $self->{_nnz} . "\n";
302 1 my $row;
303 1 my $linecount = 1;
304 1 foreach $row (sort {$a <=> $b} keys %{$self->{_data}}) {
305 5 ▶ if ($row > $linecount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row > $linecount) 0 5 5
306 # add empty lines for empty rows
307 0 my $i = 0;
308 0 my $limit = $row - $linecount;
309 0 ▶ for ($i = 0; $i < $limit; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $limit) 0 0 0
310 0 print OUTPUT "\n";
311 0 $linecount++;
312 }
313 }
314 5 my $vec = $self->{_data}->{$row};
315 5 my $line = $vec->stringify();
316 5 print OUTPUT $line . "\n";
317 5 $linecount++;
318 }
319 1 close OUTPUT;
320 }
321  
322 0 ▶sub printDims {
Subroutine printDims: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my $self = shift;
324 0 print "Rows: " . $self->{_rows} . ". Cols: " . $self->{_cols} . ".\n";
325 }
326  
327 1 1;
328 __END__
↑ Summary