Lorentz/Vector.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 16 / 16 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 91.7% 100 / 109 CC 13 SCAR 18.6
CC
13
Cov
95%
new
12.1
_overload_add
2.0
BEGIN
1.0
CRAP 6.4
« Lorentz/Transformation.pm ↑ Summary Lorentz.pm »
1 package Physics::Lorentz::Vector;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Params::Util qw/_INSTANCE _ARRAY/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = '0.01';
11  
12 use overload
13 8 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
14 '+' => \&_overload_add,
15 8 ;
16  
17 =head1 NAME
18  
19 Physics::Lorentz::Vector - Representation of 4-vectors
20  
21 =head1 SYNOPSIS
22  
23 use Physics::Lorentz;
24 my $rotation = Physics::Lorentz::Transformation->rotation_euler(
25 $alpha, $beta, $gamma
26 );
27 my $vector = Physics::Lorentz::Vector->new([$t, $x, $y, $z]);
28 my $rotated = $rotation->apply($vector);
29 # or: $rotated = $rotation * $vector;
30
31 ...
32  
33 =head1 DESCRIPTION
34  
35 This class is a representation of 4-vectors (such as 4-space
36 C<[time, x, y, z]> or 4-momentum C<E, kx, ky, kz>).
37  
38 =head2 EXPORT
39  
40 None.
41  
42 =head2 OVERLOADED INTERFACE
43  
44 Addition (+) does the expected thing as does the assignment form (+=)
45 of it.
46  
47 Stringification is overloaded with the C<stringify> method.
48  
49 See also: L<Physics::Lorentz::Transformation>.
50  
51 =head1 METHODS
52  
53 =cut
54  
55 =head2 new
56  
57 Creates a new Physics::Lorentz::Vector object. Defaults to C<[0,0,0,0]>
58 or cloning if no arguments are specified.
59  
60 If one argument is present, this argument may either be a PDL which will
61 be used internally as the PDL representation of the vector or an
62 array reference to an array of four elements.
63  
64 =cut
65  
66 8681 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
67 8681 my $proto = shift;
68 8681 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
201566660
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
69 8681 my $self = {};
70 8681 ▶ if (_INSTANCE($proto, 'Physics::Lorentz::Vector')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($proto, "Physics::Lorentz::Vector")) 2015 6666 8681
71 2015 $self = $proto->clone();
72 }
73  
74 8681 my $in = shift;
75 8681 ▶ if (ref(_INSTANCE($in, 'PDL'))) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref &_INSTANCE($in, 'PDL')) { } 3325 5356 8681
elsif (&_ARRAY($in) and @$in == 4) { } 1813 3543 5356
elsif (not defined $in) { } 3341 202 3543
76 3325 my ($x, $y) = $in->dims;
77 3325 ▶ if ($x == 4 and $y == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x == 4 and $y == 1) { } 101 3224 3325
elsif ($x == 1 and $y == 4) { } 3123 101 3224
Condition66%2/3
$x == 4 and $y == 1
!ll&&!rl&&r
32240101
MC/DC50%1/2
$x == 4 and $y == 1
$x == 4 $y == 1
Truth table66%2/3
A and B
A= $x == 4B= $y == 1
ABresult
0X0
100
111
78 101 $in = $in->transpose;
79 }
80 ▶ elsif ($x == 1 and $y == 4) {
Condition66%2/3
$x == 1 and $y == 4
!ll&&!rl&&r
10103123
MC/DC50%1/2
$x == 1 and $y == 4
$x == 1 $y == 4
Truth table66%2/3
A and B
A= $x == 1B= $y == 4
ABresult
0X0
100
111
81 3123 $in = $in->new();
82 }
83 else {
84 101 croak("${class}->new() needs a 4-vector as PDL or list");
85 }
86 3224 $self->{pdl} = $in;
87 }
88 ▶ elsif (_ARRAY($in) and @$in == 4) {
Condition66%2/3
&_ARRAY($in) and @$in == 4
!ll&&!rl&&r
354301813
MC/DC50%1/2
&_ARRAY($in) and @$in == 4
&_ARRAY($in) @$in == 4
Truth table66%2/3
A and B
A= &_ARRAY($in)B= @$in == 4
ABresult
0X0
100
111
89 1813 $self->{pdl} = pdl([$in])->transpose;
90 }
91 elsif (not defined $in) {
92 3341 ▶ $self->{pdl} = zeroes(1,4) if not defined $self->{pdl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pdl"} 1826 1515 3341
93 }
94 else {
95 202 croak("${class}->new() needs a 4-vector as PDL or list");
96 }
97
98 8378 return bless($self => $class);
99 }
100  
101 =head2 clone
102  
103 Returns a copy of the object.
104  
105 =cut
106  
107 2318 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
108 2318 my $self = shift;
109 2318 my $new = {};
110 2318 $new->{pdl} = $self->{pdl}->new();
111 2318 return bless($new, ref($self));
112 }
113  
114 =head2 stringify
115  
116 Returns a string representation of the object. Currently, this is
117 the string representation of the internal PDL vector/matrix.
118  
119 =cut
120  
121 14837 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
122 14837 my $self = shift;
123 14837 return "".$self->{pdl};
124 }
125  
126  
127 =head2 add
128  
129 Adds two vectors. Syntax:
130  
131 $v3 = $v1->add($v2);
132  
133 (This leaves C<$v1> and C<$v2> unchanged!)
134  
135 =cut
136  
137 500 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
138 500 my $self = shift;
139 500 my $v2 = shift;
140 500 return $self->new($self->{pdl}+$v2->{pdl});
141 }
142  
143  
144 =head2 get_pdl
145  
146 Returns the PDL representation of the object.
147 This is the actual PDL object used inside. Beware of action
148 at a distance.
149  
150 =cut
151  
152 1002 ▶sub get_pdl { $_[0]->{pdl} }
Subroutine get_pdl: called CC=1 SCAR=0.0
Pod documented
153  
154  
155  
156 # Overloaded interface:
157  
158 300 ▶sub _overload_add {
Subroutine _overload_add: called CC=2 SCAR=6.9
159 300 my ($self, $o2, $reverse) = @_;
160 300 my $res = $self->add($o2);
161 300 ▶ if (not defined $reverse) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reverse) 100 200 300
162 100 $self->{pdl} = $res->{pdl};
163 100 return $self;
164 }
165 200 return $res;
166 }
167  
168 8 1;
169 __END__
« Lorentz/Transformation.pm ↑ Summary Lorentz.pm »