blib/lib/Math/Cephes/Matrix.pm

filter: Statementstmt 94.7% 145 / 153 Branchbran 77.9% 53 / 68 Conditioncond 63.6% 7 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 94.4% 17 / 18 Podpod 6.6% 1 / 15 totaltotal 82.3% 224 / 272 CC 40 SCAR 19.4
CC
40
Cov
88%
check
17.4
new
10.7
mul
7.0
CRAP 6.9
« blib/lib/Math/Cephes.pm ↑ Summary blib/lib/Math/Cephes/Complex.pm »
1 package Math::Cephes::Matrix;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw(@EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7 1 *import = \&Exporter::import;
8 1 @EXPORT_OK = qw(mat);
9  
10 1 $VERSION = '0.5308';
11  
12 1 require Math::Cephes;
13  
14 15 ▶sub new {
Subroutine new: called CC=10 SCAR=23.7
Pod undocumented
15 15 my ($caller, $arr) = @_;
16 15 my $refer = ref($caller);
17 15 ▶ my $class = $refer || $caller;
Condition66%2/3
$refer || $caller
l!l&&r!l&&!r
1140
MC/DC0%0/2
$refer || $caller
$refer $caller
Truth table66%2/3
A || B
A= $referB= $caller
ABresult
1X1
011
000
18 15 ▶ die "Must supply data for the matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless $refer or $arr 0 15 15
Condition66%2/3
$refer or $arr
l!l&&r!l&&!r
1140
MC/DC0%0/2
$refer or $arr
$refer $arr
Truth table66%2/3
A or B
A= $referB= $arr
ABresult
1X1
011
000
19 unless ($refer or $arr);
20 15 ▶ unless ($refer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($refer) 14 1 15
21 14 ▶ die "Please supply an array of arrays for the matrix data"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arr eq "ARRAY" and ref $arr->[0] eq "ARRAY" 0 14 14
Condition33%1/3
ref $arr eq "ARRAY" and ref $arr->[0] eq "ARRAY"
!ll&&!rl&&r
0014
MC/DC0%0/2
ref $arr eq "ARRAY" and ref $arr->[0] eq "ARRAY"
ref $arr eq "ARRAY" ref $arr->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= ref $arr eq "ARRAY"B= ref $arr->[0] eq "ARRAY"
ABresult
0X0
100
111
22 unless (ref($arr) eq 'ARRAY' and ref($arr->[0]) eq 'ARRAY');
23 14 my $n = scalar @$arr;
24 14 my $m = scalar @{$arr->[0]};
25 14 ▶ die "Matrices must be square" unless $m == $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $m == $n 0 14 14
26 }
27 15 my ($coef, $n);
28 15 ▶ if ($refer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refer) { } 1 14 15
29 1 $n = $caller->{n};
30 1 my $cdata = $caller->{coef};
31 1 foreach (@$cdata) {
32 3 push @$coef, [ @$_];
33 }
34 }
35 else {
36 14 ($coef, $n) = ($arr, scalar @$arr);
37 }
38 15 bless { coef => $coef,
39 n => $n,
40 }, $class;
41 }
42  
43 0 ▶sub mat {
Subroutine mat: not called CC=1 SCAR=6.9
Pod undocumented
44 0 return Math::Cephes::Matrix->new(shift);
45 }
46  
47 4 ▶sub mat_to_vec {
Subroutine mat_to_vec: called CC=3 SCAR=11.0
Pod undocumented
48 4 my $self = shift;
49 4 my ($M, $n) = ($self->{coef}, $self->{n});
50 4 my $A = [];
51 4 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12 4 16
52 12 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 36 12 48
53 36 my $index = $i*$n+$j;
54 36 $A->[$index] = $M->[$i]->[$j];
55 }
56 }
57 4 return $A;
58 }
59  
60 4 ▶sub vec_to_mat {
Subroutine vec_to_mat: called CC=3 SCAR=11.0
Pod undocumented
61 4 my ($self, $X) = @_;
62 4 my $n = $self->{n};
63 4 my $I = [];
64 4 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12 4 16
65 12 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 36 12 48
66 36 my $index = $i*$n+$j;
67 36 $I->[$i]->[$j] = $X->[$index];
68 }
69 }
70 4 return $I;
71 }
72  
73 12 ▶sub check {
Subroutine check: called CC=9 SCAR=28.6
Pod undocumented
74 12 my ($self, $B) = @_;
75 12 my $na = $self->{n};
76 12 my $ref = ref($B);
77 12 ▶ if ($ref eq 'Math::Cephes::Matrix') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Math::Cephes::Matrix') { } 7 5 12
elsif ($ref eq 'ARRAY') { } 5 0 5
78 die "Matrices must be of the same size"
79 7 ▶ unless $B->{n} == $na;
Branch: 1 branch
BranchTrueFalseTotal
unless $B->{"n"} == $na 0 7 7
80 7 return $B->coef;
81 }
82 elsif ($ref eq 'ARRAY') {
83 5 my $nb = scalar @$B;
84 5 my $ref0 = ref($B->[0]);
85 5 ▶ if ($ref0 eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref0 eq 'ARRAY') { } 0 5 5
elsif (not $ref0) { } 5 0 5
86 0 my $m = scalar @{$B->[0]};
87 0 ▶ die "Can only use square matrices" unless $m == $nb;
Branch: 1 branch
BranchTrueFalseTotal
unless $m == $nb 0 0 0
88 0 ▶ die "Can only use matrices of the same size"
Branch: 1 branch
BranchTrueFalseTotal
unless $na == $nb 0 0 0
89 unless $na == $nb;
90 0 return $B;
91 }
92 elsif (not $ref0) {
93 5 ▶ die "Can only use vectors of the same size" unless $nb == $na;
Branch: 1 branch
BranchTrueFalseTotal
unless $nb == $na 0 5 5
94 5 return $B;
95 }
96 else {
97 0 die "Unknown reference '$ref0' for data";
98 }
99 }
100 else {
101 0 die "Unknown reference '$ref' for data";
102 }
103 }
104  
105 18 ▶sub coef {
Subroutine coef: called CC=1 SCAR=0.0
Pod undocumented
106 18 return $_[0]->{coef};
107 }
108  
109 2 ▶sub clr {
Subroutine clr: called CC=4 SCAR=13.9
Pod undocumented
110 2 my $self = shift;
111 2 ▶ my $what = shift || 0;
Condition100%2/2
shift() || 0
l!l
11
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
112 2 my $n = $self->{n};
113 2 my $B = [];
114 2 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 6 2 8
115 6 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 18 6 24
116 18 $B->[$i]->[$j] = $what;
117 }
118 }
119 2 $self->{coef} = $B;
120 }
121  
122 1 ▶sub simq {
Subroutine simq: called CC=3 SCAR=11.1
Pod undocumented
123 1 my ($self, $B) = @_;
124 1 $B = $self->check($B);
125 1 my ($M, $n) = ($self->{coef}, $self->{n});
126 1 ▶ die "Must supply an array reference for B" unless ref($B) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $B eq "ARRAY" 0 1 1
127 1 my $A = $self->mat_to_vec();
128 1 my $X = [split //, 0 x $n];
129 1 my $IPS = [split //, 0 x $n];
130 1 my $flag = 0;
131 1 my $ret = Math::Cephes::simq($A, $B, $X, $n, $flag, $IPS);
132 1 ▶ return $ret ? undef : $X;
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 1 1
133 }
134  
135  
136 2 ▶sub inv {
Subroutine inv: called CC=2 SCAR=6.9
Pod undocumented
137 2 my $self = shift;
138 2 my ($M, $n) = ($self->{coef}, $self->{n});
139 2 my $A = $self->mat_to_vec();
140 2 my $X = [split //, 0 x ($n*$n)];
141 2 my $B = [split //, 0 x $n];
142 2 my $IPS = [split //, 0 x $n];
143 2 my $flag = 0;
144 2 my $ret = Math::Cephes::minv($A, $X, $n, $B, $IPS);
145 2 ▶ return undef if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 2 2
146 2 my $I = $self->vec_to_mat($X);
147 2 return Math::Cephes::Matrix->new($I);
148 }
149  
150 1 ▶sub transp {
Subroutine transp: called CC=1 SCAR=0.0
Pod undocumented
151 1 my $self = shift;
152 1 my ($M, $n) = ($self->{coef}, $self->{n});
153 1 my $A = $self->mat_to_vec();
154 1 my $T = [split //, 0 x ($n*$n)];
155 1 Math::Cephes::mtransp($n, $A, $T);
156 1 my $R = $self->vec_to_mat($T);
157 1 return Math::Cephes::Matrix->new($R);
158 }
159  
160 1 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
161 1 my ($self, $B) = @_;
162 1 $B = $self->check($B);
163 1 my ($A, $n) = ($self->{coef}, $self->{n});
164 1 my $C = [];
165 1 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3 1 4
166 3 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 9 3 12
167 9 $C->[$i]->[$j] = $A->[$i]->[$j] + $B->[$i]->[$j];
168 }
169 }
170 1 return Math::Cephes::Matrix->new($C);
171 }
172  
173 1 ▶sub sub {
Subroutine sub: called CC=3 SCAR=11.0
Pod undocumented
174 1 my ($self, $B) = @_;
175 1 $B = $self->check($B);
176 1 my ($A, $n) = ($self->{coef}, $self->{n});
177 1 my $C = [];
178 1 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3 1 4
179 3 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 9 3 12
180 9 $C->[$i]->[$j] = $A->[$i]->[$j] - $B->[$i]->[$j];
181 }
182 }
183 1 return Math::Cephes::Matrix->new($C);
184 }
185  
186 8 ▶sub mul {
Subroutine mul: called CC=7 SCAR=19.5
Pod undocumented
187 8 my ($self, $B) = @_;
188 8 $B = $self->check($B);
189 8 my ($A, $n) = ($self->{coef}, $self->{n});
190 8 my $C = [];
191 8 ▶ if (ref($B->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $B->[0] eq 'ARRAY') { } 4 4 8
192 4 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12 4 16
193 12 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 36 12 48
194 36 ▶ for (my $m=0; $m<$n; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $n) 108 36 144
195 108 $C->[$i]->[$j] += $A->[$i]->[$m] * $B->[$m]->[$j];
196 }
197 }
198 }
199 4 return Math::Cephes::Matrix->new($C);
200 }
201 else {
202 4 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12 4 16
203 12 ▶ for (my $m=0; $m<$n; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $n) 36 12 48
204 36 $C->[$i] += $A->[$i]->[$m] * $B->[$m];
205 }
206 }
207 4 return $C;
208 }
209 }
210  
211 1 ▶sub div {
Subroutine div: called CC=1 SCAR=0.0
Pod undocumented
212 1 my ($self, $B) = @_;
213 1 $B = $self->check($B);
214 1 my $C = Math::Cephes::Matrix->new($B)->inv();
215 1 my $D = $self->mul($C);
216 1 return $D;
217 }
218  
219 1 ▶sub eigens {
Subroutine eigens: called CC=3 SCAR=11.0
Pod undocumented
220 1 my $self = shift;
221 1 my ($M, $n) = ($self->{coef}, $self->{n});
222 1 my $A = [];
223 1 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3 1 4
224 3 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 9 3 12
225 9 my $index = ($i*$i+$i)/2 + $j;
226 9 $A->[$index] = $M->[$i]->[$j];
227 }
228 }
229 1 my $EV1 = [split //, 0 x ($n*$n)];
230 1 my $E = [split //, 0 x $n];
231 1 my $IPS = [split //, 0 x $n];
232 1 Math::Cephes::eigens($A, $EV1, $E, $n);
233 1 my $EV = $self->vec_to_mat($EV1);
234 1 return ($E, Math::Cephes::Matrix->new($EV));
235 }
236  
237 1 1;
238  
239 __END__
« blib/lib/Math/Cephes.pm ↑ Summary blib/lib/Math/Cephes/Complex.pm »