blib/lib/Math/SparseVector.pm

filter: Statementstmt 78.8% 97 / 123 Branchbran 56.2% 18 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 15 totaltotal 69.0% 134 / 194 CC 26 SCAR 14.8
CC
26
Cov
73%
dot
10.4
print
6.0
set
5.7
CRAP 4.4
↑ Summary
1 package Math::SparseVector;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10  
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14  
15 # This allows declaration use Math::SparseVector ':all';
16 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17 # will save memory.
18 1 our %EXPORT_TAGS = ( 'all' => [ qw(
19
20 ) ] );
21  
22 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23  
24 1 our @EXPORT = qw(
25
26 );
27  
28 1 our $VERSION = '0.03';
29  
30 use overload
31 1 ▶ '++' => 'incr',
Subroutine BEGIN: called CC=1 SCAR=0.0
32 '+' => 'add',
33 1 'fallback' => undef;
34  
35 # sparse vector contructor
36 # creates an empty sparse vector
37 sub new
38 8 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
39 8 my $class = shift;
40 8 my $self = {};
41 8 bless($self,$class);
42 8 return $self;
43 }
44  
45 # sets value at given index
46 sub set
47 46 ▶{
Subroutine set: called CC=4 SCAR=17.3
Pod undocumented
48 46 my $self = shift;
49 46 my $key = shift;
50 46 my $value = shift;
51 46 ▶ if(!defined $key || !defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and defined $value) 0 46 46
Condition33%1/3
defined $key and defined $value
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $key and defined $value
defined $key defined $value
Truth table33%1/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
52 {
53 0 print STDERR "Usage: vector->set(key,value)\n";
54 0 exit;
55 }
56 46 ▶ if($value==0)
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 0 46 46
57 {
58 0 print STDERR "Can not store 0 in the Math::SparseVector.\n";
59 0 exit;
60 }
61 46 $self->{$key} = $value;
62 }
63  
64 # returns value at given index
65 sub get
66 67 ▶{
Subroutine get: called CC=3 SCAR=13.0
Pod undocumented
67 67 my $self = shift;
68 67 my $key = shift;
69 67 ▶ if(!defined $key)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 67 67
70 {
71 0 print STDERR "Usage: vector->get(key)\n";
72 0 exit;
73 }
74 67 ▶ if(defined $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) 67 0 67
75 {
76 67 return $self->{$key};
77 }
78 0 return 0;
79 }
80  
81 # returns indices of non-zero values in sorted order
82 sub keys
83 24 ▶{
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
84 24 my $self = shift;
85 24 my @indices = keys %{$self};
86 24 my @sorted = sort {$a <=> $b} @indices;
87 24 return @sorted;
88 }
89  
90 # returns 1 if the vector is empty
91 sub isnull
92 3 ▶{
Subroutine isnull: called CC=2 SCAR=6.9
Pod undocumented
93 3 my $self = shift;
94 3 my @indices = $self->keys;
95 3 ▶ if(scalar(@indices)==0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @indices == 0) 2 1 3
96 {
97 2 return 1;
98 }
99 1 return 0;
100 }
101  
102 # prints sparse vector
103 sub print
104 0 ▶{
Subroutine print: not called CC=2 SCAR=17.9
Pod undocumented
105 0 my $self = shift;
106 0 foreach my $ind ($self->keys)
107 {
108 0 print "$ind " . $self->get($ind) . " ";
109 }
110 0 print "\n";
111 }
112  
113 # returns the equivalent string form
114 sub stringify
115 10 ▶{
Subroutine stringify: called CC=2 SCAR=6.9
Pod undocumented
116 10 my $self = shift;
117 10 my $str="";
118 10 foreach my $ind ($self->keys)
119 {
120 49 $str.= "$ind " . $self->get($ind) . " ";
121 }
122 10 chop $str;
123 10 return $str;
124 }
125  
126 # increments value at given index
127 sub incr
128 1 ▶{
Subroutine incr: called CC=2 SCAR=7.9
Pod undocumented
129 1 my $self = shift;
130 1 my $key = shift;
131 1 ▶ if(!defined $key)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 1 1
132 {
133 0 print STDERR "Usage: vector->incr(key)\n";
134 0 exit;
135 }
136 1 $self->{$key}++;
137 }
138  
139 # adds 2 sparse vectors
140 sub add
141 2 ▶{
Subroutine add: called CC=4 SCAR=14.3
Pod undocumented
142 2 my $self = shift;
143 2 my $v2 = shift;
144 2 ▶ if(!defined $v2)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v2) 0 2 2
145 {
146 0 print STDERR "Usage: v1->add(v2)\n";
147 0 exit;
148 }
149 2 foreach my $key ($v2->keys)
150 {
151 13 ▶ if(defined $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) { } 5 8 13
152 {
153 5 $self->{$key}+=$v2->get($key);
154 }
155 else
156 {
157 8 $self->{$key}=$v2->get($key);
158 }
159 }
160 }
161  
162 # returns the norm
163 sub norm
164 2 ▶{
Subroutine norm: called CC=2 SCAR=6.9
Pod undocumented
165 2 my $self = shift;
166 2 my $sum = 0;
167 2 foreach my $key ($self->keys)
168 {
169 12 my $value = $self->{$key};
170 12 $sum += $value ** 2;
171 }
172 2 return sqrt $sum;
173 }
174  
175 # normalizes given sparse vector
176 sub normalize
177 1 ▶{
Subroutine normalize: called CC=2 SCAR=7.0
Pod undocumented
178 1 my $self = shift;
179 1 my $vnorm = $self->norm;
180 1 ▶ if($vnorm != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($vnorm != 0) 1 0 1
181 {
182 1 $self->div($vnorm);
183 }
184 }
185  
186 sub dot
187 1 ▶{
Subroutine dot: called CC=7 SCAR=23.4
Pod undocumented
188 1 my $self = shift;
189 1 my $v2 = shift;
190 1 ▶ if(!defined $v2)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v2) 0 1 1
191 {
192 0 print STDERR "Usage: v1->dot(v2)\n";
193 0 exit;
194 }
195 1 my $dotprod = 0;
196  
197 # optimize to do lesser comparisons by looping on
198 # the smaller vector
199 1 ▶ if (scalar($v2->keys) < scalar($self->keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $v2->keys < scalar $self->keys) { } 1 0 1
200 # v2 is smaller
201 1 foreach my $key ($v2->keys)
202 {
203 5 ▶ if(defined $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) 3 2 5
204 {
205 3 $dotprod += $v2->get($key) * $self->{$key};
206 }
207 }
208 } else {
209 # self is smaller or equal to v2
210 0 foreach my $key ($self->keys)
211 {
212 0 ▶ if(defined $v2->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v2->{$key}) 0 0 0
213 {
214 0 $dotprod += $v2->get($key) * $self->{$key};
215 }
216 }
217 }
218  
219 1 return $dotprod;
220 }
221  
222 # divides each vector entry by a given divisor
223 sub div
224 2 ▶{
Subroutine div: called CC=4 SCAR=16.6
Pod undocumented
225 2 my $self = shift;
226 2 my $divisor = shift;
227 2 ▶ if(!defined $divisor)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $divisor) 0 2 2
228 {
229 0 print STDERR "Usage: v1->div(DIVISOR)\n";
230 0 exit;
231 }
232 2 ▶ if($divisor==0)
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor == 0) 0 2 2
233 {
234 0 print STDERR "Divisor 0 not allowed in Math::SparseVector::div().\n";
235 0 exit;
236 }
237 2 foreach my $key ($self->keys)
238 {
239 12 $self->{$key}/=$divisor;
240 }
241 }
242  
243 # adds a given sparse vector to a binary sparse vector
244 sub binadd
245 1 ▶{
Subroutine binadd: called CC=3 SCAR=12.0
Pod undocumented
246 1 my $v1 = shift;
247 1 my $v2 = shift;
248  
249 1 ▶ if(!defined $v2)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v2) 0 1 1
250 {
251 0 print STDERR "Usage: v1->binadd(v2)\n";
252 0 exit;
253 }
254  
255 1 foreach my $key ($v2->keys)
256 {
257 6 $v1->{$key}=1;
258 }
259 }
260  
261 # deallocates all the vector entries
262 sub free
263 5 ▶{
Subroutine free: called CC=1 SCAR=0.0
Pod undocumented
264 5 my $self = shift;
265 5 %{$self}=();
266 5 undef %{$self};
267 }
268  
269 1 1;
270 __END__
↑ Summary