blib/lib/Math/MatrixReal/Ext1.pm

filter: Statementstmt 100% 66 / 66 Branchbran 89.2% 25 / 28 Conditioncond 55.5% 10 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 82.9% 112 / 135 CC 23 SCAR 25.6
CC
23
Cov
90%
_new_from_rows_or_cols
19.1
new_from_cols
3.3
new_from_rows
3.3
CRAP 12.9
↑ Summary
1 package Math::MatrixReal::Ext1;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Math::MatrixReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base qw/Math::MatrixReal/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.07';
10  
11 10 ▶sub new_from_cols {
Subroutine new_from_cols: called CC=3 SCAR=12.0
Pod documented
12 10 my $this = shift;
13 10 ▶ my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[-1] eq 'HASH' ? : 0 10 10
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
1000
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
14 10 $extra_args->{_type} = 'column';
15  
16 10 return $this->_new_from_rows_or_cols(@_, $extra_args );
17 }
18 4 ▶sub new_from_columns {
Subroutine new_from_columns: called CC=1 SCAR=0.0
Pod undocumented
19 4 my $this = shift;
20 4 $this->new_from_cols(@_);
21 }
22 8 ▶sub new_from_rows {
Subroutine new_from_rows: called CC=3 SCAR=12.0
Pod documented
23 8 my $this = shift;
24 8 ▶ my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[-1] eq 'HASH' ? : 0 8 8
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
800
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
25 8 $extra_args->{_type} = 'row';
26  
27 8 return $this->_new_from_rows_or_cols(@_, $extra_args );
28 }
29  
30 18 ▶sub _new_from_rows_or_cols {
Subroutine _new_from_rows_or_cols: called CC=19 SCAR=29.5
31 18 my $proto = shift;
32 18 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1170
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
33 18 my $ref_to_vectors = shift;
34  
35 # these additional args are internal at the moment,
36 # but in the future the user could pass e.g. {pad=>1} to
37 # request padding
38 18 my $args = pop;
39 18 my $vector_type = $args->{_type};
40 18 ▶ die "Internal ".__PACKAGE__." error" unless $vector_type =~ /^(row|column)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $vector_type =~ /^(row|column)$/ 0 18 18
41  
42 # step back one frame because this private method is
43 # not how the user called it
44 18 my $caller_subname = (caller(1))[3];
45  
46 # note--this die() could be inconvenient if someone had something
47 # really fancy that knew how to be dereffed as an array
48 # (can you do that with a tied scalar?), but I'm not putting
49 # the rest of the world through an eval--they can just
50 # deref and pass a reference themselves. If that ever happens
51 # we can add an arg to skip this check
52 18 ▶ croak "$caller_subname: need a reference to an array of ${vector_type}s" unless ref($ref_to_vectors) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref_to_vectors eq "ARRAY" 2 16 18
53 16 my @vectors = @{$ref_to_vectors};
54  
55 16 my $matrix;
56  
57 16 my $other_type = {row=>'column', column=>'row'}->{$vector_type};
58  
59 16 my %matrix_dim = (
60 $vector_type => scalar( @vectors ),
61 $other_type => 0, # we will correct this in a bit
62 );
63  
64 # row and column indices are one based
65 16 my $current_vector_count = 1;
66 16 foreach my $current_vector (@vectors) {
67 # dimension is one-based, so we're
68 # starting with one here and incrementing
69 # as we go. The other dimension is fixed (for now, until
70 # we add the 'pad' option), and gets set later
71 39 my $ref = ref( $current_vector ) ;
72  
73 39 ▶ if ( $ref eq '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq '') { } 9 30 39
elsif ($ref eq 'ARRAY') { } 12 18 30
elsif ($ref ne 'HASH' and $current_vector->isa('Math::MatrixReal')) { } 17 1 18
74 # we hope this is a properly formatted Math::MatrixReal string,
75 # but if not we just let the Math::MatrixReal die() do it's
76 # thing
77 9 $current_vector = $class->new_from_string( $current_vector );
78 }
79 elsif ( $ref eq 'ARRAY' ) {
80 12 my @array = @$current_vector;
81 12 ▶ 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 11 12
82 # we need to create the right kind of string based on whether
83 # they said they were sending us rows or columns:
84 11 ▶ if ($vector_type eq 'row') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vector_type eq 'row') { } 4 7 11
85 4 $current_vector = $class->new_from_string( '[ '. join( " ", @array) ." ]\n" );
86 }
87 else {
88 7 $current_vector = $class->new_from_string( '[ '. join( " ]\n[ ", @array) ." ]\n" );
89 }
90 }
91 ▶ elsif ( $ref ne 'HASH' and $current_vector->isa('Math::MatrixReal') ) {
Condition66%2/3
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal")
!ll&&!rl&&r
1017
MC/DC50%1/2
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal")
$ref ne "HASH" $current_vector->isa("Math::MatrixReal")
Truth table66%2/3
A and B
A= $ref ne "HASH"B= $current_vector->isa("Math::MatrixReal")
ABresult
0X0
100
111
92 # it's already a Math::MatrixReal something.
93 # we don't need to do anything, it will all
94 # work out
95 }
96 else {
97 # we have no idea, error time!
98 1 croak "$caller_subname: I only know how to deal with array refs, strings, and things that inherit from Math::MatrixReal\n";
99 }
100  
101 # starting now we know $current_vector isa Math::MatrixReal thingy
102 36 my @vector_dims = $current_vector->dim;
103  
104 #die unless the appropriate dimension is 1
105 36 ▶ croak "$caller_subname: I don't accept $other_type vectors"
Branch: 2 branches
BranchTrueFalseTotal
unless $vector_dims[$vector_type eq "row" ? 0 : 1] == 1 2 34 36
$vector_type eq 'row' ? : 21 15 36
106 unless ($vector_dims[ $vector_type eq 'row' ? 0 : 1 ] == 1) ;
107  
108 # the other dimension is the length of our vector
109 34 ▶ my $length = $vector_dims[ $vector_type eq 'row' ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$vector_type eq 'row' ? : 20 14 34
110  
111 # set the "other" dimension to the length of this
112 # vector the first time through
113 34 ▶ $matrix_dim{$other_type} ||= $length;
Condition66%2/3
$matrix_dim{$other_type} ||= $length
l!l&&r!l&&!r
20140
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
114  
115 # die unless length of this vector matches the first length
116 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"
117 34 ▶ unless ($length == $matrix_dim{$other_type}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $length == $matrix_dim{$other_type} 2 32 34
118  
119 # create the matrix the first time through
120 32 ▶ $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
18140
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
121  
122 # step along the vector assigning the value of each element
123 # to the correct place in the matrix we're building
124 32 foreach my $element_index ( 1..$length ){
125 # args for vector assignment:
126 # initialize both to one and reset the correct
127 # one below
128 92 my ($v_r, $v_c) = (1,1);
129  
130 # args for matrix assignment
131 92 my ($row_index, $column_index, $value);
132  
133 92 ▶ if ($vector_type eq 'row') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vector_type eq 'row') { } 57 35 92
134 57 $row_index = $current_vector_count;
135 57 $v_c = $column_index = $element_index;
136 }
137 else {
138 35 $v_r = $row_index = $element_index;
139 35 $column_index = $current_vector_count;
140 }
141 92 $value = $current_vector->element($v_r, $v_c);
142 92 $matrix->assign($row_index, $column_index, $value);
143 }
144 32 $current_vector_count ++ ;
145 }
146 9 return $matrix;
147 }
148  
149  
150 1 1;
151 __END__
↑ Summary