| 1 | package PDF::API2::Matrix; | ||||||||||
| 2 | |||||||||||
| 3 | 38 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 38 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 38 | our $VERSION = '2.044'; # VERSION | |||||||||
| 8 | |||||||||||
| 9 | sub new { | ||||||||||
| 10 | 132 | ▶ | my $type = shift(); | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||
| 11 | 132 | my $self = []; | |||||||||
| 12 | 132 | my $col_count = scalar(@{$_[0]}); | |||||||||
| 13 | 132 | foreach my $row (@_) { | |||||||||
| 14 | 396 | ▶ | unless (scalar(@$row) == $col_count) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | carp 'Inconsistent column count in matrix'; | |||||||||
| 16 | 0 | return; | |||||||||
| 17 | } | ||||||||||
| 18 | 396 | push @$self, [@$row]; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 132 | return bless($self, $type); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | sub transpose { | ||||||||||
| 25 | 39 | ▶ | my $self = shift(); | ||||||||
|
Subroutine
transpose: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 26 | 39 | my @result; | |||||||||
| 27 | my $m; | ||||||||||
| 28 | |||||||||||
| 29 | 39 | for my $col (@{$self->[0]}) { | |||||||||
| 30 | 117 | push @result, []; | |||||||||
| 31 | } | ||||||||||
| 32 | 39 | for my $row (@$self) { | |||||||||
| 33 | 117 | $m = 0; | |||||||||
| 34 | 117 | for my $col (@$row) { | |||||||||
| 35 | 351 | push @{$result[$m++]}, $col; | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 39 | return PDF::API2::Matrix->new(@result); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | sub vector_product { | ||||||||||
| 43 | 351 | ▶ | my ($a, $b) = @_; | ||||||||
|
Subroutine
vector_product: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 44 | 351 | my $result = 0; | |||||||||
| 45 | |||||||||||
| 46 | 351 | for my $i (0 .. $#{$a}) { | |||||||||
| 47 | 1053 | $result += $a->[$i] * $b->[$i]; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 351 | return $result; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | sub multiply { | ||||||||||
| 54 | 39 | ▶ | my $self = shift(); | ||||||||
|
Subroutine
multiply: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||
| 55 | 39 | my $other = shift->transpose(); | |||||||||
| 56 | 39 | my @result; | |||||||||
| 57 | |||||||||||
| 58 | 39 | ▶ | unless ($#{$self->[0]} == $#{$other->[0]}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | carp 'Mismatched dimensions in matrix multiplication'; | |||||||||
| 60 | 0 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | 39 | for my $row (@$self) { | |||||||||
| 63 | 117 | my $result_col = []; | |||||||||
| 64 | 117 | for my $col (@$other) { | |||||||||
| 65 | 351 | push @$result_col, vector_product($row, $col); | |||||||||
| 66 | } | ||||||||||
| 67 | 117 | push @result, $result_col; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 39 | return PDF::API2::Matrix->new(@result); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 38 | 1; | |||||||||