blib/lib/Math/MatrixSparse.pm

filter: Statementstmt 40.9% 656 / 1602 Branchbran 26.2% 208 / 794 Conditioncond 21.3% 44 / 206 MC/DCmcdc 5.4% 7 / 129 Subroutinesub 46.3% 63 / 136 Podpod 0.0% 0 / 106 totaltotal 32.9% 978 / 2973 CC 574 SCAR 49.6
CC
574
Cov
35%
quickmultiplyfree
930.0
multiply
756.0
multiplyfree
650.0
CRAP 142.0
↑ Summary
1 package Math::MatrixSparse;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 require Exporter;
10  
11 7 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use Math::MatrixSparse ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 7 our %EXPORT_TAGS = ( 'all' => [ qw(
21 splitkey
22 makekey
23 ) ] );
24  
25 7 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} });
26  
27 7 our @EXPORT = qw(
28
29 );
30 7 our $VERSION = '0.01';
31  
32 use overload
33 7 ▶ "+" => "add",
Subroutine BEGIN: called CC=1 SCAR=0.0
34 "*" => "quickmultiply",
35 "x" => "kronecker",
36 "-" => "subtract",
37 "neg" => "negate",
38 "~" => "transpose",
39 "**" => "exponentiate",
40 "==" => "equals",
41 '""' => "print",
42 "&" => "matrixand",
43 "|" => "matrixor",
44 7 "fallback" => undef;
45  
46  
47 # Preloaded methods go here.
48  
49 ### CREATION METHODS
50 338 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
51 338 my ($proto,$name) = @_;
52 338 my $this;
53 338 $this->{data} = undef;
54 338 $this->{name} = $name;
55 338 $this->{rows} = 0;
56 338 $this->{columns} =0;
57 338 $this->{special}->{bandwidth} = 0;
58 338 $this->{special}->{structure} = "";
59 338 $this->{special}->{shape} = "";
60 338 $this->{special}->{sign} = "";
61 338 $this->{special}->{pattern} = 0;
62 338 $this->{special}->{field} = "real";
63  
64 338 bless ($this);
65 338 return $this;
66 }
67  
68  
69 6 ▶sub newfromstring {
Subroutine newfromstring: called CC=2 SCAR=6.9
Pod undocumented
70 6 my ($proto, $string,$name) = @_;
71 6 my $this=new Math::MatrixSparse;
72 6 my @entries = split(/\n/,$string);
73 6 $this->{special}->{structure} = ".";
74 6 foreach my $entry (@entries) {
75 45 my ($i,$j,$value) = $entry=~m/^\s*(\d+)\s+(\d+)\s+(.+)\s*$/;
76 45 $this->assign($i,$j,$value);
77 }
78 6 $this->{name} =$name;
79 6 $this->{special}->{field} = "real";
80 6 return $this;
81 }
82  
83 0 ▶sub newdiag {
Subroutine newdiag: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my ($proto, $diag, $name) = @_;
85 0 my @data = @{$diag};
86 0 my $this = new Math::MatrixSparse;
87 0 $this->name($name);
88 0 my $i=1;
89 0 foreach my $entry (@data){
90 0 $this->assign($i,$i,$entry);
91 0 $i++;
92 }
93 0 $this->{special}->{structure} = "symmetric";
94 0 $this->{special}->{shape} = "diagonal";
95 0 $this->{special}->{field} = "real";
96 0 $this->{special}->{square} = 1;
97 0 return $this;
98 }
99  
100  
101  
102 0 ▶sub newdiagfromstring {
Subroutine newdiagfromstring: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my ($proto, $string, $name) = @_;
104 0 my $this=new Math::MatrixSparse;
105 0 my @entries = split(/\n/,$string);
106 0 foreach my $entry (@entries) {
107 0 my ($i,$value) = $entry=~m/^(\d+)\s+(.+)$/;
108 0 $this->assign($i,$i,$value);
109 }
110 0 $this->{name} =$name;
111 0 $this->{special}->{structure} = "symmetric";
112 0 $this->{special}->{field} = "real";
113 0 $this->{special}->{shape} = "diagonal";
114 0 $this->{special}->{square} = 1;
115 0 return $this;
116 }
117  
118  
119 4 ▶sub newidentity {
Subroutine newidentity: called CC=5 SCAR=16.1
Pod undocumented
120 4 my ($proto, $n,$m) = @_;
121 4 my $this = new Math::MatrixSparse;
122 4 ▶ $m=$n unless ($m);
Branch: 1 branch
BranchTrueFalseTotal
unless $m 3 1 4
123 4 $this->{name} = "I$n";
124 4 ▶ my $min = $m<$n ? $m :$n;
Branch: 1 branch
BranchTrueFalseTotal
$m < $n ? : 0 4 4
125 4 for my $i (1..$min) {
126 340 $this->assign($i,$i,1);
127 }
128 4 $this->{rows} = $n;
129 4 $this->{columns} = $m;
130 4 $this->{special}->{structure} = "symmetric";
131 4 $this->{special}->{field} = "real";
132 4 $this->{special}->{shape} = "diagonal";
133 4 $this->{special}->{sign} = "nonnegative";
134 4 ▶ $this->{special}->{square} = 1 if $m==$n;
Branch: 1 branch
BranchTrueFalseTotal
if $m == $n 3 1 4
135 4 $this->{special}->{pattern} = 0;
136 4 return $this;
137 }
138  
139 4 ▶sub newrandom {
Subroutine newrandom: called CC=14 SCAR=27.0
Pod undocumented
140 4 my ($proto,$maxrow,$maxcol,$max,$density,$name) = @_;
141 4 ▶ $maxcol = $maxrow unless defined $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxcol 0 4 4
142 4 ▶ $density = 1 unless (defined $density)&&($density>=0) && ($density<=1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $density and $density >= 0 and $density <= 1 1 3 4
Condition66%4/6
defined $density and $density >= 0 and $density <= 1
!ll&&!rl&&r
103
defined $density and $density >= 0
!ll&&!rl&&r
103
MC/DC33%1/3
defined $density and $density >= 0 and $density <= 1
defined $density $density >= 0 $density <= 1
Truth table50%2/4
A and B and C
A= defined $densityB= $density >= 0C= $density <= 1
ABCresult
0XX0
10X0
1100
1111
143 4 my $stoch = new Math::MatrixSparse($name);
144 4 ▶ unless (defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max) 1 3 4
145 1 for my $i (1..$maxrow) {
146 32 for my $j (1..$maxcol) {
147 32 my $uniform = rand;
148 32 ▶ next unless $uniform<=$density;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniform <= $density 0 32 32
149 32 $stoch->assign($i,$j,rand);
150 }
151 }
152 1 return $stoch;
153 }
154 3 ▶ $name = "" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 0 3
155 3 $stoch->assign($maxrow,$maxcol,0);
156 3 my $k = 1;
157 3 ▶ while ($k<=$max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $max) 1115 3 1118
158 1115 my ($i,$j) = (int($maxrow* rand)+1,int($maxcol* rand)+1);
159 1115 ▶ next if $stoch->element($i,$j);
Branch: 1 branch
BranchTrueFalseTotal
if $stoch->element($i, $j) 15 1100 1115
160 1100 my $uniform = rand;
161 1100 ▶ if ($uniform>$density) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniform > $density) 0 1100 1100
162 0 carp "Math::MatrixReal::newrandom ignoring element";
163 }
164 1100 ▶ next unless $uniform <= $density;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniform <= $density 0 1100 1100
165 1100 $stoch->assign($i,$j,rand);
166 1100 $k++;
167 }
168 3 $stoch->{special}->{sign}="nonnegative";
169 3 return $stoch;
170 }
171  
172 ### INPUT-OUTPUT METHODS
173 1 ▶sub newharwellboeing {
Subroutine newharwellboeing: called CC=14 SCAR=27.4
Pod undocumented
174 1 my ($proto, $filename) = @_;
175 1 ▶ open(HB,"<$filename" ) || croak "Math::MatrixSparse::newharwellboeing Can't open $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
open HB, "<$filename" or croak("Math::MatrixSparse::newharwellboeing Can't open $filename\n") 1 0 1
176 1 my $this = new Math::MatrixSparse;
177 1 $_ = <HB>;
178 1 chomp();
179 1 my ($ident, $key) = $_ =~ m/^(.*)(........)\s*$/;
180 1 $key =~ s/\s*$//;
181 1 $this->{name} = $key;
182 1 $_ = <HB>;
183 1 chomp();
184 1 my ($lines, $pline, $rline, $nvline, $rhsline) = $_ =~
185 m/^\s*(\d{1,14})\s*(\d{1,14})\s*(\d{1,14})\s*(\d{1,14})\s*(\d{1,14})\s*$/;
186 1 $_ = <HB>;
187 1 chomp();
188 1 my ($mattype, $rows, $columns, $rvi, $eme) = $_ =~
189 m/^\s*(...)\s*(\d{1,14})\s*(\d{1,14})\s*(\d{1,14})\s*(\d{1,14})\s*$/;
190 1 ▶ if ($mattype =~ /^C/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mattype =~ /^C/) { } 0 1 1
191 0 croak "Math::MatrixSparse::newharwellboeing Complex types not implemented, exiting\n";
192 } else {
193 1 $this->{special}->{field} = "real";
194 }
195 1 $this->{rows} = $rows;
196 1 $this->{columns} = $columns;
197 1 my $formatline = <HB>;
198 1 chomp($formatline);
199 1 my ($colft, $rowft, $valft,$rhsft) = $formatline =~ m/(\([a-zA-Z0-9.+-+]+\))/g;
200 1 ▶ unless (($colft =~ /I/i)&&($rowft =~ /I/i)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($colft =~ /I/i and $rowft =~ /I/i) 0 1 1
Condition33%1/3
$colft =~ /I/i and $rowft =~ /I/i
!ll&&!rl&&r
001
MC/DC0%0/2
$colft =~ /I/i and $rowft =~ /I/i
$colft =~ /I/i $rowft =~ /I/i
Truth table33%1/3
A and B
A= $colft =~ /I/iB= $rowft =~ /I/i
ABresult
0X0
100
111
201 0 carp "Math::MatrixSparse::newharwellboeing non-integer format for rows and columns";
202 }
203 1 $valft =~ s/\d+P//g;
204 1 my ($valrep, $valsize) = $valft =~ m/(\d+)[a-zA-Z](\d+)/;
205 1 my $valregex = '.' x $valsize;
206 1 my $rhsspec;
207 1 ▶ if ($rhsline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhsline) 0 1 1
208 0 $rhsspec = <HB>;
209 0 chomp($rhsspec);
210 }
211 #now read the column pointer data...
212 1 my @colpointers;
213 1 for my $i (1..$pline) {
214 27 $_ = <HB>;
215 27 s/^\s*//;
216 27 s/\s*$//;
217 27 push @colpointers, split(/\s+/);
218 }
219 #...and the row data...
220 1 my @rowindex;
221 1 for my $i (1..$rline) {
222 259 $_ = <HB>;
223 259 s/^\s*//;
224 259 s/\s*$//;
225 259 push @rowindex, split(/\s+/);
226 }
227 #...and any value data. If the matrix is a pattern type, fill
228 # @values with ones.
229 1 my @values;
230 1 ▶ if ($mattype =~ m/^P..$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mattype =~ /^P..$/i) { } 0 1 1
231 0 @values = (1) x $rvi;
232 } else {
233 1 for my $i (1..$nvline) {
234 1035 $_ = <HB>;
235 1035 s/D/e/g;
236 1035 push @values, map {s/\s//g; $_+0} m/$valregex/g;
237 }
238 }
239  
240 1 my $curcol = 1;
241 1 foreach my $i (0..($#colpointers-1)) {
242 420 my $thiscol = $colpointers[$i];
243 420 my $nextcol = $colpointers[$i+1];
244 420 my @rowslice = @rowindex[$thiscol-1..$nextcol-2];
245 420 my @valslice = @values[$thiscol-1..$nextcol-2];
246 420 foreach my $j (0..$#rowslice) {
247 4140 $this->assign($rowslice[$j],$curcol,$valslice[$j]);
248 }
249 420 $curcol++;
250 }
251 1 ▶ if ($mattype =~ /^.S.$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mattype =~ /^.S.$/) { } 1 0 1
elsif ($mattype =~ /^.Z.$/) { } 0 0 0
252 1 $this->_symmetrify();
253 } elsif ($mattype =~ /^.Z.$/) {
254 0 $this->_skewsymmetrify();
255 }
256  
257 1 return $this;
258 }
259  
260 2 ▶sub newmatrixmarket {
Subroutine newmatrixmarket: called CC=13 SCAR=27.8
Pod undocumented
261 2 my ($proto, $filename) = @_;
262 2 my $this = new Math::MatrixSparse;
263 2 ▶ confess "Math::MatrixSparse::newmatrixmarket undefined filename" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 2 2
264 2 ▶ open(MM,"<$filename" ) || croak "Math::MatrixSparse::newmatrixmarket Can't open $filename for reading";
Branch: 1 branch
BranchTrueFalseTotal
open MM, "<$filename" or croak("Math::MatrixSparse::newmatrixmarket Can't open $filename for reading") 2 0 2
265 2 $_=<MM>;
266 2 ▶ unless (/^\%\%MatrixMarket/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\%\%MatrixMarket/) 0 2 2
267 0 confess "Math::MatrixSparse::newmatrixmarket Invalid start of file";
268 }
269 2 ▶ unless (/coordinate/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/coordinate/i) 0 2 2
270 0 confess "Math::MatrixSparse::newmatrixmarket dense format not implemented";
271 }
272 2 ▶ if (/complex/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/complex/i) { } 0 2 2
273 0 carp "Math::MatrixSparse::newmatrixmarket Complex matrices not implemented, ignoring imaginary part\n";
274 0 $this->{special}->{field} = "real";
275 } else {
276 2 $this->{special}->{field} = "real";
277 }
278 2 my $specifications = $_;
279 2 my $ispattern;
280 my $issymmetric;
281 2 my $isskewsymmetric;
282 2 ▶ if ($specifications =~ /pattern/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specifications =~ /pattern/i) 2 0 2
283 2 $ispattern = 1;
284 2 $this->{special}->{pattern} = 1;
285 }
286 2 $this->{name} = $filename;
287 2 my $startdata=0;
288 2 my $entries =0;
289 2 my ($rows,$columns);
290  
291 2 ▶ while (<MM>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MM)) 254 2 256
292 254 ▶ next if /^\%/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\%/ 0 254 254
293 254 ▶ unless ($startdata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($startdata) { } 252 2 254
294 2 s/^\s*//;
295 2 s/\s*$//;
296 2 ($rows,$columns,$entries) = split(/\s+/);
297 2 $this->{rows} = $rows;
298 2 $this->{columns} = $columns;
299 2 $startdata = 1;
300 } else {
301 252 s/^\s*//;
302 252 s/\s*$//;
303 252 ($rows,$columns,$entries) = split(/\s+/);
304 252 ▶ $entries = 1 if $ispattern;
Branch: 1 branch
BranchTrueFalseTotal
if $ispattern 252 0 252
305 252 $this->assign($rows,$columns,$entries);
306 }
307 }
308 2 ▶ if ($specifications =~ /\Wsymmetric/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specifications =~ /\Wsymmetric/i) 0 2 2
309 0 $this->{special}->{structure} = "symmetric";
310 0 return $this->symmetrify();
311 }
312 2 ▶ if ($specifications =~ /skewsymmetric/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specifications =~ /skewsymmetric/i) 0 2 2
313 0 $this->{special}->{structure} = "skewsymmetric";
314 0 return $this->skewsymmetrify();
315 }
316 # if ($specifications =~ /hermetian/i) {
317 # $this->{special}->{structure} = "hermetian";
318 # return $this->symmetrify();
319 # }
320 2 return $this;
321 }
322  
323 0 ▶sub writematrixmarket {
Subroutine writematrixmarket: not called CC=9 SCAR=45.0
Pod undocumented
324 0 my ($matrix, $filename) = @_;
325 0 ▶ open(MM,">$filename" ) || croak "Math::MatrixSparse::newmatrixmarket Can't open $filename for writing";
Branch: 1 branch
BranchTrueFalseTotal
open MM, ">$filename" or croak("Math::MatrixSparse::newmatrixmarket Can't open $filename for writing") 0 0 0
326 0 print MM '%%MatrixMarket matrix coordinate ';
327 0 ▶ print MM $matrix->{special}->{pattern} ? "pattern " : "real ";
Branch: 1 branch
BranchTrueFalseTotal
$matrix->{'special'}{'pattern'} ? : 0 0 0
328 0 ▶ if ($matrix->{special}->{structure} =~ m/^symmetric/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($matrix->{'special'}{'structure'} =~ /^symmetric/i) { } 0 0 0
elsif ($matrix->{'special'}{'structure'} =~ /skewsymmetric/i) { } 0 0 0
329 0 $matrix=$matrix->symmetrify();
330 0 print MM "symmetric";
331 } elsif ($matrix->{special}->{structure} =~ m/skewsymmetric/i) {
332 0 $matrix=$matrix->skewsymmetrify();
333 0 print MM "skewsymmetric";
334 } else {
335 0 print MM "general\n";
336 }
337 0 print MM "$matrix->{rows} $matrix->{columns} ";
338 0 print MM $matrix->count() , "\n";
339 0 ▶ if ($matrix->{special}->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->{'special'}{'pattern'}) { } 0 0 0
340 0 foreach my $key ($matrix->sortbycolumn()) {
341 0 my ($i,$j) = &splitkey($key);
342 0 ▶ next unless $matrix->element($i,$j);
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->element($i, $j) 0 0 0
343 0 print MM "$i $j\n";
344 }
345 } else {
346 0 foreach my $key ($matrix->sortbycolumn()) {
347 0 my ($i,$j) = &splitkey($key);
348 0 print MM "$i $j ", $matrix->{data}{$key}, "\n";
349 }
350 }
351 0 return;
352 }
353  
354  
355 163 ▶sub copy {
Subroutine copy: called CC=4 SCAR=13.9
Pod undocumented
356 163 my ($proto,$name) = @_;
357 163 my $this = new Math::MatrixSparse;
358 163 ▶ return $this unless defined $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto 0 163 163
359 163 ▶ if (defined $proto->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto->{"data"}) 162 1 163
360 162 %{$this->{data}} = %{$proto->{data}};
361 }
362 163 %{$this->{special}} = %{$proto->{special}};
363 163 ▶ $this->{name} = defined $name ? $name : $proto->{name};
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 163 163
364 163 $this->{rows} = $proto->{rows};
365 163 $this->{columns} = $proto->{columns};
366 163 return $this;
367 }
368  
369 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
370 0 my ($object,$name) = @_;
371 0 $object->{name} = $name;
372 0 return $name;
373 }
374  
375 ### INSERTION AND LOOKUP METHODS
376 16330 ▶sub assign {
Subroutine assign: called CC=40 SCAR=52.5
Pod undocumented
377 16330 my ($object, $i,$j,$x)=@_;
378 16330 ▶ return undef unless ((defined $i) && (defined $j)&&(defined $object));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i and defined $j and defined $object 0 16330 16330
Condition33%2/6
defined $i and defined $j and defined $object
!ll&&!rl&&r
0016330
defined $i and defined $j
!ll&&!rl&&r
0016330
MC/DC0%0/3
defined $i and defined $j and defined $object
defined $i defined $j defined $object
Truth table25%1/4
A and B and C
A= defined $iB= defined $jC= defined $object
ABCresult
0XX0
10X0
1100
1111
379 16330 ▶ $x = 1 if $object->{special}->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $object->{"special"}{"pattern"} 252 16078 16330
380 16330 ▶ return undef unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 16330 16330
381 #update matrix's shape if necessary.
382 16330 ▶ if ((defined $object->{special}) &&
Branch: 5 branches
BranchTrueFalseTotal
if (defined $object->{'special'} and $object->{'special'}{'shape'} =~ /diagonal/i and $i != $j) { } 0 16330 16330
elsif ($object->{'special'}{'shape'} =~ /strictlower/i and $i <= $j) { } 0 16330 16330
elsif ($object->{'special'}{'shape'} =~ /strictupper/i and $i >= $j) { } 1 16329 16330
elsif ($object->{'special'}{'shape'} =~ /^lower/i and $i < $j) { } 0 16329 16329
elsif ($object->{'special'}{'shape'} =~ /^upper/i and $i = $j) { } 1 32656 32657
Condition33%2/6
defined $object->{"special"} and $object->{"special"}{"shape"} =~ /diagonal/i and $i != $j
!ll&&!rl&&r
1633000
defined $object->{"special"} and $object->{"special"}{"shape"} =~ /diagonal/i
!ll&&!rl&&r
0163300
MC/DC0%0/3
defined $object->{"special"} and $object->{"special"}{"shape"} =~ /diagonal/i and $i != $j
defined $object->{"special"} $object->{"special"}{"shape"} =~ /diagonal/i $i != $j
Truth table25%1/4
A and B and C
A= defined $object->{"special"}B= $object->{"special"}{"shape"} =~ /diagonal/iC= $i != $j
ABCresult
0XX0
10X0
1100
1111
383 ($object->{special}->{shape} =~ m/diagonal/i) &&
384 ($i!= $j) ) {
385 0 ▶ if ($i<$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $j) { } 0 0 0
386 0 $object->{special}->{shape} = "upper"
387 } else {
388 0 $object->{special}->{shape} = "lower"
389 }
390 ▶ } elsif (($object->{special}->{shape} =~ m/strictlower/i)
Condition33%1/3
$object->{"special"}{"shape"} =~ /strictlower/i and $i <= $j
!ll&&!rl&&r
1633000
MC/DC0%0/2
$object->{"special"}{"shape"} =~ /strictlower/i and $i <= $j
$object->{"special"}{"shape"} =~ /strictlower/i $i <= $j
Truth table33%1/3
A and B
A= $object->{"special"}{"shape"} =~ /strictlower/iB= $i <= $j
ABresult
0X0
100
111
391 && ($i<= $j) ) {
392 0 ▶ if ($i==$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $j) { } 0 0 0
393 0 $object->{special}->{shape} = "lower";
394 } else {
395 0 $object->{special}->{shape}="";
396 }
397 ▶ } elsif (($object->{special}->{shape} =~ m/strictupper/i)
Condition66%2/3
$object->{"special"}{"shape"} =~ /strictupper/i and $i >= $j
!ll&&!rl&&r
1632901
MC/DC50%1/2
$object->{"special"}{"shape"} =~ /strictupper/i and $i >= $j
$object->{"special"}{"shape"} =~ /strictupper/i $i >= $j
Truth table66%2/3
A and B
A= $object->{"special"}{"shape"} =~ /strictupper/iB= $i >= $j
ABresult
0X0
100
111
398 && ($i>= $j) ) {
399 1 ▶ if ($i==$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $j) { } 0 1 1
400 0 $object->{special}->{shape} = "upper";
401 } else {
402 1 $object->{special}->{shape}="";
403 }
404 ▶ } elsif (($object->{special}->{shape} =~ m/^lower/i) && ($i< $j) ) {
Condition33%1/3
$object->{"special"}{"shape"} =~ /^lower/i and $i < $j
!ll&&!rl&&r
1632900
MC/DC0%0/2
$object->{"special"}{"shape"} =~ /^lower/i and $i < $j
$object->{"special"}{"shape"} =~ /^lower/i $i < $j
Truth table33%1/3
A and B
A= $object->{"special"}{"shape"} =~ /^lower/iB= $i < $j
ABresult
0X0
100
111
405 0 $object->{special}->{shape}="";
406 ▶ } elsif (($object->{special}->{shape} =~ m/^upper/i) && ($i= $j) ) {
Condition66%2/3
$object->{"special"}{"shape"} =~ /^upper/i and $i = $j
!ll&&!rl&&r
1632801
MC/DC50%1/2
$object->{"special"}{"shape"} =~ /^upper/i and $i = $j
$object->{"special"}{"shape"} =~ /^upper/i $i = $j
Truth table66%2/3
A and B
A= $object->{"special"}{"shape"} =~ /^upper/iB= $i = $j
ABresult
0X0
100
111
407 1 $object->{special}->{shape}="";
408 }
409 16330 ▶ $object->{special}->{pattern} = 0 unless ($x==1);
Branch: 1 branch
BranchTrueFalseTotal
unless $x == 1 15387 943 16330
410 #update bandwidth
411 16330 ▶ if (abs($i-$j) > $object->{special}->{bandwidth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $i - $j > $object->{"special"}{"bandwidth"}) 208 16122 16330
412 208 $object->{special}->{bandwidth} = abs($i-$j);
413 }
414 #update symmetric and skew-symmetric structure if necessary
415 16330 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($i != $j and defined $object->{'special'}{'structure'} and $object->{'special'}{'structure'} =~ /symmetric/i) { } 0 16330 16330
elsif ($i == $j and $object->{'special'}{'structure'} =~ /skewsymmetric/i and $x) { } 0 32660 32660
Condition66%4/6
$i != $j and defined $object->{"special"}{"structure"} and $object->{"special"}{"structure"} =~ /symmetric/i
!ll&&!rl&&r
1108152220
$i != $j and defined $object->{"special"}{"structure"}
!ll&&!rl&&r
1108015222
MC/DC0%0/3
$i != $j and defined $object->{"special"}{"structure"} and $object->{"special"}{"structure"} =~ /symmetric/i
$i != $j defined $object->{"special"}{"structure"} $object->{"special"}{"structure"} =~ /symmetric/i
Truth table50%2/4
A and B and C
A= $i != $jB= defined $object->{"special"}{"structure"}C= $object->{"special"}{"structure"} =~ /symmetric/i
ABCresult
0XX0
10X0
1100
1111
416 ($i!=$j) && ( defined $object->{special}->{structure}) &&
417 (
418 ($object->{special}->{structure} =~ /symmetric/i)
419 # ||($object->{special}->{structure} =~ /hermetian/i)
420 )
421 ) {
422 0 $object->{special}->{structure} = "";
423 ▶ } elsif (($i==$j)&&
Condition50%3/6
$i == $j and $object->{"special"}{"structure"} =~ /skewsymmetric/i and $x
!ll&&!rl&&r
1633000
$i == $j and $object->{"special"}{"structure"} =~ /skewsymmetric/i
!ll&&!rl&&r
1522211080
MC/DC0%0/3
$i == $j and $object->{"special"}{"structure"} =~ /skewsymmetric/i and $x
$i == $j $object->{"special"}{"structure"} =~ /skewsymmetric/i $x
Truth table50%2/4
A and B and C
A= $i == $jB= $object->{"special"}{"structure"} =~ /skewsymmetric/iC= $x
ABCresult
0XX0
10X0
1100
1111
424 ($object->{special}->{structure} =~ /skewsymmetric/i)&&
425 ($x)) {
426 #skew-symmetric matrices must have zero diagonal
427 0 $object->{special}->{structure} = "";
428 }
429 #update sign if necessary
430 16330 ▶ if ( ($object->{special}->{sign} =~ /^positive/) && ($x<=0) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($object->{'special'}{'sign'} =~ /^positive/ and $x <= 0) { } 0 16330 16330
elsif ($object->{'special'}{'sign'} =~ /^negative/ and $x >= 0) { } 0 16330 16330
elsif ($object->{'special'}{'sign'} =~ /nonnegative/i and $x < 0) { } 2 16328 16330
elsif ($object->{'special'}{'sign'} =~ /nonpositive/i and $x > 0) { } 0 32656 32656
Condition33%1/3
$object->{"special"}{"sign"} =~ /^positive/ and $x <= 0
!ll&&!rl&&r
1633000
MC/DC0%0/2
$object->{"special"}{"sign"} =~ /^positive/ and $x <= 0
$object->{"special"}{"sign"} =~ /^positive/ $x <= 0
Truth table33%1/3
A and B
A= $object->{"special"}{"sign"} =~ /^positive/B= $x <= 0
ABresult
0X0
100
111
431 0 ▶ if ($x<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) { } 0 0 0
432 0 $object->{special}->{sign}="";
433 } else {
434 0 $object->{special}->{sign} = "nonnegative";
435 }
436 ▶ } elsif ( ($object->{special}->{sign} =~ /^negative/) && ($x>=0) ) {
Condition33%1/3
$object->{"special"}{"sign"} =~ /^negative/ and $x >= 0
!ll&&!rl&&r
1633000
MC/DC0%0/2
$object->{"special"}{"sign"} =~ /^negative/ and $x >= 0
$object->{"special"}{"sign"} =~ /^negative/ $x >= 0
Truth table33%1/3
A and B
A= $object->{"special"}{"sign"} =~ /^negative/B= $x >= 0
ABresult
0X0
100
111
437 0 ▶ if ($x>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) { } 0 0 0
438 0 $object->{special}->{sign}="";
439 } else {
440 0 $object->{special}->{sign} = "nonpositive";
441 }
442 ▶ } elsif ( ($object->{special}->{sign} =~ /nonnegative/i) && ($x<0) ) {
Condition100%3/3
$object->{"special"}{"sign"} =~ /nonnegative/i and $x < 0
!ll&&!rl&&r
1505012782
MC/DC100%2/2
$object->{"special"}{"sign"} =~ /nonnegative/i and $x < 0
$object->{"special"}{"sign"} =~ /nonnegative/i $x < 0
Truth table100%3/3
A and B
A= $object->{"special"}{"sign"} =~ /nonnegative/iB= $x < 0
ABresult
0X0
100
111
443 2 $object->{special}->{sign}="";
444 ▶ } elsif ( ($object->{special}->{sign} =~ /nonpositive/i) && ($x>0) ) {
Condition33%1/3
$object->{"special"}{"sign"} =~ /nonpositive/i and $x > 0
!ll&&!rl&&r
1632800
MC/DC0%0/2
$object->{"special"}{"sign"} =~ /nonpositive/i and $x > 0
$object->{"special"}{"sign"} =~ /nonpositive/i $x > 0
Truth table33%1/3
A and B
A= $object->{"special"}{"sign"} =~ /nonpositive/iB= $x > 0
ABresult
0X0
100
111
445 0 $object->{special}->{sign}="";
446 }
447 16330 my $key = &makekey($i,$j);
448 16330 delete $object->{sortedrows};
449 16330 delete $object->{sortedcolumns};
450 16330 delete $object->{data}->{$key};
451 16330 $object->{data}->{$key} = $x;
452 #update size of matrix, and squareness, if necessary
453 16330 ▶ $object->{rows} = $i if ($i>$object->{rows});
Branch: 1 branch
BranchTrueFalseTotal
if $i > $object->{"rows"} 514 15816 16330
454 16330 ▶ $object->{columns} = $j if ($j>$object->{columns});
Branch: 1 branch
BranchTrueFalseTotal
if $j > $object->{"columns"} 477 15853 16330
455 16330 $object->{special}->{square} = ( $object->{columns}==$object->{rows});
456 16330 return $x;
457 }
458  
459 0 ▶sub assignspecial {
Subroutine assignspecial: not called CC=11 SCAR=48.8
Pod undocumented
460 #as assign, except that it respects special properties of
461 #the matrix. For example, symmetrical matrices are kept symmetric.
462 0 my ($object, $i,$j,$x)=@_;
463 0 ▶ return undef unless ((defined $i) && (defined $j)&&(defined $object));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i and defined $j and defined $object 0 0 0
Condition0%0/6
defined $i and defined $j and defined $object
!ll&&!rl&&r
000
defined $i and defined $j
!ll&&!rl&&r
000
MC/DC0%0/3
defined $i and defined $j and defined $object
defined $i defined $j defined $object
Truth table0%0/4
A and B and C
A= defined $iB= defined $jC= defined $object
ABCresult
0XX0
10X0
1100
1111
464 0 ▶ $x = 1 if $object->{special}->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $object->{"special"}{"pattern"} 0 0 0
465 0 ▶ return undef unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 0 0
466 0 my $key = &makekey($i,$j);
467 0 my $revkey = &makekey($j,$i);
468 0 ▶ if ($object->{special}->{structure} =~ m/^symmetric/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->{'special'}{'structure'} =~ /^symmetric/i) { } 0 0 0
elsif ($object->{'special'}{'structure'} =~ /^symmetric/i) { } 0 0 0
469 0 ▶ if ($i==$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $j) { } 0 0 0
470 0 $object->{data}{$key} = $x;
471 } else {
472 0 $object->{data}{$key} = $x;
473 0 $object->{data}{$revkey} = $x;
474 }
475 } elsif ($object->{special}->{structure} =~ m/^symmetric/i) {
476 0 ▶ if (($i==$j)&&($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $j and $x) { } 0 0 0
Condition0%0/3
$i == $j and $x
!ll&&!rl&&r
000
MC/DC0%0/2
$i == $j and $x
$i == $j $x
Truth table0%0/3
A and B
A= $i == $jB= $x
ABresult
0X0
100
111
477 0 croak "Math::MatrixSparse::assignspecial skewsymmetric matrices must have zero diagonal";
478 } else {
479 0 $object->{data}{$key} = $x;
480 0 $object->{data}{$revkey} = -$x;
481 }
482 } else {
483 0 $object->assign($i,$j,$x);
484 }
485 0 return $x;
486 }
487  
488 2008 ▶sub assignkey {
Subroutine assignkey: called CC=3 SCAR=11.8
Pod undocumented
489 2008 my ($object, $key,$x)=@_;
490 2008 my ($i,$j) = &splitkey($key);
491 2008 ▶ return unless ((defined $i) && (defined $j));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i and defined $j 0 2008 2008
Condition33%1/3
defined $i and defined $j
!ll&&!rl&&r
002008
MC/DC0%0/2
defined $i and defined $j
defined $i defined $j
Truth table33%1/3
A and B
A= defined $iB= defined $j
ABresult
0X0
100
111
492 2008 $object->assign($i,$j,$x);
493 2008 return $x;
494 }
495  
496 60055 ▶sub element {
Subroutine element: called CC=2 SCAR=6.9
Pod undocumented
497 60055 my ($object, $i,$j) = @_;
498 60055 my $key = &makekey($i,$j);
499 60055 ▶ if (defined $object->{data}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $object->{'data'}{$key}) { } 57579 2476 60055
500 57579 return $object->{data}{$key};
501 } else {
502 2476 return 0;
503 }
504 }
505  
506 28823 ▶sub elementkey {
Subroutine elementkey: called CC=2 SCAR=6.9
Pod undocumented
507 28823 my ($object, $key) = @_;
508 28823 ▶ if (defined $object->{data}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $object->{'data'}{$key}) { } 28323 500 28823
509 28323 return $object->{data}{$key};
510 } else {
511 500 return 0;
512 }
513 }
514  
515 0 ▶sub elements {
Subroutine elements: not called CC=1 SCAR=6.9
Pod undocumented
516 0 my ($matrix) = @_;
517 0 return keys %{$matrix->{data}};
518 }
519  
520  
521 #returns row number $row of matrix as a row matrix
522 0 ▶sub row {
Subroutine row: not called CC=15 SCAR=54.8
Pod undocumented
523 0 my ($matrix, $row,$persist) = @_;
524 0 my $matrow = new Math::MatrixSparse;
525 0 $matrow->{columns} = $matrix->{columns};
526 0 $matrow->{rows} = 1;
527 0 $matrow->name($matrix->{name});
528 0 ▶ reuturn $matrow unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
529 0 ▶ if ($persist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($persist) 0 0 0
530 0 @{$matrix->{sortedrows}} = $matrix->sortbyrow()
531 0 ▶ unless defined $matrix->{sortedrows};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix->{"sortedrows"} 0 0 0
532 }
533 0 ▶ if (defined $matrix->{sortedrows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix->{"sortedrows"}) 0 0 0
534 #binary search for proper values
535 0 my @rows = @{$matrix->{sortedrows}};
536 0 my ($left,$right) = (0,$#rows);
537 0 my $mid = int(($right+$left)/2.0);
538 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ((&splitkey($rows[$mid]))[0] != $row and $right - $left > 0) 0 0 0
Condition0%0/3
(&splitkey($rows[$mid]))[0] != $row and $right - $left > 0
!ll&&!rl&&r
000
MC/DC0%0/2
(&splitkey($rows[$mid]))[0] != $row and $right - $left > 0
(&splitkey($rows[$mid]))[0] != $row $right - $left > 0
Truth table0%0/3
A and B
A= (&splitkey($rows[$mid]))[0] != $rowB= $right - $left > 0
ABresult
0X0
100
111
539 ((&splitkey($rows[$mid]))[0] != $row )
540 && ($right-$left>0)
541 ) {
542 0 ▶ if ((&splitkey($rows[$mid]))[0] < $row) {
Branch: 1 branch
BranchTrueFalseTotal
if ((&splitkey($rows[$mid]))[0] < $row) { } 0 0 0
543 0 $left = $mid;
544 } else {
545 0 $right = $mid;
546 }
547 0 $mid = int(($right+$left)/2.0);
548 }
549 0 ▶ return $matrow unless (&splitkey($rows[$mid]))[0]==$row;
Branch: 1 branch
BranchTrueFalseTotal
unless (&splitkey($rows[$mid]))[0] == $row 0 0 0
550 0 $right = $mid;
551 0 ▶ while ( ($right<=$#rows)&&
Branch: 1 branch
BranchTrueFalseTotal
if ($right <= $#rows and (&splitkey($rows[$right]))[0] == $row) 0 0 0
Condition0%0/3
$right <= $#rows and (&splitkey($rows[$right]))[0] == $row
!ll&&!rl&&r
000
MC/DC0%0/2
$right <= $#rows and (&splitkey($rows[$right]))[0] == $row
$right <= $#rows (&splitkey($rows[$right]))[0] == $row
Truth table0%0/3
A and B
A= $right <= $#rowsB= (&splitkey($rows[$right]))[0] == $row
ABresult
0X0
100
111
552 ((&splitkey($rows[$right]))[0]==$row)
553 )
554 {
555 0 $matrow->assign(1,
556 (&splitkey($rows[$right]))[1],
557 $matrix->elementkey($rows[$right]));
558 0 $right++;
559 }
560 0 $left = $mid-1;
561 0 ▶ while ( ($left) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($left and (&splitkey($rows[$left]))[0] == $row) 0 0 0
Condition0%0/3
$left and (&splitkey($rows[$left]))[0] == $row
!ll&&!rl&&r
000
MC/DC0%0/2
$left and (&splitkey($rows[$left]))[0] == $row
$left (&splitkey($rows[$left]))[0] == $row
Truth table0%0/3
A and B
A= $leftB= (&splitkey($rows[$left]))[0] == $row
ABresult
0X0
100
111
562 ((&splitkey($rows[$left]))[0]==$row)
563 ){
564 0 $matrow->assign(1,
565 (&splitkey($rows[$left]))[1],
566 $matrix->elementkey($rows[$left]));
567 0 $left--;
568 }
569 0 return $matrow;
570 }
571 0 foreach my $key (keys %{$matrix->{data}}){
572 0 my ($i,$j) = &splitkey($key);
573 0 ▶ $matrow->assignkey($key, $matrix->{data}{$key}) if ($i==$row);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $row 0 0 0
574 }
575 0 return $matrow;
576 }
577  
578 #returns column number $col of matrix as a column matrix
579 0 ▶sub column {
Subroutine column: not called CC=14 SCAR=53.5
Pod undocumented
580 0 my ($matrix, $col,$persist) = @_;
581 0 my $matcol = new Math::MatrixSparse;
582 0 $matcol->{columns} = $matrix->{columns};
583 0 $matcol->{cols} = 1;
584 0 $matcol->name($matrix->{name});
585 0 ▶ if ($persist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($persist) 0 0 0
586 0 ▶ @{$matrix->{sortedcols}} = $matrix->sortbycolumn() unless defined $matrix->{sortedcols};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix->{"sortedcols"} 0 0 0
587 }
588 0 ▶ if (defined $matrix->{sortedcols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix->{"sortedcols"}) 0 0 0
589 #binary search for proper values
590 0 my @cols = @{$matrix->{sortedcols}};
591 0 my ($left,$right) = (0,$#cols);
592 0 my $mid = int(($right+$left)/2.0);
593 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ((&splitkey($cols[$mid]))[1] != $col and $right - $left > 0) 0 0 0
Condition0%0/3
(&splitkey($cols[$mid]))[1] != $col and $right - $left > 0
!ll&&!rl&&r
000
MC/DC0%0/2
(&splitkey($cols[$mid]))[1] != $col and $right - $left > 0
(&splitkey($cols[$mid]))[1] != $col $right - $left > 0
Truth table0%0/3
A and B
A= (&splitkey($cols[$mid]))[1] != $colB= $right - $left > 0
ABresult
0X0
100
111
594 ((&splitkey($cols[$mid]))[1] != $col )
595 && ($right-$left>0)
596 ) {
597 0 ▶ if ((&splitkey($cols[$mid]))[1] < $col) {
Branch: 1 branch
BranchTrueFalseTotal
if ((&splitkey($cols[$mid]))[1] < $col) { } 0 0 0
598 0 $left = $mid;
599 } else {
600 0 $right = $mid;
601 }
602 0 $mid = int(($right+$left)/2.0);
603 }
604 0 ▶ return $matcol unless (&splitkey($cols[$mid]))[1]==$col;
Branch: 1 branch
BranchTrueFalseTotal
unless (&splitkey($cols[$mid]))[1] == $col 0 0 0
605 0 $right = $mid;
606 0 ▶ while ( ($right<=$#cols)&&
Branch: 1 branch
BranchTrueFalseTotal
if ($right <= $#cols and (&splitkey($cols[$right]))[1] == $col) 0 0 0
Condition0%0/3
$right <= $#cols and (&splitkey($cols[$right]))[1] == $col
!ll&&!rl&&r
000
MC/DC0%0/2
$right <= $#cols and (&splitkey($cols[$right]))[1] == $col
$right <= $#cols (&splitkey($cols[$right]))[1] == $col
Truth table0%0/3
A and B
A= $right <= $#colsB= (&splitkey($cols[$right]))[1] == $col
ABresult
0X0
100
111
607 ((&splitkey($cols[$right]))[1]==$col)
608 )
609 {
610 0 $matcol->assign((&splitkey($cols[$right]))[0],1,
611 $matrix->elementkey($cols[$right]));
612 0 $right++;
613 }
614 0 $left = $mid-1;
615 0 ▶ while ( ($left) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($left and (&splitkey($cols[$left]))[1] == $col) 0 0 0
Condition0%0/3
$left and (&splitkey($cols[$left]))[1] == $col
!ll&&!rl&&r
000
MC/DC0%0/2
$left and (&splitkey($cols[$left]))[1] == $col
$left (&splitkey($cols[$left]))[1] == $col
Truth table0%0/3
A and B
A= $leftB= (&splitkey($cols[$left]))[1] == $col
ABresult
0X0
100
111
616 ((&splitkey($cols[$left]))[1]==$col)
617 ){
618 0 $matcol->assign((&splitkey($cols[$left]))[0],1,
619 $matrix->elementkey($cols[$left]));
620 0 $left--;
621 }
622 0 return $matcol;
623 }
624 0 foreach my $key (keys %{$matrix->{data}}){
625 0 my ($i,$j) = &splitkey($key);
626 0 ▶ $matcol->assignkey($key, $matrix->{data}{$key}) if ($j==$col);
Branch: 1 branch
BranchTrueFalseTotal
if $j == $col 0 0 0
627 }
628 0 return $matcol;
629 }
630  
631  
632 ### PRINT
633  
634 0 ▶sub print {
Subroutine print: not called CC=7 SCAR=40.3
Pod undocumented
635 0 my ($object,$name,$rc) = @_;
636 0 ▶ return unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
637 0 ▶ my $label = $name ? $name : $object->{name};
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
638 0 my @order ;
639 0 ▶ $rc = "n" unless defined($rc);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 0 0
640 0 ▶ if ($rc =~ /^r/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rc =~ /^r/i) { } 0 0 0
elsif ($rc =~ /^c/i) { } 0 0 0
641 0 @order = $object->sortbyrow();
642 } elsif ($rc =~ /^c/i) {
643 0 @order = $object->sortbycolumn();
644
645 } else {
646 0 @order = keys %{$object->{data}};
647 }
648 0 foreach my $key (@order){
649 0 my ($i,$j) = &splitkey($key);
650 0 print "$label($i, $j) = ", $object->{data}{$key},"\n";
651 }
652 0 return "";
653 }
654  
655  
656 ###ARITHMETIC METHODS
657  
658 #left+$right, dimensions must be identical
659 7 ▶sub add {
Subroutine add: called CC=3 SCAR=12.0
Pod undocumented
660 7 my ($left,$right,$switch) = @_;
661 7 ▶ if (($left->{rows} == $right->{rows})&&($left->{columns} == $right->{columns})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{'rows'} == $right->{'rows'} and $left->{'columns'} == $right->{'columns'}) { } 7 0 7
Condition33%1/3
$left->{"rows"} == $right->{"rows"} and $left->{"columns"} == $right->{"columns"}
!ll&&!rl&&r
007
MC/DC0%0/2
$left->{"rows"} == $right->{"rows"} and $left->{"columns"} == $right->{"columns"}
$left->{"rows"} == $right->{"rows"} $left->{"columns"} == $right->{"columns"}
Truth table33%1/3
A and B
A= $left->{"rows"} == $right->{"rows"}B= $left->{"columns"} == $right->{"columns"}
ABresult
0X0
100
111
662
663 7 my $sum= $left->addfree($right);
664  
665 7 $sum->{rows} = $left->{rows};
666 7 $sum->{columns} = $left->{columns};
667 7 return $sum;
668 } else {
669 0 return undef;
670 }
671 }
672  
673 #as add, but no restrictions on dimensions.
674 7 ▶sub addfree {
Subroutine addfree: called CC=9 SCAR=22.5
Pod undocumented
675 7 my ($left, $right,$switch) = @_;
676 7 my $sum = new Math::MatrixSparse;
677 7 $sum = $left->copy();
678 7 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 7 7
Condition33%1/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
700
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table33%1/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
679 0 $sum->{name} = $left->{name} . "+" . $right->{name};
680 }
681 7 foreach my $rightkey (keys %{$right->{data}}) {
682 2557 ▶ if (defined $sum->{data}{$rightkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sum->{'data'}{$rightkey}) { } 1008 1549 2557
683 1008 $sum->{data}{$rightkey}+= $right->{data}{$rightkey};
684 } else {
685 1549 $sum->{data}{$rightkey} = $right->{data}{$rightkey};
686 }
687 }
688 $sum->{rows} = $left->{rows} >$right->{rows} ?
689 7 ▶ $left->{rows} : $right->{rows};
Branch: 1 branch
BranchTrueFalseTotal
$left->{'rows'} > $right->{'rows'} ? : 0 7 7
690 $sum->{columns} = $left->{columns} >$right->{columns} ?
691 7 ▶ $left->{columns} : $right->{columns};
Branch: 1 branch
BranchTrueFalseTotal
$left->{'columns'} > $right->{'columns'} ? : 0 7 7
692 7 ▶ if ($left->{special}->{structure} eq $right->{special}->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"structure"} eq $right->{"special"}{"structure"}) 6 1 7
693 6 $sum->{special}->{structure} = $left->{special}->{structure};
694 }
695 7 ▶ if ($left->{special}->{shape} eq $right->{special}->{shape}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"shape"} eq $right->{"special"}{"shape"}) 3 4 7
696 3 $sum->{special}->{shape} = $left->{special}->{shape};
697 }
698 7 return $sum;
699 }
700  
701  
702 77 ▶sub subtract {
Subroutine subtract: called CC=4 SCAR=15.8
Pod undocumented
703 77 my ($left, $right, $switch) = @_;
704 77 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) 0 77 77
705 0 ($left,$right) = ($right, $left);
706 }
707 77 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{'rows'} == $right->{'rows'} and $left->{'columns'} == $right->{'columns'}) { } 77 0 77
Condition33%1/3
$left->{"rows"} == $right->{"rows"} and $left->{"columns"} == $right->{"columns"}
!ll&&!rl&&r
0077
MC/DC0%0/2
$left->{"rows"} == $right->{"rows"} and $left->{"columns"} == $right->{"columns"}
$left->{"rows"} == $right->{"rows"} $left->{"columns"} == $right->{"columns"}
Truth table33%1/3
A and B
A= $left->{"rows"} == $right->{"rows"}B= $left->{"columns"} == $right->{"columns"}
ABresult
0X0
100
111
708 ($left->{rows} == $right->{rows})&&
709 ($left->{columns} == $right->{columns})
710 ) {
711 77 my $diff= $left->subtractfree($right);
712 77 $diff->{rows} = $left->{rows};
713 77 $diff->{columns} = $left->{columns};
714 77 return $diff;
715 } else {
716 0 return undef;
717 }
718
719 }
720  
721 #as subtract, but no restrictions on dimensions.
722 77 ▶sub subtractfree {
Subroutine subtractfree: called CC=9 SCAR=23.3
Pod undocumented
723 77 my ($left, $right,$switch) = @_;
724 77 my $diff = new Math::MatrixSparse;
725 77 $diff = $left->copy();
726 77 foreach my $rightkey (keys %{$right->{data}}) {
727 2656 ▶ if (defined $diff->{data}{$rightkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $diff->{'data'}{$rightkey}) { } 2084 572 2656
728 2084 $diff->{data}{$rightkey}-= $right->{data}{$rightkey};
729 } else {
730 572 $diff->{data}{$rightkey} = -1* $right->{data}{$rightkey};
731 }
732 }
733 77 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 77 77
Condition33%1/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
7700
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table33%1/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
734 0 $diff->{name} = $left->{name} . "-" . $right->{name};
735 }
736 $diff->{rows} = $left->{rows} >$right->{rows} ?
737 77 ▶ $left->{rows} : $right->{rows};
Branch: 1 branch
BranchTrueFalseTotal
$left->{'rows'} > $right->{'rows'} ? : 0 77 77
738 $diff->{columns} = $left->{columns} >$right->{columns} ?
739 77 ▶ $left->{columns} : $right->{columns};
Branch: 1 branch
BranchTrueFalseTotal
$left->{'columns'} > $right->{'columns'} ? : 0 77 77
740 77 ▶ if ($left->{special}->{structure} eq $right->{special}->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"structure"} eq $right->{"special"}{"structure"}) 77 0 77
741 77 $diff->{special}->{structure} = $left->{special}->{structure};
742 }
743 77 ▶ if ($left->{special}->{shape} eq $right->{special}->{shape}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"shape"} eq $right->{"special"}{"shape"}) 77 0 77
744 77 $diff->{special}->{shape} = $left->{special}->{shape};
745 }
746 77 return $diff;
747 }
748  
749 0 ▶sub negate {
Subroutine negate: not called CC=1 SCAR=6.9
Pod undocumented
750 0 my ($matrix) = @_;
751 0 return $matrix->multiplyscalar(-1);
752 }
753  
754 0 ▶sub _negate {
Subroutine _negate: not called CC=1 SCAR=6.9
755 0 my ($matrix) = @_;
756 0 return $matrix->_multiplyscalar(-1);
757 }
758  
759 3 ▶sub multiplyscalar {
Subroutine multiplyscalar: called CC=10 SCAR=32.2
Pod undocumented
760 3 my ($matrix, $scalar) = @_;
761 3 my $product = $matrix->copy();
762 3 ▶ $scalar = 0 unless $scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless $scalar 0 3 3
763 3 foreach my $key (keys %{$product->{data}}) {
764 2008 $product->assignkey($key,$matrix->elementkey($key)*$scalar);
765 }
766 3 ▶ $product->{name} = "$scalar*".$product->{name} if defined $product->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $product->{"name"} 0 3 3
767 3 ▶ if ($scalar <0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar < 0) { } 0 3 3
768 0 ▶ if ($matrix->{special}->{sign} =~ m/positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($matrix->{'special'}{'sign'} =~ /positive/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /negative/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
769 0 $product->{special}->{sign} = "negative";
770 } elsif ($matrix->{special}->{sign} =~ m/negative/i) {
771 0 $product->{special}->{sign} = "positive";
772 } elsif ($matrix->{special}->{sign} =~ m/nonpositive/i) {
773 0 $product->{special}->{sign} = "nonnegative";
774 } elsif ($matrix->{special}->{sign} =~ m/nonnegative/i) {
775 0 $product->{special}->{sign} = "nonpositive";
776 }
777 } else {
778 3 $product->{special}->{sign} =$matrix->{special}->{sign};
779 }
780 3 ▶ $product->{special}->{sign} = "zero" unless $scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless $scalar 0 3 3
781 3 return $product;
782 }
783  
784 0 ▶sub _multiplyscalar {
Subroutine _multiplyscalar: not called CC=9 SCAR=45.0
785 0 my ($matrix, $scalar) = @_;
786 0 ▶ $scalar = 0 unless $scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless $scalar 0 0 0
787 0 foreach my $key (keys %{$matrix->{data}}) {
788 0 $matrix->{data}{$key} = $matrix->{data}{$key}*$scalar;
789 }
790 0 ▶ if ($scalar <0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar < 0) 0 0 0
791 0 ▶ if ($matrix->{special}->{sign} =~ m/positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($matrix->{'special'}{'sign'} =~ /positive/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /negative/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($matrix->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
792 0 $matrix->{special}->{sign} = "negative";
793 } elsif ($matrix->{special}->{sign} =~ m/negative/i) {
794 0 $matrix->{special}->{sign} = "positive";
795 } elsif ($matrix->{special}->{sign} =~ m/nonpositive/i) {
796 0 $matrix->{special}->{sign} = "nonnegative";
797 } elsif ($matrix->{special}->{sign} =~ m/nonnegative/i) {
798 0 $matrix->{special}->{sign} = "nonpositive";
799 }
800 }
801 0 ▶ $matrix->{special}->{sign} = "zero" unless $scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless $scalar 0 0 0
802 0 return $matrix;
803 }
804  
805  
806 #finds $left*$right, if compatible
807 0 ▶sub multiply {
Subroutine multiply: not called CC=27 SCAR=66.3
Pod undocumented
808 0 my ($left,$right,$switch) = @_;
809 0 ▶ unless (ref($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $right) 0 0 0
810 0 return $left->multiplyscalar($right);
811 }
812 0 ▶ return undef if ($left->{columns} != $right->{rows});
Branch: 1 branch
BranchTrueFalseTotal
if $left->{"columns"} != $right->{"rows"} 0 0 0
813 0 my $product = new Math::MatrixSparse;
814 0 $product->{rows} = $left->{rows};
815 0 $product->{columns} = $right->{columns};
816 0 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 0 0
Condition0%0/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table0%0/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
817 0 $product->{name} = $left->{name} . "*" . $right->{name};
818 }
819 0 foreach my $leftkey (keys %{$left->{data}}) {
820 0 my ($li,$lj) = &splitkey($leftkey);
821 0 foreach my $rightkey (keys %{$right->{data}}) {
822 0 my ($ri,$rj) = &splitkey($rightkey);
823 0 ▶ next unless ($lj==$ri);
Branch: 1 branch
BranchTrueFalseTotal
unless $lj == $ri 0 0 0
824 0 my $thiskey = &makekey($li, $rj);
825 0 my $prod = $left->{data}{$leftkey}*$right->{data}{$rightkey};
826 0 ▶ if (defined $product->{data}{$thiskey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $product->{'data'}{$thiskey}) { } 0 0 0
827 0 $product->{data}{$thiskey} += $prod;
828 } else {
829 0 $product->{data}{$thiskey} = $prod;
830 }
831  
832 }
833
834 }
835 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i) 0 0 0
Condition0%0/3
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
$left->{"special"}{"sign"} =~ /zero/i $right->{"special"}{"sign"} =~ /zero/i
Truth table0%0/3
A or B
A= $left->{"special"}{"sign"} =~ /zero/iB= $right->{"special"}{"sign"} =~ /zero/i
ABresult
1X1
011
000
836 ($left->{special}->{sign} =~ /zero/i)||
837 ($right->{special}->{sign} =~ /zero/i)) {
838 0 $product->{special}->{sign} = "zero";
839 0 return $product;
840 }
841 0 ▶ if ($left->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($left->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
842 0 $product->{special}->{sign} = $right->{special}->{sign};
843 } elsif ($left->{special}->{sign} =~ /nonpositive/i) {
844 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
845 0 $product->{special}->{sign} = "nonpositive";
846 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
847 0 $product->{special}->{sign} = "nonnegative";
848 } elsif ($right->{special}->{sign} =~ /^negative/i) {
849 0 $product->{special}->{sign} = "nonnegative";
850 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
851 0 $product->{special}->{sign} = "nonpositive";
852 }
853 } elsif ($left->{special}->{sign} =~ /^negative/i) {
854 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
855 0 $product->{special}->{sign} = "negative";
856 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
857 0 $product->{special}->{sign} = "nonnegative";
858 } elsif ($right->{special}->{sign} =~ /^negative/i) {
859 0 $product->{special}->{sign} = "positive";
860 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
861 0 $product->{special}->{sign} = "nonpositive";
862 }
863 } elsif ($left->{special}->{sign} =~ /nonnegative/i) {
864 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
865 0 $product->{special}->{sign} = "nonnegative";
866 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
867 0 $product->{special}->{sign} = "nonpositive";
868 } elsif ($right->{special}->{sign} =~ /^negative/i) {
869 0 $product->{special}->{sign} = "nonpositive";
870 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
871 0 $product->{special}->{sign} = "nonnegative";
872 }
873 }
874 0 return $product;
875
876 }
877  
878  
879 46 ▶sub quickmultiply {
Subroutine quickmultiply: called CC=31 SCAR=44.7
Pod undocumented
880 46 my ($left,$right,$switch) = @_;
881 46 ▶ unless (ref($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $right) 3 43 46
882 3 return $left->multiplyscalar($right);
883 }
884 43 ▶ return undef if ($left->{columns} != $right->{rows});
Branch: 1 branch
BranchTrueFalseTotal
if $left->{"columns"} != $right->{"rows"} 0 43 43
885 43 my $product = new Math::MatrixSparse;
886 43 $product->{special}->{structure} = "";
887 43 $product->{special}->{pattern}=0;
888 43 $product->{special}->{shape} = "";
889 43 $product->{rows} = $left->{rows};
890 43 $product->{columns} = $right->{columns};
891 43 my @leftcols = $left->sortbycolumn();
892 43 my @rightrows = $right->sortbyrow();
893 43 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 2 41 43
Condition100%3/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
3832
MC/DC100%2/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table100%3/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
894 2 $product->{name} = $left->{name} . "*" . $right->{name};
895 }
896 43 my $lastrow = 0;
897 43 foreach my $leftkey (@leftcols) {
898 10491 my ($li,$lj) = &splitkey($leftkey);
899 10491 my $i = 0;
900 10491 my $thiskey;
901 10491 ▶ if ($lj >$lastrow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lj > $lastrow) 2282 8209 10491
902 2282 $lastrow = $lj;
903 #remove elements that won't be used again in multiplication
904 2282 ▶ while (defined ($thiskey = $rightrows[0])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($thiskey = $rightrows[0])) 11586 36 11622
905 11586 my ($ri,$rj) = &splitkey($thiskey);
906 11586 ▶ last if $ri>=$lj;
Branch: 1 branch
BranchTrueFalseTotal
if $ri >= $lj 2246 9340 11586
907 9340 shift @rightrows;
908 }
909 }
910 10491 foreach my $rightkey (@rightrows) {
911 256456 my ($ri,$rj) = &splitkey($rightkey);
912 256456 ▶ last if ($ri>$lj);
Branch: 1 branch
BranchTrueFalseTotal
if $ri > $lj 10274 246182 256456
913 246182 ▶ next if ($ri<$lj);
Branch: 1 branch
BranchTrueFalseTotal
if $ri < $lj 0 246182 246182
914 246182 my $thiskey = &makekey($li, $rj);
915 246182 my $prod = $left->{data}{$leftkey}*$right->{data}{$rightkey};
916 246182 ▶ if (defined $product->{data}{$thiskey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $product->{'data'}{$thiskey}) { } 222230 23952 246182
917 222230 $product->{data}{$thiskey} += $prod;
918 } else {
919 23952 $product->{data}{$thiskey} = $prod;
920 }
921  
922 }
923
924 }
925 43 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i) 0 43 43
Condition33%1/3
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
l!l&&r!l&&!r
0043
MC/DC0%0/2
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
$left->{"special"}{"sign"} =~ /zero/i $right->{"special"}{"sign"} =~ /zero/i
Truth table33%1/3
A or B
A= $left->{"special"}{"sign"} =~ /zero/iB= $right->{"special"}{"sign"} =~ /zero/i
ABresult
1X1
011
000
926 ($left->{special}->{sign} =~ /zero/i)||
927 ($right->{special}->{sign} =~ /zero/i)) {
928 0 $product->{special}->{sign} = "zero";
929 0 return $product;
930 }
931 43 ▶ if ($left->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($left->{'special'}{'sign'} =~ /^positive/i) { } 0 43 43
elsif ($left->{'special'}{'sign'} =~ /nonpositive/i) { } 0 43 43
elsif ($left->{'special'}{'sign'} =~ /^negative/i) { } 0 43 43
elsif ($left->{'special'}{'sign'} =~ /nonnegative/i) { } 15 28 43
932 0 $product->{special}->{sign} = $right->{special}->{sign};
933 } elsif ($left->{special}->{sign} =~ /nonpositive/i) {
934 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
935 0 $product->{special}->{sign} = "nonpositive";
936 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
937 0 $product->{special}->{sign} = "nonnegative";
938 } elsif ($right->{special}->{sign} =~ /^negative/i) {
939 0 $product->{special}->{sign} = "nonnegative";
940 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
941 0 $product->{special}->{sign} = "nonpositive";
942 }
943 } elsif ($left->{special}->{sign} =~ /^negative/i) {
944 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
945 0 $product->{special}->{sign} = "negative";
946 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
947 0 $product->{special}->{sign} = "nonnegative";
948 } elsif ($right->{special}->{sign} =~ /^negative/i) {
949 0 $product->{special}->{sign} = "positive";
950 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
951 0 $product->{special}->{sign} = "nonpositive";
952 }
953 } elsif ($left->{special}->{sign} =~ /nonnegative/i) {
954 15 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 15 15
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 15 15
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 15 15
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 15 0 15
955 0 $product->{special}->{sign} = "nonnegative";
956 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
957 0 $product->{special}->{sign} = "nonpositive";
958 } elsif ($right->{special}->{sign} =~ /^negative/i) {
959 0 $product->{special}->{sign} = "nonpositive";
960 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
961 15 $product->{special}->{sign} = "nonnegative";
962 }
963 }
964 43 return $product;
965
966 }
967  
968 0 ▶sub quickmultiplyfree {
Subroutine quickmultiplyfree: not called CC=30 SCAR=68.4
Pod undocumented
969 0 my ($left,$right,$switch) = @_;
970 0 ▶ unless (ref($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $right) 0 0 0
971 0 return $left->multiplyscalar($right);
972 }
973 0 my $product = new Math::MatrixSparse;
974 0 $product->{rows} = $left->{rows};
975 0 $product->{columns} = $right->{columns};
976 0 my @leftcols = $left->sortbycolumn();
977 0 my @rightrows = $right->sortbyrow();
978 0 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 0 0
Condition0%0/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table0%0/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
979 0 $product->{name} = $left->{name} . "*" . $right->{name};
980 }
981 0 my $lastrow = 0;
982 0 foreach my $leftkey (@leftcols) {
983 0 my ($li,$lj) = &splitkey($leftkey);
984 0 my $i = 0;
985 0 my $thiskey;
986 0 ▶ if ($lj >$lastrow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lj > $lastrow) 0 0 0
987 0 $lastrow = $lj;
988 #remove elements that won't be used again in multiplication
989 0 ▶ while (defined ($thiskey = $rightrows[0])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($thiskey = $rightrows[0])) 0 0 0
990 0 my ($ri,$rj) = &splitkey($thiskey);
991 0 ▶ last if $ri>=$lj;
Branch: 1 branch
BranchTrueFalseTotal
if $ri >= $lj 0 0 0
992 0 shift @rightrows;
993 }
994 }
995 0 foreach my $rightkey (@rightrows) {
996 0 my ($ri,$rj) = &splitkey($rightkey);
997 0 ▶ last if ($ri>$lj);
Branch: 1 branch
BranchTrueFalseTotal
if $ri > $lj 0 0 0
998 0 ▶ next if ($ri<$lj);
Branch: 1 branch
BranchTrueFalseTotal
if $ri < $lj 0 0 0
999 0 my $thiskey = &makekey($li , $rj);
1000 0 my $prod = $left->{data}{$leftkey}*$right->{data}{$rightkey};
1001 0 ▶ if (defined $product->{data}{$thiskey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $product->{'data'}{$thiskey}) { } 0 0 0
1002 0 $product->{data}{$thiskey} += $prod;
1003 } else {
1004 0 $product->{data}{$thiskey} = $prod;
1005 }
1006  
1007 }
1008
1009 }
1010 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i) 0 0 0
Condition0%0/3
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
$left->{"special"}{"sign"} =~ /zero/i $right->{"special"}{"sign"} =~ /zero/i
Truth table0%0/3
A or B
A= $left->{"special"}{"sign"} =~ /zero/iB= $right->{"special"}{"sign"} =~ /zero/i
ABresult
1X1
011
000
1011 ($left->{special}->{sign} =~ /zero/i)||
1012 ($right->{special}->{sign} =~ /zero/i)) {
1013 0 $product->{special}->{sign} = "zero";
1014 0 return $product;
1015 }
1016 0 ▶ if ($left->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($left->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1017 0 $product->{special}->{sign} = $right->{special}->{sign};
1018 } elsif ($left->{special}->{sign} =~ /nonpositive/i) {
1019 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1020 0 $product->{special}->{sign} = "nonpositive";
1021 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1022 0 $product->{special}->{sign} = "nonnegative";
1023 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1024 0 $product->{special}->{sign} = "nonnegative";
1025 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1026 0 $product->{special}->{sign} = "nonpositive";
1027 }
1028 } elsif ($left->{special}->{sign} =~ /^negative/i) {
1029 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1030 0 $product->{special}->{sign} = "negative";
1031 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1032 0 $product->{special}->{sign} = "nonnegative";
1033 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1034 0 $product->{special}->{sign} = "positive";
1035 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1036 0 $product->{special}->{sign} = "nonpositive";
1037 }
1038 } elsif ($left->{special}->{sign} =~ /nonnegative/i) {
1039 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1040 0 $product->{special}->{sign} = "nonnegative";
1041 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1042 0 $product->{special}->{sign} = "nonpositive";
1043 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1044 0 $product->{special}->{sign} = "nonpositive";
1045 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1046 0 $product->{special}->{sign} = "nonnegative";
1047 }
1048 }
1049 0 return $product;
1050
1051 }
1052  
1053  
1054 #as multiply, but no restrictions on dimensions.
1055 0 ▶sub multiplyfree {
Subroutine multiplyfree: not called CC=25 SCAR=64.8
Pod undocumented
1056 0 my ($left,$right,$switch) = @_;
1057 0 my $product = new Math::MatrixSparse;
1058 0 $product->{rows} = $left->{rows};
1059 0 $product->{columns} = $right->{columns};
1060 0 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 0 0
Condition0%0/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table0%0/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
1061 0 $product->{name} = $left->{name} . "*" . $right->{name};
1062 }
1063 0 foreach my $leftkey (keys %{$left->{data}}) {
1064 0 my ($li,$lj) = &splitkey($leftkey);
1065 0 foreach my $rightkey (keys %{$right->{data}}) {
1066 0 my ($ri,$rj) = &splitkey($rightkey);
1067 0 ▶ next unless ($lj==$ri);
Branch: 1 branch
BranchTrueFalseTotal
unless $lj == $ri 0 0 0
1068 0 my $thiskey = &makekey($li, $rj);
1069 0 my $prod = $left->{data}{$leftkey}*$right->{data}{$rightkey};
1070 0 ▶ if (defined $product->{data}{$thiskey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $product->{'data'}{$thiskey}) { } 0 0 0
1071 0 $product->{data}{$thiskey} += $prod;
1072 } else {
1073 0 $product->{data}{$thiskey} = $prod;
1074 }
1075  
1076 }
1077 }
1078 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i) 0 0 0
Condition0%0/3
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$left->{"special"}{"sign"} =~ /zero/i or $right->{"special"}{"sign"} =~ /zero/i
$left->{"special"}{"sign"} =~ /zero/i $right->{"special"}{"sign"} =~ /zero/i
Truth table0%0/3
A or B
A= $left->{"special"}{"sign"} =~ /zero/iB= $right->{"special"}{"sign"} =~ /zero/i
ABresult
1X1
011
000
1079 ($left->{special}->{sign} =~ /zero/i)||
1080 ($right->{special}->{sign} =~ /zero/i)) {
1081 0 $product->{special}->{sign} = "zero";
1082 0 return $product;
1083 }
1084 0 ▶ if ($left->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($left->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($left->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1085 0 $product->{special}->{sign} = $right->{special}->{sign};
1086 } elsif ($left->{special}->{sign} =~ /nonpositive/i) {
1087 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1088 0 $product->{special}->{sign} = "nonpositive";
1089 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1090 0 $product->{special}->{sign} = "nonnegative";
1091 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1092 0 $product->{special}->{sign} = "nonnegative";
1093 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1094 0 $product->{special}->{sign} = "nonpositive";
1095 }
1096 } elsif ($left->{special}->{sign} =~ /^negative/i) {
1097 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1098 0 $product->{special}->{sign} = "negative";
1099 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1100 0 $product->{special}->{sign} = "nonnegative";
1101 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1102 0 $product->{special}->{sign} = "positive";
1103 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1104 0 $product->{special}->{sign} = "nonpositive";
1105 }
1106 } elsif ($left->{special}->{sign} =~ /nonnegative/i) {
1107 0 ▶ if ($right->{special}->{sign} =~ /^positive/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($right->{'special'}{'sign'} =~ /^positive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonpositive/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /^negative/i) { } 0 0 0
elsif ($right->{'special'}{'sign'} =~ /nonnegative/i) { } 0 0 0
1108 0 $product->{special}->{sign} = "nonnegative";
1109 } elsif ($right->{special}->{sign} =~ /nonpositive/i) {
1110 0 $product->{special}->{sign} = "nonpositive";
1111 } elsif ($right->{special}->{sign} =~ /^negative/i) {
1112 0 $product->{special}->{sign} = "nonpositive";
1113 } elsif ($right->{special}->{sign} =~ /nonnegative/i) {
1114 0 $product->{special}->{sign} = "nonnegative";
1115 }
1116 }
1117 0 return $product;
1118
1119 }
1120  
1121 0 ▶sub kronecker {
Subroutine kronecker: not called CC=6 SCAR=37.4
Pod undocumented
1122 0 my ($left,$right);
1123 0 my ($rr,$rc) = $right->size();
1124 0 ▶ return undef unless ($rr&&$rc);
Branch: 1 branch
BranchTrueFalseTotal
unless $rr and $rc 0 0 0
Condition0%0/3
$rr and $rc
!ll&&!rl&&r
000
MC/DC0%0/2
$rr and $rc
$rr $rc
Truth table0%0/3
A and B
A= $rrB= $rc
ABresult
0X0
100
111
1125 0 my $kroprod = new Math::MatrixSparse;
1126 0 foreach my $key (keys %{$left->{data}}) {
1127 0 my ($i,$j) = &splitkey($key);
1128 0 $kroprod->_insert($i*$rr,$j*$rc,$right*$left->elementkey($key));
1129 }
1130 0 ▶ if ((defined $left->{name})&&(defined $right->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"name"} and defined $right->{"name"}) 0 0 0
Condition0%0/3
defined $left->{"name"} and defined $right->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left->{"name"} and defined $right->{"name"}
defined $left->{"name"} defined $right->{"name"}
Truth table0%0/3
A and B
A= defined $left->{"name"}B= defined $right->{"name"}
ABresult
0X0
100
111
1131 0 $kroprod->{name} = $left->{name} . "x" . $right->{name};
1132 }
1133 0 return $kroprod;
1134 }
1135  
1136 0 ▶sub termmult {
Subroutine termmult: not called CC=2 SCAR=17.9
Pod undocumented
1137 0 my ($left,$right) = @_;
1138 0 my $termprod = $left->copy();
1139 0 foreach my $key (keys %{$termprod->{data}}) {
1140 0 $termprod->assignkey($key,$termprod->elementkey($key)* $right->elementkey($key));
1141 }
1142 0 return $termprod;
1143
1144 }
1145  
1146 1 ▶sub exponentiate {
Subroutine exponentiate: called CC=7 SCAR=23.7
Pod undocumented
1147 1 my ($matrix,$power) = @_;
1148 1 ▶ unless ($matrix->is_square()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_square) 0 1 1
1149 0 carp "Math::MatrixSparse::exponentiate matrix must be square";
1150 0 return undef ;
1151 }
1152 1 ▶ return Math::MatrixSparse->newidentity($matrix->{rows}) unless $power;
Branch: 1 branch
BranchTrueFalseTotal
unless $power 0 1 1
1153 1 ▶ unless ($power>0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($power > 0) 0 1 1
1154 0 carp "Math::MatrixSparse::exponentiate exponent must be positive";
1155 0 return undef ;
1156 }
1157 1 ▶ unless ($power =~ /^[+]?\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($power =~ /^[+]?\d+/) 0 1 1
1158 0 carp "Math::MatrixSparse::exponentiate exponent must be an integer";
1159 0 return undef ;
1160 }
1161 1 my $product = $matrix->copy();
1162 # $product->clearspecials();
1163 1 for my $i (2..$power) {
1164 2 $product = $product->quickmultiply($matrix);
1165 }
1166 1 ▶ $product->{name} = $matrix->{name} ."^$power" if $matrix->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"name"} 0 1 1
1167 1 return $product;
1168 }
1169  
1170 2 ▶sub largeexponentiate {
Subroutine largeexponentiate: called CC=8 SCAR=23.2
Pod undocumented
1171 #find matrix^power using the square-and-multiply method
1172 2 my ($matrix,$power) = @_;
1173 2 ▶ unless ($matrix->is_square()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_square) 0 2 2
1174 0 carp "Math::MatrixSparse::exponentiate matrix must be square";
1175 0 return undef ;
1176 }
1177 2 ▶ return Math::MatrixSparse->newidentity($matrix->{rows}) unless $power;
Branch: 1 branch
BranchTrueFalseTotal
unless $power 0 2 2
1178 2 ▶ unless ($power>0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($power > 0) 0 2 2
1179 0 carp "Math::MatrixSparse::exponentiate exponent must be positive";
1180 0 return undef ;
1181 }
1182 2 ▶ unless ($power =~ /^[+]?\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($power =~ /^[+]?\d+/) 0 2 2
1183 0 carp "Math::MatrixSparse::exponentiate exponent must be an integer";
1184 0 return undef ;
1185 }
1186 #get a representation of $exponent in binary
1187 2 my $bitstr = unpack('B32',pack('N',$power));
1188 2 $bitstr =~s/^0*//;
1189 2 my @bitstr=split(//,$bitstr);
1190 2 my $z = Math::MatrixSparse->newidentity($matrix->{rows});
1191 2 foreach my $bit (@bitstr){
1192 6 $z = ($z*$z);
1193 6 ▶ if ($bit){
Branch: 1 branch
BranchTrueFalseTotal
if ($bit) 3 3 6
1194 3 $z = ($z*$matrix);
1195 }
1196 }
1197 2 $z->{name} = "";
1198 2 ▶ $z->{name} = $matrix->{name} . "^$power" if $matrix->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"name"} 0 2 2
1199 2 return $z;
1200 }
1201  
1202  
1203 7 ▶sub transpose {
Subroutine transpose: called CC=8 SCAR=21.1
Pod undocumented
1204 7 my ($matrix) = @_;
1205 7 my $this= new Math::MatrixSparse;
1206 7 $this->{rows} = $matrix->{columns};
1207 7 $this->{columns} = $matrix->{rows};
1208 7 foreach my $key (keys %{$matrix->{data}}) {
1209 2780 my ($i,$j) = &splitkey($key);
1210 2780 $this->assign($j,$i,$matrix->{data}{$key});
1211 }
1212 7 ▶ $this->{name} = $matrix->{name} . "'" if $matrix->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"name"} 0 7 7
1213 7 $this->{special}->{structure} = $matrix->{special}->{structure};
1214 7 $this->{special}->{sign} = $matrix->{special}->{sign};
1215 7 $this->{special}->{pattern} = $matrix->{special}->{pattern};
1216 7 $this->{special}->{square} = $matrix->{special}->{square};
1217 7 ▶ if ($matrix->{special}->{shape} =~ /diagonal/i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($matrix->{'special'}{'shape'} =~ /diagonal/i) { } 0 7 7
elsif ($matrix->{'special'}{'shape'} =~ /^lower/i) { } 0 7 7
elsif ($matrix->{'special'}{'shape'} =~ /^upper/i) { } 1 6 7
elsif ($matrix->{'special'}{'shape'} =~ /strictupper/i) { } 1 5 6
elsif ($matrix->{'special'}{'shape'} =~ /strictlower/i) { } 1 4 5
1218 0 $this->{special}->{shape} = "diagonal";
1219 } elsif ($matrix->{special}->{shape} =~ /^lower/i) {
1220 0 $this->{special}->{shape} = "upper";
1221 } elsif ($matrix->{special}->{shape} =~ /^upper/i) {
1222 1 $this->{special}->{shape} = "lower";
1223 } elsif ($matrix->{special}->{shape} =~ /strictupper/i) {
1224 1 $this->{special}->{shape} = "strictlower";
1225 } elsif ($matrix->{special}->{shape} =~ /strictlower/i) {
1226 1 $this->{special}->{shape} = "strictlower";
1227 } else {
1228 4 $this->{special}->{shape} = "";
1229 }
1230 7 return $this;
1231 }
1232  
1233  
1234 3 ▶sub terminvert {
Subroutine terminvert: called CC=3 SCAR=11.0
Pod undocumented
1235 3 my ($matrix) = @_;
1236 3 my $this = $matrix->copy();
1237 3 foreach my $key (keys %{$this->{data}}) {
1238 96 ▶ next unless $this->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"data"}{$key} 0 96 96
1239 96 $this->{data}{$key} = 1.0/($this->{data}{$key});
1240 }
1241 3 return $this;
1242 }
1243  
1244 0 ▶sub _terminvert {
Subroutine _terminvert: not called CC=3 SCAR=24.8
1245 0 my ($matrix) = @_;
1246 0 foreach my $key (keys %{$matrix->{data}}) {
1247 0 ▶ next unless $matrix->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->{"data"}{$key} 0 0 0
1248 0 $matrix->{data}{$key} = 1.0/($matrix->{data}{$key});
1249 }
1250 0 return $matrix;
1251 }
1252  
1253  
1254  
1255 ### DISSECTION METHODS
1256 7 ▶sub diagpart {
Subroutine diagpart: called CC=4 SCAR=13.9
Pod undocumented
1257 7 my ($matrix,$offset) = @_;
1258 7 my $diag = new Math::MatrixSparse;
1259 7 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 7 0 7
1260 7 foreach my $key (keys %{$matrix->{data}}){
1261 1846 my ($i,$j) = &splitkey($key);
1262 1846 ▶ next unless ($i == ($j+$offset));
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $j + $offset 1647 199 1846
1263 199 $diag->assign($i,$j,$matrix->{data}{$key});
1264 }
1265 7 $diag->{rows} = $matrix->{rows};
1266 7 $diag->{columns} = $matrix->{columns};
1267 7 $diag->{special}->{shape} = "diagonal";
1268 7 return $diag;
1269 }
1270  
1271 1 ▶sub nondiagpart {
Subroutine nondiagpart: called CC=4 SCAR=13.9
Pod undocumented
1272 1 my ($matrix,$offset) = @_;
1273 1 my $diag = new Math::MatrixSparse;
1274 1 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 0 1
1275 1 foreach my $key (keys %{$matrix->{data}}){
1276 501 my ($i,$j) = &splitkey($key);
1277 501 ▶ next if ($i == ($j+$offset));
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j + $offset 1 500 501
1278 500 $diag->assign($i,$j,$matrix->{data}{$key});
1279 }
1280 1 $diag->{rows} = $matrix->{rows};
1281 1 $diag->{columns} = $matrix->{columns};
1282 1 return $diag;
1283 }
1284  
1285 6 ▶sub lowerpart {
Subroutine lowerpart: called CC=3 SCAR=11.0
Pod undocumented
1286 6 my ($matrix) = @_;
1287 6 my $lower = new Math::MatrixSparse;
1288 6 foreach my $key (keys %{$matrix->{data}}){
1289 1746 my ($i,$j) = &splitkey($key);
1290 1746 ▶ next unless ($i > $j);
Branch: 1 branch
BranchTrueFalseTotal
unless $i > $j 1071 675 1746
1291 675 $lower->assign($i,$j,$matrix->{data}{$key});
1292 }
1293 6 $lower->{rows} = $matrix->{rows};
1294 6 $lower->{columns} = $matrix->{columns};
1295 6 $lower->{special}->{shape} = "strictlower";
1296  
1297 6 return $lower;
1298 }
1299  
1300 2 ▶sub nonlowerpart {
Subroutine nonlowerpart: called CC=3 SCAR=11.0
Pod undocumented
1301 2 my ($matrix) = @_;
1302 2 my $lower = new Math::MatrixSparse;
1303 2 foreach my $key (keys %{$matrix->{data}}){
1304 1002 my ($i,$j) = &splitkey($key);
1305 1002 ▶ next if ($i > $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i > $j 450 552 1002
1306 552 $lower->assign($i,$j,$matrix->{data}{$key});
1307 }
1308 2 $lower->{rows} = $matrix->{rows};
1309 2 $lower->{columns} = $matrix->{columns};
1310 2 $lower->{special}->{shape} = "upper";
1311 2 return $lower;
1312 }
1313  
1314 6 ▶sub upperpart {
Subroutine upperpart: called CC=3 SCAR=11.0
Pod undocumented
1315 6 my ($matrix) = @_;
1316 6 my $upper = new Math::MatrixSparse;
1317 6 foreach my $key (keys %{$matrix->{data}}){
1318 1746 my ($i,$j) = &splitkey($key);
1319 1746 ▶ next unless ($i < $j);
Branch: 1 branch
BranchTrueFalseTotal
unless $i < $j 774 972 1746
1320 972 $upper->assign($i,$j,$matrix->{data}{$key});
1321 }
1322 6 $upper->{rows} = $matrix->{rows};
1323 6 $upper->{columns} = $matrix->{columns};
1324 6 $upper->{special}->{shape} = "strictupper";
1325 6 return $upper;
1326 }
1327  
1328 1 ▶sub nonupperpart {
Subroutine nonupperpart: called CC=3 SCAR=11.0
Pod undocumented
1329 1 my ($matrix) = @_;
1330 1 my $upper = new Math::MatrixSparse;
1331 1 foreach my $key (keys %{$matrix->{data}}){
1332 501 my ($i,$j) = &splitkey($key);
1333 501 ▶ next if ($i < $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i < $j 275 226 501
1334 226 $upper->assign($i,$j,$matrix->{data}{$key});
1335 }
1336 1 $upper->{rows} = $matrix->{rows};
1337 1 $upper->{columns} = $matrix->{columns};
1338 1 $upper->{special}->{shape} = "lower";
1339  
1340 1 return $upper;
1341 }
1342  
1343  
1344 0 ▶sub _diagpart {
Subroutine _diagpart: not called CC=4 SCAR=30.0
1345 0 my ($matrix,$offset) = @_;
1346 0 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
1347 0 foreach my $key (keys %{$matrix->{data}}){
1348 0 my ($i,$j) = &splitkey($key);
1349 0 ▶ $matrix->delete($i,$j) unless ($i == ($j+$offset));
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $j + $offset 0 0 0
1350 }
1351 0 $matrix->{special}->{shape} = "diagonal";
1352 0 return $matrix;
1353 }
1354  
1355 0 ▶sub _nondiagpart {
Subroutine _nondiagpart: not called CC=5 SCAR=34.0
1356 0 my ($matrix,$offset) = @_;
1357 0 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
1358 0 foreach my $key (keys %{$matrix->{data}}){
1359 0 my ($i,$j) = &splitkey($key);
1360 0 ▶ $matrix->delete($i,$j) if ($i == ($j+$offset));
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j + $offset 0 0 0
1361 }
1362 $matrix->{special}->{shape}="" if
1363 0 ▶ $matrix->{special}->{shape} =~ m/diagonal/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /diagonal/i 0 0 0
1364 0 return $matrix;
1365 }
1366  
1367  
1368 0 ▶sub _lowerpart {
Subroutine _lowerpart: not called CC=3 SCAR=24.8
1369 0 my ($matrix) = @_;
1370 0 foreach my $key (keys %{$matrix->{data}}){
1371 0 my ($i,$j) = &splitkey($key);
1372 0 ▶ $matrix->delete($i,$j) unless ($i > $j);
Branch: 1 branch
BranchTrueFalseTotal
unless $i > $j 0 0 0
1373 }
1374 0 $matrix->{special}->{shape} = "strictlower";
1375 0 return $matrix;
1376 }
1377  
1378 3 ▶sub _nonlowerpart {
Subroutine _nonlowerpart: called CC=3 SCAR=11.0
1379 3 my ($matrix) = @_;
1380 3 foreach my $key (keys %{$matrix->{data}}){
1381 4767 my ($i,$j) = &splitkey($key);
1382 4767 ▶ $matrix->delete($i,$j) if ($i > $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i > $j 3990 777 4767
1383 }
1384 3 $matrix->{special}->{shape} = "upper";
1385 3 return $matrix;
1386 }
1387  
1388 1 ▶sub _upperpart {
Subroutine _upperpart: called CC=3 SCAR=11.0
1389 1 my ($matrix) = @_;
1390 1 foreach my $key (keys %{$matrix->{data}}){
1391 501 my ($i,$j) = &splitkey($key);
1392 501 ▶ $matrix->delete($i,$j) unless ($i < $j);
Branch: 1 branch
BranchTrueFalseTotal
unless $i < $j 226 275 501
1393 }
1394 1 $matrix->{special}->{shape} = "strictupper";
1395 1 return $matrix;
1396 }
1397  
1398 0 ▶sub _nonupperpart {
Subroutine _nonupperpart: not called CC=3 SCAR=24.8
1399 0 my ($matrix) = @_;
1400 0 foreach my $key (keys %{$matrix->{data}}){
1401 0 my ($i,$j) = &splitkey($key);
1402 0 ▶ $matrix->delete($i,$j) if ($i < $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i < $j 0 0 0
1403 }
1404 0 $matrix->{special}->{shape} = "lower";
1405 0 return $matrix;
1406 }
1407  
1408  
1409 1 ▶sub symmetricpart {
Subroutine symmetricpart: called CC=1 SCAR=0.0
Pod undocumented
1410 #.5*( A+A' )
1411 1 my ($matrix) = @_;
1412 1 my $sp =($matrix+$matrix->transpose());
1413 1 $sp = 0.5*$sp;
1414 1 $sp->{special}->{structure} = "symmetric";
1415 1 return $sp;
1416
1417 }
1418 0 ▶sub _symmetricpart {
Subroutine _symmetricpart: not called CC=1 SCAR=6.9
1419 #.5*( A+A' )
1420 0 my ($matrix) = @_;
1421 0 my $sp =($matrix+$matrix->transpose());
1422 0 $sp = 0.5*$sp;
1423 0 $sp->{special}->{structure} = "symmetric";
1424 0 return $matrix=$sp->copy();
1425 }
1426  
1427 1 ▶sub skewsymmetricpart {
Subroutine skewsymmetricpart: called CC=1 SCAR=0.0
Pod undocumented
1428 #.5*( A-A' )
1429 1 my ($matrix) = @_;
1430 1 my $ssp= 0.5*($matrix-$matrix->transpose());
1431 1 $ssp->{special}->{structure} = "skewsymmetric";
1432 1 return $ssp;
1433 }
1434  
1435 0 ▶sub _skewsymmetricpart {
Subroutine _skewsymmetricpart: not called CC=1 SCAR=6.9
1436 #.5*( A-A' )
1437 0 my ($matrix) = @_;
1438 0 my $ssp= 0.5*($matrix-$matrix->transpose());
1439 0 $ssp->{special}->{structure} = "skewsymmetric";
1440 0 return $matrix=$ssp->copy();
1441 }
1442  
1443  
1444 0 ▶sub positivepart {
Subroutine positivepart: not called CC=3 SCAR=24.8
Pod undocumented
1445 0 my ($matrix) = @_;
1446 0 my $pos = new Math::MatrixSparse;
1447 0 foreach my $key (keys %{$matrix->{data}}){
1448 0 ▶ next unless $matrix->elementkey($key) >0;
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->elementkey($key) > 0 0 0 0
1449 0 $pos->assignkey($key,$matrix->{data}{$key});
1450 }
1451 0 $pos->{rows} = $matrix->{rows};
1452 0 $pos->{columns} = $matrix->{columns};
1453 0 return $pos;
1454 }
1455  
1456  
1457 0 ▶sub negativepart {
Subroutine negativepart: not called CC=3 SCAR=24.8
Pod undocumented
1458 0 my ($matrix) = @_;
1459 0 my $neg = new Math::MatrixSparse;
1460 0 foreach my $key (keys %{$matrix->{data}}){
1461 0 ▶ next unless $matrix->elementkey($key) <0;
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->elementkey($key) < 0 0 0 0
1462 0 $neg->assignkey($key,$matrix->{data}{$key});
1463 }
1464 0 $neg->{rows} = $matrix->{rows};
1465 0 $neg->{columns} = $matrix->{columns};
1466 0 return $neg;
1467 }
1468  
1469 0 ▶sub _positivepart {
Subroutine _positivepart: not called CC=3 SCAR=24.8
1470 0 my ($matrix) = @_;
1471 0 foreach my $key (keys %{$matrix->{data}}){
1472 0 ▶ next if $matrix->elementkey($key) >0;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->elementkey($key) > 0 0 0 0
1473 0 $matrix->deletekey($key,$matrix->{data}{$key});
1474 }
1475 0 return $matrix;
1476 }
1477  
1478 0 ▶sub _negativepart {
Subroutine _negativepart: not called CC=3 SCAR=24.8
1479 0 my ($matrix) = @_;
1480 0 foreach my $key (keys %{$matrix->{data}}){
1481 0 ▶ next if $matrix->elementkey($key) <0;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->elementkey($key) < 0 0 0 0
1482 0 $matrix->deletekey($key,$matrix->{data}{$key});
1483 }
1484 0 return $matrix;
1485 }
1486  
1487  
1488 0 ▶sub mask{
Subroutine mask: not called CC=8 SCAR=42.8
Pod undocumented
1489 0 my ($matrix, $i1,$i2,$j1,$j2) = @_;
1490 0 ▶ return undef unless (($i1<=$i2)&&($j1<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
unless $i1 <= $i2 and $j1 <= $j2 0 0 0
Condition0%0/3
$i1 <= $i2 and $j1 <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$i1 <= $i2 and $j1 <= $j2
$i1 <= $i2 $j1 <= $j2
Truth table0%0/3
A and B
A= $i1 <= $i2B= $j1 <= $j2
ABresult
0X0
100
111
1491 0 my $mask = new Math::MatrixSparse;
1492 0 foreach my $key (keys %{$matrix->{data}}) {
1493 0 my ($i,$j) = &splitkey($key);
1494 0 ▶ next unless (($i>=$i1)&&($i<=$i2));
Branch: 1 branch
BranchTrueFalseTotal
unless $i >= $i1 and $i <= $i2 0 0 0
Condition0%0/3
$i >= $i1 and $i <= $i2
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= $i1 and $i <= $i2
$i >= $i1 $i <= $i2
Truth table0%0/3
A and B
A= $i >= $i1B= $i <= $i2
ABresult
0X0
100
111
1495 0 ▶ next unless (($j>=$j1)&&($j<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
unless $j >= $j1 and $j <= $j2 0 0 0
Condition0%0/3
$j >= $j1 and $j <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$j >= $j1 and $j <= $j2
$j >= $j1 $j <= $j2
Truth table0%0/3
A and B
A= $j >= $j1B= $j <= $j2
ABresult
0X0
100
111
1496 0 $mask->assignkey($key,$matrix->{data}{$key});
1497 }
1498 0 return $mask;
1499 }
1500  
1501 0 ▶sub _mask{
Subroutine _mask: not called CC=8 SCAR=42.8
1502 0 my ($matrix, $i1,$i2,$j1,$j2) = @_;
1503 0 ▶ return undef unless (($i1<=$i2)&&($j1<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
unless $i1 <= $i2 and $j1 <= $j2 0 0 0
Condition0%0/3
$i1 <= $i2 and $j1 <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$i1 <= $i2 and $j1 <= $j2
$i1 <= $i2 $j1 <= $j2
Truth table0%0/3
A and B
A= $i1 <= $i2B= $j1 <= $j2
ABresult
0X0
100
111
1504 0 foreach my $key (keys %{$matrix->{data}}) {
1505 0 my ($i,$j) = &splitkey($key);
1506 0 ▶ next if (($i>=$i1)&&($i<=$i2));
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $i1 and $i <= $i2 0 0 0
Condition0%0/3
$i >= $i1 and $i <= $i2
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= $i1 and $i <= $i2
$i >= $i1 $i <= $i2
Truth table0%0/3
A and B
A= $i >= $i1B= $i <= $i2
ABresult
0X0
100
111
1507 0 ▶ next if (($j>=$j1)&&($j<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $j1 and $j <= $j2 0 0 0
Condition0%0/3
$j >= $j1 and $j <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$j >= $j1 and $j <= $j2
$j >= $j1 $j <= $j2
Truth table0%0/3
A and B
A= $j >= $j1B= $j <= $j2
ABresult
0X0
100
111
1508 0 $matrix->assignkey($key,0);
1509 }
1510 0 return $matrix;
1511 }
1512  
1513 0 ▶sub submatrix{
Subroutine submatrix: not called CC=8 SCAR=42.8
Pod undocumented
1514 0 my ($matrix, $i1,$i2,$j1,$j2) = @_;
1515 0 ▶ return undef unless (($i1<=$i2)&&($j1<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
unless $i1 <= $i2 and $j1 <= $j2 0 0 0
Condition0%0/3
$i1 <= $i2 and $j1 <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$i1 <= $i2 and $j1 <= $j2
$i1 <= $i2 $j1 <= $j2
Truth table0%0/3
A and B
A= $i1 <= $i2B= $j1 <= $j2
ABresult
0X0
100
111
1516 0 my $subm = new Math::MatrixSparse;
1517 0 foreach my $key (keys %{$matrix->{data}}) {
1518 0 my ($i,$j) = &splitkey($key);
1519 0 ▶ next unless (($i>=$i1)&&($i<=$i2));
Branch: 1 branch
BranchTrueFalseTotal
unless $i >= $i1 and $i <= $i2 0 0 0
Condition0%0/3
$i >= $i1 and $i <= $i2
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= $i1 and $i <= $i2
$i >= $i1 $i <= $i2
Truth table0%0/3
A and B
A= $i >= $i1B= $i <= $i2
ABresult
0X0
100
111
1520 0 ▶ next unless (($j>=$j1)&&($j<=$j2));
Branch: 1 branch
BranchTrueFalseTotal
unless $j >= $j1 and $j <= $j2 0 0 0
Condition0%0/3
$j >= $j1 and $j <= $j2
!ll&&!rl&&r
000
MC/DC0%0/2
$j >= $j1 and $j <= $j2
$j >= $j1 $j <= $j2
Truth table0%0/3
A and B
A= $j >= $j1B= $j <= $j2
ABresult
0X0
100
111
1521 0 $subm->assign($i-$i1,$j-$j1,$matrix->{data}{$key});
1522 }
1523 0 return $subm;
1524 }
1525  
1526 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod undocumented
1527 0 my ($big, $i0,$j0,$small) = @_;
1528 0 my $insert = $big->copy();
1529 0 foreach my $key (keys %{$small->{data}}){
1530 0 my ($i,$j) = &splitkey($key);
1531 0 $insert->assignkey($i+$i0,$j+$j0,$small->elementkey($key));
1532 }
1533 0 return $insert;
1534 }
1535  
1536 0 ▶sub _insert {
Subroutine _insert: not called CC=2 SCAR=17.9
1537 0 my ($big, $i0,$j0,$small) = @_;
1538
1539 0 foreach my $key (keys %{$small->{data}}){
1540 0 my ($i,$j) = &splitkey($key);
1541 0 $big->assignkey($i+$i0,$j+$j0,$small->elementkey($key));
1542 }
1543 0 return $big;
1544 }
1545  
1546  
1547 0 ▶sub shift {
Subroutine shift: not called CC=2 SCAR=17.9
Pod undocumented
1548 0 my ($matrix, $i1,$j1) = @_;
1549 0 my $this = new Math::MatrixSparse;
1550 0 $this->{name}=$matrix->{name};
1551 0 foreach my $key (keys %{$matrix->{data}}) {
1552 0 my ($i,$j) = &splitkey($key);
1553 0 $this->assign($i+$i1,$j+$j1,$matrix->{data}{$key});
1554 }
1555 0 return $this;
1556 }
1557  
1558 0 ▶sub each {
Subroutine each: not called CC=2 SCAR=17.9
Pod undocumented
1559 0 my ($matrix,$coderef) = @_;
1560 0 my $this = $matrix->copy();
1561 0 foreach my $key (keys %{$this->{data}}) {
1562 0 my ($i,$j) = &splitkey($key);
1563 0 $this->assign($i,$j,&$coderef($this->{data}{$key},$i,$j));
1564 }
1565 0 return $this;
1566 }
1567  
1568 0 ▶sub _each {
Subroutine _each: not called CC=2 SCAR=17.9
1569 0 my ($matrix,$coderef) = @_;
1570 0 foreach my $key (keys %{$matrix->{data}}) {
1571 0 my ($i,$j) = &splitkey($key);
1572 0 $matrix->assign($i,$j,&$coderef($matrix->{data}{$key},$i,$j));
1573 }
1574 0 return $matrix;
1575 }
1576  
1577 ### INFORMATIONAL AND STATISTICAL METHODS
1578  
1579  
1580 0 ▶sub printstats {
Subroutine printstats: not called CC=13 SCAR=52.0
Pod undocumented
1581 0 my ($matrix,$name) = @_;
1582 0 ▶ return unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1583  
1584 0 ▶ $name = $name || $matrix->{name} || "unnamed matrix";
Condition0%0/5
$name || $matrix->{'name'} || 'unnamed matrix'
l!l
00
$name or $matrix->{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$name || $matrix->{'name'} || 'unnamed matrix'
$name $matrix->{"name"}
Truth table0%0/3
A or B
A= $nameB= $matrix->{"name"}
ABresult
1X1
011
000
1585 0 print "Statistics for $name :\n";
1586 0 print "rows: $matrix->{rows}\t";
1587 0 print "columns: $matrix->{columns}\tdefined elements: ";
1588 0 print $matrix->count(), "\n";
1589 0 my ($width) = $matrix->width();
1590 0 print "Bandwidth: $width\t";
1591 0 ▶ print "Symmetric " if $matrix->is_symmetric();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_symmetric 0 0 0
1592 0 ▶ print "Skew-Symmetric " if $matrix->is_skewsymmetric();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_skewsymmetric 0 0 0
1593 # print "Hermetian " if $matrix->{special}->{structure} =~m/^hermetian/i;
1594 0 ▶ print "Real " if $matrix->{special}->{field} =~ m/real/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"field"} =~ /real/i 0 0 0
1595 # print "Complex " if $matrix->{special}->{field} =~ m/complex/i;
1596 0 ▶ print "Strictly " if $matrix->{special}->{shape} =~ m/strict/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /strict/ 0 0 0
1597 0 ▶ print "Upper Triangular " if $matrix->{special}->{shape} =~ m/upper/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /upper/ 0 0 0
1598 0 ▶ print "Lower Triangular " if $matrix->{special}->{shape} =~ m/lower/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /lower/ 0 0 0
1599 0 ▶ print "Diagonal" if $matrix->is_diagonal();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_diagonal 0 0 0
1600 0 ▶ print "Pattern " if $matrix->is_pattern();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_pattern 0 0 0
1601 0 ▶ print "Square " if $matrix->is_square();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_square 0 0 0
1602 0 print "\n";
1603 }
1604  
1605  
1606 0 ▶sub dim {
Subroutine dim: not called CC=1 SCAR=6.9
Pod undocumented
1607 0 my ($matrix) = @_;
1608 0 return ($matrix->{rows},$matrix->{columns});
1609 }
1610  
1611 2 ▶sub exactdim {
Subroutine exactdim: called CC=4 SCAR=13.9
Pod undocumented
1612 2 my ($matrix) = @_;
1613 2 my $rows=0;
1614 2 my $columns = 0;
1615 2 foreach my $key (keys %{$matrix->{data}}) {
1616 971 my ($i,$j) = &splitkey($key);
1617 971 ▶ $rows = $i if $i>$rows;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $rows 10 961 971
1618 971 ▶ $columns = $j if $j > $rows;
Branch: 1 branch
BranchTrueFalseTotal
if $j > $rows 5 966 971
1619 }
1620 2 return ($rows,$columns);
1621 }
1622  
1623  
1624 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
1625 0 my ($matrix) = @_;
1626 0 return scalar keys %{$matrix->{data}};
1627 }
1628  
1629 0 ▶sub width {
Subroutine width: not called CC=4 SCAR=30.0
Pod undocumented
1630 0 my ($matrix) = @_;
1631 0 ▶ return $matrix->{special}->{bandwidth} if $matrix->{special}->{bandwidth};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"bandwidth"} 0 0 0
1632 0 my $width = 0;
1633 0 foreach my $key (keys %{$matrix->{data}}) {
1634 0 my ($i,$j) = &splitkey($key);
1635 0 ▶ $width = abs($i- $j) if abs($i-$j) > $width;
Branch: 1 branch
BranchTrueFalseTotal
if abs $i - $j > $width 0 0 0
1636 }
1637 0 return ($width);
1638 }
1639  
1640 1 ▶sub sum {
Subroutine sum: called CC=2 SCAR=6.9
Pod undocumented
1641 1 my ($matrix) = @_;
1642 1 my $sum = 0;
1643 1 foreach my $key (keys %{$matrix->{data}}) {
1644 100 $sum += $matrix->elementkey($key);
1645 }
1646 1 return $sum;
1647 }
1648  
1649 0 ▶sub sumeach {
Subroutine sumeach: not called CC=2 SCAR=17.9
Pod undocumented
1650 0 my ($matrix,$coderef) = @_;
1651 0 my $sum = 0;
1652 0 foreach my $key (keys %{$matrix->{data}}) {
1653 0 my ($i,$j) = &splitkey( $key);
1654 0 $sum += &$coderef($matrix->elementkey($key),$i,$j);
1655 }
1656 0 return $sum;
1657 }
1658  
1659 67 ▶sub abssum {
Subroutine abssum: called CC=2 SCAR=6.9
Pod undocumented
1660 67 my ($matrix) = @_;
1661 67 my $sum = 0;
1662 67 foreach my $key (keys %{$matrix->{data}}) {
1663 2035 $sum += abs($matrix->elementkey($key));
1664 }
1665 67 return $sum;
1666 }
1667  
1668 0 ▶sub rownorm {
Subroutine rownorm: not called CC=3 SCAR=24.8
Pod undocumented
1669 0 my ($matrix) = @_;
1670 0 my %rowsums;
1671 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1672 0 foreach my $key (keys %{$matrix->{data}}) {
1673 0 my ($i,$j) = &splitkey($key);
1674 0 $rowsums{$i}+= abs($matrix->elementkey($key));
1675 }
1676 0 return (sort {$a <=> $b} values %rowsums)[0];
1677 }
1678  
1679  
1680 0 ▶sub columnnorm {
Subroutine columnnorm: not called CC=3 SCAR=24.8
Pod undocumented
1681 0 my ($matrix) = @_;
1682 0 my %columnsums;
1683 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1684 0 foreach my $key (keys %{$matrix->{data}}) {
1685 0 my ($i,$j) = &splitkey($key);
1686 0 $columnsums{$i}+= $matrix->elementkey($key);
1687 }
1688 0 return (sort {$a <=> $b} values %columnsums)[0];
1689 }
1690  
1691 0 ▶sub norm_max {
Subroutine norm_max: not called CC=1 SCAR=6.9
Pod undocumented
1692 0 return $_[0]->rownorm();
1693 }
1694  
1695 0 ▶sub norm_one {
Subroutine norm_one: not called CC=1 SCAR=6.9
Pod undocumented
1696 0 return $_[0]->columnnorm();
1697 }
1698  
1699 0 ▶sub norm_frobenius {
Subroutine norm_frobenius: not called CC=1 SCAR=6.9
Pod undocumented
1700 0 ▶ return sqrt($_[0]->sumeach(sub {$_[0]*$_[0]}));
Subroutine __ANON__: not called CC=1 SCAR=6.9
1701 }
1702  
1703  
1704 1 ▶sub trace {
Subroutine trace: called CC=1 SCAR=0.0
Pod undocumented
1705 1 my ($matrix) = @_;
1706 1 return $matrix->diagpart()->sum();
1707 }
1708  
1709 ### BOOLEAN METHODS
1710  
1711 10 ▶sub equals {
Subroutine equals: called CC=6 SCAR=19.0
Pod undocumented
1712 10 my ($left,$right) = @_;
1713 10 ▶ return 1 unless ( defined $left|| defined $right);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left or defined $right 0 10 10
Condition33%1/3
defined $left or defined $right
l!l&&r!l&&!r
1000
MC/DC0%0/2
defined $left or defined $right
defined $left defined $right
Truth table33%1/3
A or B
A= defined $leftB= defined $right
ABresult
1X1
011
000
1714 10 ▶ return 0 unless defined $left;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left 0 10 10
1715 10 ▶ return 0 unless defined $right;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $right 0 10 10
1716 10 my $truth = 1;
1717 10 foreach my $key (keys %{$left->{data}}, keys %{$right->{data}}) {
1718 10232 $truth *= ($left->elementkey($key) == $right->elementkey($key));
1719 }
1720 10 return $truth;
1721 }
1722  
1723 3 ▶sub is_square {
Subroutine is_square: called CC=2 SCAR=7.1
Pod undocumented
1724 3 my ($matrix) = @_;
1725 3 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 3 3
1726 3 return $matrix->{rows} == $matrix->{columns};
1727 }
1728 0 ▶sub is_quadratic {
Subroutine is_quadratic: not called CC=2 SCAR=17.9
Pod undocumented
1729 0 my ($matrix) = @_;
1730 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1731 0 return $matrix->{rows} == $matrix->{columns};
1732 }
1733  
1734 1 ▶sub is_symmetric {
Subroutine is_symmetric: called CC=6 SCAR=30.5
Pod undocumented
1735 1 my ($matrix) = @_;
1736 1 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 1 1
1737 1 ▶ return 1 if $matrix->{special}->{structure} =~ m/^symmetric/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"structure"} =~ /^symmetric/ 1 0 1
1738 0 ▶ return 0 if $matrix->{special}->{structure} =~ m/skewsymmetric/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"structure"} =~ /skewsymmetric/ 0 0 0
1739 # return 0 if $matrix->{special}->{structure} =~ m/hermetian/;
1740
1741 0 my $truth = 1;
1742 0 foreach my $key (keys %{$matrix->{data}}) {
1743 0 my ($i,$j) = &splitkey($key);
1744 0 my $reversekey = &makekey($j, $i);
1745 0 $truth *= ($matrix->elementkey($key)
1746 == $matrix->elementkey($reversekey));
1747 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1748 }
1749 0 return $truth;
1750 }
1751  
1752 1 ▶sub is_skewsymmetric {
Subroutine is_skewsymmetric: called CC=6 SCAR=27.7
Pod undocumented
1753 1 my ($matrix) = @_;
1754 1 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 1 1
1755 1 ▶ return 0 if $matrix->{special}->{structure} =~ m/^symmetric/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"structure"} =~ /^symmetric/ 0 1 1
1756 1 ▶ return 1 if $matrix->{special}->{structure} =~ m/^skewsymmetric/;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"structure"} =~ /^skewsymmetric/ 1 0 1
1757 0 my $truth = 1;
1758 0 foreach my $key (keys %{$matrix->{data}}) {
1759 0 my ($i,$j) = &splitkey($key);
1760 0 my $reversekey = &makekey($j , $i);
1761 0 $truth *= ($matrix->elementkey($key)
1762 == -1*$matrix->elementkey($reversekey));
1763 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1764 }
1765 0 return $truth;
1766 }
1767  
1768 1 ▶sub is_diagonal {
Subroutine is_diagonal: called CC=5 SCAR=25.7
Pod undocumented
1769 1 my ($matrix) = @_;
1770 1 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 1 1
1771 1 ▶ return 1 if $matrix->{special}->{shape}=~m/diagonal/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /diagonal/i 1 0 1
1772 0 my $truth = 1;
1773 0 foreach my $key (keys %{$matrix->{data}}) {
1774 0 my ($i,$j) = &splitkey($key);
1775 0 $truth *= ($i==$j);
1776 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1777 }
1778 0 return $truth;
1779 }
1780  
1781 2 ▶sub is_strictlowertriangular {
Subroutine is_strictlowertriangular: called CC=5 SCAR=25.7
Pod undocumented
1782 2 my ($matrix) = @_;
1783 2 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 2 2
1784 2 ▶ return 1 if $matrix->{special}->{shape}=~m/strictlower/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /strictlower/i 2 0 2
1785 0 my $truth = 1;
1786 0 foreach my $key (keys %{$matrix->{data}}) {
1787 0 my ($i,$j) = &splitkey($key);
1788 0 $truth *= ($i > $j);
1789 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1790 }
1791 0 return $truth;
1792 }
1793  
1794 2 ▶sub is_strictuppertriangular {
Subroutine is_strictuppertriangular: called CC=5 SCAR=16.2
Pod undocumented
1795 2 my ($matrix) = @_;
1796 2 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 2 2
1797 2 ▶ return 1 if $matrix->{special}->{shape}=~m/strictupper/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /strictupper/i 1 1 2
1798 1 my $truth = 1;
1799 1 foreach my $key (keys %{$matrix->{data}}) {
1800 225 my ($i,$j) = &splitkey($key);
1801 225 $truth *= ($i < $j);
1802 225 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 225 225
1803 }
1804 1 return $truth;
1805 }
1806  
1807 2 ▶sub is_lowertriangular {
Subroutine is_lowertriangular: called CC=5 SCAR=24.1
Pod undocumented
1808 2 my ($matrix) = @_;
1809 2 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 2 2
1810 2 my $truth = 1;
1811 2 ▶ return 1 if $matrix->{special}->{shape}=~m/lower/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /lower/i 2 0 2
1812 0 foreach my $key (keys %{$matrix->{data}}) {
1813 0 my ($i,$j) = &splitkey($key);
1814 0 $truth *= ($i >= $j);
1815 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1816 }
1817 0 return $truth;
1818 }
1819  
1820 2 ▶sub is_uppertriangular {
Subroutine is_uppertriangular: called CC=5 SCAR=24.1
Pod undocumented
1821 2 my ($matrix) = @_;
1822 2 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 2 2
1823 2 my $truth = 1;
1824 2 ▶ return 1 if $matrix->{special}->{shape}=~m/upper/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /upper/i 2 0 2
1825 0 foreach my $key (keys %{$matrix->{data}}) {
1826 0 my ($i,$j) = &splitkey($key);
1827 0 $truth *= ($i <= $j);
1828 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1829 }
1830 0 return $truth;
1831 }
1832  
1833 0 ▶sub is_positive {
Subroutine is_positive: not called CC=5 SCAR=34.0
Pod undocumented
1834 0 my ($matrix) = @_;
1835 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1836 0 ▶ return 1 if $matrix->{special}->{sign} =~ m/^positive/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"sign"} =~ /^positive/i 0 0 0
1837 0 my $truth = 1;
1838 0 foreach my $key (keys %{$matrix->{data}}) {
1839 0 $truth *= ($matrix->elementkey($key)>0);
1840 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1841 }
1842 0 return $truth;
1843 }
1844  
1845 0 ▶sub is_zero {
Subroutine is_zero: not called CC=5 SCAR=34.0
Pod undocumented
1846 0 my ($matrix) = @_;
1847 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1848 0 my $truth = 1;
1849 0 ▶ return 1 if $matrix->{special}->{sign} =~ /zero/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"sign"} =~ /zero/i 0 0 0
1850 0 foreach my $key (keys %{$matrix->{data}}) {
1851 0 $truth *= ($matrix->elementkey($key)==0);
1852 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1853 }
1854 0 return $truth;
1855 }
1856  
1857  
1858 0 ▶sub is_nonpositive {
Subroutine is_nonpositive: not called CC=4 SCAR=30.0
Pod undocumented
1859 0 my ($matrix) = @_;
1860 0 my $truth = 1;
1861 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1862 0 foreach my $key (keys %{$matrix->{data}}) {
1863 0 $truth *= ($matrix->elementkey($key)<=0);
1864 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1865 }
1866 0 return $truth;
1867 }
1868  
1869 0 ▶sub is_negative {
Subroutine is_negative: not called CC=4 SCAR=30.0
Pod undocumented
1870 0 my ($matrix) = @_;
1871 0 my $truth = 1;
1872 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1873 0 foreach my $key (keys %{$matrix->{data}}) {
1874 0 $truth *= ($matrix->elementkey($key)<0);
1875 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1876 }
1877 0 return $truth;
1878 }
1879  
1880 0 ▶sub is_nonnegative {
Subroutine is_nonnegative: not called CC=4 SCAR=30.0
Pod undocumented
1881 0 my ($matrix) = @_;
1882 0 my $truth = 1;
1883 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1884 0 foreach my $key (keys %{$matrix->{data}}) {
1885 0 $truth *= ($matrix->elementkey($key)>=0);
1886 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1887 }
1888 0 return $truth;
1889 }
1890  
1891 0 ▶sub is_boolean {
Subroutine is_boolean: not called CC=6 SCAR=37.4
Pod undocumented
1892 0 my ($matrix) = @_;
1893 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1894 0 ▶ return 1 if $matrix->{special}->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"pattern"} 0 0 0
1895 0 my $truth = 1;
1896 0 foreach my $key (keys %{$matrix->{data}}) {
1897 0 ▶ $truth *= (($matrix->elementkey($key) == 0)||($matrix->elementkey($key) == 1));
Condition0%0/3
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
$matrix->elementkey($key) == 0 $matrix->elementkey($key) == 1
Truth table0%0/3
A || B
A= $matrix->elementkey($key) == 0B= $matrix->elementkey($key) == 1
ABresult
1X1
011
000
1898 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1899 }
1900 0 return $truth;
1901 }
1902  
1903 0 ▶sub is_pattern {
Subroutine is_pattern: not called CC=6 SCAR=37.4
Pod undocumented
1904 0 my ($matrix) = @_;
1905 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1906 0 ▶ return 1 if $matrix->{special}->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"pattern"} 0 0 0
1907 0 my $truth = 1;
1908 0 foreach my $key (keys %{$matrix->{data}}) {
1909 0 ▶ $truth *= (($matrix->elementkey($key) == 0)||($matrix->elementkey($key) == 1));
Condition0%0/3
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
$matrix->elementkey($key) == 0 $matrix->elementkey($key) == 1
Truth table0%0/3
A || B
A= $matrix->elementkey($key) == 0B= $matrix->elementkey($key) == 1
ABresult
1X1
011
000
1910 0 ▶ return 0 unless $truth;
Branch: 1 branch
BranchTrueFalseTotal
unless $truth 0 0 0
1911 }
1912 0 return $truth;
1913 }
1914  
1915  
1916 0 ▶sub diagnose {
Subroutine diagnose: not called CC=16 SCAR=56.1
Pod undocumented
1917 0 my ($matrix) = @_;
1918 0 ▶ return 0 unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
1919 0 my $boolean = 1;
1920 0 $matrix->_sizetofit();
1921 0 $matrix->{special}->{square} = ($matrix->{rows} == $matrix->{columns});
1922 0 my $upper = 1;
1923 0 my $diagonal = 1;
1924 0 my $lower = 1;
1925 0 my $strictupper = 1;
1926 0 my $strictlower = 1;
1927 0 my $positive = 1;
1928 0 my $nonpositive = 1;
1929 0 my $negative = 1;
1930 0 my $nonnegative = 1;
1931 0 my $zero = 1;
1932 0 my $symmetric = 1;
1933 0 my $skewsymmetric = 1;
1934 0 foreach my $key (keys %{$matrix->{data}}) {
1935 0 my ($i,$j) = &splitkey($key);
1936 0 my $reversekey = &makekey($j,$i);
1937 0 $symmetric *= ($matrix->elementkey($key)
1938 == $matrix->elementkey($reversekey));
1939 0 $skewsymmetric *= ($matrix->elementkey($key)
1940 == -1*$matrix->elementkey($reversekey));
1941 0 $diagonal *= ($i==$j);
1942 0 $strictlower *= ($i > $j);
1943 0 $lower *= ($i >= $j);
1944 0 $strictupper *= ($i > $j);
1945 0 $upper *= ($i >= $j);
1946 0 $positive *= ($matrix->elementkey($key)>0);
1947 0 $nonpositive *= ($matrix->elementkey($key)<=0);
1948 0 $negative *= ($matrix->elementkey($key)<0);
1949 0 $nonnegative *= ($matrix->elementkey($key)>=0);
1950 0 ▶ $boolean *= (($matrix->elementkey($key) == 0)
Condition0%0/3
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$matrix->elementkey($key) == 0 || $matrix->elementkey($key) == 1
$matrix->elementkey($key) == 0 $matrix->elementkey($key) == 1
Truth table0%0/3
A || B
A= $matrix->elementkey($key) == 0B= $matrix->elementkey($key) == 1
ABresult
1X1
011
000
1951 ||($matrix->elementkey($key) == 1));
1952 0 $zero *= !($matrix->elementkey($key));
1953 }
1954 0 $matrix->{special}->{pattern} = $boolean;
1955 0 ▶ if ($diagonal) {
Branch: 3 branches
BranchTrueFalseTotal
if ($diagonal) { } 0 0 0
elsif ($lower) { } 0 0 0
elsif ($upper) { } 0 0 0
1956 0 $matrix->{special}->{shape} = "diagonal";
1957 } elsif ($lower) {
1958 0 ▶ if ($strictlower) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strictlower) { } 0 0 0
1959 0 $matrix->{special}->{shape} = "strictlower";
1960 } else {
1961 0 $matrix->{special}->{shape} = "lower";
1962 }
1963 } elsif ($upper) {
1964 0 ▶ if ($strictlower) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strictlower) { } 0 0 0
1965 0 $matrix->{special}->{shape} = "strictupper";
1966 } else {
1967 0 $matrix->{special}->{shape} = "upper";
1968 }
1969 } else {
1970 0 $matrix->{special}->{shape}="";
1971 }
1972 0 ▶ if ($symmetric) {
Branch: 2 branches
BranchTrueFalseTotal
if ($symmetric) { } 0 0 0
elsif ($skewsymmetric) { } 0 0 0
1973 0 $matrix->{special}->{structure}="symmetric";
1974 } elsif ($skewsymmetric) {
1975 0 $matrix->{special}->{structure}="skewsymmetric";
1976 } else {
1977 0 $matrix->{special}->{structure}="";
1978 }
1979 0 ▶ if ($zero) {
Branch: 3 branches
BranchTrueFalseTotal
if ($zero) { } 0 0 0
elsif ($nonpositive) { } 0 0 0
elsif ($nonnegative) { } 0 0 0
1980 0 $matrix->{special}->{sign} = "zero";
1981 } elsif ($nonpositive) {
1982 0 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) { } 0 0 0
1983 0 $matrix->{special}->{sign}="negative";
1984 } else {
1985 0 $matrix->{special}->{sign}="nonpositive";
1986 }
1987 } elsif ($nonnegative) {
1988 0 ▶ if ($positive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($positive) { } 0 0 0
1989 0 $matrix->{special}->{sign}="positive";
1990 } else {
1991 0 $matrix->{special}->{sign}="nonnegative";
1992 }
1993 } else {
1994 0 $matrix->{special}->{sign}="";
1995 }
1996 0 return $matrix;
1997 }
1998  
1999  
2000 ### BOOLEAN ARITHMETIC METHODS
2001  
2002 0 ▶sub matrixand {
Subroutine matrixand: not called CC=7 SCAR=40.3
Pod undocumented
2003 0 my ($matrix1, $matrix2) = @_;
2004 0 my $truth = new Math::MatrixSparse;
2005 0 ▶ $truth->{rows} = ($matrix1->{rows}<$matrix2->{rows}) ? $matrix1->{rows} : $matrix2->{rows};
Branch: 1 branch
BranchTrueFalseTotal
$matrix1->{'rows'} < $matrix2->{'rows'} ? : 0 0 0
2006 0 ▶ $truth->{columns} = ($matrix1->{columns}<$matrix2->{columns}) ? $matrix1->{columns} : $matrix2->{columns};
Branch: 1 branch
BranchTrueFalseTotal
$matrix1->{'columns'} < $matrix2->{'columns'} ? : 0 0 0
2007 0 foreach my $key (keys %{$matrix1->{data}}) {
2008 0 ▶ $truth->assignkey($key,$matrix1->elementkey($key)&&
Condition0%0/3
$matrix1->elementkey($key) && $matrix2->elementkey($key)
!ll&&!rl&&r
000
MC/DC0%0/2
$matrix1->elementkey($key) && $matrix2->elementkey($key)
$matrix1->elementkey($key) $matrix2->elementkey($key)
Truth table0%0/3
A && B
A= $matrix1->elementkey($key)B= $matrix2->elementkey($key)
ABresult
0X0
100
111
2009 $matrix2->elementkey($key));
2010 }
2011 0 ▶ if ((defined $matrix1->{name})&&(defined $matrix2->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix1->{"name"} and defined $matrix2->{"name"}) 0 0 0
Condition0%0/3
defined $matrix1->{"name"} and defined $matrix2->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $matrix1->{"name"} and defined $matrix2->{"name"}
defined $matrix1->{"name"} defined $matrix2->{"name"}
Truth table0%0/3
A and B
A= defined $matrix1->{"name"}B= defined $matrix2->{"name"}
ABresult
0X0
100
111
2012 0 $truth->{name} = $matrix1->{name} . "&" . $matrix2->{name};
2013 }
2014 #should be updated already.
2015 0 $truth->{special}->{pattern} = 1;
2016 0 return $truth;
2017 }
2018  
2019 0 ▶sub matrixor {
Subroutine matrixor: not called CC=7 SCAR=40.3
Pod undocumented
2020 0 my ($matrix1, $matrix2) = @_;
2021 0 my $truth = new Math::MatrixSparse;
2022 0 ▶ $truth->{rows} = ($matrix1->{rows}<$matrix2->{rows}) ? $matrix1->{rows} : $matrix2->{rows};
Branch: 1 branch
BranchTrueFalseTotal
$matrix1->{'rows'} < $matrix2->{'rows'} ? : 0 0 0
2023 0 ▶ $truth->{columns} = ($matrix1->{columns}<$matrix2->{columns}) ? $matrix1->{columns} : $matrix2->{columns};
Branch: 1 branch
BranchTrueFalseTotal
$matrix1->{'columns'} < $matrix2->{'columns'} ? : 0 0 0
2024 0 foreach my $key (keys %{$matrix1->{data}}) {
2025 0 ▶ $truth->assignkey($key,$matrix1->elementkey($key)||
Condition0%0/3
$matrix1->elementkey($key) || $matrix2->elementkey($key)
l!l&&r!l&&!r
000
MC/DC0%0/2
$matrix1->elementkey($key) || $matrix2->elementkey($key)
$matrix1->elementkey($key) $matrix2->elementkey($key)
Truth table0%0/3
A || B
A= $matrix1->elementkey($key)B= $matrix2->elementkey($key)
ABresult
1X1
011
000
2026 $matrix2->elementkey($key));
2027 }
2028 0 ▶ if ((defined $matrix1->{name})&&(defined $matrix2->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix1->{"name"} and defined $matrix2->{"name"}) 0 0 0
Condition0%0/3
defined $matrix1->{"name"} and defined $matrix2->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $matrix1->{"name"} and defined $matrix2->{"name"}
defined $matrix1->{"name"} defined $matrix2->{"name"}
Truth table0%0/3
A and B
A= defined $matrix1->{"name"}B= defined $matrix2->{"name"}
ABresult
0X0
100
111
2029 0 $truth->{name} = $matrix1->{name} . "|" . $matrix2->{name};
2030 }
2031 #should be updated already.
2032 0 $truth->{special}->{pattern} = 1;
2033 0 return $truth;
2034 }
2035  
2036  
2037 ### DELETION FUNCTIONS
2038 4216 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
2039 4216 my ($matrix,$i,$j) = @_;
2040 4216 my $key = &makekey($i,$j);
2041 4216 $matrix->deletekey($key);
2042 4216 return;
2043 }
2044  
2045 4216 ▶sub deletekey {
Subroutine deletekey: called CC=6 SCAR=19.7
Pod undocumented
2046 4216 my ($matrix, $key) = @_;
2047 4216 my $old = $matrix->elementkey($key);
2048 4216 my ($i,$j) = &splitkey($key);
2049 4216 delete $matrix->{data}{$key};
2050 #sign
2051 4216 ▶ if ($matrix->{special}->{sign} =~ /^positive/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($matrix->{'special'}{'sign'} =~ /^positive/i) { } 0 4216 4216
elsif ($matrix->{'special'}{'sign'} =~ /^positive/i) { } 0 4216 4216
2052 0 $matrix->{special}->{sign} = "nonnegative";
2053 } elsif ($matrix->{special}->{sign} =~ /^positive/i) {
2054 0 $matrix->{special}->{sign} = "nonpositive";
2055 }
2056 #structure
2057 4216 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->{"special"}{"structure"} =~ /symmetric/i and $i != $j) 0 4216 4216
Condition33%1/3
$matrix->{"special"}{"structure"} =~ /symmetric/i and $i != $j
!ll&&!rl&&r
421600
MC/DC0%0/2
$matrix->{"special"}{"structure"} =~ /symmetric/i and $i != $j
$matrix->{"special"}{"structure"} =~ /symmetric/i $i != $j
Truth table33%1/3
A and B
A= $matrix->{"special"}{"structure"} =~ /symmetric/iB= $i != $j
ABresult
0X0
100
111
2058 ($matrix->{special}->{structure} =~ m/symmetric/i)
2059 &&($i!=$j)
2060 ) {
2061 0 $matrix->{special}->{structure} = "";
2062 }
2063 #band
2064 4216 ▶ if (abs($i-$j) >= $matrix->{special}->{bandwidth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $i - $j >= $matrix->{"special"}{"bandwidth"}) 3598 618 4216
2065 3598 $matrix->{special}->{bandwidth} = 0;
2066 }
2067 #pattern--no change necessary
2068 #shape--no change necessary
2069 #persistent row and column data
2070 4216 delete $matrix->{sortedrows};
2071 4216 delete $matrix->{sortedcolumns};
2072 4216 return undef;
2073 }
2074  
2075 0 ▶sub cull {
Subroutine cull: not called CC=3 SCAR=24.8
Pod undocumented
2076 0 my ($matrix) = @_;
2077 0 my $this = $matrix->copy();
2078 0 foreach my $key (keys %{$this->{data}}) {
2079 0 ▶ next if $this->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"data"}{$key} 0 0 0
2080 0 $this->deletekey($key);
2081 }
2082 0 return $this;
2083 }
2084  
2085 0 ▶sub _cull {
Subroutine _cull: not called CC=3 SCAR=24.8
2086 0 my ($matrix) = @_;
2087 0 foreach my $key (keys %{$matrix->{data}}) {
2088 0 ▶ next if $matrix->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"data"}{$key} 0 0 0
2089 0 $matrix->deletekey($key);
2090 }
2091 0 return $matrix;
2092 }
2093  
2094 0 ▶sub threshold {
Subroutine threshold: not called CC=4 SCAR=30.0
Pod undocumented
2095 0 my ($matrix,$thresh) = @_;
2096 0 ▶ return undef if $thresh <0;
Branch: 1 branch
BranchTrueFalseTotal
if $thresh < 0 0 0 0
2097 0 my $this = $matrix->copy();
2098 0 foreach my $key (keys %{$this->{data}}) {
2099 0 ▶ next if abs($this->{data}{$key})>$thresh;
Branch: 1 branch
BranchTrueFalseTotal
if abs $this->{"data"}{$key} > $thresh 0 0 0
2100 0 $this->deletekey($key);
2101 }
2102 0 return $this;
2103 }
2104  
2105 0 ▶sub _threshold {
Subroutine _threshold: not called CC=4 SCAR=30.0
2106 0 my ($matrix,$thresh) = @_;
2107 0 ▶ return undef if $thresh <0;
Branch: 1 branch
BranchTrueFalseTotal
if $thresh < 0 0 0 0
2108 0 foreach my $key (keys %{$matrix->{data}}) {
2109 0 ▶ next if abs($matrix->{data}{$key})>$thresh;
Branch: 1 branch
BranchTrueFalseTotal
if abs $matrix->{"data"}{$key} > $thresh 0 0 0
2110 0 $matrix->deletekey($key);
2111 }
2112 0 return $matrix;
2113 }
2114  
2115 0 ▶sub sizetofit {
Subroutine sizetofit: not called CC=1 SCAR=6.9
Pod undocumented
2116 0 my ($matrix) = @_;
2117 0 my $this = $matrix->copy();
2118 0 my ($maxrow,$maxcol) = $this->exactdim();
2119 0 $this->{rows} = $maxrow;
2120 0 $this->{columns} = $maxcol;
2121 0 return $this;
2122 }
2123  
2124  
2125 2 ▶sub _sizetofit {
Subroutine _sizetofit: called CC=1 SCAR=0.0
2126 2 my ($matrix) = @_;
2127 2 my ($maxrow,$maxcol) = $matrix->exactdim();
2128 2 $matrix->{rows} = $maxrow;
2129 2 $matrix->{columns} = $maxcol;
2130 2 return $matrix;
2131 }
2132  
2133  
2134 0 ▶sub clearspecials {
Subroutine clearspecials: not called CC=1 SCAR=6.9
Pod undocumented
2135 0 my ($matrix) = @_;
2136 0 $matrix->{special}->{pattern} = 0;
2137 0 $matrix->{special}->{sign} = "";
2138 0 $matrix->{special}->{structure} = "";
2139 0 $matrix->{special}->{shape} = "";
2140 }
2141  
2142 ### SOLVER METHODS
2143  
2144 1 ▶sub gaussseidel {
Subroutine gaussseidel: called CC=1 SCAR=0.0
Pod undocumented
2145 #solves Ax=b by Gauss-Seidel, or SOR with relaxation parameter 1.
2146 #b ($constant) and x0 ($guess) should be column vectors.
2147 1 my ($matrix, $constant, $guess,$tol, $steps) = @_;
2148 1 return $matrix->SOR($constant,$guess,1,$tol,$steps);
2149 }
2150  
2151 2 ▶sub SOR {
Subroutine SOR: called CC=12 SCAR=25.8
Pod undocumented
2152 #solves Ax=b by Symmetric Over-Relaxation
2153 #b ($constant) and x0 ($guess) should be column vectors.
2154 2 my ($matrix, $constant, $guess, $omega,$tol, $steps) = @_;
2155 2 my $diag = $matrix->diagpart();
2156 2 my $lower = $matrix->lowerpart();
2157 2 my $upper = $matrix->upperpart();
2158 2 my $iterator;
2159 2 my $dinv = $diag->terminvert();
2160 2 my $soln = $guess->copy();
2161 2 foreach my $key (keys %{$constant->{data}}) {
2162 20 ▶ next if defined $soln->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $soln->{"data"}{$key} 20 0 20
2163 0 $soln->{data}{$key} = 0;
2164 }
2165 2 my @lowerkeys = $lower->sortbyrow();
2166 2 my @upperkeys = $upper->sortbyrow();
2167 2 my @solnkeys = $soln->sortbyrow();
2168 2 ▶ $steps = 100 unless $steps;
Branch: 1 branch
BranchTrueFalseTotal
unless $steps 0 2 2
2169 2 for my $k (1 .. $steps) {
2170 58 my $oldsoln = $soln->copy();
2171 58 foreach my $thissolnkey (keys %{$soln->{data}}) {
2172 1856 my ($i,$j) = &splitkey($thissolnkey);
2173 1856 ▶ next unless $j == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $j == 1 0 1856 1856
2174 1856 my $prev = $oldsoln->{data}{$thissolnkey}*(1.0-$omega);
2175 1856 my $scal = $omega * $dinv->element($i,$i);
2176 1856 my $lowersum=0;
2177 1856 my $uppersum = 0;
2178 1856 foreach my $lowerkey (@lowerkeys) {
2179 0 my ($li,$lj) = &splitkey($lowerkey);
2180 #unnecessary b/c of source of @lowerkeys
2181 0 ▶ next if $lj > ($i-1);
Branch: 1 branch
BranchTrueFalseTotal
if $lj > $i - 1 0 0 0
2182 # print "L $li $lj\n";
2183 0 $lowersum += $soln->element($lj,1)*$lower->{data}{$lowerkey};
2184 }
2185 1856 foreach my $upperkey (@upperkeys) {
2186 90944 my ($ui,$uj) = &splitkey($upperkey);
2187 #unnecessary b/c of source of @upperkeys
2188 90944 ▶ next if $uj < ($i+1);
Branch: 1 branch
BranchTrueFalseTotal
if $uj < $i + 1 36366 54578 90944
2189 54578 $uppersum += $soln->element($uj,1)*$upper->{data}{$upperkey};
2190 }
2191 1856 my $update = $prev+$scal*($constant->element($i,1)-$lowersum-$uppersum);
2192 1856 $soln->assign($i,1,$update);
2193 }
2194 58 my $err = &abssum($oldsoln-$soln);
2195 58 ▶ return $soln if $err<$tol;
Branch: 1 branch
BranchTrueFalseTotal
if $err < $tol 2 56 58
2196 }
2197 0 return undef;
2198 }
2199  
2200  
2201 1 ▶sub jacobi {
Subroutine jacobi: called CC=4 SCAR=13.9
Pod undocumented
2202 #solves Ax=b by Jacobi iteration.
2203 #Note that b doesn't have to be a column vector
2204 #$guess (x0) should be the same size as $constant (b)
2205 1 my ($matrix, $constant, $guess,$tol, $steps) = @_;
2206 1 my $diag = $matrix->diagpart();
2207 1 my $lower = $matrix->lowerpart();
2208 1 my $upper = $matrix->upperpart();
2209 1 my $iterator;
2210 1 my $dinv = $diag->terminvert();
2211 1 my $nondiag = ($lower+$upper);
2212 1 my $soln = $guess->copy();
2213 1 my $oldsoln;
2214 1 ▶ $steps = 100 unless $steps;
Branch: 1 branch
BranchTrueFalseTotal
unless $steps 0 1 1
2215 1 for my $i (1 .. $steps) {
2216 9 $oldsoln=$soln->copy();;
2217 9 $soln = $dinv*$constant - $dinv*$nondiag*$soln;
2218 9 my $err = &abssum($oldsoln-$soln);
2219 9 ▶ return $soln if $err<$tol;
Branch: 1 branch
BranchTrueFalseTotal
if $err < $tol 1 8 9
2220 }
2221 0 return undef;
2222 }
2223  
2224  
2225 ### SORTING METHODS
2226  
2227 #note: column is a secondary sort criterion
2228 # 1 2 3
2229 # 4 5 6
2230 # 7 8 9
2231 49 ▶sub sortbyrow {
Subroutine sortbyrow: called CC=2 SCAR=7.0
Pod undocumented
2232 49 my ($matrix) = @_;
2233 9652 my @sorted = map { $_->[0] }
2234 83793 ▶ sort { ($a->[1] <=> $b->[1])||($a->[2]<=>$b->[2]) }
Condition66%2/3
$Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] || $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
l!l&&r!l&&!r
56591272020
MC/DC0%0/2
$Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] || $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
$Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
Truth table66%2/3
A || B
A= $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]B= $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
ABresult
1X1
011
000
2235 49 map { [ $_, &splitkey($_) ] } keys %{$matrix->{data}};
2236 49 return @sorted;
2237 }
2238  
2239 #note: row is a secondary sort criterion
2240 # 1 4 7
2241 # 2 5 8
2242 # 3 6 9
2243 43 ▶sub sortbycolumn {
Subroutine sortbycolumn: called CC=2 SCAR=7.0
Pod undocumented
2244 43 my ($matrix) = @_;
2245 10491 my @sorted = map { $_->[0] }
2246 91768 ▶ sort { ($a->[2] <=> $b->[2])||($a->[1]<=>$b->[1]) }
Condition66%2/3
$Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2] || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
l!l&&r!l&&!r
62162296060
MC/DC0%0/2
$Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2] || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
$Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2] $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
Truth table66%2/3
A || B
A= $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]B= $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
ABresult
1X1
011
000
2247 43 map { [ $_, &splitkey($_) ] } keys %{$matrix->{data}};
2248
2249 43 return @sorted;
2250 }
2251  
2252 #note: row is a secondary sort criterion
2253 #lower diagonals are sorted in front of higher ones
2254 #4 7 9
2255 #2 5 8
2256 #1 3 6
2257 0 ▶sub sortbydiagonal {
Subroutine sortbydiagonal: not called CC=2 SCAR=17.9
Pod undocumented
2258 0 my ($matrix) = @_;
2259 0 my @sorted = map { $_->[0] }
2260 0 ▶ sort { (($a->[1]-$a->[2]) <=> ($b->[1]-$b->[2]))||($a->[1]<=>$b->[1]) }
Condition0%0/3
$Math::MatrixSparse::a->[1] - $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[1] - $Math::MatrixSparse::b->[2] || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Math::MatrixSparse::a->[1] - $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[1] - $Math::MatrixSparse::b->[2] || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
$Math::MatrixSparse::a->[1] - $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[1] - $Math::MatrixSparse::b->[2] $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
Truth table0%0/3
A || B
A= $Math::MatrixSparse::a->[1] - $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[1] - $Math::MatrixSparse::b->[2]B= $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
ABresult
1X1
011
000
2261 0 map { [ $_, &splitkey($_) ] } keys %{$matrix->{data}};
2262
2263 0 return @sorted;
2264 }
2265  
2266 #sorts the elements of $matrix by value, smallest first.
2267 #row is a secondary criterion, and column is tertiaty.
2268 0 ▶sub sortbyvalue {
Subroutine sortbyvalue: not called CC=3 SCAR=24.8
Pod undocumented
2269 0 my ($matrix) = @_;
2270 0 my @sorted = map { $_->[0] }
2271 sort {
2272 0 ▶ ($matrix->elementkey($a->[0]) <=> $matrix->elementkey($a->[0]))
Condition0%0/6
$matrix->elementkey($Math::MatrixSparse::a->[0]) <=> $matrix->elementkey($Math::MatrixSparse::a->[0]) || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] || $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
l!l&&r!l&&!r
000
$matrix->elementkey($Math::MatrixSparse::a->[0]) <=> $matrix->elementkey($Math::MatrixSparse::a->[0]) or $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$matrix->elementkey($Math::MatrixSparse::a->[0]) <=> $matrix->elementkey($Math::MatrixSparse::a->[0]) || $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] || $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
$matrix->elementkey($Math::MatrixSparse::a->[0]) <=> $matrix->elementkey($Math::MatrixSparse::a->[0]) $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1] $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
Truth table0%0/4
A or B || C
A= $matrix->elementkey($Math::MatrixSparse::a->[0]) <=> $matrix->elementkey($Math::MatrixSparse::a->[0])B= $Math::MatrixSparse::a->[1] <=> $Math::MatrixSparse::b->[1]C= $Math::MatrixSparse::a->[2] <=> $Math::MatrixSparse::b->[2]
ABCresult
1XX1
01X1
0011
0000
2273 || ($a->[1]<=>$b->[1])
2274 ||($a->[2]<=>$b->[2])
2275 }
2276 0 map { [ $_, &splitkey($_) ] } keys %{$matrix->{data}};
2277
2278 0 return @sorted;
2279 }
2280  
2281  
2282 ### SYMMETRIC METHODS
2283 1 ▶sub symmetrify {
Subroutine symmetrify: called CC=2 SCAR=7.0
Pod undocumented
2284 #takes a matrix, returns the matrix obtained by reflecting
2285 #non-lower part around main diagonal
2286 1 my ($matrix) = @_;
2287 1 ▶ return $matrix if $matrix->{special}->{shape} =~ /diagonal/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /diagonal/i 0 1 1
2288 1 my $this = $matrix->copy();
2289 1 $this->_symmetrify();
2290 1 return $this;
2291 }
2292  
2293  
2294 2 ▶sub _symmetrify {
Subroutine _symmetrify: called CC=4 SCAR=13.9
2295 #takes a matrix, reflects it about main diagonal
2296 2 my ($matrix) = @_;
2297 2 ▶ return $matrix if $matrix->{special}->{shape} =~ /diagonal/i;
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->{"special"}{"shape"} =~ /diagonal/i 0 2 2
2298 2 $matrix->_nonlowerpart();
2299 2 foreach my $key (keys %{$matrix->{data}}) {
2300 696 my ($i,$j) = &splitkey($key);
2301 696 ▶ unless ($i==$j) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i == $j) 275 421 696
2302 275 my $transkey = &makekey($j , $i);
2303 275 $matrix->assign($j,$i,$matrix->element($i,$j));
2304 }
2305 }
2306 2 $matrix->_sizetofit();
2307 2 $matrix->{special}->{shape} = "";
2308 2 $matrix->{special}->{structure} = "symmetric";
2309 2 return $matrix;
2310 }
2311  
2312 1 ▶sub skewsymmetrify {
Subroutine skewsymmetrify: called CC=1 SCAR=0.0
Pod undocumented
2313 #takes a matrix, returns the matrix obtained by reflecting
2314 #upper part around main diagonal
2315 1 my ($matrix) = @_;
2316 1 my $this = $matrix->copy();
2317 1 return $this->_skewsymmetrify();
2318 }
2319  
2320 1 ▶sub _skewsymmetrify {
Subroutine _skewsymmetrify: called CC=2 SCAR=6.9
2321 #takes a matrix, reflects upper part about main diagonal
2322 1 my ($matrix) = @_;
2323 1 $matrix->_upperpart();
2324 1 foreach my $key (keys %{$matrix->{data}}) {
2325 275 my ($i,$j) = &splitkey($key);
2326 275 my $transkey = &makekey($j , $i);
2327 275 $matrix->assign($j,$i,-1*$matrix->element($i,$j));
2328 }
2329 1 $matrix->{special}->{shape} = "";
2330 1 $matrix->{special}->{structure} = "skewsymmetric";
2331 1 return $matrix;
2332 }
2333  
2334  
2335 ### PROBABILISTIC METHODS
2336 0 ▶sub normalize {
Subroutine normalize: not called CC=6 SCAR=37.4
Pod undocumented
2337 0 my ($matrix) = @_;
2338 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2339 0 my $name = $matrix->{name};
2340 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2341 0 carp "Math::MatrixSparse::normalize matrix has negative elements";
2342 0 return undef;
2343 }
2344 0 my $matsum = 0;
2345 0 foreach my $key (keys %{$matrix->{data}}) {
2346 0 $matsum += $matrix->elementkey($key);
2347 }
2348 0 ▶ $matrix= $matrix->multiplyscalar(1.0/$matsum) if $matsum;
Branch: 1 branch
BranchTrueFalseTotal
if $matsum 0 0 0
2349 0 ▶ $matrix->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2350 0 return $matrix;
2351 }
2352  
2353 0 ▶sub _normalize {
Subroutine _normalize: not called CC=6 SCAR=37.4
2354
2355 0 my ($matrix) = @_;
2356 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2357 0 my $name = $matrix->{name};
2358 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2359 0 carp "Math::MatrixSparse::normalize matrix has negative elements";
2360 0 return undef;
2361 }
2362 0 my $matsum = 0;
2363 0 foreach my $key (keys %{$matrix->{data}}) {
2364 0 $matsum += $matrix->elementkey($key);
2365 }
2366 0 ▶ $matrix= $matrix->_multiplyscalar(1.0/$matsum) if $matsum;
Branch: 1 branch
BranchTrueFalseTotal
if $matsum 0 0 0
2367 0 ▶ $matrix->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2368 0 return $matrix;
2369 }
2370  
2371  
2372 0 ▶sub normalizerows {
Subroutine normalizerows: not called CC=7 SCAR=40.3
Pod undocumented
2373 0 my ($matrix) = @_;
2374 0 my %rowsums;
2375 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2376 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2377 0 carp "Math::MatrixSparse::normalizerows matrix has negative elements";
2378 0 return undef;
2379 }
2380 0 foreach my $key (keys %{$matrix->{data}}) {
2381 0 my ($i,$j) = &splitkey($key);
2382 0 $rowsums{$i}+= $matrix->elementkey($key);
2383 }
2384 0 my $rownormed = $matrix->cull();
2385 0 foreach my $key (keys %{$rownormed->{data}}) {
2386 0 my ($i,$j) = &splitkey($key);
2387 0 ▶ next unless $rowsums{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless $rowsums{$i} 0 0 0
2388 0 $rownormed->assign($i,$j,($rownormed->elementkey($key))/$rowsums{$i});
2389 }
2390 0 my $name = $matrix->{name};
2391 0 ▶ $rownormed->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2392 0 return $rownormed;
2393 }
2394  
2395  
2396  
2397 0 ▶sub _normalizerows {
Subroutine _normalizerows: not called CC=7 SCAR=40.3
2398 0 my ($matrix) = @_;
2399 0 my %rowsums;
2400 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2401 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2402 0 carp "Math::MatrixSparse::normalizerows matrix has negative elements";
2403 0 return undef;
2404 }
2405 0 foreach my $key (keys %{$matrix->{data}}) {
2406 0 my ($i,$j) = &splitkey($key);
2407 0 $rowsums{$i}+= $matrix->{data}{$key};
2408 }
2409 0 $matrix->_cull();
2410 0 foreach my $key (keys %{$matrix->{data}}) {
2411 0 my ($i,$j) = &splitkey($key);
2412 0 ▶ next unless $rowsums{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless $rowsums{$i} 0 0 0
2413 0 $matrix->assign($i,$j,($matrix->elementkey($key))/$rowsums{$i});
2414 }
2415 0 my $name = $matrix->{name};
2416 0 ▶ $matrix->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2417 0 return $matrix;
2418 }
2419  
2420 0 ▶sub normalizecolumns {
Subroutine normalizecolumns: not called CC=6 SCAR=37.4
Pod undocumented
2421 0 my ($matrix) = @_;
2422 0 my %columnsums;
2423 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2424 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2425 0 carp "Math::MatrixSparse::normalizecolumns matrix has negative elements";
2426 0 return undef;
2427 }
2428 0 foreach my $key (keys %{$matrix->{data}}) {
2429 0 my ($i,$j) = &splitkey($key);
2430 0 $columnsums{$j}+= $matrix->elementkey($key);
2431 }
2432 0 my $columnnormed = $matrix->copy();
2433 0 foreach my $key (keys %{$columnnormed->{data}}) {
2434 0 my ($i,$j) = &splitkey($key);
2435 0 $columnnormed->assign($i,$j,$columnnormed->elementkey($key)/$columnsums{$j});
2436 }
2437 0 my $name = $matrix->{name};
2438 0 ▶ $columnnormed->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2439 0 return $columnnormed;
2440 }
2441  
2442  
2443 0 ▶sub _normalizecolumns {
Subroutine _normalizecolumns: not called CC=6 SCAR=37.4
2444 0 my ($matrix) = @_;
2445 0 my %columnsums;
2446 0 ▶ return undef unless defined $matrix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 0 0
2447 0 ▶ unless ($matrix->is_nonnegative()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix->is_nonnegative) 0 0 0
2448 0 carp "Math::MatrixSparse::normalizecolumns matrix has negative elements";
2449 0 return undef;
2450 }
2451 0 foreach my $key (keys %{$matrix->{data}}) {
2452 0 my ($i,$j) = &splitkey($key);
2453 0 $columnsums{$j}+= $matrix->elementkey($key);
2454 }
2455 0 $matrix->_cull();
2456 0 foreach my $key (keys %{$matrix->{data}}) {
2457 0 my ($i,$j) = &splitkey($key);
2458 0 $matrix->assign($i,$j,$matrix->elementkey($key)/$columnsums{$j});
2459 }
2460 0 my $name = $matrix->{name};
2461 0 ▶ $matrix->name($name . "/||" . $name . "||") if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
2462 0 return $matrix;
2463 }
2464  
2465  
2466 0 ▶sub discretepdf {
Subroutine discretepdf: not called CC=3 SCAR=24.8
Pod undocumented
2467 0 my ($matrix) = @_;
2468 0 my $uniform = rand;
2469  
2470 0 my $current;
2471 0 my $curpos = 0;
2472 0 foreach my $key (keys %{$matrix->{data}}) {
2473 0 ▶ last if $curpos >=$uniform;
Branch: 1 branch
BranchTrueFalseTotal
if $curpos >= $uniform 0 0 0
2474 0 $current = $key;
2475 0 $curpos += $matrix->{data}{$key};
2476 }
2477 0 return $current;
2478  
2479 }
2480  
2481 ### KEY-INTERFACE METHODS
2482  
2483 415257 ▶sub splitkey {
Subroutine splitkey: called CC=1 SCAR=0.0
Pod undocumented
2484 415257 my ($key) = @_;
2485 415257 my ($i,$j) = split(/\0/,$key);
2486 415257 return ($i,$j);
2487 }
2488  
2489 327333 ▶sub makekey {
Subroutine makekey: called CC=3 SCAR=13.1
Pod undocumented
2490 327333 my ($i,$j) = @_;
2491 327333 ▶ return ($i . "\0" . $j) if ((defined $i)&&(defined $j));
Branch: 1 branch
BranchTrueFalseTotal
if defined $i and defined $j 327333 0 327333
Condition33%1/3
defined $i and defined $j
!ll&&!rl&&r
00327333
MC/DC0%0/2
defined $i and defined $j
defined $i defined $j
Truth table33%1/3
A and B
A= defined $iB= defined $j
ABresult
0X0
100
111
2492  
2493 }
2494  
2495  
2496 7 1;
2497 __END__
↑ Summary