blib/lib/Math/Matrix/MaybeGSL.pm

filter: Statementstmt 58.8% 103 / 175 Branchbran 39.2% 22 / 56 Conditioncond 42.1% 8 / 19 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 65.2% 30 / 46 Podpod 100% 2 / 2 totaltotal 53.8% 167 / 310 CC 50 SCAR 24.0
CC
50
Cov
53%
_gsl_new_from_cols
42.0
_gsl_new_from_rows
42.0
_gsl_read
30.0
CRAP 11.0
↑ Summary
1 # ABSTRACT: Uniform use of Math::MatrixReal and Math::GSL::Matrix.
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Math::Matrix::MaybeGSL;
7 1 $Math::Matrix::MaybeGSL::VERSION = '0.007';
8 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our @EXPORT = qw{Matrix};
10  
11 use overload
12 1 '*=' => '_assign_multiply',
13 '*' => '_multiply',
14 1 ▶ 'fallback' => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub _choose_matrix_module {
17 1 ▶ return 'Math::GSL::Matrix' if $INC{'Math/GSL/Matrix.pm'};
Subroutine _choose_matrix_module: called CC=5 SCAR=18.5
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Math/GSL/Matrix.pm"} 0 1 1
18 1 ▶ return 'Math::MatrixReal' if $INC{'Math/MatrixReal.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Math/MatrixReal.pm"} 0 1 1
19  
20 1 my @err;
21  
22 1 ▶ return 'Math::GSL::Matrix' if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Math::GSL; require Math::GSL::Matrix; 1 } } 0 1 1
23 1 require Math::GSL;
24 0 require Math::GSL::Matrix;
25 0 1;
26 };
27 1 push @err, "Error loading Math::GSL::Matrix: $@";
28  
29 1 ▶ return 'Math::MatrixReal' if eval { require Math::MatrixReal; 1; };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Math::MatrixReal; 1 } } 1 0 1
30 0 push @err, "Error loading Math::MatrixReal: $@";
31  
32 0 die join( "\n", "Couldn't load a Matrix module:", @err );
33 }
34  
35 5 ▶sub Matrix { __PACKAGE__ }
Subroutine Matrix: called CC=1 SCAR=0.0
Pod documented
36  
37 sub _call {
38 51 ▶ my ($method, $obj, @args) = @_;
Subroutine _call: called CC=1 SCAR=0.0
39 51 $obj->{matrix}->$method(@args);
40 }
41  
42 sub isGSL {
43 0 ▶ our $matrix_module;
Subroutine isGSL: not called CC=1 SCAR=6.9
Pod documented
44 0 return $matrix_module eq "Math::GSL::Matrix";
45 }
46  
47 BEGIN {
48 1 ▶ our $matrix_module = _choose_matrix_module();
Subroutine BEGIN: called CC=3 SCAR=13.1
49 my %functions
50 = (
51 'any' => {
52 new => sub {
53 1 ▶ my (undef, $rows, $cols) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 return _new( $matrix_module->new($rows, $cols) );
55 },
56 1 ▶ dim => sub { _call(dim => @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 ▶ each => sub { _new(_call(each => @_)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶ as_list => sub { _call(as_list => @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 ▶ det => sub { _call(det => @_)} ,
Subroutine __ANON__: called CC=1 SCAR=0.0
60 },
61 'Math::GSL::Matrix' => {
62 0 ▶ assign => sub { _call(set_elem => ($_[0], $_[1]-1, $_[2]-1, $_[3])); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 ▶ element => sub { _call(get_elem => ($_[0], $_[1]-1, $_[2]-1, $_[3])); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ new_from_cols => sub { _new(_gsl_new_from_cols($_[1])) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 ▶ new_from_rows => sub { _new(_gsl_new_from_rows($_[1])) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ vconcat => sub { _new(_call(vconcat => $_[0], $_[1]{matrix})) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ hconcat => sub { _new(_call(hconcat => $_[0], $_[1]{matrix})) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 ▶ write => sub { _gsl_write(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 ▶ read => sub { _gsl_read($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 max => sub {
71 0 ▶ if (wantarray) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
72 0 my ($v, @pos) = _call(max => @_);
73 0 return ($v, map { $_ + 1 } @pos);
74 } else {
75 0 return scalar(_call(max => @_));
76 };
77 },
78 min => sub {
79 0 ▶ if (wantarray) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
80 0 my ($v, @pos) = _call(min => @_);
81 0 return ($v, map { $_ + 1 } @pos);
82 } else {
83 0 return scalar(_call(min => @_));
84 };
85 },
86 },
87 'Math::MatrixReal' => {
88 1 ▶ assign => sub { _call(assign => @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 46 ▶ element => sub { _call(element => @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 2 ▶ new_from_cols => sub { _new( $matrix_module->new_from_cols($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 ▶ new_from_rows => sub { _new( $matrix_module->new_from_rows($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 1 ▶ vconcat => sub { _new( ~((~$_[0]{matrix}) . (~$_[1]{matrix})) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 1 ▶ hconcat => sub { _new( $_[0]{matrix} . $_[1]{matrix} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 ▶ write => sub { _mreal_write(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 1 ▶ read => sub { _mreal_read($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 ▶ max => sub { _mreal_max($_[0]{matrix}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
97 1 ▶ min => sub { _mreal_min($_[0]{matrix}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 },
99 1 );
100  
101 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 1 for my $func (keys %{$functions{$matrix_module}}) {
104 # Use Sub::Install later?
105 10 $_ = __PACKAGE__ . "::$func";
106 10 *$_ = $functions{$matrix_module}{$func};
107 }
108 1 for my $func (keys %{$functions{any}}) {
109 # Use Sub::Install later?
110 5 $_ = __PACKAGE__ . "::$func";
111 5 *$_ = $functions{any}{$func};
112 }
113  
114 }
115  
116 sub _mreal_max {
117 1 ▶ my $matrix = shift;
Subroutine _mreal_max: called CC=8 SCAR=22.1
118 1 my ($rs, $cs) = $matrix->dim();
119 1 ▶ return $matrix->max() if ($rs == 1 || $cs == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $rs == 1 or $cs == 1 0 1 1
Condition33%1/3
$rs == 1 or $cs == 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$rs == 1 or $cs == 1
$rs == 1 $cs == 1
Truth table33%1/3
A or B
A= $rs == 1B= $cs == 1
ABresult
1X1
011
000
120  
121 1 my ($m, $r, $c, $v) = ($matrix->[0], 1, 1, undef);
122  
123 1 for my $i (1..$rs) {
124 2 for my $j (1..$cs) {
125 4 ▶ if (!$v || $v < $m->[$i-1][$j-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $v or $v < $$m[$i - 1][$j - 1]) 4 0 4
Condition66%2/3
not $v or $v < $$m[$i - 1][$j - 1]
l!l&&r!l&&!r
130
MC/DC0%0/2
not $v or $v < $$m[$i - 1][$j - 1]
not $v $v < $$m[$i - 1][$j - 1]
Truth table66%2/3
not(A) or B
A= not $vB= $v < $$m[$i - 1][$j - 1]
ABresult
1X1
011
000
126 4 $r = $i;
127 4 $c = $j;
128 4 $v = $m->[$i-1][$j-1];
129 }
130 }
131 }
132  
133 1 ▶ return wantarray ? ($v, $r, $c) : $v;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
134 }
135  
136 sub _mreal_min {
137 1 ▶ my $matrix = shift;
Subroutine _mreal_min: called CC=8 SCAR=21.6
138 1 my ($rs, $cs) = $matrix->dim();
139 1 ▶ return $matrix->min() if ($rs == 1 || $cs == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $rs == 1 or $cs == 1 0 1 1
Condition33%1/3
$rs == 1 or $cs == 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$rs == 1 or $cs == 1
$rs == 1 $cs == 1
Truth table33%1/3
A or B
A= $rs == 1B= $cs == 1
ABresult
1X1
011
000
140  
141 1 my ($m, $r, $c, $v) = ($matrix->[0], 1, 1, undef);
142  
143 1 for my $i (1..$rs) {
144 2 for my $j (1..$cs) {
145 4 ▶ if (!$v || $v > $m->[$i-1][$j-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $v or $v > $$m[$i - 1][$j - 1]) 1 3 4
Condition66%2/3
not $v or $v > $$m[$i - 1][$j - 1]
l!l&&r!l&&!r
103
MC/DC50%1/2
not $v or $v > $$m[$i - 1][$j - 1]
not $v $v > $$m[$i - 1][$j - 1]
Truth table66%2/3
not(A) or B
A= not $vB= $v > $$m[$i - 1][$j - 1]
ABresult
1X1
011
000
146 1 $r = $i;
147 1 $c = $j;
148 1 $v = $m->[$i-1][$j-1];
149 }
150 }
151 }
152  
153 1 ▶ return wantarray ? ($v, $r, $c) : $v;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
154 }
155  
156 sub _gsl_new_from_cols {
157 0 ▶ my $cols = shift;
Subroutine _gsl_new_from_cols: not called CC=6 SCAR=37.4
158  
159 0 my $nr_columns = scalar(@$cols);
160 0 my $nr_rows = 0;
161 0 for my $row (@$cols) {
162 0 ▶ $nr_rows = scalar(@$row) if @$row > $nr_rows;
Branch: 1 branch
BranchTrueFalseTotal
if @$row > $nr_rows 0 0 0
163 }
164 0 my $m = Math::GSL::Matrix->new($nr_rows, $nr_columns);
165 0 for my $r (0..$nr_rows - 1) {
166 0 for my $c (0..$nr_columns - 1) {
167 0 ▶ $m->set_elem($r, $c, $cols->[$c][$r] || 0);
Condition0%0/2
$cols->[$c][$r] || 0
l!l
00
MC/DC0%0/1
$cols->[$c][$r] || 0
$cols->[$c][$r]
Truth table0%0/2
A
A= $cols->[$c][$r]
Aresult
11
00
168 }
169 }
170 0 return $m;
171 }
172  
173 sub _gsl_new_from_rows {
174 0 ▶ my $rows = shift;
Subroutine _gsl_new_from_rows: not called CC=6 SCAR=37.4
175  
176 0 my $nr_rows = scalar(@$rows);
177 0 my $nr_columns = 0;
178 0 for my $col (@$rows) {
179 0 ▶ $nr_columns = scalar(@$col) if @$col > $nr_columns;
Branch: 1 branch
BranchTrueFalseTotal
if @$col > $nr_columns 0 0 0
180 }
181 0 my $m = Math::GSL::Matrix->new($nr_rows, $nr_columns);
182 0 for my $c (0..$nr_columns - 1) {
183 0 for my $r (0..$nr_rows - 1) {
184 0 ▶ $m->set_elem($r, $c, $rows->[$r][$c] || 0);
Condition0%0/2
$rows->[$r][$c] || 0
l!l
00
MC/DC0%0/1
$rows->[$r][$c] || 0
$rows->[$r][$c]
Truth table0%0/2
A
A= $rows->[$r][$c]
Aresult
11
00
185 }
186 }
187 0 return $m;
188 }
189  
190 sub _new {
191 11 ▶ my $mat = shift;
Subroutine _new: called CC=1 SCAR=0.0
192 11 return bless { matrix => $mat }, __PACKAGE__;
193 }
194  
195 sub _assign_multiply {
196 0 ▶ my($object,$argument) = @_;
Subroutine _assign_multiply: not called CC=1 SCAR=6.9
197  
198 0 return( &_multiply($object,$argument,undef) );
199 }
200  
201 sub _multiply {
202 3 ▶ my ($object, $argument, $flag) = @_;
Subroutine _multiply: called CC=5 SCAR=16.2
203  
204 3 ▶ $argument = $argument->{matrix} if ref $argument eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $argument eq __PACKAGE__ 1 2 3
205 3 ▶ $object = $object->{matrix} if ref $object eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $object eq __PACKAGE__ 3 0 3
206  
207 3 ▶ if ((defined $flag) && $flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 1 2 3
Condition66%2/3
defined $flag and $flag
!ll&&!rl&&r
021
MC/DC50%1/2
defined $flag and $flag
defined $flag $flag
Truth table66%2/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
208 1 return _new($argument * $object);
209 } else {
210 2 return _new($object * $argument);
211 }
212 }
213  
214 sub _mreal_write {
215 1 ▶ my ($m, $filename) = @_;
Subroutine _mreal_write: called CC=5 SCAR=16.1
216  
217 1 my $matrix = $m->{matrix};
218  
219 1 ▶ open my $fh, ">", $filename or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Could not create file '${filename}': $!" 0 1 1
220 die "Could not create file '$filename': $!";
221  
222 # probably faster than creating a full string in memory
223 1 my ($rows, $cols) = $matrix->dim();
224  
225 1 for my $r (0..$rows-1) {
226 2 for my $c (0..$cols-1) {
227 4 print $fh $matrix->[0][$r][$c];
228 4 ▶ print $fh "\t" unless $c == $cols-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $c == $cols - 1 2 2 4
229 }
230 2 print $fh "\n";
231 }
232 1 close $fh;
233 }
234  
235 sub _mreal_read {
236 1 ▶ my $filename = shift;
Subroutine _mreal_read: called CC=3 SCAR=11.0
237  
238 1 my $m = [];
239  
240 1 ▶ open my $fh, "<", $filename or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "could not open file '${filename}': $!" 0 1 1
241 die "could not open file '$filename': $!";
242  
243 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 1 3
244 2 chomp;
245 2 push @$m, [split /\s+/];
246 }
247  
248 1 return _new( Math::MatrixReal->new_from_rows($m) );
249 }
250  
251 sub _gsl_read {
252 0 ▶ my $filename = shift;
Subroutine _gsl_read: not called CC=5 SCAR=34.0
253  
254 0 ▶ die "$filename does not exist" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
255  
256 0 my $fh = Math::GSL::gsl_fopen($filename, "r");
257 0 ▶ die "error opening file $filename for reading" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
258  
259 0 my $dim = Math::GSL::Matrix::gsl_matrix_alloc(1, 2);
260 0 my $err = Math::GSL::Matrix::gsl_matrix_fread($fh, $dim);
261 0 ▶ die "error reading matrix" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
262  
263 0 my $m = Math::GSL::Matrix::gsl_matrix_alloc(
264 Math::GSL::Matrix::gsl_matrix_get($dim, 0, 0),
265 Math::GSL::Matrix::gsl_matrix_get($dim, 0, 1));
266 0 $err = Math::GSL::Matrix::gsl_matrix_fread($fh, $m);
267 0 ▶ die "error reading matrix" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
268  
269 0 Math::GSL::Matrix::gsl_matrix_free($dim);
270  
271 0 Math::GSL::gsl_fclose($fh);
272 0 _new( Math::GSL::Matrix->new($m) );
273 }
274  
275 sub _gsl_write {
276 0 ▶ my ($self, $filename) = @_;
Subroutine _gsl_write: not called CC=4 SCAR=30.0
277  
278 0 my $fh = Math::GSL::gsl_fopen($filename, "w");
279 0 ▶ die "error opening file: $filename" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
280  
281 # create a temporary matrix with the main matrix dimensions
282 0 my $dim = Math::GSL::Matrix::gsl_matrix_alloc(1, 2);
283 0 my ($rows, $cols) = $self->dim;
284 0 Math::GSL::Matrix::gsl_matrix_set($dim, 0, 0, $rows);
285 0 Math::GSL::Matrix::gsl_matrix_set($dim, 0, 1, $cols);
286  
287 0 my $err = Math::GSL::Matrix::gsl_matrix_fwrite($fh, $dim);
288 0 ▶ die "error gsl-writting matrix" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
289  
290 0 Math::GSL::Matrix::gsl_matrix_free($dim);
291  
292 0 $err = Math::GSL::Matrix::gsl_matrix_fwrite($fh, $self->{matrix}->raw);
293 0 ▶ die "error gsl-writting matrix" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
294  
295 0 Math::GSL::gsl_fclose($fh);
296  
297 }
298  
299  
300  
301  
302 1 1;
303  
304 __END__
↑ Summary