Object3D/Matrix.pm

filter: Statementstmt 99.5% 239 / 240 Branchbran 92.5% 50 / 54 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 34 / 34 Podpod 77.7% 7 / 9 totaltotal 95.7% 340 / 355 CC 38 SCAR 12.4
CC
38
Cov
97%
_comparison
8.0
print
5.0
add
5.0
CRAP 3.5
« Object3D/Matrix/Transform.pm ↑ Summary Object3D/Point.pm »
1 package Vector::Object3D::Matrix;
2  
3 =head1 NAME
4  
5 Vector::Object3D::Matrix - Matrix definitions and basic operations
6  
7 =head2 SYNOPSIS
8  
9 use Vector::Object3D::Matrix;
10  
11 # Create an instance of a class:
12 my $matrix = Vector::Object3D::Matrix->new(rows => [[-2, 2], [2, 1], [-1, -1]]);
13 my $matrix = Vector::Object3D::Matrix->new(cols => [[-2, 2, -1], [2, 1, -1]]);
14  
15 # Create a new object as a copy of an existing object:
16 my $copy = $matrix->copy;
17  
18 # Get number of columns/rows from a matrix object:
19 my $num_cols = $matrix->num_cols;
20 my $num_rows = $matrix->num_rows;
21  
22 # Fetch matrix data as an array of column/row values:
23 my $cols = $matrix->get_cols;
24 my $rows = $matrix->get_rows;
25  
26 # Set new precision value (which is used while printing out data and comparing
27 # the matrix object with others):
28 my $precision = 2;
29 $matrix->set(parameter => 'precision', value => $precision);
30  
31 # Get currently used precision value (undef indicates maximum possible precision
32 # which is designated to the Perl core):
33 my $precision = $matrix->get(parameter => 'precision');
34  
35 # Print out formatted matrix data:
36 $matrix->print(fh => $fh, precision => $precision);
37  
38 # Produce a matrix that is a result of scalar multiplication:
39 my $matrix2 = 2 * $matrix1;
40  
41 # Produce a matrix that is a result of matrix multiplication:
42 my $matrix3 = $matrix1 * $matrix2;
43  
44 # Add two matrices:
45 my $matrix4 = $matrix1 + $matrix2;
46  
47 # Subtract one matrix from another:
48 my $matrix5 = $matrix1 - $matrix2;
49  
50 # Compare two matrix objects:
51 my $are_the_same = $matrix1 == $matrix2;
52  
53 # Append another column to a matrix object:
54 $matrix->add(col => [2, -1, 3]);
55  
56 # Add another row to a matrix object:
57 $matrix->add(row => [0, 1, -2]);
58  
59 =head1 DESCRIPTION
60  
61 Although C<Vector::Object3D::Matrix> was originally meant as an auxiliary package supporting all the necessary calculations performed in the 3D space that are handled by C<Vector::Object3D> module only, it may still be used as a standalone module providing support for basic matrix operations (please note however that there are plenty more advanced modules available on CPAN already that serve exactly same purpose).
62  
63 Matrix definitions and basic operations like multiplication, addition and subtraction are implemented. It is also feasible to print out text-based contents of a matrix object to the standard output. Auxiliary static methods allow setting up 2D/3D transformation matrices.
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 Create an instance of a C<Vector::Object3D::Matrix> class:
70  
71 my $rows = [[-2, 2], [2, 1], [-1, -1]];
72 my $matrix = Vector::Object3D::Matrix->new(rows => $rows);
73  
74 my $cols = [[-2, 2, -1], [2, 1, -1]];
75 my $matrix = Vector::Object3D::Matrix->new(cols => $cols);
76  
77 There are two individual means of C<Vector::Object3D::Matrix> object construction, provided list of either rows or columns of numeric values. Although data is internally always stored as rows, cols constructor parameter takes precedence over rows in case both values are provided at the same time.
78  
79 =cut
80  
81 8 our $VERSION = '0.01';
82  
83 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 8 with 'Vector::Object3D::Parameters';
88 8 with 'Vector::Object3D::Matrix::Transform';
89  
90 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
91 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 8 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 8 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
94 8 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 use overload
97 8 ▶ '*' => \&_multiplication,
Subroutine BEGIN: called CC=1 SCAR=0.0
98 '+' => \&_addition,
99 '-' => \&_subtraction,
100 '==' => \&_comparison,
101 8 '!=' => \&_negative_comparison;
102  
103 8 has rows => (
104 is => 'ro',
105 isa => 'ArrayRef[ArrayRef[Num]]',
106 required => 1,
107 );
108  
109 1014 ▶sub build_default_parameter_values {
Subroutine build_default_parameter_values: called CC=1 SCAR=0.0
Pod undocumented
110 1014 my %parameter_values = (
111 precision => undef,
112 );
113  
114 1014 return \%parameter_values;
115 }
116  
117 507 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 507 my ($orig, $class, %args) = @_;
119  
120 507 my $cols = $args{cols};
121  
122 507 ▶ my $rows = defined $cols ? $class->_to_rows(cols => $cols) : $args{rows};
Branch: 1 branch
BranchTrueFalseTotal
defined $cols ? : 10 497 507
123  
124 507 return $class->$orig(rows => dclone $rows);
125 8 };
126  
127 507 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
128 507 my ($self) = @_;
129  
130 # Prepare printable version of matrix data:
131 507 my $data;
132 507 my $sh = new IO::Scalar \$data;
133 507 $self->print(fh => $sh);
134  
135 # Check rows data for inconsistencies:
136 507 my $rows = $self->get_rows;
137 507 my $num_cols = $self->num_cols;
138  
139 507 for my $row (@{$rows}) {
140 1047 ▶ croak qq{Inconsistent matrix initialization data (number of columns varies between different rows): ${data}} unless @{$row} == $num_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$row;} == $num_cols 2 1045 1047
141 }
142  
143 505 return;
144 }
145  
146 1167 ▶sub _to_cols {
Subroutine _to_cols: called CC=3 SCAR=11.0
147 1167 my ($class, %args) = @_;
148  
149 1167 my $rows = $args{rows};
150  
151 1167 my @cols;
152  
153 1167 for my $vals (@{$rows}) {
154  
155 2665 ▶ for (my $col = 0; $col < @{$vals}; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < @{$vals;}) 8929 2665 11594
156  
157 8929 push @{ $cols[$col] }, $vals->[$col];
158 }
159 }
160  
161 1167 return \@cols;
162 }
163  
164 11 ▶sub _to_rows {
Subroutine _to_rows: called CC=3 SCAR=11.0
165 11 my ($class, %args) = @_;
166  
167 11 my $cols = $args{cols};
168  
169 11 my @rows;
170  
171 11 for my $vals (@{$cols}) {
172  
173 22 ▶ for (my $row = 0; $row < @{$vals}; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < @{$vals;}) 59 22 81
174  
175 59 push @{ $rows[$row] }, $vals->[$row];
176 }
177 }
178  
179 11 return \@rows;
180 }
181  
182 =head2 copy
183  
184 Create a new C<Vector::Object3D::Matrix> object as a copy of an existing object:
185  
186 my $copy = $matrix->copy;
187  
188 =cut
189  
190 2 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
191 2 my ($self) = @_;
192  
193 2 my $rows = $self->rows;
194  
195 2 my $class = $self->meta->name;
196 2 my $copy = $class->new(rows => $rows);
197  
198 2 return $copy;
199 }
200  
201 =head2 num_cols
202  
203 Get number of columns from a matrix object:
204  
205 my $num_cols = $matrix->num_cols;
206  
207 =cut
208  
209 1159 ▶sub num_cols {
Subroutine num_cols: called CC=1 SCAR=0.0
Pod documented
210 1159 my ($self) = @_;
211  
212 1159 my $cols = $self->get_cols;
213  
214 1159 return scalar @{$cols};
215 }
216  
217 =head2 num_rows
218  
219 Get number of rows from a matrix object:
220  
221 my $num_rows = $matrix->num_rows;
222  
223 =cut
224  
225 521 ▶sub num_rows {
Subroutine num_rows: called CC=1 SCAR=0.0
Pod documented
226 521 my ($self) = @_;
227  
228 521 my $rows = $self->get_rows;
229  
230 521 return scalar @{$rows};
231 }
232  
233 =head2 get_cols
234  
235 Fetch matrix data as an array of column values:
236  
237 my $cols = $matrix->get_cols;
238  
239 =cut
240  
241 1166 ▶sub get_cols {
Subroutine get_cols: called CC=1 SCAR=0.0
Pod documented
242 1166 my ($self) = @_;
243  
244 1166 my $rows = $self->rows;
245  
246 1166 my $cols = $self->_to_cols(rows => $rows);
247  
248 1166 return $cols;
249 }
250  
251 =head2 get_rows
252  
253 Fetch matrix data as an array of row values:
254  
255 my $rows = $matrix->get_rows;
256  
257 =cut
258  
259 2015 ▶sub get_rows {
Subroutine get_rows: called CC=1 SCAR=0.0
Pod documented
260 2015 my ($self) = @_;
261  
262 2015 my $rows = $self->rows;
263  
264 2015 return dclone($rows);
265 }
266  
267 =head2 add
268  
269 Append another column to a matrix instance:
270  
271 $matrix->add(col => [2, -1, 3]);
272  
273 Add another row to an existing matrix object:
274  
275 $matrix->add(row => [0, 1, -2]);
276  
277 Both add scenarios (adding a new column and adding a new row) will skip adding values that would exceed the other size of a matrix and fill in the missing ones with zeroes.
278  
279 =cut
280  
281 136 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
282 136 my ($self, %args) = @_;
283  
284 136 my $col = $args{col};
285 136 my $row = $args{row};
286  
287 136 ▶ if (defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col) { } 132 4 136
288  
289 # Number of rows in a column needs to match a number of rows in a
290 # matrix object, otherwise adding a new column would not make sense:
291 132 my $num_rows = $self->num_rows;
292 132 my $new_col = dclone $col;
293  
294 132 ▶ if (@{$new_col} > $num_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$new_col;} > $num_rows) { } 1 131 132
295 1 $self->_reduce_array_length($new_col, $num_rows);
296 }
297 else {
298 131 $self->_fill_up_array_with_zeroes($new_col, $num_rows);
299 }
300  
301 132 ▶ for (my $i = 0; $i < @{$new_col}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$new_col;}) 138 132 270
302 138 my $val = $new_col->[$i];
303 138 push @{$self->rows->[$i]}, $val;
304 }
305 }
306 else {
307  
308 # Number of columns in a row needs to match a number of columns in
309 # a matrix object, otherwise adding a new row would not make sense:
310 4 my $num_cols = $self->num_cols;
311 4 my $new_row = dclone $row;
312  
313 4 ▶ if (@{$new_row} > $num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$new_row;} > $num_cols) { } 1 3 4
314 1 $self->_reduce_array_length($new_row, $num_cols);
315 }
316 else {
317 3 $self->_fill_up_array_with_zeroes($new_row, $num_cols);
318 }
319  
320 4 push @{$self->rows}, $new_row;
321 }
322  
323 136 return;
324 }
325  
326 2 ▶sub _reduce_array_length {
Subroutine _reduce_array_length: called CC=1 SCAR=0.0
327 2 my ($self, $array, $length) = @_;
328  
329 2 splice @{$array}, $length;
330  
331 2 return;
332 }
333  
334 134 ▶sub _fill_up_array_with_zeroes {
Subroutine _fill_up_array_with_zeroes: called CC=1 SCAR=0.0
335 134 my ($self, $array, $length) = @_;
336  
337 134 push @{$array}, split //, 0 x ($length - @{$array});
338  
339 134 return;
340 }
341  
342 =head2 set
343  
344 Set new precision value (which is used while comparing matrix objects with each other):
345  
346 my $precision = 2;
347 $matrix->set(parameter => 'precision', value => $precision);
348  
349 =head2 get
350  
351 Get currently used precision value (undef indicates maximum possible precision which is designated to the Perl core):
352  
353 my $precision = $matrix->get(parameter => 'precision');
354  
355 =head2 print
356  
357 Print out text-formatted matrix data (which might be, for instance, useful for debugging purposes):
358  
359 $matrix->print(fh => $fh, precision => $precision);
360  
361 C<fh> defaults to the standard output. C<precision> is intended for internal use by string format specifier that outputs individual matrix values as decimal floating points, and defaults to 2.
362  
363 =cut
364  
365 545 ▶sub print {
Subroutine print: called CC=5 SCAR=16.1
Pod documented
366 545 my ($self, %args) = @_;
367  
368 545 ▶ my $fh = $args{fh} || *STDOUT;
Condition33%1/3
$args{'fh'} || *STDOUT
l!l&&r!l&&!r
54500
MC/DC0%0/2
$args{'fh'} || *STDOUT
$args{'fh'} *STDOUT
Truth table33%1/3
A || B
A= $args{'fh'}B= *STDOUT
ABresult
1X1
011
000
369 545 ▶ my $precision = $args{precision} || 2;
Condition100%2/2
$args{'precision'} || 2
l!l
15530
MC/DC100%1/1
$args{'precision'} || 2
$args{'precision'}
Truth table100%2/2
A
A= $args{'precision'}
Aresult
11
00
370  
371 545 $precision =~ s/\D//;
372  
373 # Calculate maximum possible length of a single matrix item:
374 545 my $maxlen = $self->_get_item_max_length($precision);
375  
376 545 my $stdout = select $fh;
377  
378 545 foreach my $row (@{$self->rows}) {
379 1095 print qq{\n[ };
380 1095 foreach my $val (@{$row}) {
381 3728 printf qq{%${maxlen}.${precision}f }, $val;
382 }
383 1095 print qq{]};
384 }
385  
386 545 select $stdout;
387  
388 545 return;
389 }
390  
391 545 ▶sub _get_item_max_length {
Subroutine _get_item_max_length: called CC=1 SCAR=0.0
392 545 my ($self, $precision) = @_;
393  
394 545 my @values = map { @{$_} } @{$self->get_rows};
395 545 my @lengths = map { length sprintf qq{%.${precision}f}, $_ } @values;
396  
397 545 my $max = max(@lengths);
398 545 return $max;
399 }
400  
401 =head2 get_rotation_matrix
402  
403 Construct rotation matrix on a 2D plane:
404  
405 my $rotateMatrix2D = Vector::Object3D::Matrix->get_rotation_matrix(
406 rotate_xy => (30 * $pi / 180),
407 );
408  
409 Construct rotation matrix in a 3D space:
410  
411 my $rotateMatrix3D = Vector::Object3D::Matrix->get_rotation_matrix(
412 rotate_xy => (30 * $pi / 180),
413 rotate_yz => -30 * ($pi / 180),
414 rotate_xz => 45 * ($pi / 180),
415 );
416  
417 =head2 get_scaling_matrix
418  
419 Construct scaling matrix on a 2D plane:
420  
421 my $scaleMatrix2D = Vector::Object3D::Matrix->get_scaling_matrix(
422 scale_x => 2,
423 scale_y => 2,
424 );
425  
426 Construct scaling matrix in a 3D space:
427  
428 my $scaleMatrix3D = Vector::Object3D::Matrix->get_scaling_matrix(
429 scale_x => 2,
430 scale_y => 2,
431 scale_z => 3,
432 );
433  
434 =head2 get_translation_matrix
435  
436 Construct translation matrix on a 2D plane:
437  
438 my $translateMatrix2D = Vector::Object3D::Matrix->get_translation_matrix(
439 shift_x => -2,
440 shift_y => 1,
441 );
442  
443 Construct translation matrix in a 3D space:
444  
445 my $translateMatrix3D = Vector::Object3D::Matrix->get_translation_matrix(
446 shift_x => -2,
447 shift_y => 1,
448 shift_z => 3,
449 );
450  
451 =head1 OPERATORS
452  
453 =head2 multiply (*)
454  
455 Produce a matrix that is a result of scalar multiplication:
456  
457 my $matrix2 = 2 * $matrix1;
458  
459 Produce a matrix that is a result of matrix multiplication:
460  
461 my $matrix3 = $matrix1 * $matrix2;
462  
463 =cut
464  
465 134 ▶sub _multiplication {
Subroutine _multiplication: called CC=3 SCAR=11.3
466 134 my ($self, $arg) = @_;
467  
468 134 ▶ if (ref $arg eq __PACKAGE__) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq __PACKAGE__) { } 132 2 134
elsif ($arg =~ /^-?(\d+|\.\d+|\d+\.\d+)$/) { } 2 0 2
469 132 return $self->_multiplication_by_matrix($arg);
470 }
471 elsif ($arg =~ m/^-?(\d+|\.\d+|\d+\.\d+)$/) {
472 2 return $self->_multiplication_by_number($arg);
473 }
474 else {
475 0 croak "Incorrect call of overloaded operator '*' method";
476 }
477 }
478  
479 132 ▶sub _multiplication_by_matrix {
Subroutine _multiplication_by_matrix: called CC=5 SCAR=16.1
480 132 my ($self, $arg) = @_;
481  
482 132 my $num_cols1 = $self->num_cols;
483 132 my $num_rows2 = $arg->num_rows;
484  
485 132 ▶ unless ($num_cols1 == $num_rows2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num_cols1 == $num_rows2) 1 131 132
486 1 my ($matrix1, $matrix2);
487  
488 1 my $old_fh = open my $fh, '>', \$matrix1;
489 1 $self->print(fh => $fh);
490  
491 1 open $fh, '>', \$matrix2;
492 1 $arg->print(fh => $fh);
493  
494 1 close $fh;
495 1 select $old_fh;
496  
497 1 croak "Number of columns of the first matrix (${num_cols1}) does not match number of rows of the second matrix (${num_rows2}) - incompatibility makes matrix multiplication impossible:\n\nMATRIX #1$matrix1\n\nMATRIX #2$matrix2";
498 }
499  
500 131 my $rows1 = $self->get_rows;
501 131 my $rows2 = $arg->get_rows;
502 131 my $rows = [];
503  
504 131 my $num_rows = $self->num_rows;
505 131 my $num_cols = $arg->num_cols;
506  
507 131 ▶ for (my $i = 0; $i < $num_rows; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_rows) 133 131 264
508 133 ▶ for (my $j = 0; $j < $num_cols; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $num_cols) 520 133 653
509 520 my $val = 0;
510 520 ▶ for (my $k = 0; $k < $num_cols1; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $num_cols1) 2065 520 2585
511 2065 $val += $rows1->[$i]->[$k] * $rows2->[$k]->[$j];
512 }
513 520 $rows->[$i]->[$j] = $val;
514 }
515 }
516  
517 131 my $result = (ref $self)->new(rows => $rows);
518 131 return $result;
519 }
520  
521 2 ▶sub _multiplication_by_number {
Subroutine _multiplication_by_number: called CC=2 SCAR=6.9
522 2 my ($self, $arg) = @_;
523  
524 2 my $rows = $self->get_rows;
525  
526 2 for my $row (@{$rows}) {
527 5 $row = [ map { $_ * $arg } @{$row} ];
528 }
529  
530 2 my $result = (ref $self)->new(rows => $rows);
531 2 return $result;
532 }
533  
534 =head2 add (+)
535  
536 Add two matrices:
537  
538 my $matrix3 = $matrix1 + $matrix2;
539  
540 A matrix may be added to another one if they both share exactly same dimensions.
541  
542 =cut
543  
544 5 ▶sub _addition {
Subroutine _addition: called CC=2 SCAR=6.9
545 5 my ($self, $arg) = @_;
546  
547 5 ▶ if (ref $arg eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq __PACKAGE__) { } 4 1 5
548 4 return $self->_addition_to_matrix($arg);
549 }
550 else {
551 1 croak "Incorrect use of overloaded operator '+' method";
552 }
553 }
554  
555 4 ▶sub _addition_to_matrix {
Subroutine _addition_to_matrix: called CC=5 SCAR=16.1
556 4 my ($self, $arg) = @_;
557  
558 4 my $num_rows1 = $self->num_rows;
559 4 my $num_rows2 = $arg->num_rows;
560 4 my $num_cols1 = $self->num_cols;
561 4 my $num_cols2 = $arg->num_cols;
562  
563 4 ▶ croak "Size of the first matrix (${num_rows1}x${num_cols1}) does not match size of the second matrix (${num_rows2}x${num_cols2}) - incompatibility makes matrix addition impossible" unless $num_rows1 == $num_rows2 and $num_cols1 == $num_cols2;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_rows1 == $num_rows2 and $num_cols1 == $num_cols2 2 2 4
Condition100%3/3
$num_rows1 == $num_rows2 and $num_cols1 == $num_cols2
!ll&&!rl&&r
112
MC/DC100%2/2
$num_rows1 == $num_rows2 and $num_cols1 == $num_cols2
$num_rows1 == $num_rows2 $num_cols1 == $num_cols2
Truth table100%3/3
A and B
A= $num_rows1 == $num_rows2B= $num_cols1 == $num_cols2
ABresult
0X0
100
111
564  
565 2 my $rows1 = $self->get_rows;
566 2 my $rows2 = $arg->get_rows;
567 2 my $rows = [];
568  
569 2 ▶ for (my $i = 0; $i < $num_rows1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_rows1) 4 2 6
570 4 ▶ for (my $j = 0; $j < $num_cols1; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $num_cols1) 8 4 12
571 8 $rows->[$i]->[$j] = $rows1->[$i]->[$j] + $rows2->[$i]->[$j];
572 }
573 }
574  
575 2 my $result = (ref $self)->new(rows => $rows);
576 2 return $result;
577 }
578  
579 =head2 subtract (-)
580  
581 Subtract one matrix from another:
582  
583 my $matrix3 = $matrix1 - $matrix2;
584  
585 A matrix may be subtracted from another one if they both share exactly same dimensions.
586  
587 =cut
588  
589 2 ▶sub _subtraction {
Subroutine _subtraction: called CC=2 SCAR=6.9
590 2 my ($self, $arg) = @_;
591  
592 2 ▶ if (ref $arg eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq __PACKAGE__) { } 1 1 2
593 1 return $self->_subtraction_from_matrix($arg);
594 }
595 else {
596 1 croak "Incorrect use of overloaded operator '-' method";
597 }
598 }
599  
600 1 ▶sub _subtraction_from_matrix {
Subroutine _subtraction_from_matrix: called CC=3 SCAR=11.4
601 1 my ($self, $arg) = @_;
602  
603 1 my $num_rows1 = $self->num_rows;
604 1 my $num_rows2 = $arg->num_rows;
605 1 my $num_cols1 = $self->num_cols;
606 1 my $num_cols2 = $arg->num_cols;
607  
608 1 ▶ croak "Size of the first matrix (${num_rows1}x${num_cols1}) does not match size of the second matrix (${num_rows2}x${num_cols2}) - incompatibility makes matrix subtraction impossible" unless $num_rows1 == $num_rows2 and $num_cols1 == $num_cols2;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_rows1 == $num_rows2 and $num_cols1 == $num_cols2 0 1 1
Condition33%1/3
$num_rows1 == $num_rows2 and $num_cols1 == $num_cols2
!ll&&!rl&&r
001
MC/DC0%0/2
$num_rows1 == $num_rows2 and $num_cols1 == $num_cols2
$num_rows1 == $num_rows2 $num_cols1 == $num_cols2
Truth table33%1/3
A and B
A= $num_rows1 == $num_rows2B= $num_cols1 == $num_cols2
ABresult
0X0
100
111
609  
610 1 return $self + -1 * $arg;
611 }
612  
613 =head2 compare (==)
614  
615 Compare two matrix objects:
616  
617 my $are_the_same = $matrix1 == $matrix2;
618  
619 Overloaded comparison operator evaluates to true whenever two matrix objects are identical (same number of rows, columns and identical values in the corresponding cells).
620  
621 =cut
622  
623 20 ▶sub _comparison {
Subroutine _comparison: called CC=8 SCAR=20.8
624 20 my ($self, $arg) = @_;
625  
626 20 ▶ return 0 unless $self->num_cols == $arg->num_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->num_cols == $arg->num_cols 0 20 20
627 20 ▶ return 0 unless $self->num_rows == $arg->num_rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->num_rows == $arg->num_rows 0 20 20
628  
629 # Get compare precision for both matrices:
630 20 my $precision1 = $self->get(parameter => 'precision');
631 20 ▶ $precision1 = defined $precision1 ? '.' . $precision1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $precision1 ? : 5 15 20
632 20 my $precision2 = $arg->get(parameter => 'precision');
633 20 ▶ $precision2 = defined $precision2 ? '.' . $precision2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $precision2 ? : 3 17 20
634  
635 20 my $rows1 = $self->get_rows;
636 20 my $rows2 = $arg->get_rows;
637  
638 20 ▶ for (my $i = 0; $i < $self->num_rows; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->num_rows) 58 17 75
639 58 ▶ for (my $j = 0; $j < $self->num_cols; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $self->num_cols) 150 55 205
640  
641 150 my $val1 = sprintf qq{%${precision1}f}, $rows1->[$i][$j];
642 150 $val1 =~ s/^(.*\..*?)0*$/$1/;
643 150 $val1 =~ s/\.$//;
644  
645 150 my $val2 = sprintf qq{%${precision2}f}, $rows2->[$i][$j];
646 150 $val2 =~ s/^(.*\..*?)0*$/$1/;
647 150 $val2 =~ s/\.$//;
648  
649 150 ▶ return 0 if $val1 ne $val2;
Branch: 1 branch
BranchTrueFalseTotal
if $val1 ne $val2 3 147 150
650 }
651 }
652  
653 17 return 1;
654 }
655  
656 =head2 negative compare (!=)
657  
658 Compare two matrix objects:
659  
660 my $are_not_the_same = $matrix1 != $matrix2;
661  
662 Overloaded negative comparison operator evaluates to true whenever two matrix objects differ (unequal number of rows, columns or diverse values in the corresponding cells).
663  
664 =cut
665  
666 3 ▶sub _negative_comparison {
Subroutine _negative_comparison: called CC=1 SCAR=0.0
667 3 my ($self, $arg) = @_;
668  
669 3 return not $self->_comparison($arg);
670 }
671  
672 =head1 BUGS
673  
674 There are no known bugs at the moment. Please report any bugs or feature requests.
675  
676 =head1 EXPORT
677  
678 C<Vector::Object3D::Matrix> exports nothing neither by default nor explicitly.
679  
680 =head1 SEE ALSO
681  
682 L<Inline::Octave>, L<Math::Cephes::Matrix>, L<Math::Matrix>, L<PDL::MatrixOps>, L<Vector::Object3D>, L<Vector::Object3D::Matrix::Transform>, L<Vector::Object3D::Parameters>.
683  
684 =head1 AUTHOR
685  
686 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
687  
688 =head1 VERSION
689  
690 Version 0.01 (2012-12-24)
691  
692 =head1 COPYRIGHT AND LICENSE
693  
694 Copyright (C) 2012 by Pawel Krol.
695  
696 This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
697  
698 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
699  
700 =cut
701  
702 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
703 8 __PACKAGE__->meta->make_immutable;
704  
705 8 1;
« Object3D/Matrix/Transform.pm ↑ Summary Object3D/Point.pm »