blib/lib/Algorithm/RandomMatrixGeneration.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 87.5% 63 / 72 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 89.7% 184 / 205 CC 38 SCAR 35.9
CC
38
Cov
90%
generateMatrix
39.9
BEGIN
1.0
BEGIN
1.0
CRAP 36.2
↑ Summary
1 package Algorithm::RandomMatrixGeneration;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 require Exporter;
9  
10 4 our @ISA = qw(Exporter);
11  
12 4 our @EXPORT = qw( generateMatrix );
13  
14 4 our $VERSION = '0.06';
15  
16 # add check for type of marginal values
17 # modify output format to sparse.
18  
19 sub generateMatrix
20 4 ▶{
Subroutine generateMatrix: called CC=38 SCAR=36.9
Pod undocumented
21 4 my $ref_rmar = shift;
22 4 my $ref_cmar = shift;
23  
24 4 my $precision = shift;
25 4 my $seed = shift;
26  
27 4 my @tmp_rmar = @$ref_rmar;
28 4 my @tmp_cmar = @$ref_cmar;
29  
30 4 my $n = $#tmp_rmar;
31 4 my $m = $#tmp_cmar;
32  
33 # error checks
34 4 ▶ if(!$n)
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 4 4
35 {
36 0 print STDERR "No row marginals provided.\n";
37 0 exit 1;
38 }
39  
40 4 ▶ if(!$m)
Branch: 1 branch
BranchTrueFalseTotal
unless ($m) 0 4 4
41 {
42 0 print STDERR "No column marginals provided.\n";
43 0 exit 1;
44 }
45  
46 4 ▶ if(!$precision)
Branch: 1 branch
BranchTrueFalseTotal
unless ($precision) 0 4 4
47 {
48 0 print STDERR "Precision not provided.\n";
49 0 exit 1;
50 }
51  
52 4 ▶ if(defined $seed)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed) 4 0 4
53 {
54 4 srand($seed);
55 }
56  
57 # find the type of marginals values: integers/real
58 # assume integer and loop through row and col marginals.
59 # check each value for decimal values.
60 # break on first occurrence of real number.
61 4 my $format = "integer";
62  
63 # also find if any of the marginal is negative
64 # assume positive and then loop through to find a contradiction
65 4 my $signValues = "positive";
66  
67 # for each row (0..n)
68 4 ▶ for(my $i=0; $i<=$n; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 16 3 19
69 {
70 16 ▶ if($tmp_rmar[$i] =~ /\.0*[1-9]+/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_rmar[$i] =~ /\.0*[1-9]+/) 2 14 16
71 {
72 2 $format = "real";
73 2 ▶ if($signValues eq "negative")
Branch: 1 branch
BranchTrueFalseTotal
if ($signValues eq "negative") 0 2 2
74 {
75 0 last;
76 }
77 }
78 16 ▶ if($tmp_rmar[$i] =~ /^-/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_rmar[$i] =~ /^-/) 3 13 16
79 {
80 3 $signValues = "negative";
81 3 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "real") 1 2 3
82 {
83 1 last;
84 }
85 }
86 }
87  
88 4 ▶ if($format eq "integer" || $signValues eq "positive")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "integer" or $signValues eq "positive") 3 1 4
Condition100%3/3
$format eq "integer" or $signValues eq "positive"
l!l&&r!l&&!r
211
MC/DC100%2/2
$format eq "integer" or $signValues eq "positive"
$format eq "integer" $signValues eq "positive"
Truth table100%3/3
A or B
A= $format eq "integer"B= $signValues eq "positive"
ABresult
1X1
011
000
89 {
90 # for each col (0..m)
91 3 ▶ for(my $j=0; $j<=$m; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $m) 12 3 15
92 {
93 12 ▶ if($tmp_cmar[$j] =~ /\.0*[1-9]+/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_cmar[$j] =~ /\.0*[1-9]+/) 2 10 12
94 {
95 2 $format = "real";
96 2 ▶ if($signValues eq "negative")
Branch: 1 branch
BranchTrueFalseTotal
if ($signValues eq "negative") 0 2 2
97 {
98 0 last;
99 }
100 }
101 12 ▶ if($tmp_cmar[$j] =~ /^-/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_cmar[$j] =~ /^-/) 2 10 12
102 {
103 2 $signValues = "negative";
104 2 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "real") 0 2 2
105 {
106 0 last;
107 }
108 }
109 }
110 }
111  
112 4 my $fmt_str;
113 my $add_str;
114 4 my $regex;
115  
116 4 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "real") 2 2 4
117 {
118 2 ▶ if($precision !~ /^[0-9]+$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($precision =~ /^[0-9]+$/) 0 2 2
119 {
120 0 print STDERR "Please specify integer value for precision.\n";
121 0 exit 1;
122 }
123  
124 # for precision
125 2 $fmt_str = "%.$precision" . "f";
126
127 2 $add_str = "0.";
128 2 ▶ for(my $i=1; $i<$precision; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $precision) 6 2 8
129 {
130 6 $add_str .= "0";
131 }
132 2 $add_str .= "1";
133
134 2 $regex = "(\\d+\\.?\\d{0,$precision})";
135 }
136  
137 # array to hold the generated matrix
138 4 my @ref = ();
139  
140 4 my $rem_col_marg = 0;
141
142 # for each cell C(i,j)
143 # for each row (0..n)
144 4 ▶ for(my $i=0; $i<=$n; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 25 4 29
145 {
146 # for each col (0..m)
147 25 ▶ for(my $j=0; $j<=$m; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $m) 140 25 165
148 {
149 # compute the min and max (range) for the cell value
150
151 # max = MIN(row_marg[i], col_marg[j])
152 140 my $max = $tmp_rmar[$i];
153 140 ▶ if($tmp_cmar[$j] < $max)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_cmar[$j] < $max) 59 81 140
154 {
155 59 $max = $tmp_cmar[$j];
156 }
157
158 # if max = 0 then min = 0
159 # else assign min a value based on the remaining row_marginal and col_marginals to be satisfied
160 140 my $min = 0;
161 140 ▶ if($max != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($max != 0) { } 133 7 140
162 {
163 # sum-up the col_marginals for all the columns beyond the current column
164 133 $rem_col_marg = 0;
165 133 ▶ for(my $k=$j+1; $k<=$m; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $m) 365 133 498
166 {
167 365 $rem_col_marg = $rem_col_marg + $tmp_cmar[$k];
168 }
169
170 # based on the row_marg and the sum_of_col_marg decide the value for min
171 133 $min = $tmp_rmar[$i] - $rem_col_marg;
172  
173 133 ▶ if($signValues eq "positive")
Branch: 1 branch
BranchTrueFalseTotal
if ($signValues eq "positive") 43 90 133
174 {
175 43 ▶ if($min < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 0) 19 24 43
176 {
177 19 $min = 0;
178 }
179 }
180 }
181 else
182 {
183 7 $min = $max;
184 }
185
186 140 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "real") 104 36 140
187 {
188 104 $min = sprintf($fmt_str, $min);
189 104 $max = sprintf($fmt_str, $max);
190 }
191  
192 140 ▶ if($signValues eq "negative")
Branch: 1 branch
BranchTrueFalseTotal
if ($signValues eq "negative") 92 48 140
193 {
194 92 ▶ if($min > $max)
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $max) 88 4 92
195 {
196 88 my $tmp_min = $min;
197 88 $min = $max;
198 88 $max = $tmp_min;
199 }
200 }
201  
202 140 my $rand_num = 0;
203 140 ▶ if($min != $max)
Branch: 1 branch
BranchTrueFalseTotal
if ($min != $max) { } 116 24 140
204 {
205 # generate a random number between the min and max (range)
206 116 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'real') { } 94 22 116
207 {
208 94 my $rand_max = $max-$min+$add_str;
209 94 $rand_num = rand($rand_max);
210  
211 94 my $bigfloat = Math::BigFloat->new($rand_num);
212 94 my $rand_num_str = $bigfloat->bstr();
213
214 94 $rand_num_str =~/$regex/;
215 94 $rand_num = $1;
216 }
217 else
218 {
219 22 my $rand_max = $max-$min+1;
220 22 $rand_num = int(rand($rand_max));
221 }
222  
223 116 $rand_num += $min;
224 }
225 else
226 {
227 24 $rand_num = $min;
228 }
229
230 140 ▶ if($signValues eq "negative")
Branch: 1 branch
BranchTrueFalseTotal
if ($signValues eq "negative") 92 48 140
231 {
232 #last col of the row
233 92 ▶ if($j == $m)
Branch: 1 branch
BranchTrueFalseTotal
if ($j == $m) 13 79 92
234 {
235 13 $rand_num = $tmp_rmar[$i];
236 }
237
238 #last row of the cols
239 92 ▶ if($i == $n)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $n) 12 80 92
240 {
241 12 $rand_num = $tmp_cmar[$j];
242 }
243 }
244  
245 140 ▶ if($format eq "real")
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'real') { } 104 36 140
246 {
247 104 my $val = sprintf($fmt_str,$rand_num);
248  
249 104 ▶ if($val !~ /^0\.?0*$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^0\.?0*$/) 104 0 104
250 {
251 104 $ref[$i][$j] = $val;
252  
253 # adjust the marginals
254 104 $tmp_rmar[$i] = sprintf($fmt_str, $tmp_rmar[$i] - $ref[$i][$j]);
255 104 $tmp_cmar[$j] = sprintf($fmt_str, $tmp_cmar[$j] - $ref[$i][$j]);
256 }
257
258 }
259 else
260 {
261 36 ▶ if($rand_num)
Branch: 1 branch
BranchTrueFalseTotal
if ($rand_num) 26 10 36
262 {
263 26 $ref[$i][$j] = $rand_num;
264
265 # adjust the marginals
266 26 $tmp_rmar[$i] = $tmp_rmar[$i] - $ref[$i][$j];
267 26 $tmp_cmar[$j] = $tmp_cmar[$j] - $ref[$i][$j];
268 }
269 }
270 }
271 }
272  
273 4 return @ref;
274 }
275  
276 4 1;
277 __END__
↑ Summary