File Coverage

blib/lib/PDF/Builder/Matrix.pm
Criterion Covered Total %
statement 47 51 92.1
branch 2 4 50.0
condition n/a
subroutine 7 7 100.0
pod 0 4 0.0
total 56 66 84.8


line stmt bran cond sub pod time code
1             #=======================================================================
2             #
3             # PDF::Builder::Matrix
4             # Original Copyright 1995-96 Ulrich Pfeifer.
5             # modified by Alfred Reibenschuh for PDF::API2
6             # rewritten by Steve Simms and licensed under the same
7             # terms as the rest of PDF::API2
8             #
9             #=======================================================================
10             package PDF::Builder::Matrix;
11              
12 38     38   273 use strict;
  38         77  
  38         1171  
13 38     38   198 use warnings;
  38         112  
  38         871  
14 38     38   191 use Carp;
  38         84  
  38         20481  
15              
16             our $VERSION = '3.025'; # VERSION
17             our $LAST_UPDATE = '3.020'; # manually update whenever code is changed
18              
19             =head1 NAME
20              
21             PDF::Builder::Matrix - matrix operations library
22              
23             =cut
24              
25             sub new {
26 132     132 0 203 my $type = shift();
27              
28 132         188 my $self = [];
29 132         173 my $col_count = scalar(@{$_[0]});
  132         198  
30 132         200 foreach my $row (@_) {
31 396 50       684 unless (scalar(@$row) == $col_count) {
32 0         0 carp 'Inconsistent column count in matrix';
33 0         0 return;
34             }
35 396         463 push(@{$self}, [@$row]);
  396         822  
36             }
37              
38 132         556 return bless($self, $type);
39             }
40              
41             # internal routine
42             sub transpose {
43 39     39 0 47 my $self = shift();
44              
45 39         68 my @result;
46             my $m;
47              
48 39         49 for my $col (@{$self->[0]}) {
  39         88  
49 117         179 push @result, [];
50             }
51 39         69 for my $row (@$self) {
52 117         150 $m = 0;
53 117         152 for my $col (@$row) {
54 351         446 push @{$result[$m++]}, $col;
  351         584  
55             }
56             }
57              
58 39         88 return PDF::Builder::Matrix->new(@result);
59             }
60              
61             # internal routine
62             sub vector_product {
63 351     351 0 483 my ($a, $b) = @_;
64 351         439 my $result = 0;
65              
66 351         425 for my $i (0 .. $#{$a}) {
  351         556  
67 1053         1632 $result += $a->[$i] * $b->[$i];
68             }
69              
70 351         675 return $result;
71             }
72              
73             # used by Content.pm
74             sub multiply {
75 39     39 0 60 my $self = shift();
76 39         72 my $other = shift->transpose();
77              
78 39         52 my @result;
79              
80 39 50       56 unless ($#{$self->[0]} == $#{$other->[0]}) {
  39         66  
  39         122  
81 0         0 carp 'Mismatched dimensions in matrix multiplication';
82 0         0 return;
83             }
84 39         76 for my $row (@$self) {
85 117         191 my $result_col = [];
86 117         173 for my $col (@$other) {
87 351         541 push @$result_col, vector_product($row,$col);
88             }
89 117         179 push @result, $result_col;
90             }
91              
92 39         84 return PDF::Builder::Matrix->new(@result);
93             }
94              
95             1;