blib/lib/Math/MatrixReal.pm

filter: Statementstmt 83.7% 1387 / 1656 Branchbran 66.7% 690 / 1034 Conditioncond 47.3% 167 / 353 MC/DCmcdc 18.2% 37 / 203 Subroutinesub 93.4% 156 / 167 Podpod 27.0% 27 / 100 totaltotal 70.1% 2464 / 3513 CC 645 SCAR 33.3
CC
645
Cov
74%
_init_iteration
272.0
solve_GSM
210.0
solve_SSM
210.0
CRAP 28.0
↑ Summary funcs.pl »
1 # Copyright (c) 1996, 1997 by Steffen Beyer. All rights reserved.
2 # Copyright (c) 1999 by Rodolphe Ortalo. All rights reserved.
3 # Copyright (c) 2001-2016 by Jonathan Leto. All rights reserved.
4 # This package is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package Math::MatrixReal;
8  
9 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 59 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 59 require Exporter;
16  
17 59 @ISA = qw(Exporter);
18 59 @EXPORT = qw();
19 59 @EXPORT_OK = qw(min max);
20 59 %EXPORT_TAGS = (all => [@EXPORT_OK]);
21 59 $VERSION = '2.13';
22  
23 use overload
24 59 ▶ '.' => '_concat',
Subroutine BEGIN: called CC=1 SCAR=0.0
25 'neg' => '_negate',
26 '~' => '_transpose',
27 'bool' => '_boolean',
28 '!' => '_not_boolean',
29 'abs' => '_norm',
30 '+' => '_add',
31 '-' => '_subtract',
32 '*' => '_multiply',
33 '/' => '_divide',
34 '**' => '_exponent',
35 '+=' => '_assign_add',
36 '-=' => '_assign_subtract',
37 '*=' => '_assign_multiply',
38 '**=' => '_assign_exponent',
39 '==' => '_equal',
40 '!=' => '_not_equal',
41 '<' => '_less_than',
42 '<=' => '_less_than_or_equal',
43 '>' => '_greater_than',
44 '>=' => '_greater_than_or_equal',
45 'eq' => '_equal',
46 'ne' => '_not_equal',
47 'lt' => '_less_than',
48 'le' => '_less_than_or_equal',
49 'gt' => '_greater_than',
50 'ge' => '_greater_than_or_equal',
51 '=' => '_clone',
52 '""' => '_stringify',
53 59 'fallback' => undef;
54  
55 =head1 NAME
56  
57 Math::MatrixReal - Matrix of Reals
58  
59 Implements the data type "matrix of real numbers" (and consequently also
60 "vector of real numbers").
61  
62 =head1 SYNOPSIS
63  
64 my $a = Math::MatrixReal->new_random(5, 5);
65  
66 my $b = $a->new_random(10, 30, { symmetric=>1, bounded_by=>[-1,1] });
67  
68 my $c = $b * $a ** 3;
69  
70 my $d = $b->new_from_rows( [ [ 5, 3 ,4], [3, 4, 5], [ 2, 4, 1 ] ] );
71  
72 print $a;
73  
74 my $row = ($a * $b)->row(3);
75  
76 my $col = (5*$c)->col(2);
77  
78 my $transpose = ~$c;
79  
80 my $transpose = $c->transpose;
81  
82 my $inverse = $a->inverse;
83  
84 my $inverse = 1/$a;
85  
86 my $inverse = $a ** -1;
87  
88 my $determinant= $a->det;
89  
90 =cut
91  
92 sub new
93 1824 ▶{
Subroutine new: called CC=8 SCAR=22.8
Pod documented
94 1824 ▶ croak "Usage: \$new_matrix = Math::MatrixReal->new(\$rows,\$columns);" if (@_ != 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 1824 1824
95  
96 1824 my ($self,$rows,$cols) = @_;
97 1824 ▶ my $class = ref($self) || $self || 'Math::MatrixReal';
Condition60%3/5
ref $self || $self || 'Math::MatrixReal'
l!l
18240
ref $self or $self
l!l&&r!l&&!r
12915330
MC/DC0%0/2
ref $self || $self || 'Math::MatrixReal'
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
98  
99 1824 ▶ croak "Math::MatrixReal::new(): number of rows must be integer > 0"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows > 0 and $rows == int $rows 0 1824 1824
Condition33%1/3
$rows > 0 and $rows == int $rows
!ll&&!rl&&r
001824
MC/DC0%0/2
$rows > 0 and $rows == int $rows
$rows > 0 $rows == int $rows
Truth table33%1/3
A and B
A= $rows > 0B= $rows == int $rows
ABresult
0X0
100
111
100 unless ($rows > 0 and $rows == int($rows) );
101  
102 1824 ▶ croak "Math::MatrixReal::new(): number of columns must be integer > 0"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols > 0 and $cols == int $cols 0 1824 1824
Condition33%1/3
$cols > 0 and $cols == int $cols
!ll&&!rl&&r
001824
MC/DC0%0/2
$cols > 0 and $cols == int $cols
$cols > 0 $cols == int $cols
Truth table33%1/3
A and B
A= $cols > 0B= $cols == int $cols
ABresult
0X0
100
111
103 unless ($cols > 0 and $cols == int($cols) );
104  
105 1824 my $this = [ [ ], $rows, $cols ];
106  
107 # Create the first empty row and pre-lengthen
108 1824 my $empty = [ ];
109 1824 $#$empty = $cols - 1;
110  
111 1824 map { $empty->[$_] = 0.0 } ( 0 .. $cols-1 );
112  
113 # Create a row at a time
114 1824 map { $this->[0][$_] = [ @$empty ] } ( 0 .. $rows-1);
115  
116 1824 bless $this, $class;
117 }
118  
119 28 ▶sub new_diag {
Subroutine new_diag: called CC=3 SCAR=11.1
Pod documented
120 28 ▶ croak "Usage: \$new_matrix = Math::MatrixReal->new_diag( [ 1, 2, 3] );" unless (@_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 28 28
121 28 my ($self,$diag) = @_;
122 28 my $n = scalar @$diag;
123  
124 28 ▶ croak "Math::MatrixReal::new_diag(): Third argument must be an arrayref" unless (ref($diag) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $diag eq "ARRAY" 0 28 28
125  
126 28 my $matrix = Math::MatrixReal->new($n,$n);
127  
128 28 map { $matrix->[0][$_][$_] = shift @$diag } ( 0 .. $n-1);
129 28 return $matrix;
130 }
131 2 ▶sub new_tridiag {
Subroutine new_tridiag: called CC=7 SCAR=20.3
Pod documented
132 2 ▶ croak "Usage: \$new_matrix = Math::MatrixReal->new_tridiag( [ 1, 2, 3], [ 4, 5, 6, 7], [-1,-2,-3] );" unless (@_ == 4 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 2 2
133 2 my ($self,$lower,$diag,$upper) = @_;
134 2 my $matrix;
135 2 my ($l,$n,$m) = (scalar(@$lower),scalar(@$diag),scalar(@$upper));
136 2 my ($k,$p)=(-1,-1);
137  
138 2 ▶ croak "Math::MatrixReal::new_tridiag(): Arguments must be arrayrefs" unless
Branch: 1 branch
BranchTrueFalseTotal
unless ref $diag eq "ARRAY" and ref $lower eq "ARRAY" and ref $upper eq "ARRAY" 0 2 2
Condition33%2/6
ref $diag eq "ARRAY" and ref $lower eq "ARRAY" and ref $upper eq "ARRAY"
!ll&&!rl&&r
002
ref $diag eq "ARRAY" and ref $lower eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/3
ref $diag eq "ARRAY" and ref $lower eq "ARRAY" and ref $upper eq "ARRAY"
ref $diag eq "ARRAY" ref $lower eq "ARRAY" ref $upper eq "ARRAY"
Truth table25%1/4
A and B and C
A= ref $diag eq "ARRAY"B= ref $lower eq "ARRAY"C= ref $upper eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
139 ref $diag eq 'ARRAY' && ref $lower eq 'ARRAY' && ref $upper eq 'ARRAY';
140 2 ▶ croak "Math::MatrixReal::new_tridiag(): new_tridiag(\$lower,\$diag,\$upper) diagonal dimensions incompatible" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $l == $m and $n == $l + 1 0 2 2
Condition33%1/3
$l == $m and $n == $l + 1
!ll&&!rl&&r
002
MC/DC0%0/2
$l == $m and $n == $l + 1
$l == $m $n == $l + 1
Truth table33%1/3
A and B
A= $l == $mB= $n == $l + 1
ABresult
0X0
100
111
141 ($l == $m && $n == ($l+1));
142  
143 2 $matrix = Math::MatrixReal->new_diag($diag);
144 $matrix = $matrix->each(
145 32 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
146 32 my ($e,$i,$j) = @_;
147 32 ▶ if (($i-$j) == -1) { $k++; return $upper->[$k];}
Branch: 3 branches
BranchTrueFalseTotal
if ($i - $j == -1) { } 6 26 32
elsif ($i == $j) { } 8 18 26
elsif ($i - $j == 1) { } 6 12 18
148 8 elsif ( $i == $j) { return $e; }
149 6 elsif (($i-$j) == 1) { $p++; return $lower->[$p];}
150 }
151 2 );
152 2 return $matrix;
153 }
154  
155 38 ▶sub new_random {
Subroutine new_random: called CC=28 SCAR=34.3
Pod documented
156 38 ▶ croak "Usage: \$new_matrix = Math::MatrixReal->new_random(\$n,\$m, { symmetric => 1, bounded_by => [-5,5], integer => 1 } );"
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 1 37 38
157 if (@_ < 2);
158 37 my ($self, $rows, $cols, $options ) = @_;
159 37 ▶ (($options = $cols) and ($cols = $rows)) if ref $cols eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cols eq "HASH" 13 24 37
Condition50%3/6
ref $cols eq 'HASH' && (($options = $cols) && ($cols = $rows))
!ll&&!rl&&r
24130
$options = $cols and $cols = $rows
!ll&&!rl&&r
0130
MC/DC0%0/3
ref $cols eq 'HASH' && (($options = $cols) && ($cols = $rows))
ref $cols eq 'HASH' $options = $cols $cols = $rows
Truth table0%0/4
A && B and C
A= ref $cols eq 'HASH'B= $options = $colsC= $cols = $rows
ABCresult
0XX0
10X0
1100
1111
160 37 ▶ my ($min,$max) = defined $options->{bounded_by} ? @{ $options->{bounded_by} } : ( 0, 10);
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'bounded_by'} ? : 10 27 37
161 37 my $integer = $options->{integer};
162 37 ▶ $self = ref($self) || $self || 'Math::MatrixReal';
Condition60%3/5
ref $self || $self || 'Math::MatrixReal'
l!l
370
ref $self or $self
l!l&&r!l&&!r
5320
MC/DC0%0/2
ref $self || $self || 'Math::MatrixReal'
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
163
164 37 ▶ $cols ||= $rows;
Condition66%2/3
$cols ||= $rows
l!l&&r!l&&!r
22150
MC/DC0%0/2
$cols ||= $rows
$cols $rows
Truth table66%2/3
A ||= B
A= $colsB= $rows
ABresult
1X1
011
000
165 croak "Math::MatrixReal::new_random(): number of rows must = number of cols for symmetric option"
166 37 ▶ if ($rows != $cols and $options->{symmetric} );
Branch: 1 branch
BranchTrueFalseTotal
if $rows != $cols and $options->{"symmetric"} 1 36 37
Condition100%3/3
$rows != $cols and $options->{"symmetric"}
!ll&&!rl&&r
3061
MC/DC100%2/2
$rows != $cols and $options->{"symmetric"}
$rows != $cols $options->{"symmetric"}
Truth table100%3/3
A and B
A= $rows != $colsB= $options->{"symmetric"}
ABresult
0X0
100
111
167  
168 36 ▶ croak "Math::MatrixReal::new_random(): number of rows must be integer > 0"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows > 0 and $rows == int $rows and ($cols > 0 and $cols == int $cols) 1 35 36
Condition55%5/9
$rows > 0 and $rows == int $rows and ($cols > 0 and $cols == int $cols)
!ll&&!rl&&r
1035
$rows > 0 and $rows == int $rows
!ll&&!rl&&r
1035
$cols > 0 and $cols == int $cols
!ll&&!rl&&r
0035
MC/DC25%1/4
$rows > 0 and $rows == int $rows and ($cols > 0 and $cols == int $cols)
$rows > 0 $rows == int $rows $cols > 0 $cols == int $cols
Truth table40%2/5
A and B and C and D
A= $rows > 0B= $rows == int $rowsC= $cols > 0D= $cols == int $cols
ABCDresult
0XXX0
10XX0
110X0
11100
11111
169 unless ($rows > 0 and $rows == int($rows) ) && ($cols > 0 and $cols == int($cols) ) ;
170  
171 35 ▶ croak "Math::MatrixReal::new_random(): bounded_by interval length must be > 0"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min and defined $max and $min < $max 2 33 35
Condition83%5/6
defined $min and defined $max and $min < $max
!ll&&!rl&&r
1133
defined $min and defined $max
!ll&&!rl&&r
1034
MC/DC66%2/3
defined $min and defined $max and $min < $max
defined $min defined $max $min < $max
Truth table75%3/4
A and B and C
A= defined $minB= defined $maxC= $min < $max
ABCresult
0XX0
10X0
1100
1111
172 unless (defined $min && defined $max && $min < $max );
173  
174 croak "Math::MatrixReal::new_random(): tridiag option only for square matrices"
175 33 ▶ if (($options->{tridiag} || $options->{tridiagonal}) && $rows != $cols);
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"tridiag"} || $options->{"tridiagonal"} and $rows != $cols 1 32 33
Condition83%5/6
$options->{"tridiag"} || $options->{"tridiagonal"} and $rows != $cols
!ll&&!rl&&r
3021
$options->{"tridiag"} or $options->{"tridiagonal"}
l!l&&r!l&&!r
3030
MC/DC66%2/3
$options->{"tridiag"} || $options->{"tridiagonal"} and $rows != $cols
$options->{"tridiag"} $options->{"tridiagonal"} $rows != $cols
Truth table60%3/5
A or B and C
A= $options->{"tridiag"}B= $options->{"tridiagonal"}C= $rows != $cols
ABCresult
00X0
1X00
0100
1X11
0111
176  
177 croak "Math::MatrixReal::new_random(): diagonal option only for square matrices "
178 32 ▶ if (($options->{diag} || $options->{diagonal}) && ($rows != $cols));
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"diag"} || $options->{"diagonal"} and $rows != $cols 1 31 32
Condition83%5/6
$options->{"diag"} || $options->{"diagonal"} and $rows != $cols
!ll&&!rl&&r
3011
$options->{"diag"} or $options->{"diagonal"}
l!l&&r!l&&!r
2030
MC/DC66%2/3
$options->{"diag"} || $options->{"diagonal"} and $rows != $cols
$options->{"diag"} $options->{"diagonal"} $rows != $cols
Truth table60%3/5
A or B and C
A= $options->{"diag"}B= $options->{"diagonal"}C= $rows != $cols
ABCresult
00X0
1X00
0100
1X11
0111
179  
180 31 my $matrix = Math::MatrixReal->new($rows,$cols);
181 31 ▶ my $random_code = sub { $integer ? int($min + rand($max-$min)) : $min + rand($max-$min) } ;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$integer ? : 305 10685 10990
182  
183 31 ▶ $matrix = $options->{diag} || $options->{diagonal} ? $matrix->each_diag($random_code) : $matrix->each($random_code);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'diag'} || $options->{'diagonal'} ? : 1 30 31
Condition66%2/3
$options->{"diag"} or $options->{"diagonal"}
l!l&&r!l&&!r
1030
MC/DC50%1/2
$options->{"diag"} or $options->{"diagonal"}
$options->{"diag"} $options->{"diagonal"}
Truth table66%2/3
A or B
A= $options->{"diag"}B= $options->{"diagonal"}
ABresult
1X1
011
000
184 31 ▶ $matrix = $matrix->each( sub {my($e,$i,$j)=@_; ( abs($i-$j)>1 ) ? 0 : $e } ) if ($options->{tridiag} || $options->{tridiagonal} );
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if $options->{"tridiag"} or $options->{"tridiagonal"} 2 29 31
abs $i - $j > 1 ? : 24 26 50
Condition66%2/3
$options->{"tridiag"} or $options->{"tridiagonal"}
l!l&&r!l&&!r
2029
MC/DC50%1/2
$options->{"tridiag"} or $options->{"tridiagonal"}
$options->{"tridiag"} $options->{"tridiagonal"}
Truth table66%2/3
A or B
A= $options->{"tridiag"}B= $options->{"tridiagonal"}
ABresult
1X1
011
000
185 31 ▶ $options->{symmetric} ? 0.5*($matrix + ~$matrix) : $matrix;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'symmetric'} ? : 12 19 31
186 }
187
188 sub new_from_string#{{{
189 181 ▶{#{{{
Subroutine new_from_string: called CC=13 SCAR=26.6
Pod documented
190 181 ▶ croak "Usage: \$new_matrix = Math::MatrixReal->new_from_string(\$string);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 181 181
191 if (@_ != 2);
192  
193 181 my ($self,$string) = @_;
194 181 ▶ my $class = ref($self) || $self || 'Math::MatrixReal';
Condition60%3/5
ref $self || $self || 'Math::MatrixReal'
l!l
1810
ref $self or $self
l!l&&r!l&&!r
11800
MC/DC0%0/2
ref $self || $self || 'Math::MatrixReal'
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
195 181 my ($line,$values);
196 181 my ($rows,$cols);
197 181 my ($row,$col);
198 181 my ($warn,$this);
199  
200 181 $warn = $rows = $cols = 0;
201  
202 181 $values = [ ];
203 181 ▶ while ($string =~ m!^\s* \[ \s+ ( (?: [+-]? \d+ (?: \. \d*)? (?: E [+-]? \d+ )? \s+ )+ ) \] \s*? \n !ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\s* \[ \s+ ( (?: [+-]? \d+ (?: \. \d*)? (?: E [+-]? \d+ )? \s+ )+ ) \] \s*? \n /xi) 523 181 704
204 523 $line = $1; $string = $';
205 523 $values->[$rows] = [ ]; @{$values->[$rows]} = split(' ', $line);
206 523 $col = @{$values->[$rows]};
207 523 ▶ if ($col != $cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col != $cols) 180 343 523
208 0 ▶ unless ($cols == 0) { $warn = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cols == 0) 0 180 180
209 180 ▶ if ($col > $cols) { $cols = $col; }
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $cols) 180 0 180
210 }
211 523 $rows++;
212 }
213 181 ▶ if ($string !~ m/^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^\s*$/) 0 181 181
214 0 chomp $string;
215 0 my $error_msg = "Math::MatrixReal::new_from_string(): syntax error in input string: $string";
216 0 croak $error_msg;
217 }
218 181 ▶ if ($rows == 0) { croak "Math::MatrixReal::new_from_string(): empty input string"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rows == 0) 1 180 181
219 0 ▶ if ($warn) { warn "Math::MatrixReal::new_from_string(): missing elements will be set to zero!\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 180 180
220 180 $this = Math::MatrixReal::new($class,$rows,$cols);
221 180 ▶ for ( $row = 0; $row < $rows; $row++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $rows) 523 180 703
222 523 ▶ for ( $col = 0; $col < @{$values->[$row]}; $col++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < @{$values->[$row];}) 1834 523 2357
223 1834 $this->[0][$row][$col] = $values->[$row][$col];
224 }
225 }
226 180 return $this;
227 }#}}}#}}}
228  
229 # from Math::MatrixReal::Ext1 (msouth@fulcrum.org)
230 25 ▶sub new_from_cols {
Subroutine new_from_cols: called CC=3 SCAR=12.0
Pod documented
231 25 my $self = shift;
232 25 ▶ my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[-1] eq 'HASH' ? : 0 25 25
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
2500
MC/DC0%0/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1 ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
233 25 $extra_args->{_type} = 'column';
234 25 $self->_new_from_rows_or_cols(@_, $extra_args );
235 }
236 # from Math::MatrixReal::Ext1 (msouth@fulcrum.org)
237 4 ▶sub new_from_columns {
Subroutine new_from_columns: called CC=1 SCAR=0.0
Pod undocumented
238 4 my $self = shift;
239 4 $self->new_from_cols(@_);
240 }
241 # from Math::MatrixReal::Ext1 (msouth@fulcrum.org)
242 30 ▶sub new_from_rows {
Subroutine new_from_rows: called CC=3 SCAR=12.0
Pod documented
243 30 my $self = shift;
244 30 ▶ my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[-1] eq 'HASH' ? : 0 30 30
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
3000
MC/DC0%0/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1 ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
245 30 $extra_args->{_type} = 'row';
246  
247 30 $self->_new_from_rows_or_cols(@_, $extra_args );
248 }
249  
250 1 ▶sub reshape {
Subroutine reshape: called CC=2 SCAR=6.9
Pod documented
251 1 my ($self, $rows, $cols, $values) = @_;
252  
253 1 my @cols = ();
254 1 my $p = 0;
255 1 for my $c (1..$cols) {
256 3 push @cols, [@{$values}[$p .. $p + $rows - 1]];
257 3 $p += $rows;
258 }
259  
260 1 return $self->new_from_cols( \@cols );
261 }
262  
263 # from Math::MatrixReal::Ext1 (msouth@fulcrum.org)
264 55 ▶sub _new_from_rows_or_cols {
Subroutine _new_from_rows_or_cols: called CC=20 SCAR=30.1
265 55 my $proto = shift;
266 55 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
10450
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
267 55 my $ref_to_vectors = shift;
268  
269 # these additional args are internal at the moment, but in the future the user could pass e.g. {pad=>1} to
270 # request padding
271 55 my $args = pop;
272 55 my $vector_type = $args->{_type};
273 55 ▶ die "Internal ".__PACKAGE__." error" unless $vector_type =~ /^(row|column)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $vector_type =~ /^(row|column)$/ 0 55 55
274  
275 # step back one frame because this private method is not how the user called it
276 55 my $caller_subname = (caller(1))[3];
277  
278 55 ▶ croak "$caller_subname: need a reference to an array of ${vector_type}s" unless reftype($ref_to_vectors) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($ref_to_vectors) eq "ARRAY" 2 53 55
279  
280 53 my @vectors = @{$ref_to_vectors};
281 53 my $matrix;
282 53 my $other_type = {row=>'column', column=>'row'}->{$vector_type};
283 53 my %matrix_dim = (
284 $vector_type => scalar( @vectors ),
285 $other_type => 0, # we will correct this in a bit
286 );
287  
288 # row and column indices are one based
289 53 my $current_vector_count = 1;
290 53 foreach my $current_vector (@vectors) {
291 # dimension is one-based, so we're
292 # starting with one here and incrementing
293 # as we go. The other dimension is fixed (for now, until
294 # we add the 'pad' option), and gets set later
295 103 my $ref = ref( $current_vector ) ;
296  
297 103 ▶ if ( $ref eq '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq '') { } 9 94 103
elsif ($ref eq 'ARRAY') { } 76 18 94
elsif ($ref ne 'HASH' and $current_vector->isa('Math::MatrixReal') || $current_vector->isa('Math::MatrixComplex')) { } 17 1 18
298 # we hope this is a properly formatted Math::MatrixReal string,
299 # but if not we just let the Math::MatrixReal die() do it's
300 # thing
301 9 $current_vector = $class->new_from_string( $current_vector );
302 } elsif ( $ref eq 'ARRAY' ) {
303 76 my @array = @$current_vector;
304 76 ▶ croak "$caller_subname: one $vector_type you gave me was a ref to an array with no elements" unless @array;
Branch: 1 branch
BranchTrueFalseTotal
unless @array 1 75 76
305 # we need to create the right kind of string based on whether
306 # they said they were sending us rows or columns:
307 75 ▶ if ($vector_type eq 'row') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vector_type eq 'row') { } 43 32 75
308 43 $current_vector = $class->new_from_string( '[ '. join( " ", @array) ." ]\n" );
309 } else {
310 32 $current_vector = $class->new_from_string( '[ '. join( " ]\n[ ", @array) ." ]\n" );
311 }
312 ▶ } elsif ( $ref ne 'HASH' and
Condition50%3/6
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal") || $current_vector->isa("Math::MatrixComplex")
!ll&&!rl&&r
1017
$current_vector->isa("Math::MatrixReal") or $current_vector->isa("Math::MatrixComplex")
l!l&&r!l&&!r
1700
MC/DC33%1/3
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal") || $current_vector->isa("Math::MatrixComplex")
$ref ne "HASH" $current_vector->isa("Math::MatrixReal") $current_vector->isa("Math::MatrixComplex")
Truth table50%2/4
A and B or C
A= $ref ne "HASH"B= $current_vector->isa("Math::MatrixReal")C= $current_vector->isa("Math::MatrixComplex")
ABCresult
0XX0
1000
11X1
1011
313 ( $current_vector->isa('Math::MatrixReal') ||
314 $current_vector->isa('Math::MatrixComplex')
315 ) ) {
316 # it's already a Math::MatrixReal something.
317 # we don't need to do anything, it will all
318 # work out
319 } else {
320 # we have no idea, error time!
321 1 croak "$caller_subname: I only know how to deal with array refs, strings, and things that inherit from Math::MatrixReal\n";
322 }
323  
324 # starting now we know $current_vector isa Math::MatrixReal thingy
325 100 my @vector_dims = $current_vector->dim;
326  
327 #die unless the appropriate dimension is 1
328 100 ▶ croak "$caller_subname: I don't accept $other_type vectors" unless ($vector_dims[ $vector_type eq 'row' ? 0 : 1 ] == 1) ;
Branch: 2 branches
BranchTrueFalseTotal
unless $vector_dims[$vector_type eq "row" ? 0 : 1] == 1 2 98 100
$vector_type eq 'row' ? : 60 40 100
329  
330 # the other dimension is the length of our vector
331 98 ▶ my $length = $vector_dims[ $vector_type eq 'row' ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$vector_type eq 'row' ? : 59 39 98
332  
333 # set the "other" dimension to the length of this
334 # vector the first time through
335 98 ▶ $matrix_dim{$other_type} ||= $length;
Condition66%2/3
$matrix_dim{$other_type} ||= $length
l!l&&r!l&&!r
47510
MC/DC0%0/2
$matrix_dim{$other_type} ||= $length
$matrix_dim{$other_type} $length
Truth table66%2/3
A ||= B
A= $matrix_dim{$other_type}B= $length
ABresult
1X1
011
000
336  
337 # die unless length of this vector matches the first length
338 croak "$caller_subname: one $vector_type has [$length] elements and another one had [$matrix_dim{$other_type}]--all of the ${vector_type}s passed in must have the same dimension"
339 98 ▶ unless ($length == $matrix_dim{$other_type}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $length == $matrix_dim{$other_type} 2 96 98
340  
341 # create the matrix the first time through
342 96 ▶ $matrix ||= $class->new($matrix_dim{row}, $matrix_dim{column});
Condition66%2/3
$matrix ||= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
l!l&&r!l&&!r
45510
MC/DC0%0/2
$matrix ||= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
$matrix $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
Truth table66%2/3
A ||= B
A= $matrixB= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
ABresult
1X1
011
000
343  
344 # step along the vector assigning the value of each element
345 # to the correct place in the matrix we're building
346 96 foreach my $element_index ( 1..$length ){
347 # args for vector assignment:
348 # initialize both to one and reset the correct
349 # one below
350 253 my ($v_r, $v_c) = (1,1);
351  
352 # args for matrix assignment
353 253 my ($row_index, $column_index, $value);
354  
355 253 ▶ if ($vector_type eq 'row') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vector_type eq 'row') { } 140 113 253
356 140 $row_index = $current_vector_count;
357 140 $v_c = $column_index = $element_index;
358 } else {
359 113 $v_r = $row_index = $element_index;
360 113 $column_index = $current_vector_count;
361 }
362 253 $value = $current_vector->element($v_r, $v_c);
363 253 $matrix->assign($row_index, $column_index, $value);
364 }
365 96 $current_vector_count ++ ;
366 }
367 46 return $matrix;
368 }
369  
370 sub shadow
371 52 ▶{
Subroutine shadow: called CC=2 SCAR=7.1
Pod documented
372 52 ▶ croak "Usage: \$new_matrix = \$some_matrix->shadow();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 52 52
373  
374 52 my ($matrix) = @_;
375  
376 52 return $matrix->new($matrix->[1],$matrix->[2]);
377 }
378  
379 =over 4
380  
381 =item * $matrix->display_precision($integer)
382  
383 Sets the default precision when matrices are printed or stringified.
384 $matrix->display_precision(0) will only show the integer part of all the
385 entries of $matrix and $matrix->display_precision() will return to the default
386 scientific display notation. This method does not effect the precision of the
387 calculations.
388  
389 =back
390  
391 =cut
392  
393 sub display_precision
394 5 ▶{
Subroutine display_precision: called CC=3 SCAR=11.3
Pod documented
395 5 my ($self,$n) = @_;
396 5 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 5 0 5
397 5 ▶ croak "Usage: \$matrix->display_precision(\$nonnegative_integer);" if ($n < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 1 4 5
398 4 $self->[4] = int $n;
399 } else {
400 0 $self->[4] = undef;
401 }
402 }
403  
404 sub copy
405 471 ▶{
Subroutine copy: called CC=6 SCAR=18.1
Pod documented
406 471 ▶ croak "Usage: \$matrix1->copy(\$matrix2);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 471 471
407 if (@_ != 2);
408  
409 471 my ($matrix1,$matrix2) = @_;
410 471 my ($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
411 471 my ($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
412 471 my ($i,$j);
413  
414 471 ▶ croak "Math::MatrixReal::copy(): matrix size mismatch" unless $rows1 == $rows2 && $cols1 == $cols2;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $cols1 == $cols2 0 471 471
Condition33%1/3
$rows1 == $rows2 and $cols1 == $cols2
!ll&&!rl&&r
00471
MC/DC0%0/2
$rows1 == $rows2 and $cols1 == $cols2
$rows1 == $rows2 $cols1 == $cols2
Truth table33%1/3
A and B
A= $rows1 == $rows2B= $cols1 == $cols2
ABresult
0X0
100
111
415  
416 471 ▶ for ( $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 4616 471 5087
417 {
418 4616 my $r1 = [];
419 4616 my $r2 = $matrix2->[0][$i];
420 4616 @$r1 = @$r2; # Copy whole array directly
421 4616 $matrix1->[0][$i] = $r1;
422 }
423 471 ▶ if (defined $matrix2->[3]) # is an LR decomposition matrix!
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix2->[3]) 5 466 471
424 {
425 5 $matrix1->[3] = $matrix2->[3]; # $sign
426 5 $matrix1->[4] = $matrix2->[4]; # $perm_row
427 5 $matrix1->[5] = $matrix2->[5]; # $perm_col
428 }
429 }
430  
431 sub clone
432 290 ▶{
Subroutine clone: called CC=2 SCAR=7.0
Pod documented
433 290 ▶ croak "Usage: \$twin_matrix = \$some_matrix->clone();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 290 290
434  
435 290 my($matrix) = @_;
436 290 my($temp);
437  
438 290 $temp = $matrix->new($matrix->[1],$matrix->[2]);
439 290 $temp->copy($matrix);
440 290 return $temp;
441 }
442  
443 ## trace() : return the sum of the diagonal elements
444 13 ▶sub trace {
Subroutine trace: called CC=3 SCAR=11.1
Pod undocumented
445 13 ▶ croak "Usage: \$trace = \$matrix->trace();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 13 13
446  
447 13 my $matrix = shift;
448 13 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
449 13 my $trace = 0;
450  
451 13 ▶ croak "Math::MatrixReal::trace(): matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 13 13
452  
453 13 map { $trace += $matrix->[0][$_][$_] } (0 .. $cols-1);
454  
455 13 return $trace;
456 }
457 4 ▶sub submatrix {
Subroutine submatrix: called CC=7 SCAR=19.5
Pod undocumented
458 4 my $self = shift;
459 4 my ($x1, $y1, $x2, $y2) = @_;
460 4 ▶ croak "Math::MatrixReal::submatrix(): indices must be positive integers"
Branch: 1 branch
BranchTrueFalseTotal
unless $x1 >= 1 and $x2 >= 1 and $y1 >= 1 and $y2 >= 1 2 2 4
Condition77%7/9
$x1 >= 1 and $x2 >= 1 and $y1 >= 1 and $y2 >= 1
!ll&&!rl&&r
112
$x1 >= 1 and $x2 >= 1 and $y1 >= 1
!ll&&!rl&&r
103
$x1 >= 1 and $x2 >= 1
!ll&&!rl&&r
103
MC/DC50%2/4
$x1 >= 1 and $x2 >= 1 and $y1 >= 1 and $y2 >= 1
$x1 >= 1 $x2 >= 1 $y1 >= 1 $y2 >= 1
Truth table60%3/5
A and B and C and D
A= $x1 >= 1B= $x2 >= 1C= $y1 >= 1D= $y2 >= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
461 unless ($x1 >= 1 && $x2 >= 1 && $y1 >=1 && $y2 >=1 );
462 2 my($rows,$cols) = ($self->[1],$self->[2]);
463 2 my($sr,$sc) = ( 1+abs($x1-$x2), 1+abs($y1-$y2) );
464 2 my $submatrix = $self->new( $sr, $sc );
465  
466 2 ▶ for (my $i = $x1-1; $i < $x2; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $x2) 3 2 5
467 3 ▶ for (my $j = $y1-1; $j < $y2; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $y2) 5 3 8
468 5 $submatrix->[0][$i-($x1-1)][$j-($y1-1)] = $self->[0][$i][$j];
469 }
470 }
471 2 return $submatrix;
472 }
473 ## return the minor corresponding to $r and $c
474 ## eliminate row $r and col $c , and return the $r-1 by $c-1 matrix
475 137 ▶sub minor {
Subroutine minor: called CC=18 SCAR=32.0
Pod undocumented
476 137 ▶ croak "Usage: \$minor = \$matrix->minor(\$r,\$c);" unless (@_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 137 137
477 137 my ($matrix,$r,$c) = @_;
478 137 my ($rows,$cols) = $matrix->dim();
479  
480 137 ▶ croak "Math::MatrixReal::minor(): \$matrix must be at least 2x2"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows > 1 and $cols > 1 0 137 137
Condition33%1/3
$rows > 1 and $cols > 1
!ll&&!rl&&r
00137
MC/DC0%0/2
$rows > 1 and $cols > 1
$rows > 1 $cols > 1
Truth table33%1/3
A and B
A= $rows > 1B= $cols > 1
ABresult
0X0
100
111
481 unless ($rows > 1 and $cols > 1);
482 137 ▶ croak "Math::MatrixReal::minor(): $r and $c must be positive"
Branch: 1 branch
BranchTrueFalseTotal
unless $r > 0 and $c > 0 0 137 137
Condition33%1/3
$r > 0 and $c > 0
!ll&&!rl&&r
00137
MC/DC0%0/2
$r > 0 and $c > 0
$r > 0 $c > 0
Truth table33%1/3
A and B
A= $r > 0B= $c > 0
ABresult
0X0
100
111
483 unless ($r > 0 and $c > 0 );
484 137 ▶ croak "Math::MatrixReal::minor(): matrix has no $r,$c element"
Branch: 1 branch
BranchTrueFalseTotal
unless $r <= $rows and $c <= $cols 0 137 137
Condition33%1/3
$r <= $rows and $c <= $cols
!ll&&!rl&&r
00137
MC/DC0%0/2
$r <= $rows and $c <= $cols
$r <= $rows $c <= $cols
Truth table33%1/3
A and B
A= $r <= $rowsB= $c <= $cols
ABresult
0X0
100
111
485 unless ($r <= $rows and $c <= $cols );
486  
487 137 my $minor = new Math::MatrixReal($rows-1,$cols-1);
488 137 my ($i,$j) = (0,0);
489  
490 ## assign() might have been easier, but this should be faster
491 ## the element can be in any of 4 regions compared to the eliminated
492 ## row and col:
493 ## above and to the left, above and to the right
494 ## below and to the left, below and to the right
495  
496 137 ▶ for(; $i < $rows; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 677 137 814
497 677 ▶ for(;$j < $rows; $j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rows) 3577 677 4254
498 3577 ▶ if( $i >= $r && $j >= $c ){
Branch: 4 branches
BranchTrueFalseTotal
if ($i >= $r and $j >= $c) { } 595 2982 3577
elsif ($i >= $r and $j < $c) { } 864 2118 2982
elsif ($i < $r and $j < $c) { } 1260 858 2118
elsif ($i < $r and $j >= $c) { } 858 0 858
Condition100%3/3
$i >= $r and $j >= $c
!ll&&!rl&&r
2118864595
MC/DC100%2/2
$i >= $r and $j >= $c
$i >= $r $j >= $c
Truth table100%3/3
A and B
A= $i >= $rB= $j >= $c
ABresult
0X0
100
111
499 595 $minor->[0][$i-1][$j-1] = $matrix->[0][$i][$j];
500 ▶ } elsif ( $i >= $r && $j < $c ){
Condition66%2/3
$i >= $r and $j < $c
!ll&&!rl&&r
21180864
MC/DC50%1/2
$i >= $r and $j < $c
$i >= $r $j < $c
Truth table66%2/3
A and B
A= $i >= $rB= $j < $c
ABresult
0X0
100
111
501 864 $minor->[0][$i-1][$j] = $matrix->[0][$i][$j];
502 ▶ } elsif ( $i < $r && $j < $c ){
Condition66%2/3
$i < $r and $j < $c
!ll&&!rl&&r
08581260
MC/DC50%1/2
$i < $r and $j < $c
$i < $r $j < $c
Truth table66%2/3
A and B
A= $i < $rB= $j < $c
ABresult
0X0
100
111
503 1260 $minor->[0][$i][$j] = $matrix->[0][$i][$j];
504 ▶ } elsif ( $i < $r && $j >= $c ){
Condition33%1/3
$i < $r and $j >= $c
!ll&&!rl&&r
00858
MC/DC0%0/2
$i < $r and $j >= $c
$i < $r $j >= $c
Truth table33%1/3
A and B
A= $i < $rB= $j >= $c
ABresult
0X0
100
111
505 858 $minor->[0][$i][$j-1] = $matrix->[0][$i][$j];
506 } else {
507 0 croak "Very bad things";
508 }
509 }
510 677 $j = 0;
511 }
512 137 return ($minor);
513 }
514 1 ▶sub swap_col {
Subroutine swap_col: called CC=7 SCAR=21.3
Pod documented
515 1 ▶ croak "Usage: \$matrix->swap_col(\$col1,\$col2); " unless (@_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
516 1 my ($matrix,$col1,$col2) = @_;
517 1 my ($rows,$cols) = $matrix->dim();
518 1 my (@temp);
519  
520 1 ▶ croak "Math::MatrixReal::swap_col(): col index is not valid"
Branch: 1 branch
BranchTrueFalseTotal
unless $col1 <= $cols and $col2 <= $cols and $col1 == int $col1 and $col2 == int $col2 0 1 1
Condition33%3/9
$col1 <= $cols and $col2 <= $cols and $col1 == int $col1 and $col2 == int $col2
!ll&&!rl&&r
001
$col1 <= $cols and $col2 <= $cols and $col1 == int $col1
!ll&&!rl&&r
001
$col1 <= $cols and $col2 <= $cols
!ll&&!rl&&r
001
MC/DC0%0/4
$col1 <= $cols and $col2 <= $cols and $col1 == int $col1 and $col2 == int $col2
$col1 <= $cols $col2 <= $cols $col1 == int $col1 $col2 == int $col2
Truth table20%1/5
A and B and C and D
A= $col1 <= $colsB= $col2 <= $colsC= $col1 == int $col1D= $col2 == int $col2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
521 unless ( $col1 <= $cols && $col2 <= $cols &&
522 $col1 == int($col1) &&
523 $col2 == int($col2) );
524 1 $col1--;$col2--;
525 1 ▶ for(my $i=0;$i < $rows;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 4 1 5
526 4 $temp[$i] = $matrix->[0][$i][$col1];
527 4 $matrix->[0][$i][$col1] = $matrix->[0][$i][$col2];
528 4 $matrix->[0][$i][$col2] = $temp[$i];
529 }
530 }
531 1 ▶sub swap_row {
Subroutine swap_row: called CC=7 SCAR=21.3
Pod documented
532 1 ▶ croak "Usage: \$matrix->swap_row(\$row1,\$row2); " unless (@_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
533 1 my ($matrix,$row1,$row2) = @_;
534 1 my ($rows,$cols) = $matrix->dim();
535 1 my (@temp);
536  
537 1 ▶ croak "Math::MatrixReal::swap_row(): row index is not valid"
Branch: 1 branch
BranchTrueFalseTotal
unless $row1 <= $rows and $row2 <= $rows and $row1 == int $row1 and $row2 == int $row2 0 1 1
Condition33%3/9
$row1 <= $rows and $row2 <= $rows and $row1 == int $row1 and $row2 == int $row2
!ll&&!rl&&r
001
$row1 <= $rows and $row2 <= $rows and $row1 == int $row1
!ll&&!rl&&r
001
$row1 <= $rows and $row2 <= $rows
!ll&&!rl&&r
001
MC/DC0%0/4
$row1 <= $rows and $row2 <= $rows and $row1 == int $row1 and $row2 == int $row2
$row1 <= $rows $row2 <= $rows $row1 == int $row1 $row2 == int $row2
Truth table20%1/5
A and B and C and D
A= $row1 <= $rowsB= $row2 <= $rowsC= $row1 == int $row1D= $row2 == int $row2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
538 unless ( $row1 <= $rows && $row2 <= $rows &&
539 $row1 == int($row1) &&
540 $row2 == int($row2) );
541 1 $row1--;$row2--;
542 1 ▶ for(my $j=0;$j < $cols;$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 4 1 5
543 4 $temp[$j] = $matrix->[0][$row1][$j];
544 4 $matrix->[0][$row1][$j] = $matrix->[0][$row2][$j];
545 4 $matrix->[0][$row2][$j] = $temp[$j];
546 }
547 }
548  
549 4 ▶sub assign_row {
Subroutine assign_row: called CC=4 SCAR=13.9
Pod documented
550 4 ▶ croak "Usage: \$matrix->assign_row(\$row,\$row_vec);" unless (@_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 1 3 4
551 3 my ($matrix,$row,$row_vec) = @_;
552 3 my ($rows1,$cols1) = $matrix->dim();
553 3 my ($rows2,$cols2) = $row_vec->dim();
554
555 2 ▶ croak "Math::MatrixReal::assign_row(): number of columns mismatch" if ($cols1 != $cols2);
Branch: 1 branch
BranchTrueFalseTotal
if $cols1 != $cols2 1 1 2
556 1 ▶ croak "Math::MatrixReal::assign_row(): not a row vector" unless( $rows2 == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows2 == 1 0 1 1
557  
558 1 @{$matrix->[0][--$row]} = @{$row_vec->[0][0]};
559 1 return $matrix;
560 }
561 # returns the number of zeroes in a row
562 0 ▶sub _count_zeroes_row {
Subroutine _count_zeroes_row: not called CC=3 SCAR=24.8
563 0 my ($matrix) = @_;
564 0 my ($rows,$cols) = $matrix->dim();
565 0 my $count = 0;
566 0 ▶ croak "_count_zeroes_row(): only 1 row, buddy" unless ($rows == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == 1 0 0 0
567  
568 0 ▶ map { $count++ unless $matrix->[0][0][$_] } (0 .. $cols-1);
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->[0][0][$_] 0 0 0
569 0 return $count;
570 }
571 ## divide a row by it's largest abs() element
572 0 ▶sub _normalize_row {
Subroutine _normalize_row: not called CC=5 SCAR=34.0
573 0 my ($matrix) = @_;
574 0 my ($rows,$cols) = $matrix->dim();
575 0 my $new_row = Math::MatrixReal->new(1,$cols);
576  
577 0 my $big = abs($matrix->[0][0][0]);
578 0 ▶ for(my $j=0;$j < $cols; $j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 0 0 0
579 0 ▶ $big = $big < abs($matrix->[0][0][$j])
Branch: 1 branch
BranchTrueFalseTotal
$big < abs $matrix->[0][0][$j] ? : 0 0 0
580 ? abs($matrix->[0][0][$j]) : $big;
581 }
582 0 ▶ next unless $big;
Branch: 1 branch
BranchTrueFalseTotal
unless $big 0 0 0
583 # now $big is biggest element in row
584 0 ▶ for(my $j = 0;$j < $cols; $j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 0 0 0
585 0 $new_row->[0][0][$j] = $matrix->[0][0][$j] / $big;
586 }
587 0 return $new_row;
588  
589 }
590  
591 7 ▶sub cofactor {
Subroutine cofactor: called CC=2 SCAR=6.9
Pod undocumented
592 7 my ($matrix) = @_;
593 7 my ($rows,$cols) = $matrix->dim();
594  
595 7 ▶ croak "Math::MatrixReal::cofactor(): Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 7 7
596 unless ($rows == $cols);
597  
598 # black magic ahead
599 my $cofactor = $matrix->each(
600 133 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
601 133 my($v,$i,$j) = @_;
602 133 ▶ ($i+$j) % 2 == 0 ? $matrix->minor($i,$j)->det() : -1*$matrix->minor($i,$j)->det();
Branch: 1 branch
BranchTrueFalseTotal
($i + $j) % 2 == 0 ? : 69 64 133
603 7 });
604 7 return ($cofactor);
605 }
606  
607 4 ▶sub adjoint {
Subroutine adjoint: called CC=1 SCAR=0.0
Pod undocumented
608 4 my ($matrix) = @_;
609 4 return ~($matrix->cofactor);
610 }
611  
612 sub row
613 19 ▶{
Subroutine row: called CC=5 SCAR=16.6
Pod documented
614 19 ▶ croak "Usage: \$row_vector = \$matrix->row(\$row);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 19 19
615 if (@_ != 2);
616  
617 19 my($matrix,$row) = @_;
618 19 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
619 19 my($temp);
620  
621 19 ▶ croak "Math::MatrixReal::row(): row index out of range" if ($row < 1 || $row > $rows);
Branch: 1 branch
BranchTrueFalseTotal
if $row < 1 or $row > $rows 0 19 19
Condition33%1/3
$row < 1 or $row > $rows
l!l&&r!l&&!r
0019
MC/DC0%0/2
$row < 1 or $row > $rows
$row < 1 $row > $rows
Truth table33%1/3
A or B
A= $row < 1B= $row > $rows
ABresult
1X1
011
000
622  
623 19 $row--;
624 19 $temp = $matrix->new(1,$cols);
625 19 ▶ for ( my $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 107 19 126
626 {
627 107 $temp->[0][0][$j] = $matrix->[0][$row][$j];
628 }
629 19 return($temp);
630 }
631 4 ▶sub col{ return (shift)->column(shift) }
Subroutine col: called CC=1 SCAR=0.0
Pod undocumented
632 sub column
633 39 ▶{
Subroutine column: called CC=4 SCAR=14.4
Pod documented
634 39 ▶ croak "Usage: \$column_vector = \$matrix->column(\$column);" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 39 39
635  
636 39 my($matrix,$col) = @_;
637 39 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
638 #my($temp);
639 #my($i);
640 39 my $col_vector;
641  
642 39 ▶ croak "Math::MatrixReal::column(): column index out of range" if ($col < 1 || $col > $cols);
Branch: 1 branch
BranchTrueFalseTotal
if $col < 1 or $col > $cols 0 39 39
Condition33%1/3
$col < 1 or $col > $cols
l!l&&r!l&&!r
0039
MC/DC0%0/2
$col < 1 or $col > $cols
$col < 1 $col > $cols
Truth table33%1/3
A or B
A= $col < 1B= $col > $cols
ABresult
1X1
011
000
643  
644 39 $col--;
645 39 $col_vector = $matrix->new($rows,1);
646  
647 39 map { $col_vector->[0][$_][0] = $matrix->[0][$_][$col] } (0 .. $rows-1);
648  
649 39 return $col_vector;
650 }
651  
652 sub as_list
653 2 ▶{
Subroutine as_list: called CC=4 SCAR=13.9
Pod documented
654 2 ▶ croak "Usage: \$matrix->as_list();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
655  
656 2 my($self) = @_;
657 2 my($rows,$cols) = ($self->[1], $self->[2]);
658 2 my @list;
659 2 ▶ for(my $i = 0; $i < $rows; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 4 2 6
660 4 ▶ for(my $j = 0; $j < $cols; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 10 4 14
661 10 push @list, $self->[0][$i][$j];
662 }
663 }
664 2 return @list;
665 }
666  
667 sub _undo_LR
668 3305 ▶{
Subroutine _undo_LR: called CC=2 SCAR=7.0
669 3305 ▶ croak "Usage: \$matrix->_undo_LR();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 3305 3305
670 if (@_ != 1);
671  
672 3305 my($self) = @_;
673  
674 3305 undef $self->[3];
675 3305 undef $self->[4];
676 3305 undef $self->[5];
677 }
678 # brrr
679 sub zero
680 66 ▶{
Subroutine zero: called CC=2 SCAR=6.9
Pod documented
681 66 ▶ croak "Usage: \$matrix->zero();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 66 66
682  
683 66 my ($self) = @_;
684 66 my ($rows,$cols) = ($self->[1],$self->[2]);
685  
686 66 $self->_undo_LR();
687  
688 # zero out first row
689 66 map { $self->[0][0][$_] = 0.0 } (0 .. $cols-1);
690
691 # copy that to the other rows
692 66 map { @{$self->[0][$_]} = @{$self->[0][0]} } (0 .. $rows-1);
693  
694 66 return $self;
695 }
696  
697 sub one
698 24 ▶{
Subroutine one: called CC=2 SCAR=7.0
Pod documented
699 24 ▶ croak "Usage: \$matrix->one();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 24 24
700  
701 24 my ($self) = @_;
702 24 my ($rows,$cols) = ($self->[1],$self->[2]);
703  
704 24 $self->zero(); # We rely on zero() efficiency
705  
706 24 map { $self->[0][$_][$_] = 1.0 } (0 .. $rows-1);
707  
708 24 return $self;
709 }
710  
711 sub assign
712 2794 ▶{
Subroutine assign: called CC=6 SCAR=20.5
Pod documented
713 2794 ▶ croak "Usage: \$matrix->assign(\$row,\$column,\$value);" if (@_ != 4);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 2794 2794
714  
715 2794 my($self,$row,$col,$value) = @_;
716 2794 my($rows,$cols) = ($self->[1],$self->[2]);
717  
718 2794 ▶ croak "Math::MatrixReal::assign(): row index out of range" if (($row < 1) || ($row > $rows));
Branch: 1 branch
BranchTrueFalseTotal
if $row < 1 or $row > $rows 0 2794 2794
Condition33%1/3
$row < 1 or $row > $rows
l!l&&r!l&&!r
002794
MC/DC0%0/2
$row < 1 or $row > $rows
$row < 1 $row > $rows
Truth table33%1/3
A or B
A= $row < 1B= $row > $rows
ABresult
1X1
011
000
719 2794 ▶ croak "Math::MatrixReal::assign(): column index out of range" if (($col < 1) || ($col > $cols));
Branch: 1 branch
BranchTrueFalseTotal
if $col < 1 or $col > $cols 0 2794 2794
Condition33%1/3
$col < 1 or $col > $cols
l!l&&r!l&&!r
002794
MC/DC0%0/2
$col < 1 or $col > $cols
$col < 1 $col > $cols
Truth table33%1/3
A or B
A= $col < 1B= $col > $cols
ABresult
1X1
011
000
720  
721 2794 $self->_undo_LR();
722 2794 $self->[0][--$row][--$col] = $value;
723 }
724  
725 sub element
726 3277 ▶{
Subroutine element: called CC=6 SCAR=20.9
Pod documented
727 3277 ▶ croak "Usage: \$value = \$matrix->element(\$row,\$column);" if (@_ != 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 3277 3277
728  
729 3277 my($self,$row,$col) = @_;
730 3277 my($rows,$cols) = ($self->[1],$self->[2]);
731  
732 3277 ▶ croak "Math::MatrixReal::element(): row index out of range" if (($row < 1) || ($row > $rows));
Branch: 1 branch
BranchTrueFalseTotal
if $row < 1 or $row > $rows 0 3277 3277
Condition33%1/3
$row < 1 or $row > $rows
l!l&&r!l&&!r
003277
MC/DC0%0/2
$row < 1 or $row > $rows
$row < 1 $row > $rows
Truth table33%1/3
A or B
A= $row < 1B= $row > $rows
ABresult
1X1
011
000
733 3277 ▶ croak "Math::MatrixReal::element(): column index out of range" if (($col < 1) || ($col > $cols));
Branch: 1 branch
BranchTrueFalseTotal
if $col < 1 or $col > $cols 0 3277 3277
Condition33%1/3
$col < 1 or $col > $cols
l!l&&r!l&&!r
003277
MC/DC0%0/2
$col < 1 or $col > $cols
$col < 1 $col > $cols
Truth table33%1/3
A or B
A= $col < 1B= $col > $cols
ABresult
1X1
011
000
734  
735 3277 return( $self->[0][--$row][--$col] );
736 }
737  
738 sub dim # returns dimensions of a matrix
739 870 ▶{
Subroutine dim: called CC=2 SCAR=7.1
Pod undocumented
740 870 ▶ croak "Usage: (\$rows,\$columns) = \$matrix->dim();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 870 870
741  
742 870 my($matrix) = @_;
743  
744 870 return( $matrix->[1], $matrix->[2] );
745 }
746  
747 sub norm_one # maximum of sums of each column
748 191 ▶{
Subroutine norm_one: called CC=5 SCAR=16.1
Pod undocumented
749 191 ▶ croak "Usage: \$norm_one = \$matrix->norm_one();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 191 191
750  
751 191 my($self) = @_;
752 191 my($rows,$cols) = ($self->[1],$self->[2]);
753  
754 191 my $max = 0.0;
755 191 ▶ for (my $j = 0; $j < $cols; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 1343 191 1534
756 {
757 1343 my $sum = 0.0;
758 1343 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 21545 1343 22888
759 {
760 21545 $sum += abs( $self->[0][$i][$j] );
761 }
762 1343 ▶ $max = $sum if ($sum > $max);
Branch: 1 branch
BranchTrueFalseTotal
if $sum > $max 205 1138 1343
763 }
764 191 return($max);
765 }
766 ## sum of absolute value of every element
767 1 ▶sub norm_sum {
Subroutine norm_sum: called CC=2 SCAR=7.0
Pod undocumented
768 1 ▶ croak "Usage: \$norm_sum = \$matrix->norm_sum();" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
769 1 my ($matrix) = @_;
770 1 my $norm = 0;
771 1 ▶ $matrix->each( sub { $norm+=abs(shift); } );
Subroutine __ANON__: called CC=1 SCAR=0.0
772 1 return $norm;
773 }
774 1 ▶sub norm_frobenius {
Subroutine norm_frobenius: called CC=1 SCAR=0.0
Pod undocumented
775 1 my ($m) = @_;
776 1 my ($r,$c) = $m->dim;
777 1 my $s=0;
778  
779 1 ▶ $m->each( sub { $s+=abs(shift)**2 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
780 1 return sqrt($s);
781 }
782  
783 # Vector Norm
784 5 ▶sub norm_p {
Subroutine norm_p: called CC=6 SCAR=18.3
Pod undocumented
785 5 my ($v,$p) = @_;
786 # sanity check on $p
787 5 ▶ croak "Math::MatrixReal:norm_p: argument must be a row or column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->is_row_vector or $v->is_col_vector 0 5 5
Condition33%1/3
$v->is_row_vector or $v->is_col_vector
l!l&&r!l&&!r
050
MC/DC0%0/2
$v->is_row_vector or $v->is_col_vector
$v->is_row_vector $v->is_col_vector
Truth table33%1/3
A or B
A= $v->is_row_vectorB= $v->is_col_vector
ABresult
1X1
011
000
788 unless ( $v->is_row_vector || $v->is_col_vector );
789 5 ▶ croak "Math::MatrixReal::norm_p: $p must be >= 1"
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /Inf(inity)?/i or $p >= 1 0 5 5
Condition66%2/3
$p =~ /Inf(inity)?/i or $p >= 1
l!l&&r!l&&!r
140
MC/DC0%0/2
$p =~ /Inf(inity)?/i or $p >= 1
$p =~ /Inf(inity)?/i $p >= 1
Truth table66%2/3
A or B
A= $p =~ /Inf(inity)?/iB= $p >= 1
ABresult
1X1
011
000
790 unless ($p =~ m/Inf(inity)?/i || $p >= 1);
791  
792 5 ▶ if( $p =~ m/^(Inf|Infinity)$/i ){
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^(Inf|Infinity)$/i) 1 4 5
793 1 my $max = $v->element(1,1);
794 1 ▶ $v->each ( sub { my $x=abs(shift); $max = $x if( $x > $max ); } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $x > $max 2 1 3
795 1 return $max;
796 }
797  
798 4 my $s=0;
799 4 ▶ $v->each( sub { $s+= (abs(shift))**$p; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
800 4 return $s ** (1/$p);
801  
802 }
803 sub norm_max # maximum of sums of each row
804 1 ▶{
Subroutine norm_max: called CC=5 SCAR=16.1
Pod undocumented
805 1 ▶ croak "Usage: \$norm_max = \$matrix->norm_max();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
806  
807 1 my($self) = @_;
808 1 my($rows,$cols) = ($self->[1],$self->[2]);
809  
810 1 my $max = 0.0;
811 1 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 5 1 6
812 {
813 5 my $sum = 0.0;
814 5 ▶ for (my $j = 0; $j < $cols; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 25 5 30
815 {
816 25 $sum += abs( $self->[0][$i][$j] );
817 }
818 5 ▶ $max = $sum if ($sum > $max);
Branch: 1 branch
BranchTrueFalseTotal
if $sum > $max 4 1 5
819 }
820 1 return($max);
821 }
822  
823 sub negate
824 1 ▶{
Subroutine negate: called CC=6 SCAR=18.4
Pod undocumented
825 1 ▶ croak "Usage: \$matrix1->negate(\$matrix2);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 1 1
826 if (@_ != 2);
827  
828 1 my($matrix1,$matrix2) = @_;
829 1 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
830 1 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
831  
832 1 ▶ croak "Math::MatrixReal::negate(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $cols1 == $cols2 0 1 1
Condition33%1/3
$rows1 == $rows2 and $cols1 == $cols2
!ll&&!rl&&r
001
MC/DC0%0/2
$rows1 == $rows2 and $cols1 == $cols2
$rows1 == $rows2 $cols1 == $cols2
Truth table33%1/3
A and B
A= $rows1 == $rows2B= $cols1 == $cols2
ABresult
0X0
100
111
833 unless (($rows1 == $rows2) && ($cols1 == $cols2));
834  
835 1 $matrix1->_undo_LR();
836  
837 1 ▶ for (my $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 10 1 11
838 {
839 10 ▶ for (my $j = 0; $j < $cols1; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols1) 100 10 110
840 {
841 100 $matrix1->[0][$i][$j] = -($matrix2->[0][$i][$j]);
842 }
843 }
844 }
845 ## each(): evaluate a coderef on each element and return a new matrix
846 ## of said
847 92 ▶sub each {
Subroutine each: called CC=5 SCAR=16.1
Pod documented
848 92 ▶ croak "Usage: \$new_matrix = \$matrix->each( \&sub );" unless (@_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 92 92
849 92 my($matrix,$function) = @_;
850 92 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
851 92 my($new_matrix) = $matrix->clone();
852  
853 92 ▶ croak "Math::MatrixReal::each(): argument is not a sub reference" unless ref($function);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $function 0 92 92
854 92 $new_matrix->_undo_LR();
855  
856 92 ▶ for (my $i = 0; $i < $rows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 681 92 773
857 681 ▶ for (my $j = 0; $j < $cols; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 12671 681 13352
858 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
859 # $i,$j are 1-based as of 1.7
860 12671 $new_matrix->[0][$i][$j] = &{ $function }($matrix->[0][$i][$j],$i+1,$j+1) ;
861 }
862 }
863 92 return ($new_matrix);
864 }
865 ## each_diag(): same as each() but only diag elements
866 39 ▶sub each_diag {
Subroutine each_diag: called CC=7 SCAR=19.5
Pod documented
867 39 ▶ croak "Usage: \$new_matrix = \$matrix->each_diag( \&sub );" unless (@_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 39 39
868 39 my($matrix,$function) = @_;
869 39 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
870 39 my($new_matrix) = $matrix->clone();
871  
872 39 ▶ croak "Math::MatrixReal::each(): argument is not a sub reference" unless ref($function);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $function 0 39 39
873 39 ▶ croak "Matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 39 39
874  
875 39 $new_matrix->_undo_LR();
876  
877 39 ▶ for (my $i = 0; $i < $rows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 166 39 205
878 166 ▶ for (my $j = 0; $j < $cols; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 972 166 1138
879 972 ▶ next unless ($i == $j);
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $j 806 166 972
880 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
881 # $i,$j are 1-based as of 1.7
882 166 $new_matrix->[0][$i][$j] = &{ $function }($matrix->[0][$i][$j],$i+1,$j+1) ;
883 }
884 }
885 39 return ($new_matrix);
886 }
887  
888 ## Make computing the inverse more user friendly
889 19 ▶sub inverse {
Subroutine inverse: called CC=2 SCAR=7.1
Pod undocumented
890 19 ▶ croak "Usage: \$inverse = \$matrix->inverse();" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 19 19
891 19 my ($matrix) = @_;
892 19 return $matrix->decompose_LR->invert_LR;
893 }
894  
895 69 ▶sub transpose {
Subroutine transpose: called CC=9 SCAR=22.1
Pod undocumented
896  
897 69 ▶ croak "Usage: \$matrix1->transpose(\$matrix2);" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 69 69
898  
899 69 my($matrix1,$matrix2) = @_;
900 69 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
901 69 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
902  
903 69 ▶ croak "Math::MatrixReal::transpose(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $cols2 and $cols1 == $rows2 0 69 69
Condition33%1/3
$rows1 == $cols2 and $cols1 == $rows2
!ll&&!rl&&r
0069
MC/DC0%0/2
$rows1 == $cols2 and $cols1 == $rows2
$rows1 == $cols2 $cols1 == $rows2
Truth table33%1/3
A and B
A= $rows1 == $cols2B= $cols1 == $rows2
ABresult
0X0
100
111
904 unless (($rows1 == $cols2) && ($cols1 == $rows2));
905  
906 69 $matrix1->_undo_LR();
907  
908 69 ▶ if ($rows1 == $cols1)
Branch: 1 branch
BranchTrueFalseTotal
if ($rows1 == $cols1) { } 59 10 69
909 {
910 # more complicated to make in-place possible!
911  
912 59 ▶ for (my $i = 0; $i < $rows1; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 597 59 656
913 {
914 597 ▶ for (my $j = ($i + 1); $j < $cols1; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols1) 6987 597 7584
915 {
916 6987 my $swap = $matrix2->[0][$i][$j];
917 6987 $matrix1->[0][$i][$j] = $matrix2->[0][$j][$i];
918 6987 $matrix1->[0][$j][$i] = $swap;
919 }
920 597 $matrix1->[0][$i][$i] = $matrix2->[0][$i][$i];
921 }
922 } else { # ($rows1 != $cols1)
923 10 ▶ for (my $i = 0; $i < $rows1; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 38 10 48
924 {
925 38 ▶ for (my $j = 0; $j < $cols1; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols1) 38 38 76
926 {
927 38 $matrix1->[0][$i][$j] = $matrix2->[0][$j][$i];
928 }
929 }
930 }
931 }
932  
933 sub add
934 25 ▶{
Subroutine add: called CC=8 SCAR=22.7
Pod undocumented
935 25 ▶ croak "Usage: \$matrix1->add(\$matrix2,\$matrix3);" if (@_ != 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 25 25
936  
937 25 my($matrix1,$matrix2,$matrix3) = @_;
938 25 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
939 25 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
940 25 my($rows3,$cols3) = ($matrix3->[1],$matrix3->[2]);
941  
942 25 ▶ croak "Math::MatrixReal::add(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3 0 25 25
Condition33%3/9
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3
!ll&&!rl&&r
0025
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2
!ll&&!rl&&r
0025
$rows1 == $rows2 and $rows1 == $rows3
!ll&&!rl&&r
0025
MC/DC0%0/4
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3
$rows1 == $rows2 $rows1 == $rows3 $cols1 == $cols2 $cols1 == $cols3
Truth table20%1/5
A and B and C and D
A= $rows1 == $rows2B= $rows1 == $rows3C= $cols1 == $cols2D= $cols1 == $cols3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
943 unless (($rows1 == $rows2) && ($rows1 == $rows3) &&
944 ($cols1 == $cols2) && ($cols1 == $cols3));
945  
946 25 $matrix1->_undo_LR();
947  
948 25 ▶ for ( my $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 378 25 403
949 {
950 378 ▶ for ( my $j = 0; $j < $cols1; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols1) 11142 378 11520
951 {
952 11142 $matrix1->[0][$i][$j] = $matrix2->[0][$i][$j] + $matrix3->[0][$i][$j];
953 }
954 }
955 }
956  
957 sub subtract
958 171 ▶{
Subroutine subtract: called CC=8 SCAR=22.7
Pod undocumented
959 171 ▶ croak "Usage: \$matrix1->subtract(\$matrix2,\$matrix3);" if (@_ != 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 171 171
960  
961 171 my($matrix1,$matrix2,$matrix3) = @_;
962 171 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
963 171 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
964 171 my($rows3,$cols3) = ($matrix3->[1],$matrix3->[2]);
965  
966 171 ▶ croak "Math::MatrixReal::subtract(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3 0 171 171
Condition33%3/9
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3
!ll&&!rl&&r
00171
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2
!ll&&!rl&&r
00171
$rows1 == $rows2 and $rows1 == $rows3
!ll&&!rl&&r
00171
MC/DC0%0/4
$rows1 == $rows2 and $rows1 == $rows3 and $cols1 == $cols2 and $cols1 == $cols3
$rows1 == $rows2 $rows1 == $rows3 $cols1 == $cols2 $cols1 == $cols3
Truth table20%1/5
A and B and C and D
A= $rows1 == $rows2B= $rows1 == $rows3C= $cols1 == $cols2D= $cols1 == $cols3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
967 unless (($rows1 == $rows2) && ($rows1 == $rows3) &&
968 ($cols1 == $cols2) && ($cols1 == $cols3));
969  
970 171 $matrix1->_undo_LR();
971  
972 171 ▶ for ( my $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 1537 171 1708
973 {
974 1537 ▶ for ( my $j = 0; $j < $cols1; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols1) 21703 1537 23240
975 {
976 21703 $matrix1->[0][$i][$j] = $matrix2->[0][$i][$j] - $matrix3->[0][$i][$j];
977 }
978 }
979 }
980  
981 sub multiply_scalar
982 24 ▶{
Subroutine multiply_scalar: called CC=5 SCAR=16.6
Pod undocumented
983 24 ▶ croak "Usage: \$matrix1->multiply_scalar(\$matrix2,\$scalar);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 24 24
984 if (@_ != 3);
985  
986 24 my($matrix1,$matrix2,$scalar) = @_;
987 24 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
988 24 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
989  
990 24 ▶ croak "Math::MatrixReal::multiply_scalar(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $cols1 == $cols2 0 24 24
Condition33%1/3
$rows1 == $rows2 and $cols1 == $cols2
!ll&&!rl&&r
0024
MC/DC0%0/2
$rows1 == $rows2 and $cols1 == $cols2
$rows1 == $rows2 $cols1 == $cols2
Truth table33%1/3
A and B
A= $rows1 == $rows2B= $cols1 == $cols2
ABresult
0X0
100
111
991 unless (($rows1 == $rows2) && ($cols1 == $cols2));
992  
993 24 $matrix1->_undo_LR();
994  
995 24 ▶ for ( my $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 249 24 273
996 {
997 249 map { $matrix1->[0][$i][$_] = $matrix2->[0][$i][$_] * $scalar } (0 .. $cols1-1);
998 }
999 }
1000  
1001 sub multiply
1002 45 ▶{
Subroutine multiply: called CC=6 SCAR=18.0
Pod undocumented
1003 45 ▶ croak "Usage: \$product_matrix = \$matrix1->multiply(\$matrix2);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 45 45
1004 if (@_ != 2);
1005  
1006 45 my($matrix1,$matrix2) = @_;
1007 45 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
1008 45 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
1009  
1010 45 ▶ croak "Math::MatrixReal::multiply(): matrix size mismatch" unless ($cols1 == $rows2);
Branch: 1 branch
BranchTrueFalseTotal
unless $cols1 == $rows2 0 45 45
1011  
1012 45 my $temp = $matrix1->new($rows1,$cols2);
1013 45 ▶ for (my $i = 0; $i < $rows1; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows1) 417 45 462
1014 {
1015 417 ▶ for (my $j = 0; $j < $cols2; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols2) 8427 417 8844
1016 {
1017 8427 my $sum = 0.0;
1018 8427 ▶ for (my $k = 0; $k < $cols1; $k++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $cols1) 224889 8427 233316
1019 {
1020 224889 $sum += ( $matrix1->[0][$i][$k] * $matrix2->[0][$k][$j] );
1021 }
1022 8427 $temp->[0][$i][$j] = $sum;
1023 }
1024 }
1025 45 return($temp);
1026 }
1027  
1028 21 ▶sub exponent {
Subroutine exponent: called CC=13 SCAR=26.3
Pod undocumented
1029 21 ▶ croak "Usage: \$matrix_exp = \$matrix1->exponent(\$integer);" if(@_ != 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 21 21
1030 21 my($matrix,$argument) = @_;
1031 21 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1032 21 my($name) = "'**'";
1033 21 my($temp) = $matrix->clone();
1034  
1035 21 ▶ croak "Matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 21 21
1036 21 ▶ croak "Exponent must be integer" unless ($argument =~ m/^[+-]?\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $argument =~ /^[+-]?\d+$/ 0 21 21
1037  
1038 21 ▶ return($matrix) if ($argument == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $argument == 1 1 20 21
1039  
1040 20 $temp->_undo_LR();
1041  
1042 # negative exponent is (A^-1)^n
1043 20 ▶ if( $argument < 0 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($argument < 0) { } 5 15 20
elsif ($argument == 0) { } 3 12 15
1044 5 my $LR = $matrix->decompose_LR();
1045 5 my $inverse = $LR->invert_LR();
1046 5 ▶ unless (defined $inverse){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inverse) 0 5 5
1047 0 carp "Matrix has no inverse";
1048 0 return undef;
1049 }
1050 5 $temp = $inverse->clone();
1051 5 ▶ if( $inverse ){
Branch: 1 branch
BranchTrueFalseTotal
if ($inverse) { } 5 0 5
1052 5 ▶ return($inverse) if ($argument == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $argument == -1 4 1 5
1053 1 for( 2 .. abs($argument) ){
1054 1 $temp = multiply($inverse,$temp);
1055 }
1056 1 return($temp);
1057 } else {
1058 # TODO: is this the right behaviour?
1059 0 carp "Cannot compute negative exponent, inverse does not exist";
1060 0 return undef;
1061 }
1062 # matrix to zero power is identity matrix
1063 } elsif( $argument == 0 ){
1064 3 $temp->one();
1065 3 return ($temp);
1066 }
1067  
1068 # if it is diagonal, just raise diagonal entries to power
1069 12 ▶ if( $matrix->is_diagonal() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->is_diagonal) { } 10 2 12
1070 10 ▶ $temp = $temp->each_diag( sub { (shift)**$argument } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1071 10 return ($temp);
1072
1073 } else {
1074 2 for( 2 .. $argument ){
1075 2 $temp = multiply($matrix,$temp);
1076 }
1077 2 return ($temp);
1078 }
1079 }
1080  
1081 sub min
1082 3 ▶{
Subroutine min: called CC=5 SCAR=16.3
Pod undocumented
1083  
1084 3 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 2 1 3
1085 2 my $matrix = shift;
1086  
1087 2 ▶ croak "Usage: \$minimum = Math::MatrixReal::min(\$number1,\$number2) or $matrix->min" if (@_ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 2 2
1088 2 ▶ croak "invalid" unless ref $matrix eq 'Math::MatrixReal';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matrix eq "Math::MatrixReal" 0 2 2
1089  
1090 2 my $min = $matrix->element(1,1);
1091 2 ▶ $matrix->each( sub { my ($e,$i,$j) = @_; $min = $e if $e < $min; } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $e < $min 4 496 500
1092 2 return $min;
1093 }
1094 1 ▶ $_[0] < $_[1] ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 1 0 1
1095 }
1096  
1097 sub max
1098 3 ▶{
Subroutine max: called CC=5 SCAR=16.3
Pod undocumented
1099 3 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 2 1 3
1100 2 my $matrix = shift;
1101  
1102 2 ▶ croak "Usage: \$maximum = Math::MatrixReal::max(\$number1,\$number2) or $matrix->max" if (@_ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 2 2
1103 2 ▶ croak "Math::MatrixReal::max(\$matrix) \$matrix is not a Math::MatrixReal matrix" unless ref $matrix eq 'Math::MatrixReal';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matrix eq "Math::MatrixReal" 0 2 2
1104
1105 2 my $max = $matrix->element(1,1);
1106 2 ▶ $matrix->each( sub { my ($e,$i,$j) = @_; $max = $e if $e > $max; } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $e > $max 14 486 500
1107 2 return $max;
1108 }
1109  
1110 1 ▶ $_[0] > $_[1] ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 0 1 1
1111 }
1112  
1113 sub kleene
1114 0 ▶{
Subroutine kleene: not called CC=7 SCAR=40.3
Pod undocumented
1115 0 ▶ croak "Usage: \$minimal_cost_matrix = \$cost_matrix->kleene();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
1116  
1117 0 my($matrix) = @_;
1118 0 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1119  
1120 0 ▶ croak "Math::MatrixReal::kleene(): matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 0 0
1121  
1122 0 my $temp = $matrix->new($rows,$cols);
1123 0 $temp->copy($matrix);
1124 0 $temp->_undo_LR();
1125 0 my $n = $rows;
1126 0 ▶ for ( my $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1127 {
1128 0 $temp->[0][$i][$i] = min( $temp->[0][$i][$i] , 0 );
1129 }
1130 0 ▶ for ( my $k = 0; $k < $n; $k++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $n) 0 0 0
1131 {
1132 0 ▶ for ( my $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1133 {
1134 0 ▶ for ( my $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1135 {
1136 0 $temp->[0][$i][$j] = min( $temp->[0][$i][$j] ,
1137 ( $temp->[0][$i][$k] +
1138 $temp->[0][$k][$j] ) );
1139 }
1140 }
1141 }
1142 0 return($temp);
1143 }
1144  
1145 sub normalize
1146 0 ▶{
Subroutine normalize: not called CC=10 SCAR=47.0
Pod undocumented
1147 0 ▶ croak "Usage: (\$norm_matrix,\$norm_vector) = \$matrix->normalize(\$vector);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
1148 if (@_ != 2);
1149  
1150 0 my($matrix,$vector) = @_;
1151 0 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1152 0 my($norm_matrix,$norm_vector);
1153 0 my($max,$val);
1154 0 my($i,$j,$n);
1155  
1156 0 ▶ croak "Math::MatrixReal::normalize(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 0 0
1157 unless ($rows == $cols);
1158  
1159 0 $n = $rows;
1160  
1161 0 ▶ croak "Math::MatrixReal::normalize(): vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $vector->[2] == 1 0 0 0
1162 unless ($vector->[2] == 1);
1163  
1164 0 ▶ croak "Math::MatrixReal::normalize(): matrix and vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $vector->[1] == $n 0 0 0
1165 unless ($vector->[1] == $n);
1166  
1167 0 $norm_matrix = $matrix->new($n,$n);
1168 0 $norm_vector = $vector->new($n,1);
1169  
1170 0 $norm_matrix->copy($matrix);
1171 0 $norm_vector->copy($vector);
1172  
1173 0 $norm_matrix->_undo_LR();
1174  
1175 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1176 {
1177 0 $max = abs($norm_vector->[0][$i][0]);
1178 0 ▶ for ( $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1179 {
1180 0 $val = abs($norm_matrix->[0][$i][$j]);
1181 0 ▶ if ($val > $max) { $max = $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 0 0 0
1182 }
1183 0 ▶ if ($max != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($max != 0) 0 0 0
1184 {
1185 0 $norm_vector->[0][$i][0] /= $max;
1186 0 ▶ for ( $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1187 {
1188 0 $norm_matrix->[0][$i][$j] /= $max;
1189 }
1190 }
1191 }
1192 0 return($norm_matrix,$norm_vector);
1193 }
1194  
1195 sub decompose_LR
1196 177 ▶{
Subroutine decompose_LR: called CC=16 SCAR=27.7
Pod undocumented
1197 177 ▶ croak "Usage: \$LR_matrix = \$matrix->decompose_LR();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 177 177
1198 if (@_ != 1);
1199  
1200 177 my($matrix) = @_;
1201 177 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1202 177 my($perm_row,$perm_col);
1203 177 my($row,$col,$max);
1204 177 my($i,$j,$k,$n);
1205 177 my($sign) = 1;
1206 177 my($swap);
1207 my($temp);
1208  
1209 177 ▶ croak "Math::MatrixReal::decompose_LR(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 177 177
1210 unless ($rows == $cols);
1211  
1212 177 $temp = $matrix->new($rows,$cols);
1213 177 $temp->copy($matrix);
1214 177 $n = $rows;
1215 177 $perm_row = [ ];
1216 177 $perm_col = [ ];
1217 177 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 779 177 956
1218 {
1219 779 $perm_row->[$i] = $i;
1220 779 $perm_col->[$i] = $i;
1221 }
1222 NONZERO:
1223 177 ▶ for ( $k = 0; $k < $n; $k++ ) # use Gauss's algorithm:
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $n) 776 174 950
1224 {
1225 # complete pivot-search:
1226  
1227 776 $max = 0;
1228 776 ▶ for ( $i = $k; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2265 776 3041
1229 {
1230 2265 ▶ for ( $j = $k; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 8483 2265 10748
1231 {
1232 8483 ▶ if (($swap = abs($temp->[0][$i][$j])) > $max)
Branch: 1 branch
BranchTrueFalseTotal
if (($swap = abs $temp->[0][$i][$j]) > $max) 1872 6611 8483
1233 {
1234 1872 $max = $swap;
1235 1872 $row = $i;
1236 1872 $col = $j;
1237 }
1238 }
1239 }
1240 776 ▶ last NONZERO if ($max == 0); # (all remaining elements are zero)
Branch: 1 branch
BranchTrueFalseTotal
if $max == 0 3 773 776
1241 773 ▶ if ($k != $row) # swap row $k and row $row:
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $row) 384 389 773
1242 {
1243 384 $sign = -$sign;
1244 384 $swap = $perm_row->[$k];
1245 384 $perm_row->[$k] = $perm_row->[$row];
1246 384 $perm_row->[$row] = $swap;
1247 384 ▶ for ( $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1947 384 2331
1248 {
1249 # (must run from 0 since L has to be swapped too!)
1250  
1251 1947 $swap = $temp->[0][$k][$j];
1252 1947 $temp->[0][$k][$j] = $temp->[0][$row][$j];
1253 1947 $temp->[0][$row][$j] = $swap;
1254 }
1255 }
1256 773 ▶ if ($k != $col) # swap column $k and column $col:
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $col) 429 344 773
1257 {
1258 429 $sign = -$sign;
1259 429 $swap = $perm_col->[$k];
1260 429 $perm_col->[$k] = $perm_col->[$col];
1261 429 $perm_col->[$col] = $swap;
1262 429 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2139 429 2568
1263 {
1264 2139 $swap = $temp->[0][$i][$k];
1265 2139 $temp->[0][$i][$k] = $temp->[0][$i][$col];
1266 2139 $temp->[0][$i][$col] = $swap;
1267 }
1268 }
1269 773 ▶ for ( $i = ($k + 1); $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1486 773 2259
1270 {
1271 # scan the remaining rows, add multiples of row $k to row $i:
1272  
1273 1486 $swap = $temp->[0][$i][$k] / $temp->[0][$k][$k];
1274 1486 ▶ if ($swap != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($swap != 0) 1357 129 1486
1275 {
1276 # calculate a row of matrix R:
1277  
1278 1357 ▶ for ( $j = ($k + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 4114 1357 5471
1279 {
1280 4114 $temp->[0][$i][$j] -= $temp->[0][$k][$j] * $swap;
1281 }
1282  
1283 # store matrix L in same matrix as R:
1284  
1285 1357 $temp->[0][$i][$k] = $swap;
1286 }
1287 }
1288 }
1289 177 $temp->[3] = $sign;
1290 177 $temp->[4] = $perm_row;
1291 177 $temp->[5] = $perm_col;
1292 177 return($temp);
1293 }
1294  
1295 sub solve_LR
1296 113 ▶{
Subroutine solve_LR: called CC=17 SCAR=36.0
Pod undocumented
1297 113 ▶ croak "Usage: (\$dimension,\$x_vector,\$base_matrix) = \$LR_matrix->solve_LR(\$b_vector);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 113 113
1298 if (@_ != 2);
1299  
1300 113 my($LR_matrix,$b_vector) = @_;
1301 113 my($rows,$cols) = ($LR_matrix->[1],$LR_matrix->[2]);
1302 113 my($dimension,$x_vector,$base_matrix);
1303 113 my($perm_row,$perm_col);
1304 113 my($y_vector,$sum);
1305 113 my($i,$j,$k,$n);
1306  
1307 113 ▶ croak "Math::MatrixReal::solve_LR(): not an LR decomposition matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $LR_matrix->[3] and $rows == $cols 0 113 113
Condition33%1/3
defined $LR_matrix->[3] and $rows == $cols
!ll&&!rl&&r
00113
MC/DC0%0/2
defined $LR_matrix->[3] and $rows == $cols
defined $LR_matrix->[3] $rows == $cols
Truth table33%1/3
A and B
A= defined $LR_matrix->[3]B= $rows == $cols
ABresult
0X0
100
111
1308 unless ((defined $LR_matrix->[3]) && ($rows == $cols));
1309  
1310 113 $n = $rows;
1311  
1312 113 ▶ croak "Math::MatrixReal::solve_LR(): vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $b_vector->[2] == 1 0 113 113
1313 unless ($b_vector->[2] == 1);
1314  
1315 113 ▶ croak "Math::MatrixReal::solve_LR(): matrix and vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $b_vector->[1] == $n 0 113 113
1316 unless ($b_vector->[1] == $n);
1317  
1318 113 $perm_row = $LR_matrix->[4];
1319 113 $perm_col = $LR_matrix->[5];
1320  
1321 113 $x_vector = $b_vector->new($n,1);
1322 113 $y_vector = $b_vector->new($n,1);
1323 113 $base_matrix = $LR_matrix->new($n,$n);
1324  
1325 # calculate "x" so that LRx = b ==> calculate Ly = b, Rx = y:
1326  
1327 113 ▶ for ( $i = 0; $i < $n; $i++ ) # calculate $y_vector:
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 665 113 778
1328 {
1329 665 $sum = $b_vector->[0][($perm_row->[$i])][0];
1330 665 ▶ for ( $j = 0; $j < $i; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 1987 665 2652
1331 {
1332 1987 $sum -= $LR_matrix->[0][$i][$j] * $y_vector->[0][$j][0];
1333 }
1334 665 $y_vector->[0][$i][0] = $sum;
1335 }
1336  
1337 113 $dimension = 0;
1338 113 ▶ for ( $i = ($n - 1); $i >= 0; $i-- ) # calculate $x_vector:
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 665 113 778
1339 {
1340 665 ▶ if ($LR_matrix->[0][$i][$i] == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($LR_matrix->[0][$i][$i] == 0) { } 0 665 665
1341 {
1342 0 ▶ if ($y_vector->[0][$i][0] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($y_vector->[0][$i][0] != 0) { } 0 0 0
1343 {
1344 0 return(); # a solution does not exist!
1345 }
1346 else
1347 {
1348 0 $dimension++;
1349 0 $x_vector->[0][($perm_col->[$i])][0] = 0;
1350 }
1351 } else {
1352 665 $sum = $y_vector->[0][$i][0];
1353 665 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1987 665 2652
1354 {
1355 1987 $sum -= $LR_matrix->[0][$i][$j] *
1356 $x_vector->[0][($perm_col->[$j])][0];
1357 }
1358 665 $x_vector->[0][($perm_col->[$i])][0] =
1359 $sum / $LR_matrix->[0][$i][$i];
1360 }
1361 }
1362 113 ▶ if ($dimension)
Branch: 1 branch
BranchTrueFalseTotal
if ($dimension) 0 113 113
1363 {
1364 0 ▶ if ($dimension == $n)
Branch: 1 branch
BranchTrueFalseTotal
if ($dimension == $n) { } 0 0 0
1365 {
1366 0 $base_matrix->one();
1367 } else {
1368 0 ▶ for ( $k = 0; $k < $dimension; $k++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $dimension) 0 0 0
1369 {
1370 0 $base_matrix->[0][($perm_col->[($n-$k-1)])][$k] = 1;
1371 0 ▶ for ( $i = ($n-$dimension-1); $i >= 0; $i-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
1372 {
1373 0 $sum = 0;
1374 0 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1375 {
1376 0 $sum -= $LR_matrix->[0][$i][$j] *
1377 $base_matrix->[0][($perm_col->[$j])][$k];
1378 }
1379 0 $base_matrix->[0][($perm_col->[$i])][$k] =
1380 $sum / $LR_matrix->[0][$i][$i];
1381 }
1382 }
1383 }
1384 }
1385 113 return( $dimension, $x_vector, $base_matrix );
1386 }
1387  
1388 sub invert_LR
1389 25 ▶{
Subroutine invert_LR: called CC=9 SCAR=23.1
Pod undocumented
1390 25 ▶ croak "Usage: \$inverse_matrix = \$LR_matrix->invert_LR();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 25 25
1391 if (@_ != 1);
1392  
1393 25 my($matrix) = @_;
1394 25 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1395 25 my($inv_matrix,$x_vector,$y_vector);
1396 25 my($i,$j,$n);
1397  
1398 25 ▶ croak "Math::MatrixReal::invert_LR(): not an LR decomposition matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix->[3] and $rows == $cols 0 25 25
Condition33%1/3
defined $matrix->[3] and $rows == $cols
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $matrix->[3] and $rows == $cols
defined $matrix->[3] $rows == $cols
Truth table33%1/3
A and B
A= defined $matrix->[3]B= $rows == $cols
ABresult
0X0
100
111
1399 unless ((defined $matrix->[3]) && ($rows == $cols));
1400  
1401 25 $n = $rows;
1402 #print Dumper [ $matrix ];
1403 25 ▶ if ($matrix->[0][$n-1][$n-1] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->[0][$n - 1][$n - 1] != 0) { } 25 0 25
1404 {
1405 25 $inv_matrix = $matrix->new($n,$n);
1406 25 $y_vector = $matrix->new($n,1);
1407 25 ▶ for ( $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 112 25 137
1408 {
1409 112 ▶ if ($j > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) 87 25 112
1410 {
1411 87 $y_vector->[0][$j-1][0] = 0;
1412 }
1413 112 $y_vector->[0][$j][0] = 1;
1414 112 ▶ if (($rows,$x_vector,$cols) = $matrix->solve_LR($y_vector))
Branch: 1 branch
BranchTrueFalseTotal
if (($rows, $x_vector, $cols) = $matrix->solve_LR($y_vector)) { } 112 0 112
1415 {
1416 112 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 662 112 774
1417 {
1418 662 $inv_matrix->[0][$i][$j] = $x_vector->[0][$i][0];
1419 }
1420 } else {
1421 0 die "Math::MatrixReal::invert_LR(): unexpected error - please inform author!\n";
1422 }
1423 }
1424 25 return($inv_matrix);
1425 } else {
1426 0 warn __PACKAGE__ . qq{: matrix not invertible\n};
1427 0 return;
1428 }
1429 }
1430  
1431 sub condition
1432 1 ▶{
Subroutine condition: called CC=6 SCAR=19.6
Pod undocumented
1433 # 1st matrix MUST be the inverse of 2nd matrix (or vice-versa)
1434 # for a meaningful result!
1435  
1436 # make this work when given no args
1437  
1438 1 ▶ croak "Usage: \$condition = \$matrix->condition(\$inverse_matrix);" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 1 1
1439  
1440 1 my($matrix1,$matrix2) = @_;
1441 1 my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
1442 1 my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
1443  
1444 1 ▶ croak "Math::MatrixReal::condition(): 1st matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $cols1 0 1 1
1445 unless ($rows1 == $cols1);
1446  
1447 1 ▶ croak "Math::MatrixReal::condition(): 2nd matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows2 == $cols2 0 1 1
1448 unless ($rows2 == $cols2);
1449  
1450 1 ▶ croak "Math::MatrixReal::condition(): matrix size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 and $cols1 == $cols2 0 1 1
Condition33%1/3
$rows1 == $rows2 and $cols1 == $cols2
!ll&&!rl&&r
001
MC/DC0%0/2
$rows1 == $rows2 and $cols1 == $cols2
$rows1 == $rows2 $cols1 == $cols2
Truth table33%1/3
A and B
A= $rows1 == $rows2B= $cols1 == $cols2
ABresult
0X0
100
111
1451 unless (($rows1 == $rows2) && ($cols1 == $cols2));
1452  
1453 1 return( $matrix1->norm_one() * $matrix2->norm_one() );
1454 }
1455  
1456 ## easy to use determinant
1457 ## very fast if matrix is diagonal or triangular
1458  
1459 172 ▶sub det {
Subroutine det: called CC=5 SCAR=16.1
Pod undocumented
1460 172 ▶ croak "Usage: \$determinant = \$matrix->det_LR();" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 172 172
1461 172 my ($matrix) = @_;
1462 172 my ($rows,$cols) = $matrix->dim();
1463 172 my $det = 1;
1464  
1465 172 ▶ croak "Math::MatrixReal::det(): Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 172 172
1466 unless ($rows == $cols);
1467
1468 # diagonal will match too
1469 172 ▶ if( $matrix->is_upper_triangular() ){
Branch: 2 branches
BranchTrueFalseTotal
if ($matrix->is_upper_triangular) { } 24 148 172
elsif ($matrix->is_lower_triangular) { } 3 145 148
1470 24 ▶ $matrix->each_diag( sub { $det*=shift; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1471 } elsif ( $matrix->is_lower_triangular() ){
1472 3 ▶ $matrix->each_diag( sub { $det*=shift; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1473 } else {
1474 145 return $matrix->decompose_LR->det_LR();
1475 }
1476 27 return $det;
1477 }
1478  
1479 sub det_LR # determinant of LR decomposition matrix
1480 145 ▶{
Subroutine det_LR: called CC=5 SCAR=16.6
Pod undocumented
1481 145 ▶ croak "Usage: \$determinant = \$LR_matrix->det_LR();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 145 145
1482 if (@_ != 1);
1483  
1484 145 my($matrix) = @_;
1485 145 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1486 145 my($k,$det);
1487  
1488 145 ▶ croak "Math::MatrixReal::det_LR(): not an LR decomposition matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix->[3] and $rows == $cols 0 145 145
Condition33%1/3
defined $matrix->[3] and $rows == $cols
!ll&&!rl&&r
00145
MC/DC0%0/2
defined $matrix->[3] and $rows == $cols
defined $matrix->[3] $rows == $cols
Truth table33%1/3
A and B
A= defined $matrix->[3]B= $rows == $cols
ABresult
0X0
100
111
1489 unless ((defined $matrix->[3]) && ($rows == $cols));
1490  
1491 145 $det = $matrix->[3];
1492 145 ▶ for ( $k = 0; $k < $rows; $k++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $rows) 644 145 789
1493 {
1494 644 $det *= $matrix->[0][$k][$k];
1495 }
1496 145 return($det);
1497 }
1498  
1499 5 ▶sub rank_LR {
Subroutine rank_LR: called CC=1 SCAR=0.0
Pod undocumented
1500 5 return (shift)->order_LR;
1501 }
1502  
1503 sub order_LR # order of LR decomposition matrix (number of non-zero equations)
1504 5 ▶{
Subroutine order_LR: called CC=6 SCAR=18.5
Pod undocumented
1505 5 ▶ croak "Usage: \$order = \$LR_matrix->order_LR();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 5 5
1506 if (@_ != 1);
1507  
1508 5 my($matrix) = @_;
1509 5 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1510 5 my($order);
1511  
1512 5 ▶ croak "Math::MatrixReal::order_LR(): not an LR decomposition matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix->[3] and $rows == $cols 0 5 5
Condition33%1/3
defined $matrix->[3] and $rows == $cols
!ll&&!rl&&r
005
MC/DC0%0/2
defined $matrix->[3] and $rows == $cols
defined $matrix->[3] $rows == $cols
Truth table33%1/3
A and B
A= defined $matrix->[3]B= $rows == $cols
ABresult
0X0
100
111
1513 unless ((defined $matrix->[3]) && ($rows == $cols));
1514  
1515 ZERO:
1516 5 ▶ for ( $order = ($rows - 1); $order >= 0; $order-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($order >= 0) 10 1 11
1517 {
1518 10 ▶ last ZERO if ($matrix->[0][$order][$order] != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->[0][$order][$order] != 0 4 6 10
1519 }
1520 5 return(++$order);
1521 }
1522  
1523 sub scalar_product
1524 2 ▶{
Subroutine scalar_product: called CC=5 SCAR=16.6
Pod undocumented
1525 2 ▶ croak "Usage: \$scalar_product = \$vector1->scalar_product(\$vector2);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 2 2
1526 if (@_ != 2);
1527  
1528 2 my($vector1,$vector2) = @_;
1529 2 my($rows1,$cols1) = ($vector1->[1],$vector1->[2]);
1530 2 my($rows2,$cols2) = ($vector2->[1],$vector2->[2]);
1531  
1532 2 ▶ croak "Math::MatrixReal::scalar_product(): 1st vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols1 == 1 0 2 2
1533 unless ($cols1 == 1);
1534  
1535 2 ▶ croak "Math::MatrixReal::scalar_product(): 2nd vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols2 == 1 0 2 2
1536 unless ($cols2 == 1);
1537  
1538 2 ▶ croak "Math::MatrixReal::scalar_product(): vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 0 2 2
1539 unless ($rows1 == $rows2);
1540  
1541 2 my $sum = 0;
1542 2 map { $sum += $vector1->[0][$_][0] * $vector2->[0][$_][0] } ( 0 .. $rows1-1);
1543 2 return $sum;
1544 }
1545  
1546 sub vector_product
1547 1 ▶{
Subroutine vector_product: called CC=6 SCAR=18.4
Pod undocumented
1548 1 ▶ croak "Usage: \$vector_product = \$vector1->vector_product(\$vector2);" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 1 1
1549  
1550 1 my($vector1,$vector2) = @_;
1551 1 my($rows1,$cols1) = ($vector1->[1],$vector1->[2]);
1552 1 my($rows2,$cols2) = ($vector2->[1],$vector2->[2]);
1553 1 my($temp);
1554 my($n);
1555  
1556 1 ▶ croak "Math::MatrixReal::vector_product(): 1st vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols1 == 1 0 1 1
1557 unless ($cols1 == 1);
1558  
1559 1 ▶ croak "Math::MatrixReal::vector_product(): 2nd vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols2 == 1 0 1 1
1560 unless ($cols2 == 1);
1561  
1562 1 ▶ croak "Math::MatrixReal::vector_product(): vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $rows2 0 1 1
1563 unless ($rows1 == $rows2);
1564  
1565 1 $n = $rows1;
1566  
1567 1 ▶ croak "Math::MatrixReal::vector_product(): only defined for 3 dimensions"
Branch: 1 branch
BranchTrueFalseTotal
unless $n == 3 0 1 1
1568 unless ($n == 3);
1569  
1570 1 $temp = $vector1->new($n,1);
1571 1 $temp->[0][0][0] = $vector1->[0][1][0] * $vector2->[0][2][0] -
1572 $vector1->[0][2][0] * $vector2->[0][1][0];
1573 1 $temp->[0][1][0] = $vector1->[0][2][0] * $vector2->[0][0][0] -
1574 $vector1->[0][0][0] * $vector2->[0][2][0];
1575 1 $temp->[0][2][0] = $vector1->[0][0][0] * $vector2->[0][1][0] -
1576 $vector1->[0][1][0] * $vector2->[0][0][0];
1577 1 return($temp);
1578 }
1579  
1580 sub length
1581 2 ▶{
Subroutine length: called CC=6 SCAR=18.6
Pod undocumented
1582 2 ▶ croak "Usage: \$length = \$vector->length();" if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
1583  
1584 2 my($vector) = @_;
1585 2 my($rows,$cols) = ($vector->[1],$vector->[2]);
1586 2 my($k,$comp,$sum);
1587  
1588 2 ▶ croak "Math::MatrixReal::length(): vector is not a row or column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols == 1 or $rows == 1 0 2 2
Condition33%1/3
$cols == 1 or $rows == 1
l!l&&r!l&&!r
200
MC/DC0%0/2
$cols == 1 or $rows == 1
$cols == 1 $rows == 1
Truth table33%1/3
A or B
A= $cols == 1B= $rows == 1
ABresult
1X1
011
000
1589 unless ($cols == 1 || $rows ==1 );
1590  
1591 2 ▶ $vector = ~$vector if ($rows == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $rows == 1 0 2 2
1592  
1593 2 $sum = 0;
1594 2 ▶ for ( $k = 0; $k < $rows; $k++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $rows) 6 2 8
1595 {
1596 6 $comp = $vector->[0][$k][0];
1597 6 $sum += $comp * $comp;
1598 }
1599 2 return sqrt $sum;
1600 }
1601  
1602 sub _init_iteration
1603 0 ▶{
Subroutine _init_iteration: not called CC=16 SCAR=56.1
1604 0 ▶ croak "Usage: \$which_norm = \$matrix->_init_iteration();"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
1605 if (@_ != 1);
1606  
1607 0 my($matrix) = @_;
1608 0 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
1609 0 my($ok,$max,$sum,$norm);
1610 0 my($i,$j,$n);
1611  
1612 0 ▶ croak "Math::MatrixReal::_init_iteration(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 0 0
1613 unless ($rows == $cols);
1614  
1615 0 $ok = 1;
1616 0 $n = $rows;
1617 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1618 {
1619 0 ▶ if ($matrix->[0][$i][$i] == 0) { $ok = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->[0][$i][$i] == 0) 0 0 0
1620 }
1621 0 ▶ if ($ok)
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
1622 {
1623 0 $norm = 1; # norm_one
1624 0 $max = 0;
1625 0 ▶ for ( $j = 0; $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1626 {
1627 0 $sum = 0;
1628 0 ▶ for ( $i = 0; $i < $j; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $j) 0 0 0
1629 {
1630 0 $sum += abs($matrix->[0][$i][$j]);
1631 }
1632 0 ▶ for ( $i = ($j + 1); $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1633 {
1634 0 $sum += abs($matrix->[0][$i][$j]);
1635 }
1636 0 $sum /= abs($matrix->[0][$j][$j]);
1637 0 ▶ if ($sum > $max) { $max = $sum; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sum > $max) 0 0 0
1638 }
1639 0 $ok = ($max < 1);
1640 0 ▶ unless ($ok)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
1641 {
1642 0 $norm = -1; # norm_max
1643 0 $max = 0;
1644 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1645 {
1646 0 $sum = 0;
1647 0 ▶ for ( $j = 0; $j < $i; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 0 0 0
1648 {
1649 0 $sum += abs($matrix->[0][$i][$j]);
1650 }
1651 0 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1652 {
1653 0 $sum += abs($matrix->[0][$i][$j]);
1654 }
1655 0 $sum /= abs($matrix->[0][$i][$i]);
1656 0 ▶ if ($sum > $max) { $max = $sum; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sum > $max) 0 0 0
1657 }
1658 0 $ok = ($max < 1)
1659 }
1660 }
1661 0 ▶ if ($ok) { return($norm); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
1662 0 else { return(0); }
1663 }
1664  
1665 sub solve_GSM # Global Step Method
1666 0 ▶{
Subroutine solve_GSM: not called CC=14 SCAR=53.5
Pod undocumented
1667 0 ▶ croak "Usage: \$xn_vector = \$matrix->solve_GSM(\$x0_vector,\$b_vector,\$epsilon);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 0 0
1668 if (@_ != 4);
1669  
1670 0 my($matrix,$x0_vector,$b_vector,$epsilon) = @_;
1671 0 my($rows1,$cols1) = ( $matrix->[1], $matrix->[2]);
1672 0 my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
1673 0 my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
1674 0 my($norm,$sum,$diff);
1675 0 my($xn_vector);
1676 0 my($i,$j,$n);
1677  
1678 0 ▶ croak "Math::MatrixReal::solve_GSM(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $cols1 0 0 0
1679 unless ($rows1 == $cols1);
1680  
1681 0 $n = $rows1;
1682  
1683 0 ▶ croak "Math::MatrixReal::solve_GSM(): 1st vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols2 == 1 0 0 0
1684 unless ($cols2 == 1);
1685  
1686 0 ▶ croak "Math::MatrixReal::solve_GSM(): 2nd vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols3 == 1 0 0 0
1687 unless ($cols3 == 1);
1688  
1689 0 ▶ croak "Math::MatrixReal::solve_GSM(): matrix and vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows2 == $n and $rows3 == $n 0 0 0
Condition0%0/3
$rows2 == $n and $rows3 == $n
!ll&&!rl&&r
000
MC/DC0%0/2
$rows2 == $n and $rows3 == $n
$rows2 == $n $rows3 == $n
Truth table0%0/3
A and B
A= $rows2 == $nB= $rows3 == $n
ABresult
0X0
100
111
1690 unless (($rows2 == $n) && ($rows3 == $n));
1691  
1692 0 ▶ return() unless ($norm = $matrix->_init_iteration());
Branch: 1 branch
BranchTrueFalseTotal
unless $norm = $matrix->_init_iteration 0 0 0
1693  
1694 0 $xn_vector = $x0_vector->new($n,1);
1695  
1696 0 $diff = $epsilon + 1;
1697 0 ▶ while ($diff >= $epsilon)
Branch: 1 branch
BranchTrueFalseTotal
if ($diff >= $epsilon) 0 0 0
1698 {
1699 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1700 {
1701 0 $sum = $b_vector->[0][$i][0];
1702 0 ▶ for ( $j = 0; $j < $i; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 0 0 0
1703 {
1704 0 $sum -= $matrix->[0][$i][$j] * $x0_vector->[0][$j][0];
1705 }
1706 0 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1707 {
1708 0 $sum -= $matrix->[0][$i][$j] * $x0_vector->[0][$j][0];
1709 }
1710 0 $xn_vector->[0][$i][0] = $sum / $matrix->[0][$i][$i];
1711 }
1712 0 $x0_vector->subtract($x0_vector,$xn_vector);
1713 0 ▶ if ($norm > 0) { $diff = $x0_vector->norm_one(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($norm > 0) { } 0 0 0
1714 0 else { $diff = $x0_vector->norm_max(); }
1715 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1716 {
1717 0 $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
1718 }
1719 }
1720 0 return($xn_vector);
1721 }
1722  
1723 sub solve_SSM # Single Step Method
1724 0 ▶{
Subroutine solve_SSM: not called CC=14 SCAR=53.5
Pod undocumented
1725 0 ▶ croak "Usage: \$xn_vector = \$matrix->solve_SSM(\$x0_vector,\$b_vector,\$epsilon);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 0 0
1726 if (@_ != 4);
1727  
1728 0 my($matrix,$x0_vector,$b_vector,$epsilon) = @_;
1729 0 my($rows1,$cols1) = ( $matrix->[1], $matrix->[2]);
1730 0 my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
1731 0 my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
1732 0 my($norm,$sum,$diff);
1733 0 my($xn_vector);
1734 0 my($i,$j,$n);
1735  
1736 0 ▶ croak "Math::MatrixReal::solve_SSM(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $cols1 0 0 0
1737 unless ($rows1 == $cols1);
1738  
1739 0 $n = $rows1;
1740  
1741 0 ▶ croak "Math::MatrixReal::solve_SSM(): 1st vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols2 == 1 0 0 0
1742 unless ($cols2 == 1);
1743  
1744 0 ▶ croak "Math::MatrixReal::solve_SSM(): 2nd vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols3 == 1 0 0 0
1745 unless ($cols3 == 1);
1746  
1747 0 ▶ croak "Math::MatrixReal::solve_SSM(): matrix and vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows2 == $n and $rows3 == $n 0 0 0
Condition0%0/3
$rows2 == $n and $rows3 == $n
!ll&&!rl&&r
000
MC/DC0%0/2
$rows2 == $n and $rows3 == $n
$rows2 == $n $rows3 == $n
Truth table0%0/3
A and B
A= $rows2 == $nB= $rows3 == $n
ABresult
0X0
100
111
1748 unless (($rows2 == $n) && ($rows3 == $n));
1749  
1750 0 ▶ return() unless ($norm = $matrix->_init_iteration());
Branch: 1 branch
BranchTrueFalseTotal
unless $norm = $matrix->_init_iteration 0 0 0
1751  
1752 0 $xn_vector = $x0_vector->new($n,1);
1753 0 $xn_vector->copy($x0_vector);
1754  
1755 0 $diff = $epsilon + 1;
1756 0 ▶ while ($diff >= $epsilon)
Branch: 1 branch
BranchTrueFalseTotal
if ($diff >= $epsilon) 0 0 0
1757 {
1758 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1759 {
1760 0 $sum = $b_vector->[0][$i][0];
1761 0 ▶ for ( $j = 0; $j < $i; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 0 0 0
1762 {
1763 0 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
1764 }
1765 0 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1766 {
1767 0 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
1768 }
1769 0 $xn_vector->[0][$i][0] = $sum / $matrix->[0][$i][$i];
1770 }
1771 0 $x0_vector->subtract($x0_vector,$xn_vector);
1772 0 ▶ if ($norm > 0) { $diff = $x0_vector->norm_one(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($norm > 0) { } 0 0 0
1773 0 else { $diff = $x0_vector->norm_max(); }
1774 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1775 {
1776 0 $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
1777 }
1778 }
1779 0 return($xn_vector);
1780 }
1781  
1782 sub solve_RM # Relaxation Method
1783 0 ▶{
Subroutine solve_RM: not called CC=14 SCAR=53.5
Pod undocumented
1784 0 ▶ croak "Usage: \$xn_vector = \$matrix->solve_RM(\$x0_vector,\$b_vector,\$weight,\$epsilon);"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 5 0 0 0
1785 if (@_ != 5);
1786  
1787 0 my($matrix,$x0_vector,$b_vector,$weight,$epsilon) = @_;
1788 0 my($rows1,$cols1) = ( $matrix->[1], $matrix->[2]);
1789 0 my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
1790 0 my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
1791 0 my($norm,$sum,$diff);
1792 0 my($xn_vector);
1793 0 my($i,$j,$n);
1794  
1795 0 ▶ croak "Math::MatrixReal::solve_RM(): matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows1 == $cols1 0 0 0
1796 unless ($rows1 == $cols1);
1797  
1798 0 $n = $rows1;
1799  
1800 0 ▶ croak "Math::MatrixReal::solve_RM(): 1st vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols2 == 1 0 0 0
1801 unless ($cols2 == 1);
1802  
1803 0 ▶ croak "Math::MatrixReal::solve_RM(): 2nd vector is not a column vector"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols3 == 1 0 0 0
1804 unless ($cols3 == 1);
1805  
1806 0 ▶ croak "Math::MatrixReal::solve_RM(): matrix and vector size mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows2 == $n and $rows3 == $n 0 0 0
Condition0%0/3
$rows2 == $n and $rows3 == $n
!ll&&!rl&&r
000
MC/DC0%0/2
$rows2 == $n and $rows3 == $n
$rows2 == $n $rows3 == $n
Truth table0%0/3
A and B
A= $rows2 == $nB= $rows3 == $n
ABresult
0X0
100
111
1807 unless (($rows2 == $n) && ($rows3 == $n));
1808  
1809 0 ▶ return() unless ($norm = $matrix->_init_iteration());
Branch: 1 branch
BranchTrueFalseTotal
unless $norm = $matrix->_init_iteration 0 0 0
1810  
1811 0 $xn_vector = $x0_vector->new($n,1);
1812 0 $xn_vector->copy($x0_vector);
1813  
1814 0 $diff = $epsilon + 1;
1815 0 ▶ while ($diff >= $epsilon)
Branch: 1 branch
BranchTrueFalseTotal
if ($diff >= $epsilon) 0 0 0
1816 {
1817 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1818 {
1819 0 $sum = $b_vector->[0][$i][0];
1820 0 ▶ for ( $j = 0; $j < $i; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 0 0 0
1821 {
1822 0 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
1823 }
1824 0 ▶ for ( $j = ($i + 1); $j < $n; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 0 0 0
1825 {
1826 0 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
1827 }
1828 0 $xn_vector->[0][$i][0] = $weight * ( $sum / $matrix->[0][$i][$i] )
1829 + (1 - $weight) * $xn_vector->[0][$i][0];
1830 }
1831 0 $x0_vector->subtract($x0_vector,$xn_vector);
1832 0 ▶ if ($norm > 0) { $diff = $x0_vector->norm_one(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($norm > 0) { } 0 0 0
1833 0 else { $diff = $x0_vector->norm_max(); }
1834 0 ▶ for ( $i = 0; $i < $n; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1835 {
1836 0 $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
1837 }
1838 }
1839 0 return($xn_vector);
1840 }
1841  
1842 # Core householder reduction routine (when eigenvector
1843 # are wanted).
1844 # Adapted from: Numerical Recipes, 2nd edition.
1845 sub _householder_vectors ($)
1846 38 ▶{
Subroutine _householder_vectors: called CC=16 SCAR=27.7
1847 38 my ($Q) = @_;
1848 38 my ($rows, $cols) = ($Q->[1], $Q->[2]);
1849
1850 # Creates tridiagonal
1851 # Set up tridiagonal needed elements
1852 38 my $d = []; # N Diagonal elements 0...n-1
1853 38 my $e = []; # N-1 Off-Diagonal elements 0...n-2
1854
1855 38 my @p = ();
1856 38 ▶ for (my $i = ($rows-1); $i > 1; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 814 38 852
1857 {
1858 814 my $scale = 0.0;
1859 # Computes norm of one column (below diagonal)
1860 814 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 11150 814 11964
1861 {
1862 11150 $scale += abs($Q->[0][$i][$k]);
1863 }
1864 814 ▶ if ($scale == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 0.0) { } 0 814 814
1865 { # skip the transformation
1866 0 $e->[$i-1] = $Q->[0][$i][$i-1];
1867 }
1868 else
1869 {
1870 814 my $h = 0.0;
1871 814 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 11150 814 11964
1872 { # Used scaled Q for transformation
1873 11150 $Q->[0][$i][$k] /= $scale;
1874 # Form sigma in h
1875 11150 $h += $Q->[0][$i][$k] * $Q->[0][$i][$k];
1876 }
1877 814 my $t1 = $Q->[0][$i][$i-1];
1878 814 ▶ my $t2 = (($t1 >= 0.0) ? -sqrt($h) : sqrt($h));
Branch: 1 branch
BranchTrueFalseTotal
$t1 >= 0.0 ? : 268 546 814
1879 814 $e->[$i-1] = $scale * $t2; # Update off-diagonals
1880 814 $h -= $t1 * $t2;
1881 814 $Q->[0][$i][$i-1] -= $t2;
1882 814 my $f = 0.0;
1883 814 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 11150 814 11964
1884 {
1885 11150 $Q->[0][$j][$i] = $Q->[0][$i][$j] / $h;
1886 11150 my $g = 0.0;
1887 11150 ▶ for (my $k = 0; $k <= $j; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $j) 108062 11150 119212
1888 {
1889 108062 $g += $Q->[0][$j][$k] * $Q->[0][$i][$k];
1890 }
1891 11150 ▶ for (my $k = $j+1; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 96912 11150 108062
1892 {
1893 96912 $g += $Q->[0][$k][$j] * $Q->[0][$i][$k];
1894 }
1895 # Form elements of P
1896 11150 $p[$j] = $g / $h;
1897 11150 $f += $p[$j] * $Q->[0][$i][$j];
1898 }
1899 814 my $hh = $f / ($h + $h);
1900 814 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 11150 814 11964
1901 {
1902 11150 my $t3 = $Q->[0][$i][$j];
1903 11150 my $t4 = $p[$j] - $hh * $t3;
1904 11150 $p[$j] = $t4;
1905 11150 ▶ for (my $k = 0; $k <= $j; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $j) 108062 11150 119212
1906 {
1907 108062 $Q->[0][$j][$k] -= $t3 * $p[$k]
1908 + $t4 * $Q->[0][$i][$k];
1909 }
1910 }
1911 }
1912 }
1913 # Updates for i == 0,1
1914 38 $e->[0] = $Q->[0][1][0];
1915 38 $d->[0] = $Q->[0][0][0]; # i==0
1916 38 $Q->[0][0][0] = 1.0;
1917 38 $d->[1] = $Q->[0][1][1]; # i==1
1918 38 $Q->[0][1][1] = 1.0;
1919 38 $Q->[0][1][0] = $Q->[0][0][1] = 0.0;
1920 38 ▶ for (my $i = 2; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 814 38 852
1921 {
1922 814 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 11150 814 11964
1923 {
1924 11150 my $g = 0.0;
1925 11150 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 204974 11150 216124
1926 {
1927 204974 $g += $Q->[0][$i][$k] * $Q->[0][$k][$j];
1928 }
1929 11150 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 204974 11150 216124
1930 {
1931 204974 $Q->[0][$k][$j] -= $g * $Q->[0][$k][$i];
1932 }
1933 }
1934 814 $d->[$i] = $Q->[0][$i][$i];
1935 # Reset row and column of Q for next iteration
1936 814 $Q->[0][$i][$i] = 1.0;
1937 814 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 11150 814 11964
1938 {
1939 11150 $Q->[0][$i][$j] = $Q->[0][$j][$i] = 0.0;
1940 }
1941 }
1942 38 return ($d, $e);
1943 }
1944  
1945 # Computes sqrt(a*a + b*b), but more carefully...
1946 sub _pythag ($$)
1947 60032 ▶{
Subroutine _pythag: called CC=3 SCAR=11.0
1948 60032 my ($a, $b) = @_;
1949 60032 my $aa = abs($a);
1950 60032 my $ab = abs($b);
1951 60032 ▶ if ($aa > $ab)
Branch: 1 branch
BranchTrueFalseTotal
if ($aa > $ab) { } 17942 42090 60032
1952 {
1953 # NB: Not needed!: return 0.0 if ($aa == 0.0);
1954 17942 my $t = $ab / $aa;
1955 17942 return ($aa * sqrt(1.0 + $t*$t));
1956 } else {
1957 42090 ▶ return 0.0 if ($ab == 0.0);
Branch: 1 branch
BranchTrueFalseTotal
if $ab == 0 0 42090 42090
1958 42090 my $t = $aa / $ab;
1959 42090 return ($ab * sqrt(1.0 + $t*$t));
1960 }
1961 }
1962  
1963 # QL algorithm with implicit shifts to determine the eigenvalues
1964 # of a tridiagonal matrix. Internal routine.
1965 sub _tridiagonal_QLimplicit
1966 38 ▶{
Subroutine _tridiagonal_QLimplicit: called CC=11 SCAR=24.0
1967 38 my ($EV, $d, $e) = @_;
1968 38 my ($rows, $cols) = ($EV->[1], $EV->[2]);
1969  
1970 38 $e->[$rows-1] = 0.0;
1971 # Start real computation
1972 38 ▶ for (my $l = 0; $l < $rows; $l++)
Branch: 1 branch
BranchTrueFalseTotal
if ($l < $rows) 890 38 928
1973 {
1974 890 my $iter = 0;
1975 890 my $m;
1976 OUTER:
1977 890 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$m != $l and do { do { for ($m = $l; $m < $rows - 1; ++$m) { my $dd = abs($d->[$m]) + abs($$d[$m + 1]); last if abs($e->[$m]) + $dd == $dd; } ; if ($m != $l) { croak("Too many iterations!") if $iter++ >= 30; my $g = ($$d[$l + 1] - $d->[$l]) / (2 * $e->[$l]); my $r = &_pythag($g, 1); $g = $d->[$m] - $d->[$l] + $e->[$l] / ($g + ($g >= 0 ? abs $r : -abs($r))); () = (0, 1, 1); for (my $i = $m - 1; $i >= $l; --$i) { my $ii = $i + 1; my $f = $s * $e->[$i]; my $t = &_pythag($f, $g); $e->[$ii] = $t; if ($t == 0) { $d->[$ii] -= $p; $e->[$m] = 0; next OUTER; } ; my $b = $c * $e->[$i]; $s = $f / $t; $c = $g / $t; $g = $d->[$ii] - $p; my $t2 = ($d->[$i] - $g) * $s + 2 * $c * $b; $p = $s * $t2; $d->[$ii] = $g + $p; $g = $c * $t2 - $b; for (my $k = 0; $k < $rows; ++$k) { my $t1 = $EV->[0][$k][$ii]; my $t2 = $EV->[0][$k][$i]; $EV->[0][$k][$ii] = $s * $t2 + $c * $t1; $EV->[0][$k][$i] = $c * $t2 - $s * $t1; } ; } ; $d->[$l] -= $p; $e->[$l] = $g; $e->[$m] = 0; } } } 1911 890 2801
1978 2801 ▶ for ($m = $l; $m < ($rows - 1); $m++)
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $rows - 1) 29170 750 29920
1979 {
1980 29170 my $dd = abs($d->[$m]) + abs($d->[$m+1]);
1981 29170 ▶ last if ((abs($e->[$m]) + $dd) == $dd);
Branch: 1 branch
BranchTrueFalseTotal
if abs($e->[$m]) + $dd == $dd 2051 27119 29170
1982 }
1983 2801 ▶ if ($m != $l)
Branch: 1 branch
BranchTrueFalseTotal
if ($m != $l) 1911 890 2801
1984 {
1985 ## why only allow 30 iterations?
1986 1911 ▶ croak("Too many iterations!") if ($iter++ >= 30);
Branch: 1 branch
BranchTrueFalseTotal
if $iter++ >= 30 0 1911 1911
1987 1911 my $g = ($d->[$l+1] - $d->[$l])
1988 / (2.0 * $e->[$l]);
1989 1911 my $r = _pythag($g, 1.0);
1990 1911 ▶ $g = $d->[$m] - $d->[$l]
Branch: 1 branch
BranchTrueFalseTotal
$g >= 0.0 ? : 1003 908 1911
1991 + $e->[$l] / ($g + (($g >= 0.0) ? abs($r) : -abs($r)));
1992 1911 my ($p,$s,$c) = (0.0, 1.0,1.0);
1993 1911 ▶ for (my $i = ($m-1); $i >= $l; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $l) 27119 1911 29030
1994 {
1995 27119 my $ii = $i + 1;
1996 27119 my $f = $s * $e->[$i];
1997 27119 my $t = _pythag($f, $g);
1998 27119 $e->[$ii] = $t;
1999 27119 ▶ if ($t == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 0) 0 27119 27119
2000 {
2001 0 $d->[$ii] -= $p;
2002 0 $e->[$m] = 0.0;
2003 0 next OUTER;
2004 }
2005 27119 my $b = $c * $e->[$i];
2006 27119 $s = $f / $t;
2007 27119 $c = $g / $t;
2008 27119 $g = $d->[$ii] - $p;
2009 27119 my $t2 = ($d->[$i] - $g) * $s + 2.0 * $c * $b;
2010 27119 $p = $s * $t2;
2011 27119 $d->[$ii] = $g + $p;
2012 27119 $g = $c * $t2 - $b;
2013 27119 ▶ for (my $k = 0; $k < $rows; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $rows) 757286 27119 784405
2014 {
2015 757286 my $t1 = $EV->[0][$k][$ii];
2016 757286 my $t2 = $EV->[0][$k][$i];
2017 757286 $EV->[0][$k][$ii] = $s * $t2 + $c * $t1;
2018 757286 $EV->[0][$k][$i] = $c * $t2 - $s * $t1;
2019 }
2020 }
2021 1911 $d->[$l] -= $p;
2022 1911 $e->[$l] = $g;
2023 1911 $e->[$m] = 0.0;
2024 }
2025 } while ($m != $l);
2026 }
2027 38 return;
2028 }
2029  
2030 # Core householder reduction routine (when eigenvector
2031 # are NOT wanted).
2032 sub _householder_values ($)
2033 45 ▶{
Subroutine _householder_values: called CC=12 SCAR=24.8
2034 45 my ($Q) = @_; # NB: Q is destroyed on output...
2035 45 my ($rows, $cols) = ($Q->[1], $Q->[2]);
2036
2037 # Creates tridiagonal
2038 # Set up tridiagonal needed elements
2039 45 my $d = []; # N Diagonal elements 0...n-1
2040 45 my $e = []; # N-1 Off-Diagonal elements 0...n-2
2041
2042 45 my @p = ();
2043 45 ▶ for (my $i = ($rows - 1); $i > 1; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 849 45 894
2044 {
2045 849 my $scale = 0.0;
2046 849 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 12110 849 12959
2047 {
2048 12110 $scale += abs($Q->[0][$i][$k]);
2049 }
2050 849 ▶ if ($scale == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 0.0) { } 15 834 849
2051 { # skip the transformation
2052 15 $e->[$i-1] = $Q->[0][$i][$i-1];
2053 }
2054 else
2055 {
2056 834 my $h = 0.0;
2057 834 ▶ for (my $k = 0; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 12065 834 12899
2058 { # Used scaled Q for transformation
2059 12065 $Q->[0][$i][$k] /= $scale;
2060 # Form sigma in h
2061 12065 $h += $Q->[0][$i][$k] * $Q->[0][$i][$k];
2062 }
2063 834 my $t = $Q->[0][$i][$i-1];
2064 834 ▶ my $t2 = (($t >= 0.0) ? -sqrt($h) : sqrt($h));
Branch: 1 branch
BranchTrueFalseTotal
$t >= 0.0 ? : 271 563 834
2065 834 $e->[$i-1] = $scale * $t2; # Updates off-diagonal
2066 834 $h -= $t * $t2;
2067 834 $Q->[0][$i][$i-1] -= $t2;
2068 834 my $f = 0.0;
2069 834 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 12065 834 12899
2070 {
2071 12065 my $g = 0.0;
2072 12065 ▶ for (my $k = 0; $k <= $j; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $j) 133392 12065 145457
2073 {
2074 133392 $g += $Q->[0][$j][$k] * $Q->[0][$i][$k];
2075 }
2076 12065 ▶ for (my $k = $j+1; $k < $i; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $i) 121327 12065 133392
2077 {
2078 121327 $g += $Q->[0][$k][$j] * $Q->[0][$i][$k];
2079 }
2080 # Form elements of P
2081 12065 $p[$j] = $g / $h;
2082 12065 $f += $p[$j] * $Q->[0][$i][$j];
2083 }
2084 834 my $hh = $f / ($h + $h);
2085 834 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 12065 834 12899
2086 {
2087 12065 my $t = $Q->[0][$i][$j];
2088 12065 my $g = $p[$j] - $hh * $t;
2089 12065 $p[$j] = $g;
2090 12065 ▶ for (my $k = 0; $k <= $j; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $j) 133392 12065 145457
2091 {
2092 133392 $Q->[0][$j][$k] -= $t * $p[$k]
2093 + $g * $Q->[0][$i][$k];
2094 }
2095 }
2096 }
2097 }
2098 # Updates for i==1
2099 45 $e->[0] = $Q->[0][1][0];
2100 # Updates diagonal elements
2101 45 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 939 45 984
2102 {
2103 939 $d->[$i] = $Q->[0][$i][$i];
2104 }
2105 45 return ($d, $e);
2106 }
2107  
2108 # QL algorithm with implicit shifts to determine the
2109 # eigenvalues ONLY. This is O(N^2) only...
2110 sub _tridiagonal_QLimplicit_values
2111 45 ▶{
Subroutine _tridiagonal_QLimplicit_values: called CC=10 SCAR=23.1
2112 45 my ($M, $d, $e) = @_; # NB: M is not touched...
2113 45 my ($rows, $cols) = ($M->[1], $M->[2]);
2114  
2115 45 $e->[$rows-1] = 0.0;
2116 # Start real computation
2117 45 ▶ for (my $l = 0; $l < $rows; $l++)
Branch: 1 branch
BranchTrueFalseTotal
if ($l < $rows) 939 45 984
2118 {
2119 939 my $iter = 0;
2120 939 my $m;
2121 OUTER:
2122 939 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$m != $l and do { do { for ($m = $l; $m < $rows - 1; ++$m) { my $dd = abs($d->[$m]) + abs($$d[$m + 1]); last if abs($e->[$m]) + $dd == $dd; } ; if ($m != $l) { croak("Too many iterations!") if $iter++ >= 30; my $g = ($$d[$l + 1] - $d->[$l]) / (2 * $e->[$l]); my $r = &_pythag($g, 1); $g = $d->[$m] - $d->[$l] + $e->[$l] / ($g + ($g >= 0 ? abs $r : -abs($r))); () = (0, 1, 1); for (my $i = $m - 1; $i >= $l; --$i) { my $ii = $i + 1; my $f = $s * $e->[$i]; my $t = &_pythag($f, $g); $e->[$ii] = $t; if ($t == 0) { $d->[$ii] -= $p; $e->[$m] = 0; next OUTER; } ; my $b = $c * $e->[$i]; $s = $f / $t; $c = $g / $t; $g = $d->[$ii] - $p; my $t2 = ($d->[$i] - $g) * $s + 2 * $c * $b; $p = $s * $t2; $d->[$ii] = $g + $p; $g = $c * $t2 - $b; } ; $d->[$l] -= $p; $e->[$l] = $g; $e->[$m] = 0; } } } 1953 939 2892
2123 2892 ▶ for ($m = $l; $m < ($rows - 1); $m++)
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $rows - 1) 31179 762 31941
2124 {
2125 31179 my $dd = abs($d->[$m]) + abs($d->[$m+1]);
2126 31179 ▶ last if ((abs($e->[$m]) + $dd) == $dd);
Branch: 1 branch
BranchTrueFalseTotal
if abs($e->[$m]) + $dd == $dd 2130 29049 31179
2127 }
2128 2892 ▶ if ($m != $l)
Branch: 1 branch
BranchTrueFalseTotal
if ($m != $l) 1953 939 2892
2129 {
2130 1953 ▶ croak("Too many iterations!") if ($iter++ >= 30);
Branch: 1 branch
BranchTrueFalseTotal
if $iter++ >= 30 0 1953 1953
2131 1953 my $g = ($d->[$l+1] - $d->[$l])
2132 / (2.0 * $e->[$l]);
2133 1953 my $r = _pythag($g, 1.0);
2134 1953 ▶ $g = $d->[$m] - $d->[$l]
Branch: 1 branch
BranchTrueFalseTotal
$g >= 0.0 ? : 1013 940 1953
2135 + $e->[$l] / ($g + (($g >= 0.0) ? abs($r) : -abs($r)));
2136 1953 my ($p,$s,$c) = (0.0, 1.0,1.0);
2137 1953 ▶ for (my $i = ($m-1); $i >= $l; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $l) 29049 1953 31002
2138 {
2139 29049 my $ii = $i + 1;
2140 29049 my $f = $s * $e->[$i];
2141 29049 my $t = _pythag($f, $g);
2142 29049 $e->[$ii] = $t;
2143 29049 ▶ if ($t == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 0) 0 29049 29049
2144 {
2145 0 $d->[$ii] -= $p;
2146 0 $e->[$m] = 0.0;
2147 0 next OUTER;
2148 }
2149 29049 my $b = $c * $e->[$i];
2150 29049 $s = $f / $t;
2151 29049 $c = $g / $t;
2152 29049 $g = $d->[$ii] - $p;
2153 29049 my $t2 = ($d->[$i] - $g) * $s + 2.0 * $c * $b;
2154 29049 $p = $s * $t2;
2155 29049 $d->[$ii] = $g + $p;
2156 29049 $g = $c * $t2 - $b;
2157 }
2158 1953 $d->[$l] -= $p;
2159 1953 $e->[$l] = $g;
2160 1953 $e->[$m] = 0.0;
2161 }
2162 } while ($m != $l);
2163 }
2164 45 return;
2165 }
2166  
2167 # Householder reduction of a real, symmetric matrix A.
2168 # Returns a tridiagonal matrix T and the orthogonal matrix
2169 # Q effecting the transformation between A and T.
2170 sub householder ($)
2171 33 ▶{
Subroutine householder: called CC=5 SCAR=16.1
Pod undocumented
2172 33 my ($A) = @_;
2173 33 my ($rows, $cols) = ($A->[1], $A->[2]);
2174  
2175 33 ▶ croak "Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows = $cols 0 33 33
2176 unless ($rows = $cols);
2177 33 ▶ croak "Matrix is not symmetric"
Branch: 1 branch
BranchTrueFalseTotal
unless $A->is_symmetric 0 33 33
2178 unless ($A->is_symmetric());
2179  
2180 # Copy given matrix TODO: study if we should do in-place modification
2181 33 my $Q = $A->clone();
2182  
2183 # Do the computation of tridiagonal elements and of
2184 # transformation matrix
2185 33 my ($diag, $offdiag) = $Q->_householder_vectors();
2186  
2187 # Creates the tridiagonal matrix
2188 33 my $T = $A->shadow();
2189 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2190 { # Set diagonal
2191 790 $T->[0][$i][$i] = $diag->[$i];
2192 }
2193 33 ▶ for (my $i = 0; $i < ($rows-1); $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows - 1) 757 33 790
2194 { # Set off diagonals
2195 757 $T->[0][$i+1][$i] = $offdiag->[$i];
2196 757 $T->[0][$i][$i+1] = $offdiag->[$i];
2197 }
2198 33 return ($T, $Q);
2199 }
2200  
2201 # QL algorithm with implicit shifts to determine the eigenvalues
2202 # and eigenvectors of a real tridiagonal matrix - or of a matrix
2203 # previously reduced to tridiagonal form.
2204 sub tri_diagonalize ($;$)
2205 33 ▶{
Subroutine tri_diagonalize: called CC=7 SCAR=19.7
Pod undocumented
2206 33 my ($T,$Q) = @_; # Q may be 0 if the original matrix is really tridiagonal
2207  
2208 33 my ($rows, $cols) = ($T->[1], $T->[2]);
2209  
2210 33 ▶ croak "Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows = $cols 0 33 33
2211 unless ($rows = $cols);
2212 33 ▶ croak "Matrix is not tridiagonal"
Branch: 1 branch
BranchTrueFalseTotal
unless $T->is_tridiagonal 0 33 33
2213 unless ($T->is_tridiagonal()); # DONE
2214  
2215 33 my $EV;
2216 # Obtain/Creates the todo eigenvectors matrix
2217 33 ▶ if ($Q)
Branch: 1 branch
BranchTrueFalseTotal
if ($Q) { } 33 0 33
2218 {
2219 33 $EV = $Q->clone();
2220 }
2221 else
2222 {
2223 0 $EV = $T->shadow();
2224 0 $EV->one();
2225 }
2226 # Allocates diagonal vector
2227 33 my $diag = [ ];
2228 # Initializes it with T
2229 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2230 {
2231 790 $diag->[$i] = $T->[0][$i][$i];
2232 }
2233 # Allocate temporary vector for off-diagonal elements
2234 33 my $offdiag = [ ];
2235 33 ▶ for (my $i = 1; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 757 33 790
2236 {
2237 757 $offdiag->[$i-1] = $T->[0][$i][$i-1];
2238 }
2239  
2240 # Calls the calculus routine
2241 33 $EV->_tridiagonal_QLimplicit($diag, $offdiag);
2242  
2243 # Allocate eigenvalues vector
2244 33 my $v = Math::MatrixReal->new($rows,1);
2245 # Fills it
2246 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2247 {
2248 790 $v->[0][$i][0] = $diag->[$i];
2249 }
2250 33 return ($v, $EV);
2251 }
2252  
2253 # Main routine for diagonalization of a real symmetric
2254 # matrix M. Operates by transforming M into a tridiagonal
2255 # matrix and then obtaining the eigenvalues and eigenvectors
2256 # for that matrix (taking into account the transformation to
2257 # tridiagonal).
2258 sub sym_diagonalize ($)
2259 5 ▶{
Subroutine sym_diagonalize: called CC=4 SCAR=13.9
Pod undocumented
2260 5 my ($M) = @_;
2261 5 my ($rows, $cols) = ($M->[1], $M->[2]);
2262
2263 5 ▶ croak "Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows = $cols 0 5 5
2264 unless ($rows = $cols);
2265 5 ▶ croak "Matrix is not symmetric"
Branch: 1 branch
BranchTrueFalseTotal
unless $M->is_symmetric 0 5 5
2266 unless ($M->is_symmetric());
2267
2268 # Copy initial matrix
2269 # TODO: study if we should allow in-place modification
2270 5 my $VEC = $M->clone();
2271  
2272 # Do the computation of tridiagonal elements and of
2273 # transformation matrix
2274 5 my ($diag, $offdiag) = $VEC->_householder_vectors();
2275 # Calls the calculus routine for diagonalization
2276 5 $VEC->_tridiagonal_QLimplicit($diag, $offdiag);
2277  
2278 # Allocate eigenvalues vector
2279 5 my $val = Math::MatrixReal->new($rows,1);
2280 # Fills it
2281 5 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 100 5 105
2282 {
2283 100 $val->[0][$i][0] = $diag->[$i];
2284 }
2285 5 return ($val, $VEC);
2286 }
2287  
2288 # Householder reduction of a real, symmetric matrix A.
2289 # Returns a tridiagonal matrix T equivalent to A.
2290 sub householder_tridiagonal ($)
2291 33 ▶{
Subroutine householder_tridiagonal: called CC=5 SCAR=16.1
Pod undocumented
2292 33 my ($A) = @_;
2293 33 my ($rows, $cols) = ($A->[1], $A->[2]);
2294  
2295 33 ▶ croak "Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows = $cols 0 33 33
2296 unless ($rows = $cols);
2297 33 ▶ croak "Matrix is not symmetric"
Branch: 1 branch
BranchTrueFalseTotal
unless $A->is_symmetric 0 33 33
2298 unless ($A->is_symmetric());
2299  
2300 # Copy given matrix
2301 33 my $Q = $A->clone();
2302  
2303 # Do the computation of tridiagonal elements and of
2304 # transformation matrix
2305 # Q is destroyed after reduction
2306 33 my ($diag, $offdiag) = $Q->_householder_values();
2307  
2308 # Creates the tridiagonal matrix in Q (avoid allocation)
2309 33 my $T = $Q;
2310 33 $T->zero();
2311 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2312 { # Set diagonal
2313 790 $T->[0][$i][$i] = $diag->[$i];
2314 }
2315 33 ▶ for (my $i = 0; $i < ($rows-1); $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows - 1) 757 33 790
2316 { # Set off diagonals
2317 757 $T->[0][$i+1][$i] = $offdiag->[$i];
2318 757 $T->[0][$i][$i+1] = $offdiag->[$i];
2319 }
2320 33 return $T;
2321 }
2322  
2323 # QL algorithm with implicit shifts to determine ONLY
2324 # the eigenvalues a real tridiagonal matrix - or of a
2325 # matrix previously reduced to tridiagonal form.
2326 sub tri_eigenvalues ($;$)
2327 33 ▶{
Subroutine tri_eigenvalues: called CC=6 SCAR=17.9
Pod documented
2328 33 my ($T) = @_;
2329 33 my ($rows, $cols) = ($T->[1], $T->[2]);
2330  
2331 33 ▶ croak "Matrix is not quadratic"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows = $cols 0 33 33
2332 unless ($rows = $cols);
2333 33 ▶ croak "Matrix is not tridiagonal"
Branch: 1 branch
BranchTrueFalseTotal
unless $T->is_tridiagonal 0 33 33
2334 unless ($T->is_tridiagonal() ); # DONE
2335  
2336 # Allocates diagonal vector
2337 33 my $diag = [ ];
2338 # Initializes it with T
2339 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2340 {
2341 790 $diag->[$i] = $T->[0][$i][$i];
2342 }
2343 # Allocate temporary vector for off-diagonal elements
2344 33 my $offdiag = [ ];
2345 33 ▶ for (my $i = 1; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 757 33 790
2346 {
2347 757 $offdiag->[$i-1] = $T->[0][$i][$i-1];
2348 }
2349  
2350 # Calls the calculus routine (T is not touched)
2351 33 $T->_tridiagonal_QLimplicit_values($diag, $offdiag);
2352  
2353 # Allocate eigenvalues vector
2354 33 my $v = Math::MatrixReal->new($rows,1);
2355 # Fills it
2356 33 ▶ for (my $i = 0; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 790 33 823
2357 {
2358 790 $v->[0][$i][0] = $diag->[$i];
2359 }
2360 33 return $v;
2361 }
2362  
2363 ## more general routine than sym_eigenvalues
2364 19 ▶sub eigenvalues ($){
Subroutine eigenvalues: called CC=5 SCAR=16.5
Pod undocumented
2365 19 my ($matrix) = @_;
2366 19 my ($rows,$cols) = $matrix->dim();
2367  
2368 19 ▶ croak "Matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 19 19
2369  
2370 19 ▶ if($matrix->is_upper_triangular() || $matrix->is_lower_triangular() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->is_upper_triangular or $matrix->is_lower_triangular) 17 2 19
Condition100%3/3
$matrix->is_upper_triangular or $matrix->is_lower_triangular
l!l&&r!l&&!r
1612
MC/DC100%2/2
$matrix->is_upper_triangular or $matrix->is_lower_triangular
$matrix->is_upper_triangular $matrix->is_lower_triangular
Truth table100%3/3
A or B
A= $matrix->is_upper_triangularB= $matrix->is_lower_triangular
ABresult
1X1
011
000
2371 17 my $l = Math::MatrixReal->new($rows,1);
2372 17 map { $l->[0][$_][0] = $matrix->[0][$_][$_] } (0 .. $rows-1);
2373 17 return $l;
2374 }
2375  
2376 2 ▶ return sym_eigenvalues($matrix) if $matrix->is_symmetric();
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->is_symmetric 2 0 2
2377  
2378 0 carp "Math::MatrixReal::eigenvalues(): Matrix is not symmetric or triangular";
2379 0 return undef;
2380  
2381 }
2382 # Main routine for diagonalization of a real symmetric
2383 # matrix M. Operates by transforming M into a tridiagonal
2384 # matrix and then obtaining the eigenvalues and eigenvectors
2385 # for that matrix (taking into account the transformation to
2386 # tridiagonal).
2387 sub sym_eigenvalues ($)
2388 12 ▶{
Subroutine sym_eigenvalues: called CC=3 SCAR=11.1
Pod undocumented
2389 12 my ($M) = @_;
2390 12 my ($rows, $cols) = ($M->[1], $M->[2]);
2391
2392 12 ▶ croak "Matrix is not quadratic" unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 0 12 12
2393 12 ▶ croak "Matrix is not symmetric" unless ($M->is_symmetric);
Branch: 1 branch
BranchTrueFalseTotal
unless $M->is_symmetric 0 12 12
2394  
2395 # Copy matrix in temporary
2396 12 my $A = $M->clone();
2397 # Do the computation of tridiagonal elements and of
2398 # transformation matrix. A is destroyed
2399 12 my ($diag, $offdiag) = $A->_householder_values();
2400 # Calls the calculus routine for diagonalization
2401 # (M is not touched)
2402 12 $M->_tridiagonal_QLimplicit_values($diag, $offdiag);
2403  
2404 # Allocate eigenvalues vector
2405 12 my $val = Math::MatrixReal->new($rows,1);
2406 # Fills it
2407 12 map { $val->[0][$_][0] = $diag->[$_] } ( 0 .. $rows-1);
2408  
2409 12 return $val;
2410 }
2411 #TODO: docs+test
2412 4 ▶sub is_positive_definite {
Subroutine is_positive_definite: called CC=3 SCAR=11.0
Pod undocumented
2413 4 my ($matrix) = @_;
2414 4 my ($r,$c) = $matrix->dim;
2415  
2416 4 ▶ croak "Math::MatrixReal::is_positive_definite(): Matrix is not square" unless ($r == $c);
Branch: 1 branch
BranchTrueFalseTotal
unless $r == $c 0 4 4
2417 # must have positive (i.e REAL) eigenvalues to be positive definite
2418 4 ▶ return 0 unless $matrix->is_symmetric;
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->is_symmetric 1 3 4
2419  
2420 3 my $ev = $matrix->eigenvalues;
2421 3 my $pos = 1;
2422 3 ▶ $ev->each(sub { my $x = shift; if ($x <= 0){ $pos=0;return; } } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= 0) 2 7 9
2423 3 return $pos;
2424 }
2425 #TODO: docs+test
2426 4 ▶sub is_positive_semidefinite {
Subroutine is_positive_semidefinite: called CC=3 SCAR=11.0
Pod undocumented
2427 4 my ($matrix) = @_;
2428 4 my ($r,$c) = $matrix->dim;
2429  
2430 4 ▶ croak "Math::MatrixReal::is_positive_semidefinite(): Matrix is not square" unless ($r == $c);
Branch: 1 branch
BranchTrueFalseTotal
unless $r == $c 0 4 4
2431 # must have nonnegative (i.e REAL) eigenvalues to be positive semidefinite
2432 4 ▶ return 0 unless $matrix->is_symmetric;
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->is_symmetric 1 3 4
2433  
2434 3 my $ev = $matrix->eigenvalues;
2435 3 my $pos = 1;
2436 3 ▶ $ev->each(sub { my $x = shift; if ($x < 0){ $pos=0;return; } } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 1 8 9
2437 3 return $pos;
2438 }
2439 0 ▶sub is_row { return (shift)->is_row_vector }
Subroutine is_row: not called CC=1 SCAR=6.9
Pod undocumented
2440 0 ▶sub is_col { return (shift)->is_col_vector }
Subroutine is_col: not called CC=1 SCAR=6.9
Pod undocumented
2441  
2442 9 ▶sub is_row_vector {
Subroutine is_row_vector: called CC=2 SCAR=6.9
Pod undocumented
2443 9 my ($m) = @_;
2444 9 my $r = $m->[1];
2445 9 ▶ $r == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r == 1 ? : 2 7 9
2446 }
2447 11 ▶sub is_col_vector {
Subroutine is_col_vector: called CC=2 SCAR=6.9
Pod undocumented
2448 11 my ($m) = @_;
2449 11 my $c = $m->[2];
2450 11 ▶ $c == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c == 1 ? : 7 4 11
2451 }
2452  
2453 4 ▶sub is_orthogonal($) {
Subroutine is_orthogonal: called CC=3 SCAR=11.3
Pod undocumented
2454 4 my ($matrix) = @_;
2455  
2456 4 ▶ return 0 unless $matrix->is_quadratic;
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix->is_quadratic 0 4 4
2457  
2458 4 my $one = $matrix->shadow();
2459 4 $one->one;
2460  
2461 4 ▶ abs(~$matrix * $matrix - $one) < 1e-12 ? return 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
abs ~$matrix * $matrix - $one < 1e-12 ? : 4 0 4
2462  
2463 }
2464  
2465 4 ▶sub is_positive($) {
Subroutine is_positive: called CC=1 SCAR=0.0
Pod undocumented
2466 4 my ($m) = @_;
2467 4 my $pos = 1;
2468 4 ▶ $m->each( sub { if( (shift) <= 0){ $pos = 0;return;} } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (shift() <= 0) 68 32 100
2469 4 return $pos;
2470 }
2471 4 ▶sub is_negative($) {
Subroutine is_negative: called CC=1 SCAR=0.0
Pod undocumented
2472 4 my ($m) = @_;
2473 4 my $neg = 1;
2474 4 ▶ $m->each( sub { if( (shift) >= 0){ $neg = 0;return;} } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (shift() >= 0) 75 25 100
2475 4 return $neg;
2476 }
2477  
2478  
2479 7 ▶sub is_periodic($$) {
Subroutine is_periodic: called CC=3 SCAR=11.1
Pod undocumented
2480 7 my ($m,$k) = @_;
2481 7 ▶ return 0 unless $m->is_quadratic();
Branch: 1 branch
BranchTrueFalseTotal
unless $m->is_quadratic 2 5 7
2482 5 ▶ abs($m**(int($k)+1) - $m) < 1e-12 ? return 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
abs $m ** (int($k) + 1) - $m < 1e-12 ? : 5 0 5
2483 }
2484 3 ▶sub is_idempotent($) {
Subroutine is_idempotent: called CC=1 SCAR=0.0
Pod undocumented
2485 3 return (shift)->is_periodic(1);
2486 }
2487  
2488 # Boolean check routine to see if a matrix is
2489 # symmetric
2490 sub is_symmetric ($)
2491 108 ▶{
Subroutine is_symmetric: called CC=5 SCAR=16.1
Pod undocumented
2492 108 my ($M) = @_;
2493 108 my ($rows, $cols) = ($M->[1], $M->[2]);
2494 # if it is not quadratic it cannot be symmetric...
2495 108 ▶ return 0 unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 2 106 108
2496 # skip when $i=$j?
2497 106 ▶ for (my $i = 1; $i < $rows; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 1805 102 1907
2498 {
2499 1805 ▶ for (my $j = 0; $j < $i; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 23484 1801 25285
2500 {
2501 23484 ▶ return 0 unless ($M->[0][$i][$j] == $M->[0][$j][$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless $M->[0][$i][$j] == $M->[0][$j][$i] 4 23480 23484
2502 }
2503 }
2504 102 return 1;
2505 }
2506 # Boolean check to see if matrix is tridiagonal
2507 72 ▶sub is_tridiagonal ($) {
Subroutine is_tridiagonal: called CC=8 SCAR=20.8
Pod undocumented
2508 72 my ($M) = @_;
2509 72 my ($rows,$cols) = ($M->[1],$M->[2]);
2510 72 my ($i,$j) = (0,0);
2511 # if it is not quadratic it cannot be tridiag
2512 72 ▶ return 0 unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 1 71 72
2513  
2514 71 ▶ for(;$i < $rows; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 1600 70 1670
2515 1600 ▶ for(;$j < $cols; $j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 39526 1599 41125
2516 #print "debug: testing $i,$j = " . $M->[0][$i][$j] . "\n";
2517 # skip diag and diag+-1
2518 39526 ▶ next if ($i == $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 1600 37926 39526
2519 37926 ▶ next if ($i+1 == $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 == $j 1530 36396 37926
2520 36396 ▶ next if ($i-1 == $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i - 1 == $j 1529 34867 36396
2521 34867 ▶ return 0 if $M->[0][$i][$j];
Branch: 1 branch
BranchTrueFalseTotal
if $M->[0][$i][$j] 1 34866 34867
2522 }
2523 1599 $j = 0;
2524 }
2525 70 return 1;
2526 }
2527 # Boolean check to see if matrix is upper triangular
2528 # i.e all nonzero elements are above main diagonal
2529 196 ▶sub is_upper_triangular {
Subroutine is_upper_triangular: called CC=6 SCAR=17.9
Pod undocumented
2530 196 my ($M) = @_;
2531 196 my ($rows,$cols) = $M->dim();
2532 196 my ($i,$j) = (1,0);
2533 196 ▶ return 0 unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 2 194 196
2534
2535 194 ▶ for(;$i < $rows; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 261 42 303
2536 261 ▶ for(;$j < $cols;$j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 646 109 755
2537 646 ▶ next if ($i <= $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i <= $j 253 393 646
2538 393 ▶ return 0 if $M->[0][$i][$j];
Branch: 1 branch
BranchTrueFalseTotal
if $M->[0][$i][$j] 152 241 393
2539 }
2540 109 $j = 0;
2541 }
2542 42 return 1;
2543 }
2544 # Boolean check to see if matrix is lower triangular
2545 # i.e all nonzero elements are lower main diagonal
2546 156 ▶sub is_lower_triangular {
Subroutine is_lower_triangular: called CC=6 SCAR=17.9
Pod undocumented
2547 156 my ($M) = @_;
2548 156 my ($rows,$cols) = $M->dim();
2549 156 my ($i,$j) = (0,1);
2550 156 ▶ return 0 unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 2 154 156
2551  
2552 154 ▶ for(;$i < $rows; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 176 6 182
2553 176 ▶ for(;$j < $cols;$j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 309 28 337
2554 309 ▶ next if ($i >= $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $j 90 219 309
2555 219 ▶ return 0 if $M->[0][$i][$j];
Branch: 1 branch
BranchTrueFalseTotal
if $M->[0][$i][$j] 148 71 219
2556 }
2557 28 $j = 0;
2558 }
2559 6 return 1;
2560 }
2561  
2562 # Boolean check to see if matrix is diagonal
2563 17 ▶sub is_diagonal ($) {
Subroutine is_diagonal: called CC=6 SCAR=17.9
Pod undocumented
2564 17 my ($M) = @_;
2565 17 my ($rows,$cols) = ($M->[1],$M->[2]);
2566 17 my ($i,$j) = (0,0);
2567 17 ▶ return 0 unless ($rows == $cols );
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 1 16 17
2568 16 ▶ for(;$i < $rows; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 91 13 104
2569 91 ▶ for(;$j < $cols; $j++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 707 88 795
2570 # skip diag elements
2571 707 ▶ next if ($i == $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 91 616 707
2572 616 ▶ return 0 if $M->[0][$i][$j];
Branch: 1 branch
BranchTrueFalseTotal
if $M->[0][$i][$j] 3 613 616
2573 }
2574 88 $j = 0;
2575 }
2576 13 return 1;
2577 }
2578 14 ▶sub is_quadratic ($) {
Subroutine is_quadratic: called CC=3 SCAR=11.1
Pod undocumented
2579 14 ▶ croak "Usage: \$matrix->is_quadratic()" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 14 14
2580 14 my ($matrix) = @_;
2581 14 ▶ $matrix->[1] == $matrix->[2] ? return 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$matrix->[1] == $matrix->[2] ? : 11 3 14
2582 }
2583  
2584 1 ▶sub is_square($) {
Subroutine is_square: called CC=2 SCAR=7.2
Pod undocumented
2585 1 ▶ croak "Usage: \$matrix->is_square()" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
2586 1 return (shift)->is_quadratic();
2587 }
2588  
2589 9 ▶sub is_LR($) {
Subroutine is_LR: called CC=3 SCAR=11.1
Pod undocumented
2590 9 ▶ croak "Usage: \$matrix->is_LR()" unless (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 9 9
2591 9 ▶ return (shift)->[3] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[3] ? : 2 7 9
2592 }
2593  
2594 2 ▶sub is_normal{
Subroutine is_normal: called CC=3 SCAR=11.0
Pod undocumented
2595 2 my ($matrix,$eps) = @_;
2596 2 my ($rows,$cols) = $matrix->dim;
2597
2598 2 ▶ $eps ||= 1e-8;
Condition50%1/2
$eps ||= "1e-08"
l!l
02
MC/DC0%0/1
$eps ||= "1e-08"
$eps
Truth table50%1/2
A
A= $eps
Aresult
11
00
2599  
2600 2 ▶ (~$matrix * $matrix - $matrix * ~$matrix < $eps ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
~$matrix * $matrix - $matrix * ~$matrix < $eps ? : 1 1 2
2601  
2602 }
2603  
2604 4 ▶sub is_skew_symmetric{
Subroutine is_skew_symmetric: called CC=5 SCAR=16.1
Pod undocumented
2605 4 my ($m) = @_;
2606 4 my ($rows, $cols) = $m->dim;
2607 # if it is not quadratic it cannot be skew symmetric...
2608 4 ▶ return 0 unless ($rows == $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == $cols 1 3 4
2609 3 ▶ for (my $i = 1; $i < $rows; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 11 3 14
2610 11 ▶ for (my $j = 0; $j < $i; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 26 11 37
2611 26 ▶ return 0 unless ($m->[0][$i][$j] == -$m->[0][$j][$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless $m->[0][$i][$j] == -$m->[0][$j][$i] 0 26 26
2612 }
2613 }
2614 3 return 1;
2615  
2616 }
2617 ####
2618 6 ▶sub is_gramian{
Subroutine is_gramian: called CC=3 SCAR=11.0
Pod undocumented
2619 6 my ($m) = @_;
2620 6 my ($rows,$cols) = $m->dim;
2621 6 my $neg=0;
2622 # gramian matrix must be symmetric
2623 6 ▶ return 0 unless $m->is_symmetric;
Branch: 1 branch
BranchTrueFalseTotal
unless $m->is_symmetric 2 4 6
2624  
2625 # must have all non-negative eigenvalues
2626 4 my $ev = $m->eigenvalues;
2627 4 ▶ $ev->each(sub { $neg++ if ((shift)<0) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if shift() < 0 1 14 15
2628  
2629 4 ▶ return $neg ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 1 3 4
2630 }
2631 4 ▶sub is_binary{
Subroutine is_binary: called CC=5 SCAR=16.1
Pod undocumented
2632 4 my ($m) = @_;
2633 4 my ($rows, $cols) = $m->dim;
2634 4 ▶ for (my $i = 0; $i < $rows; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 13 3 16
2635 13 ▶ for (my $j = 0; $j < $cols; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 62 12 74
2636 62 ▶ return 0 unless ($m->[0][$i][$j] == 1 || $m->[0][$i][$j] == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $m->[0][$i][$j] == 1 or $m->[0][$i][$j] == 0 1 61 62
Condition100%3/3
$m->[0][$i][$j] == 1 or $m->[0][$i][$j] == 0
l!l&&r!l&&!r
9521
MC/DC100%2/2
$m->[0][$i][$j] == 1 or $m->[0][$i][$j] == 0
$m->[0][$i][$j] == 1 $m->[0][$i][$j] == 0
Truth table100%3/3
A or B
A= $m->[0][$i][$j] == 1B= $m->[0][$i][$j] == 0
ABresult
1X1
011
000
2637 }
2638 }
2639 3 return 1;
2640  
2641 }
2642 0 ▶sub as_scilab {
Subroutine as_scilab: not called CC=1 SCAR=6.9
Pod undocumented
2643 0 return (shift)->as_matlab;
2644 }
2645  
2646 2 ▶sub as_matlab {
Subroutine as_matlab: called CC=3 SCAR=11.0
Pod undocumented
2647 2 my ($m) = shift;
2648 2 my %args = (
2649 format => "%s",
2650 name => "",
2651 semi => 0,
2652 @_);
2653 2 my ($row,$col) = $m->dim;
2654 2 my $s = "";
2655
2656 2 ▶ if( $args{name} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 1 1 2
2657 1 $s = "$args{name} = ";
2658 }
2659 2 $s .= "[";
2660 $m->each(
2661 12 ▶ sub { my($x,$i,$j) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
2662 12 $s .= sprintf(" $args{format}",$x);
2663 12 ▶ $s .= ";\n" if( $j == $col && $i != $row);
Branch: 1 branch
BranchTrueFalseTotal
if $j == $col and $i != $row 4 8 12
Condition100%3/3
$j == $col and $i != $row
!ll&&!rl&&r
624
MC/DC100%2/2
$j == $col and $i != $row
$j == $col $i != $row
Truth table100%3/3
A and B
A= $j == $colB= $i != $row
ABresult
0X0
100
111
2664 }
2665 2 );
2666 2 $s .= "]";
2667 2 ▶ $s .= ";" if $args{semi};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"semi"} 0 2 2
2668 2 return $s;
2669 }
2670 #TODO: docs+test
2671 2 ▶sub as_yacas{
Subroutine as_yacas: called CC=2 SCAR=6.9
Pod undocumented
2672 2 my ($m) = shift;
2673 2 my %args = (
2674 format => "%s",
2675 name => "",
2676 semi => 0,
2677 @_);
2678 2 my ($row,$col) = $m->dim;
2679 2 my $s = "";
2680  
2681 2 ▶ if( $args{name} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 1 1 2
2682 1 $s = "$args{name} := ";
2683 }
2684 2 $s .= "{";
2685  
2686 $m->each(
2687 12 ▶ sub { my($x,$i,$j) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
2688 12 ▶ $s .= "{" if ($j == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $j == 1 6 6 12
2689 12 $s .= sprintf("$args{format}",$x);
2690 12 ▶ $s .= "," if( $j != $col );
Branch: 1 branch
BranchTrueFalseTotal
if $j != $col 6 6 12
2691 12 ▶ $s .= "}," if ($j == $col && $i != $row);
Branch: 1 branch
BranchTrueFalseTotal
if $j == $col and $i != $row 4 8 12
Condition100%3/3
$j == $col and $i != $row
!ll&&!rl&&r
624
MC/DC100%2/2
$j == $col and $i != $row
$j == $col $i != $row
Truth table100%3/3
A and B
A= $j == $colB= $i != $row
ABresult
0X0
100
111
2692 }
2693 2 );
2694 2 $s .= "}}";
2695  
2696 2 return $s;
2697 }
2698  
2699 2 ▶sub as_latex{
Subroutine as_latex: called CC=4 SCAR=13.9
Pod undocumented
2700 2 my ($m) = shift;
2701 2 my %args = (
2702 format => "%s",
2703 name => "",
2704 align => "c",
2705 display_math => 0,
2706 @_);
2707  
2708  
2709 2 my ($row,$col) = $m->dim;
2710 2 my $inside;
2711 2 my $s = <<LATEX;
2712 \\left( \\begin{array}{%COLS%}
2713 %INSIDE%\\end{array} \\right)
2714 LATEX
2715 2 $args{align} = lc $args{align};
2716 2 ▶ if( $args{align} !~ m/^(c|l|r)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"align"} =~ /^(c|l|r)$/) 0 2 2
2717 0 croak "Math::MatrixReal::as_latex(): Invalid alignment '$args{align}'";
2718 }
2719  
2720 2 $s =~ s/%COLS%/$args{align} x $col/em;
2721  
2722 2 ▶ if( $args{name} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 1 1 2
2723 1 $s = "$args{name} = $s";
2724 }
2725 $m->each(
2726 12 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
2727 12 my ($x,$i,$j) = @_;
2728 12 $x = sprintf($args{format},$x);
2729  
2730 # last element in each row gets a \\
2731 12 ▶ if ($j == $col && $i != $row){
Branch: 2 branches
BranchTrueFalseTotal
if ($j == $col and $i != $row) { } 4 8 12
elsif ($j == $col and $i == $row) { } 2 6 8
Condition100%3/3
$j == $col and $i != $row
!ll&&!rl&&r
624
MC/DC100%2/2
$j == $col and $i != $row
$j == $col $i != $row
Truth table100%3/3
A and B
A= $j == $colB= $i != $row
ABresult
0X0
100
111
2732 4 $inside .= "$x \\\\"."\n";
2733 # the annoying last line has neither
2734 ▶ } elsif( $j == $col && $i == $row){
Condition66%2/3
$j == $col and $i == $row
!ll&&!rl&&r
602
MC/DC50%1/2
$j == $col and $i == $row
$j == $col $i == $row
Truth table66%2/3
A and B
A= $j == $colB= $i == $row
ABresult
0X0
100
111
2735 2 $inside .= "$x\n";
2736 } else {
2737 6 $inside .= "$x&";
2738 }
2739 }
2740 2 );
2741 2 ▶ if($args{displaymath}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'displaymath'}) { } 0 2 2
2742 0 $s = "\\[$s\\]";
2743 } else {
2744 2 $s = "\$$s\$";
2745 }
2746 2 $s =~ s/%INSIDE%/$inside/gm;
2747 2 return $s;
2748 }
2749 ####
2750 sub spectral_radius
2751 5 ▶{
Subroutine spectral_radius: called CC=1 SCAR=0.0
Pod undocumented
2752 5 my ($matrix) = @_;
2753 5 my ($r,$c) = $matrix->dim;
2754 5 my $ev = $matrix->eigenvalues;
2755 5 my $radius=0;
2756 5 ▶ $ev->each(sub { my $x = shift; $radius = $x if (abs($x) > $radius); } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if abs $x > $radius 9 10 19
2757 5 return $radius;
2758 }
2759  
2760 8 ▶sub maximum {
Subroutine maximum: called CC=5 SCAR=16.1
Pod documented
2761 8 my ($matrix) = @_;
2762 8 my ($rows, $columns) = $matrix->dim;
2763  
2764 8 my $max = [];
2765 8 my $max_p = [];
2766  
2767 8 ▶ if ($rows == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows == 1) { } 2 6 8
elsif ($columns == 1) { } 2 4 6
2768 2 ($max, $max_p) = _max_column($matrix->row(1)->_transpose, $columns);
2769 } elsif ($columns == 1) {
2770 2 ($max, $max_p) = _max_column($matrix->column(1), $rows);
2771 } else {
2772 4 for my $c (1..$columns) {
2773 12 my ($m, $mp) = _max_column($matrix->column($c), $rows);
2774 12 push @$max, $m;
2775 12 push @$max_p, $mp;
2776 }
2777 }
2778 8 ▶ return wantarray ? ($max, $max_p) : $max
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
2779 }
2780  
2781 16 ▶sub _max_column {
Subroutine _max_column: called CC=3 SCAR=11.0
2782 # passing $rows allows for some extra (minimal) efficiency
2783 16 my ($column, $rows) = @_;
2784  
2785 16 my ($m, $mp) = ($column->element(1, 1), 1);
2786 16 for my $l (1..$rows) {
2787 52 ▶ if ($column->element($l, 1) > $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->element($l, 1) > $m) 28 24 52
2788 28 $m = $column->element($l, 1);
2789 28 $mp = $l;
2790 }
2791 }
2792 16 return ($m, $mp);
2793 }
2794  
2795 8 ▶sub minimum {
Subroutine minimum: called CC=5 SCAR=16.1
Pod documented
2796 8 my ($matrix) = @_;
2797 8 my ($rows, $columns) = $matrix->dim;
2798  
2799 8 my $min = [];
2800 8 my $min_p = [];
2801  
2802 8 ▶ if ($rows == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows == 1) { } 2 6 8
elsif ($columns == 1) { } 2 4 6
2803 2 ($min, $min_p) = _min_column($matrix->row(1)->_transpose, $columns);
2804 } elsif ($columns == 1) {
2805 2 ($min, $min_p) = _min_column($matrix->column(1), $rows);
2806 } else {
2807 4 for my $c (1..$columns) {
2808 12 my ($m, $mp) = _min_column($matrix->column($c), $rows);
2809 12 push @$min, $m;
2810 12 push @$min_p, $mp;
2811 }
2812 }
2813 8 ▶ return wantarray ? ($min, $min_p) : $min
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
2814 }
2815  
2816 16 ▶sub _min_column {
Subroutine _min_column: called CC=3 SCAR=12.0
2817 # passing $rows allows for some extra (minimal) efficiency
2818 16 my ($column, $rows) = @_;
2819  
2820 16 my ($m, $mp) = ($column->element(1, 1), 1);
2821 16 for my $l (1..$rows) {
2822 52 ▶ if ($column->element($l, 1) < $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->element($l, 1) < $m) 0 52 52
2823 0 $m = $column->element($l, 1);
2824 0 $mp = $l;
2825 }
2826 }
2827 16 return ($m, $mp);
2828 }
2829  
2830  
2831  
2832 ########################################
2833 # #
2834 # define overloaded operators section: #
2835 # #
2836 ########################################
2837 sub _concat
2838 5 ▶{
Subroutine _concat: called CC=9 SCAR=22.2
2839 5 my($object,$argument,$flag) = @_;
2840 5 my($orows,$ocols) = ($object->[1],$object->[2]);
2841 5 my($name) = "concat";
2842  
2843  
2844 5 ▶ if ((defined $argument) && ref($argument) && (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 4 1 5
elsif (defined $argument) { } 1 0 1
Condition66%4/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
104
defined $argument and ref $argument
!ll&&!rl&&r
014
MC/DC33%1/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table50%2/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
2845 4 my($arows,$acols) = ($argument->[1],$argument->[2]);
2846 4 ▶ croak "Math::MatrixReal: Matrices must have same number of rows in concatenation" unless ($orows == $arows);
Branch: 1 branch
BranchTrueFalseTotal
unless $orows == $arows 1 3 4
2847 3 my $result = $object->new($orows,$ocols+$acols);
2848 3 ▶ for ( my $i = 0; $i < $arows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $arows) 9 3 12
2849 9 ▶ for ( my $j = 0; $j < $ocols + $acols; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $ocols + $acols) 51 9 60
2850 51 ▶ $result->[0][$i][$j] = ( $j < $ocols ) ? $object->[0][$i][$j] : $argument->[0][$i][$j - $ocols] ;
Branch: 1 branch
BranchTrueFalseTotal
$j < $ocols ? : 21 30 51
2851 }
2852 }
2853 3 return $result;
2854 } elsif (defined $argument) {
2855 1 return "$object" . $argument;
2856  
2857 } else {
2858 0 croak "Math::MatrixReal $name: wrong argument type";
2859 }
2860 }
2861 sub _negate
2862 1 ▶{
Subroutine _negate: called CC=1 SCAR=0.0
2863 1 my($object) = @_;
2864  
2865 1 my $temp = $object->new($object->[1],$object->[2]);
2866 1 $temp->negate($object);
2867 1 return($temp);
2868 }
2869  
2870 sub _transpose
2871 62 ▶{
Subroutine _transpose: called CC=1 SCAR=0.0
2872 62 my ($object) = @_;
2873 62 my $temp = $object->new($object->[2],$object->[1]);
2874 62 $temp->transpose($object);
2875 62 return $temp;
2876 }
2877  
2878 sub _boolean
2879 86 ▶{
Subroutine _boolean: called CC=4 SCAR=14.0
2880 86 my($object) = @_;
2881 86 my($rows,$cols) = ($object->[1],$object->[2]);
2882  
2883 86 my $result = 0;
2884  
2885 BOOL:
2886 86 ▶ for ( my $i = 0; $i < $rows; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 86 0 86
2887 {
2888 86 ▶ for ( my $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 88 0 88
2889 {
2890 88 ▶ if ($object->[0][$i][$j] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($object->[0][$i][$j] != 0) 86 2 88
2891 {
2892 86 $result = 1;
2893 86 last BOOL;
2894 }
2895 }
2896 }
2897 86 return($result);
2898 }
2899 #TODO: ugly copy+paste
2900 sub _not_boolean
2901 1 ▶{
Subroutine _not_boolean: called CC=4 SCAR=14.2
2902 1 my ($object) = @_;
2903 1 my ($rows,$cols) = ($object->[1],$object->[2]);
2904  
2905 1 my $result = 1;
2906 NOTBOOL:
2907 1 ▶ for ( my $i = 0; $i < $rows; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 5 1 6
2908 {
2909 5 ▶ for ( my $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 25 5 30
2910 {
2911 25 ▶ if ($object->[0][$i][$j] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($object->[0][$i][$j] != 0) 0 25 25
2912 {
2913 0 $result = 0;
2914 0 last NOTBOOL;
2915 }
2916 }
2917 }
2918 1 return($result);
2919 }
2920  
2921 sub _stringify
2922 7 ▶{
Subroutine _stringify: called CC=6 SCAR=17.9
2923 7 my ($self) = @_;
2924 7 my ($rows,$cols) = ($self->[1],$self->[2]);
2925  
2926 7 my $precision = $self->[4];
2927  
2928 7 ▶ my $format = !defined $precision ? '% #-19.12E ' : '% #-19.'.$precision.'f ';
Branch: 1 branch
BranchTrueFalseTotal
!defined($precision) ? : 3 4 7
2929 7 ▶ $format = '% #-12d' if defined $precision && $precision == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $precision and $precision == 0 2 5 7
Condition100%3/3
defined $precision and $precision == 0
!ll&&!rl&&r
322
MC/DC100%2/2
defined $precision and $precision == 0
defined $precision $precision == 0
Truth table100%3/3
A and B
A= defined $precisionB= $precision == 0
ABresult
0X0
100
111
2930  
2931 7 my $s = '';
2932 7 ▶ for ( my $i = 0; $i < $rows; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 26 7 33
2933 {
2934 26 $s .= "[ ";
2935 26 ▶ for ( my $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 98 26 124
2936 {
2937 98 $s .= sprintf $format , $self->[0][$i][$j];
2938 }
2939 26 $s .= "]\n";
2940 }
2941 7 return $s;
2942 }
2943  
2944 sub _norm
2945 159 ▶{
Subroutine _norm: called CC=1 SCAR=0.0
2946 159 my ($self) = @_;
2947  
2948 159 return $self->norm_one() ;
2949 }
2950  
2951 sub _add
2952 25 ▶{
Subroutine _add: called CC=5 SCAR=17.4
2953 25 my($object,$argument,$flag) = @_;
2954 25 my($name) = "'+'";
2955  
2956 25 ▶ if ((defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 25 0 25
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
0025
defined $argument and ref $argument
!ll&&!rl&&r
0025
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
2957 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
2958 {
2959 25 ▶ if (defined $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) { } 23 2 25
2960 {
2961 23 my $temp = $object->new($object->[1],$object->[2]);
2962 23 $temp->add($object,$argument);
2963 23 return($temp);
2964 }
2965 else
2966 {
2967 2 $object->add($object,$argument);
2968 2 return($object);
2969 }
2970 }
2971 else
2972 {
2973 0 croak "Math::MatrixReal $name: wrong argument type";
2974 }
2975 }
2976  
2977 sub _subtract
2978 164 ▶{
Subroutine _subtract: called CC=6 SCAR=19.9
2979 164 my($object,$argument,$flag) = @_;
2980 164 my($name) = "'-'";
2981  
2982 164 ▶ if ((defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 164 0 164
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
00164
defined $argument and ref $argument
!ll&&!rl&&r
00164
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
2983 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
2984 {
2985 164 ▶ if (defined $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) { } 163 1 164
2986 {
2987 163 my $temp = $object->new($object->[1],$object->[2]);
2988 0 ▶ if ($flag) { $temp->subtract($argument,$object); }
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 163 163
2989 163 else { $temp->subtract($object,$argument); }
2990 163 return $temp;
2991 }
2992 else
2993 {
2994 1 $object->subtract($object,$argument);
2995 1 return($object);
2996 }
2997 }
2998 else
2999 {
3000 0 croak "Math::MatrixReal $name: wrong argument type";
3001 }
3002 }
3003  
3004 sub _exponent
3005 20 ▶{
Subroutine _exponent: called CC=1 SCAR=0.0
3006 20 my($matrix, $exp) = @_;
3007 20 my($rows,$cols) = ($matrix->[1],$matrix->[2]);
3008  
3009 20 return $matrix->exponent( $exp );
3010 }
3011 sub _divide
3012 6 ▶{
Subroutine _divide: called CC=9 SCAR=22.6
3013 6 my($matrix,$argument,$flag) = @_;
3014 # TODO: check dimensions of everything!
3015 6 my($mrows,$mcols) = ($matrix->[1],$matrix->[2]);
3016 6 my($arows,$acols)=(0,0);
3017 6 my($name) = "'/'";
3018 6 my $temp = $matrix->clone();
3019 6 my $arg;
3020 6 my ($inv,$m1);
3021  
3022 6 ▶ if( ref($argument) =~ /Math::MatrixReal/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref($argument) =~ /Math::MatrixReal/) 2 4 6
3023 2 $arg = $argument->clone();
3024 2 ($arows,$acols)=($arg->[1],$arg->[2]);
3025 }
3026 #print "DEBUG: flag= $flag\n";
3027 #print "DEBUG: arg=$arg\n";
3028 6 ▶ if( $flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 1) { } 2 4 6
3029 #print "DEBUG: ref(arg)= " . ref($arg) . "\n";
3030 2 ▶ if( ref($argument) =~ /Math::MatrixReal/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref($argument) =~ /Math::MatrixReal/) { } 0 2 2
3031 #print "DEBUG: arg is a matrix \n";
3032 # Matrix Division = A/B = A*B^(-1)
3033 0 ▶ croak "Math::MatrixReal $name: this operation is defined only for square matrices" unless ($arows == $acols);
Branch: 1 branch
BranchTrueFalseTotal
unless $arows == $acols 0 0 0
3034 0 return $temp->multiply( $arg->inverse() );
3035  
3036 } else {
3037 #print "DEBUG: Arg is scalar\n";
3038 #print "DEBUG:arows,acols=$arows,$acols\n";
3039 #print "DEBGU:mrows,mcols=$mrows,$mcols\n";
3040 2 ▶ croak "Math::MatrixReal $name: this operation is defined only for square matrices" unless ($mrows == $mcols);
Branch: 1 branch
BranchTrueFalseTotal
unless $mrows == $mcols 1 1 2
3041 1 $temp->multiply_scalar( $temp , $argument);
3042 1 return $temp;
3043 }
3044 } else {
3045 #print "DEBUG: temp=\n";
3046 #print $temp . "\n";
3047 #print "DEBUG: ref(arg)= " . ref($arg) . "\n";
3048 #print "DEBUG: arg=\n";
3049 #print $arg ."\n";
3050 4 ▶ if( ref($arg) =~ /Math::MatrixReal/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref($arg) =~ /Math::MatrixReal/) { } 2 2 4
3051 #print "DEBUG: matrix division\n";
3052 2 ▶ if( $arg->is_col_vector() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->is_col_vector) 0 2 2
3053 0 print "DEBUG: $arg is a col vector\n";
3054 }
3055 2 ▶ croak "Math::MatrixReal $name: this operation is defined only for square matrices" unless ($arows == $acols);
Branch: 1 branch
BranchTrueFalseTotal
unless $arows == $acols 0 2 2
3056 2 $inv = $arg->inverse();
3057 2 return $temp->multiply($inv);
3058 } else {
3059 2 $temp->multiply_scalar($temp,1/$argument);
3060 2 return $temp;
3061 }
3062 }
3063
3064 }
3065  
3066 sub _multiply
3067 53 ▶{
Subroutine _multiply: called CC=9 SCAR=24.0
3068 53 my($object,$argument,$flag) = @_;
3069 53 my($name) = "'*'";
3070 53 my($temp);
3071  
3072 53 ▶ if ((defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 32 21 53
elsif (defined $argument and not ref $argument) { } 21 0 21
Condition66%4/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
21032
defined $argument and ref $argument
!ll&&!rl&&r
02132
MC/DC33%1/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table50%2/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3073 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3074 {
3075 32 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 32 32
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
0320
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3076 {
3077 0 return( multiply($argument,$object) );
3078 }
3079 else
3080 {
3081 32 return( multiply($object,$argument) );
3082 }
3083 }
3084 ▶ elsif ((defined $argument) && !(ref($argument)))
Condition33%1/3
defined $argument and not ref $argument
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table33%1/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
3085 {
3086 21 ▶ if (defined $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) { } 20 1 21
3087 {
3088 20 $temp = $object->new($object->[1],$object->[2]);
3089 20 $temp->multiply_scalar($object,$argument);
3090 20 return($temp);
3091 }
3092 else
3093 {
3094 1 $object->multiply_scalar($object,$argument);
3095 1 return($object);
3096 }
3097 }
3098 else
3099 {
3100 0 croak "Math::MatrixReal $name: wrong argument type";
3101 }
3102 }
3103  
3104 sub _assign_add
3105 2 ▶{
Subroutine _assign_add: called CC=1 SCAR=0.0
3106 2 my($object,$argument) = @_;
3107  
3108 2 return( &_add($object,$argument,undef) );
3109 }
3110  
3111 sub _assign_subtract
3112 1 ▶{
Subroutine _assign_subtract: called CC=1 SCAR=0.0
3113 1 my($object,$argument) = @_;
3114  
3115 1 return( &_subtract($object,$argument,undef) );
3116 }
3117  
3118 sub _assign_multiply
3119 1 ▶{
Subroutine _assign_multiply: called CC=1 SCAR=0.0
3120 1 my($object,$argument) = @_;
3121  
3122 1 return( &_multiply($object,$argument,undef) );
3123 }
3124  
3125 1 ▶sub _assign_exponent {
Subroutine _assign_exponent: called CC=1 SCAR=0.0
3126 1 my($object,$arg) = @_;
3127 1 return ( &_exponent($object,$arg,undef) );
3128 }
3129  
3130 sub _equal
3131 10 ▶{
Subroutine _equal: called CC=7 SCAR=19.9
3132 10 my($object,$argument,$flag) = @_;
3133 10 my($name) = "'=='";
3134 10 my($rows,$cols) = ($object->[1],$object->[2]);
3135 10 my($i,$j,$result);
3136  
3137 10 ▶ if ((defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 9 1 10
Condition66%4/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
109
defined $argument and ref $argument
!ll&&!rl&&r
019
MC/DC33%1/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table50%2/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3138 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3139 {
3140 9 $result = 1;
3141 EQUAL:
3142 9 ▶ for ( $i = 0; $i < $rows; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 65 9 74
3143 {
3144 65 ▶ for ( $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 485 65 550
3145 {
3146 485 ▶ if ($object->[0][$i][$j] != $argument->[0][$i][$j])
Branch: 1 branch
BranchTrueFalseTotal
if ($object->[0][$i][$j] != $argument->[0][$i][$j]) 0 485 485
3147 {
3148 0 $result = 0;
3149 0 last EQUAL;
3150 }
3151 }
3152 }
3153 9 return($result);
3154 }
3155 else
3156 {
3157 1 croak "Math::MatrixReal $name: wrong argument type";
3158 }
3159 }
3160  
3161 sub _not_equal
3162 4 ▶{
Subroutine _not_equal: called CC=9 SCAR=22.5
3163 4 my($object,$argument,$flag) = @_;
3164 4 my($name) = "'!='";
3165 4 my($rows,$cols) = ($object->[1],$object->[2]);
3166  
3167 4 ▶ if ((defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 3 1 4
Condition66%4/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
103
defined $argument and ref $argument
!ll&&!rl&&r
013
MC/DC33%1/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table50%2/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3168 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3169 {
3170 3 my ($r,$c) = $argument->dim;
3171 3 ▶ return 1 unless ($r == $rows && $c == $cols );
Branch: 1 branch
BranchTrueFalseTotal
unless $r == $rows and $c == $cols 1 2 3
Condition66%2/3
$r == $rows and $c == $cols
!ll&&!rl&&r
102
MC/DC50%1/2
$r == $rows and $c == $cols
$r == $rows $c == $cols
Truth table66%2/3
A and B
A= $r == $rowsB= $c == $cols
ABresult
0X0
100
111
3172 2 my $result = 0;
3173 NOTEQUAL:
3174 2 ▶ for ( my $i = 0; $i < $rows; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rows) 2 0 2
3175 {
3176 2 ▶ for ( my $j = 0; $j < $cols; $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $cols) 2 0 2
3177 {
3178 2 ▶ if ($object->[0][$i][$j] != $argument->[0][$i][$j])
Branch: 1 branch
BranchTrueFalseTotal
if ($object->[0][$i][$j] != $argument->[0][$i][$j]) 2 0 2
3179 {
3180 2 $result = 1;
3181 2 last NOTEQUAL;
3182 }
3183 }
3184 }
3185 2 return $result;
3186 } else {
3187 1 croak "Math::MatrixReal $name: wrong argument type";
3188 }
3189 }
3190  
3191 sub _less_than
3192 5 ▶{
Subroutine _less_than: called CC=10 SCAR=28.7
3193 5 my($object,$argument,$flag) = @_;
3194 5 my($name) = "'<'";
3195  
3196 5 ▶ if ((defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 3 2 5
elsif (defined $argument and not ref $argument) { } 2 0 2
Condition66%4/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
203
defined $argument and ref $argument
!ll&&!rl&&r
023
MC/DC33%1/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table50%2/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3197 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3198 {
3199 3 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 3 3
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
030
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3200 {
3201 0 return( $argument->norm_one() < $object->norm_one() );
3202 } else {
3203 3 return( $object->norm_one() < $argument->norm_one() );
3204 }
3205 }
3206 ▶ elsif ((defined $argument) && !(ref($argument)))
Condition33%1/3
defined $argument and not ref $argument
!ll&&!rl&&r
002
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table33%1/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
3207 {
3208 2 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 2 2
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
020
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3209 {
3210 0 return( abs($argument) < $object->norm_one() );
3211 } else {
3212 2 return( $object->norm_one() < abs($argument) );
3213 }
3214 } else {
3215 0 croak "Math::MatrixReal $name: wrong argument type";
3216 }
3217 }
3218  
3219 sub _less_than_or_equal
3220 2 ▶{
Subroutine _less_than_or_equal: called CC=10 SCAR=37.8
3221 2 my($object,$argument,$flag) = @_;
3222 2 my($name) = "'<='";
3223  
3224 2 ▶ if ((defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 2 0 2
elsif (defined $argument and not ref $argument) { } 0 0 0
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
002
defined $argument and ref $argument
!ll&&!rl&&r
002
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3225 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3226 {
3227 2 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 2 2
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
020
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3228 {
3229 0 return( $argument->norm_one() <= $object->norm_one() );
3230 } else {
3231 2 return( $object->norm_one() <= $argument->norm_one() );
3232 }
3233 ▶ } elsif ((defined $argument) && !(ref($argument))) {
Condition0%0/3
defined $argument and not ref $argument
!ll&&!rl&&r
000
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table0%0/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
3234 0 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 0 0
Condition0%0/3
defined $flag and $flag
!ll&&!rl&&r
000
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table0%0/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3235 {
3236 0 return( abs($argument) <= $object->norm_one() );
3237 } else {
3238 0 return( $object->norm_one() <= abs($argument) );
3239 }
3240 } else {
3241 0 croak "Math::MatrixReal $name: wrong argument type";
3242 }
3243 }
3244  
3245 sub _greater_than
3246 3 ▶{
Subroutine _greater_than: called CC=10 SCAR=37.8
3247 3 my($object,$argument,$flag) = @_;
3248 3 my($name) = "'>'";
3249  
3250 3 ▶ if ((defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 3 0 3
elsif (defined $argument and not ref $argument) { } 0 0 0
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
003
defined $argument and ref $argument
!ll&&!rl&&r
003
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3251 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3252 {
3253 3 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 3 3
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
030
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3254 {
3255 0 return( $argument->norm_one() > $object->norm_one() );
3256 } else {
3257 3 return( $object->norm_one() > $argument->norm_one() );
3258 }
3259 ▶ } elsif ((defined $argument) && !(ref($argument))) {
Condition0%0/3
defined $argument and not ref $argument
!ll&&!rl&&r
000
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table0%0/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
3260 0 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 0 0
Condition0%0/3
defined $flag and $flag
!ll&&!rl&&r
000
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table0%0/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3261 {
3262 0 return( abs($argument) > $object->norm_one() );
3263 } else {
3264 0 return( $object->norm_one() > abs($argument) );
3265 }
3266 } else {
3267 0 croak "Math::MatrixReal $name: wrong argument type";
3268 }
3269 }
3270  
3271 sub _greater_than_or_equal
3272 2 ▶{
Subroutine _greater_than_or_equal: called CC=10 SCAR=37.8
3273 2 my($object,$argument,$flag) = @_;
3274 2 my($name) = "'>='";
3275  
3276 2 ▶ if ((defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 2 0 2
elsif (defined $argument and not ref $argument) { } 0 0 0
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
002
defined $argument and ref $argument
!ll&&!rl&&r
002
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
3277 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
3278 {
3279 2 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 2 2
Condition33%1/3
defined $flag and $flag
!ll&&!rl&&r
020
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table33%1/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3280 {
3281 0 return( $argument->norm_one() >= $object->norm_one() );
3282 } else {
3283 2 return( $object->norm_one() >= $argument->norm_one() );
3284 }
3285 ▶ } elsif ((defined $argument) && !(ref($argument))) {
Condition0%0/3
defined $argument and not ref $argument
!ll&&!rl&&r
000
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table0%0/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
3286 0 ▶ if ((defined $flag) && $flag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag and $flag) { } 0 0 0
Condition0%0/3
defined $flag and $flag
!ll&&!rl&&r
000
MC/DC0%0/2
defined $flag and $flag
defined $flag $flag
Truth table0%0/3
A and B
A= defined $flagB= $flag
ABresult
0X0
100
111
3287 {
3288 0 return( abs($argument) >= $object->norm_one() );
3289 } else {
3290 0 return( $object->norm_one() >= abs($argument) );
3291 }
3292 } else {
3293 0 croak "Math::MatrixReal $name: wrong argument type";
3294 }
3295 }
3296  
3297 sub _clone
3298 4 ▶{
Subroutine _clone: called CC=1 SCAR=0.0
3299 4 my($object) = @_;
3300  
3301 4 my $temp = $object->new($object->[1],$object->[2]);
3302 4 $temp->copy($object);
3303 4 $temp->_undo_LR();
3304 4 return $temp;
3305 }
3306 59 ▶{ no warnings; 42 }
Subroutine BEGIN: called CC=1 SCAR=0.0
3307 __END__
↑ Summary funcs.pl »