File Coverage

blib/lib/PDF/API2/Matrix.pm
Criterion Covered Total %
statement 43 47 91.4
branch 2 4 50.0
condition n/a
subroutine 6 6 100.0
pod 0 4 0.0
total 51 61 83.6


line stmt bran cond sub pod time code
1             package PDF::API2::Matrix;
2              
3 39     39   270 use strict;
  39         82  
  39         1150  
4              
5 39     39   194 use Carp;
  39         76  
  39         18562  
6              
7             our $VERSION = '2.045'; # VERSION
8              
9             sub new {
10 132     132 0 194 my $type = shift();
11 132         186 my $self = [];
12 132         161 my $col_count = scalar(@{$_[0]});
  132         189  
13 132         186 foreach my $row (@_) {
14 396 50       631 unless (scalar(@$row) == $col_count) {
15 0         0 carp 'Inconsistent column count in matrix';
16 0         0 return;
17             }
18 396         807 push @$self, [@$row];
19             }
20              
21 132         467 return bless($self, $type);
22             }
23              
24             sub transpose {
25 39     39 0 57 my $self = shift();
26 39         55 my @result;
27             my $m;
28              
29 39         60 for my $col (@{$self->[0]}) {
  39         86  
30 117         178 push @result, [];
31             }
32 39         107 for my $row (@$self) {
33 117         141 $m = 0;
34 117         153 for my $col (@$row) {
35 351         410 push @{$result[$m++]}, $col;
  351         548  
36             }
37             }
38              
39 39         77 return PDF::API2::Matrix->new(@result);
40             }
41              
42             sub vector_product {
43 351     351 0 521 my ($a, $b) = @_;
44 351         418 my $result = 0;
45              
46 351         390 for my $i (0 .. $#{$a}) {
  351         554  
47 1053         1523 $result += $a->[$i] * $b->[$i];
48             }
49              
50 351         623 return $result;
51             }
52              
53             sub multiply {
54 39     39 0 58 my $self = shift();
55 39         74 my $other = shift->transpose();
56 39         59 my @result;
57              
58 39 50       51 unless ($#{$self->[0]} == $#{$other->[0]}) {
  39         64  
  39         76  
59 0         0 carp 'Mismatched dimensions in matrix multiplication';
60 0         0 return;
61             }
62 39         70 for my $row (@$self) {
63 117         152 my $result_col = [];
64 117         169 for my $col (@$other) {
65 351         539 push @$result_col, vector_product($row, $col);
66             }
67 117         181 push @result, $result_col;
68             }
69              
70 39         86 return PDF::API2::Matrix->new(@result);
71             }
72              
73             1;