| 1 | package Sparse::Vector; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
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 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | use overload | ||||||||||||||||||||||||||||
| 11 | 1 | ▶ | '++' => 'incr', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | '+' => 'add', | ||||||||||||||||||||||||||||
| 13 | 1 | 'fallback' => undef; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | @EXPORT_OK = (); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | @EXPORT = (); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # sparse vector contructor | ||||||||||||||||||||||||||||
| 26 | # creates an empty sparse vector | ||||||||||||||||||||||||||||
| 27 | sub new | ||||||||||||||||||||||||||||
| 28 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | 8 | my $self = {}; | |||||||||||||||||||||||||||
| 31 | 8 | bless($self,$class); | |||||||||||||||||||||||||||
| 32 | 8 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # sets value at given index | ||||||||||||||||||||||||||||
| 36 | sub set | ||||||||||||||||||||||||||||
| 37 | 46 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 46 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 46 | my $key = shift; | |||||||||||||||||||||||||||
| 40 | 46 | my $value = shift; | |||||||||||||||||||||||||||
| 41 | 46 | ▶ | if(!defined $key || !defined $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $key and defined $value
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
| |||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 0 | print STDERR "Usage: vector->set(key,value)\n"; | |||||||||||||||||||||||||||
| 44 | 0 | exit; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 46 | ▶ | if($value==0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 0 | print STDERR "Can not store 0 in the Sparse::Vector.\n"; | |||||||||||||||||||||||||||
| 49 | 0 | exit; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 46 | $self->{$key} = $value; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # returns value at given index | ||||||||||||||||||||||||||||
| 55 | sub get | ||||||||||||||||||||||||||||
| 56 | 67 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=13.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 67 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 67 | my $key = shift; | |||||||||||||||||||||||||||
| 59 | 67 | ▶ | if(!defined $key) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | print STDERR "Usage: vector->get(key)\n"; | |||||||||||||||||||||||||||
| 62 | 0 | exit; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 67 | ▶ | if(defined $self->{$key}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 67 | return $self->{$key}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | return 0; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # returns indices of non-zero values in sorted order | ||||||||||||||||||||||||||||
| 72 | sub keys | ||||||||||||||||||||||||||||
| 73 | 22 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 22 | my @indices = keys %{$self}; | |||||||||||||||||||||||||||
| 76 | 22 | my @sorted = sort {$a <=> $b} @indices; | |||||||||||||||||||||||||||
| 77 | 22 | return @sorted; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # returns 1 if the vector is empty | ||||||||||||||||||||||||||||
| 81 | sub isnull | ||||||||||||||||||||||||||||
| 82 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
isnull: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 3 | my @indices = $self->keys; | |||||||||||||||||||||||||||
| 85 | 3 | ▶ | if(scalar(@indices)==0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 2 | return 1; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 1 | return 0; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # prints sparse vector | ||||||||||||||||||||||||||||
| 93 | sub print | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
print: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 0 | foreach my $ind ($self->keys) | |||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | print "$ind " . $self->get($ind) . " "; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # returns the equivalent string form | ||||||||||||||||||||||||||||
| 104 | sub stringify | ||||||||||||||||||||||||||||
| 105 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 10 | my $str=""; | |||||||||||||||||||||||||||
| 108 | 10 | foreach my $ind ($self->keys) | |||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 49 | $str.= "$ind " . $self->get($ind) . " "; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 10 | chop $str; | |||||||||||||||||||||||||||
| 113 | 10 | return $str; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # increments value at given index | ||||||||||||||||||||||||||||
| 117 | sub incr | ||||||||||||||||||||||||||||
| 118 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
incr: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 1 | my $key = shift; | |||||||||||||||||||||||||||
| 121 | 1 | ▶ | if(!defined $key) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 0 | print STDERR "Usage: vector->incr(key)\n"; | |||||||||||||||||||||||||||
| 124 | 0 | exit; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 1 | $self->{$key}++; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # adds 2 sparse vectors | ||||||||||||||||||||||||||||
| 130 | sub add | ||||||||||||||||||||||||||||
| 131 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 2 | my $v2 = shift; | |||||||||||||||||||||||||||
| 134 | 2 | ▶ | if(!defined $v2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | 0 | print STDERR "Usage: v1->add(v2)\n"; | |||||||||||||||||||||||||||
| 137 | 0 | exit; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 2 | foreach my $key ($v2->keys) | |||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 13 | ▶ | if(defined $self->{$key}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||
| 143 | 5 | $self->{$key}+=$v2->get($key); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else | ||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||
| 147 | 8 | $self->{$key}=$v2->get($key); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # returns the norm | ||||||||||||||||||||||||||||
| 153 | sub norm | ||||||||||||||||||||||||||||
| 154 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
norm: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | 2 | my $sum = 0; | |||||||||||||||||||||||||||
| 157 | 2 | foreach my $key ($self->keys) | |||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 12 | my $value = $self->{$key}; | |||||||||||||||||||||||||||
| 160 | 12 | $sum += $value ** 2; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 2 | return sqrt $sum; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # normalizes given sparse vector | ||||||||||||||||||||||||||||
| 166 | sub normalize | ||||||||||||||||||||||||||||
| 167 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
normalize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 169 | 1 | my $vnorm = $self->norm; | |||||||||||||||||||||||||||
| 170 | 1 | ▶ | if($vnorm != 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | { | ||||||||||||||||||||||||||||
| 172 | 1 | $self->div($vnorm); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | sub dot | ||||||||||||||||||||||||||||
| 177 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dot: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 1 | my $v2 = shift; | |||||||||||||||||||||||||||
| 180 | 1 | ▶ | if(!defined $v2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 0 | print STDERR "Usage: v1->dot(v2)\n"; | |||||||||||||||||||||||||||
| 183 | 0 | exit; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 1 | my $dotprod = 0; | |||||||||||||||||||||||||||
| 186 | 1 | foreach my $key ($v2->keys) | |||||||||||||||||||||||||||
| 187 | { | ||||||||||||||||||||||||||||
| 188 | 5 | ▶ | if(defined $self->{$key}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | { | ||||||||||||||||||||||||||||
| 190 | 3 | $dotprod += $v2->get($key) * $self->{$key}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 1 | return $dotprod; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # divides each vector entry by a given divisor | ||||||||||||||||||||||||||||
| 197 | sub div | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
div: called CC=4 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 200 | 2 | my $divisor = shift; | |||||||||||||||||||||||||||
| 201 | 2 | ▶ | if(!defined $divisor) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||
| 203 | 0 | print STDERR "Usage: v1->div(DIVISOR)\n"; | |||||||||||||||||||||||||||
| 204 | 0 | exit; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 2 | ▶ | if($divisor==0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||
| 208 | 0 | print STDERR "Divisor 0 not allowed in Sparse::Vector::div().\n"; | |||||||||||||||||||||||||||
| 209 | 0 | exit; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | 2 | foreach my $key ($self->keys) | |||||||||||||||||||||||||||
| 212 | { | ||||||||||||||||||||||||||||
| 213 | 12 | $self->{$key}/=$divisor; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # adds a given sparse vector to a binary sparse vector | ||||||||||||||||||||||||||||
| 218 | sub binadd | ||||||||||||||||||||||||||||
| 219 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
binadd: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 220 | 1 | my $v1 = shift; | |||||||||||||||||||||||||||
| 221 | 1 | my $v2 = shift; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | ▶ | if(!defined $v2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||
| 225 | 0 | print STDERR "Usage: v1->binadd(v2)\n"; | |||||||||||||||||||||||||||
| 226 | 0 | exit; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 1 | foreach my $key ($v2->keys) | |||||||||||||||||||||||||||
| 230 | { | ||||||||||||||||||||||||||||
| 231 | 6 | $v1->{$key}=1; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # deallocates all the vector entries | ||||||||||||||||||||||||||||
| 236 | sub free | ||||||||||||||||||||||||||||
| 237 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
free: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 238 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 239 | 5 | %{$self}=(); | |||||||||||||||||||||||||||
| 240 | 5 | undef %{$self}; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 1 | 1; | |||||||||||||||||||||||||||
| 244 | __END__ | ||||||||||||||||||||||||||||