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 <areibens@cpan.org> for PDF::API2
6             # rewritten by Steve Simms <steve@deefs.net> and licensed under the same
7             # terms as the rest of PDF::API2
8             #
9             #=======================================================================
10             package PDF::Builder::Matrix;
11              
12 39     39   284 use strict;
  39         95  
  39         1622  
13 39     39   298 use warnings;
  39         115  
  39         2301  
14 39     39   246 use Carp;
  39         98  
  39         24731  
15              
16             our $VERSION = '3.028'; # VERSION
17             our $LAST_UPDATE = '3.027'; # 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 192 my $type = shift();
27              
28 132         166 my $self = [];
29 132         176 my $col_count = scalar(@{$_[0]});
  132         197  
30 132         232 foreach my $row (@_) {
31 396 50       647 unless (scalar(@$row) == $col_count) {
32 0         0 carp 'Inconsistent column count in matrix';
33 0         0 return;
34             }
35 396         465 push(@{$self}, [@$row]);
  396         820  
36             }
37              
38 132         624 return bless($self, $type);
39             }
40              
41             # internal routine
42             sub transpose {
43 39     39 0 51 my $self = shift();
44              
45 39         314 my @result;
46             my $m;
47              
48 39         64 for my $col (@{$self->[0]}) {
  39         85  
49 117         320 push @result, [];
50             }
51 39         65 for my $row (@$self) {
52 117         259 $m = 0;
53 117         164 for my $col (@$row) {
54 351         362 push @{$result[$m++]}, $col;
  351         536  
55             }
56             }
57              
58 39         80 return PDF::Builder::Matrix->new(@result);
59             }
60              
61             # internal routine
62             sub vector_product {
63 351     351 0 477 my ($a, $b) = @_;
64 351         403 my $result = 0;
65              
66 351         400 for my $i (0 .. $#{$a}) {
  351         607  
67 1053         1640 $result += $a->[$i] * $b->[$i];
68             }
69              
70 351         678 return $result;
71             }
72              
73             # used by Content.pm
74             sub multiply {
75 39     39 0 58 my $self = shift();
76 39         84 my $other = shift->transpose();
77              
78 39         55 my @result;
79              
80 39 50       47 unless ($#{$self->[0]} == $#{$other->[0]}) {
  39         65  
  39         110  
81 0         0 carp 'Mismatched dimensions in matrix multiplication';
82 0         0 return;
83             }
84 39         84 for my $row (@$self) {
85 117         204 my $result_col = [];
86 117         182 for my $col (@$other) {
87 351         556 push @$result_col, vector_product($row,$col);
88             }
89 117         209 push @result, $result_col;
90             }
91              
92 39         112 return PDF::Builder::Matrix->new(@result);
93             }
94              
95             1;