Lorentz/Transformation.pm

filter: Statementstmt 97.2% 179 / 184 Branchbran 87.5% 35 / 40 Conditioncond 46.0% 23 / 50 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 100% 27 / 27 Podpod 100% 17 / 17 totaltotal 81.4% 286 / 351 CC 38 SCAR 19.1
CC
38
Cov
86%
new
17.5
_overload_multiply
7.8
_rotate_to_z
4.0
CRAP 6.7
↑ Summary Lorentz/Vector.pm »
1 package Physics::Lorentz::Transformation;
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 8 ▶use constant EPS => 1e-12;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use overload
15 8 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
16 '*' => \&_overload_multiply,
17 8 ;
18  
19 =head1 NAME
20  
21 Physics::Lorentz::Transformation - Representation of Poincare Transformations
22  
23 =head1 SYNOPSIS
24  
25 use Physics::Lorentz;
26 my $rotation = Physics::Lorentz::Transformation->rotation_euler(
27 $alpha, $beta, $gamma
28 );
29 my $vector = Physics::Lorentz::Vector->new([$t, $x, $y, $z]);
30 my $rotated = $rotation->apply($vector);
31 # or: $rotated = $rotation * $vector;
32
33 ...
34  
35 =head1 DESCRIPTION
36  
37 This class represents a Poincare transformation. That is a proper or
38 improper Lorentz transformation plus a shift by some 4-vector.
39 (C<x' = lamda*x + a>)
40  
41 Yes, the class name might be misleading, but honestly, when most non-physicists
42 talk about Lorentz transformations, they mean Poincare transformations anyway.
43 (Pun intended.)
44  
45 To sum this up, the set of Poincare transformations contains, among others
46  
47 =over 2
48  
49 =item * Boosts
50  
51 =item * Rotations
52  
53 =item * Space Inversions / Parity
54  
55 =item * Time Inversion
56  
57 =item * Shifts by a constant vector
58  
59 =item * Combinations thereof
60  
61 =back
62  
63 =head2 EXPORT
64  
65 None.
66  
67 =head2 OVERLOADED INTERFACE
68  
69 Stringification is overloaded with the C<stringify> method.
70  
71 Multiplication (*) is overloaded with the C<merge> method for other
72 transformations:
73 C<$t3 = $t1 * $t2> corresponds to the following application on a vector:
74 C<t1 * ( t2 * vec )>. (I.e. t2 first, then t1)
75 Of course, B<Poincare transformations do not commute>!
76  
77 The assignment form of multiplication is supported for merging transformations
78 but its use is discouraged unless you're into obfuscation.
79  
80 Multiplication is also overloaded for application to vectors, but only if the
81 vector is on the right of the transformation: C<$t * $v> is okay, but C<$v * $t>
82 is not.
83  
84 =head1 CONSTRUCTORS
85  
86 =cut
87  
88 =head2 new
89  
90 Creates a new C<Physics::Lorentz::Transformation> object.
91 Defaults to the unity transformation.
92  
93 If one argument is present, this argument may either be a 4x4 PDL which will
94 be used internally as the PDL representation of the or an equivalent Perl
95 datastructure (4x4 matrix).
96  
97 If two arguments are present, the second argument will be used as or
98 converted to a L<Physics::Lorentz::Vector>. It defaults to C<[0, 0, 0, 0]>
99  
100 This result will be of the form C<x' = lamda * x + a> where C<lamda> is the
101 matrix and C<a> is the vector. (And C<x> is the 4-vector that's acted upon.)
102  
103 =cut
104  
105 3943 ▶sub new {
Subroutine new: called CC=17 SCAR=28.6
Pod documented
106 3943 my $proto = shift;
107 3943 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
60633370
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
108 3943 my $self = {};
109 3943 ▶ if (_INSTANCE($proto, 'Physics::Lorentz::Transformation')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($proto, "Physics::Lorentz::Transformation")) 606 3337 3943
110 606 $self = $proto->clone();
111 }
112  
113 3943 my $matrix = shift;
114 3943 my $vector = shift;
115  
116 3943 ▶ if (ref(_INSTANCE($matrix, 'PDL'))) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref &_INSTANCE($matrix, 'PDL')) { } 2732 1211 3943
elsif (&_ARRAY($matrix) and @$matrix == 4) { } 503 708 1211
elsif (not defined $matrix) { } 607 101 708
117 2732 my ($x, $y) = $matrix->dims;
118 2732 ▶ if ($x == 4 and $y == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 4 and $y == 4) { } 2631 101 2732
Condition66%2/3
$x == 4 and $y == 4
!ll&&!rl&&r
01012631
MC/DC50%1/2
$x == 4 and $y == 4
$x == 4 $y == 4
Truth table66%2/3
A and B
A= $x == 4B= $y == 4
ABresult
0X0
100
111
119 2631 $matrix = $matrix->new();
120 }
121 else {
122 101 croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
123 }
124 2631 $self->{matrix} = $matrix;
125 }
126 ▶ elsif (_ARRAY($matrix) and @$matrix == 4) {
Condition66%2/3
&_ARRAY($matrix) and @$matrix == 4
!ll&&!rl&&r
7080503
MC/DC50%1/2
&_ARRAY($matrix) and @$matrix == 4
&_ARRAY($matrix) @$matrix == 4
Truth table66%2/3
A and B
A= &_ARRAY($matrix)B= @$matrix == 4
ABresult
0X0
100
111
127 503 my $pdl = pdl $matrix;
128 503 my ($x, $y) = $pdl->dims;
129 503 ▶ unless ($x == 4 and $y == 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x == 4 and $y == 4) 0 503 503
Condition33%1/3
$x == 4 and $y == 4
!ll&&!rl&&r
00503
MC/DC0%0/2
$x == 4 and $y == 4
$x == 4 $y == 4
Truth table33%1/3
A and B
A= $x == 4B= $y == 4
ABresult
0X0
100
111
130 0 croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
131 }
132 503 $self->{matrix} = $pdl;
133 }
134 elsif (not defined $matrix) {
135 607 ▶ $self->{matrix} = identity(4) if not defined $self->{matrix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"matrix"} 1 606 607
136 }
137 else {
138 101 croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
139 }
140
141 3741 ▶ if (_INSTANCE($vector, 'Physics::Lorentz::Vector')) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_INSTANCE($vector, 'Physics::Lorentz::Vector')) { } 2226 1515 3741
elsif (not defined $vector) { } 607 908 1515
142 2226 $self->{vector} = $vector;
143 }
144 elsif (not defined $vector) {
145 $self->{vector} = Physics::Lorentz::Vector->new()
146 607 ▶ if not defined $self->{vector};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"vector"} 1 606 607
147 }
148 else {
149 908 my $vec;
150 908 eval { $vec = Physics::Lorentz::Vector->new($vector); };
151 908 ▶ if ($@ or not _INSTANCE($vec, 'Physics::Lorentz::Vector')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not &_INSTANCE($vec, "Physics::Lorentz::Vector")) 101 807 908
Condition66%2/3
$@ or not &_INSTANCE($vec, "Physics::Lorentz::Vector")
l!l&&r!l&&!r
1010807
MC/DC50%1/2
$@ or not &_INSTANCE($vec, "Physics::Lorentz::Vector")
$@ not &_INSTANCE($vec, "Physics::Lorentz::Vector")
Truth table66%2/3
A or not(B)
A= $@B= not &_INSTANCE($vec, "Physics::Lorentz::Vector")
ABresult
1X1
011
000
152 101 croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
153 }
154  
155 807 $self->{vector} = $vec;
156 }
157  
158 3640 return bless($self => $class);
159 }
160  
161 =head2 rotation_euler
162  
163 Alternative constructor to construct a specific type of Lorentz transformation:
164 A 3D rotation with the Euler angles alpha, beta, gamma.
165  
166 Three arguments: alpha, beta, gamma.
167  
168 (First rotate about fixed z-axis by alpha, the about fixed y-axis about
169 beta, then about fixed z-axis by gamma.)
170  
171 =cut
172  
173 101 ▶sub rotation_euler {
Subroutine rotation_euler: called CC=2 SCAR=7.0
Pod documented
174 101 my $proto = shift;
175 101 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
176  
177 101 my ($gamma, $beta, $alpha) = @_;
178  
179 101 my $ca = cos $alpha;
180 101 my $cb = cos $beta;
181 101 my $cg = cos $gamma;
182 101 my $sa = sin $alpha;
183 101 my $sb = sin $beta;
184 101 my $sg = sin $gamma;
185  
186 101 my $m = pdl([
187 [ 1, 0, 0, 0 ],
188 [ 0, $cg*$cb*$ca-$sa*$sg, $cg*$cb*$sa+$sg*$ca, -$cg*$sb ],
189 [ 0, -$sg*$cb*$ca-$cg*$sa, -$sg*$cb*$sa+$cg*$ca, $sg*$sb ],
190 [ 0, $sb*$ca, $sb*$sa, $cb ],
191 ]);
192  
193 # 0,0,0,0
194 101 my $vec = Physics::Lorentz::Vector->new();
195  
196 101 return($class->new($m, $vec));
197 }
198  
199 =head2 rotation_x
200  
201 Alternative constructor to construct a specific type of Lorentz transformation:
202 A 3D rotation about the x or 1-axis. First argument is the rotation angle.
203  
204 =cut
205  
206 303 ▶sub rotation_x {
Subroutine rotation_x: called CC=2 SCAR=7.1
Pod documented
207 303 my $proto = shift;
208 303 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
03030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
209  
210 303 my ($phi) = @_;
211  
212 303 my $cp = cos $phi;
213 303 my $sp = sin $phi;
214  
215 303 my $m = pdl([
216 [ 1, 0, 0, 0 ],
217 [ 0, 1, 0, 0 ],
218 [ 0, 0, $cp, $sp ],
219 [ 0, 0, -$sp, $cp ],
220 ]);
221  
222 # 0,0,0,0
223 303 my $vec = Physics::Lorentz::Vector->new();
224  
225 303 return($class->new($m, $vec));
226 }
227  
228 =head2 rotation_y
229  
230 Alternative constructor to construct a specific type of Lorentz transformation:
231 A 3D rotation about the y or 2-axis. First argument is the rotation angle.
232  
233 =cut
234  
235 404 ▶sub rotation_y {
Subroutine rotation_y: called CC=2 SCAR=7.1
Pod documented
236 404 my $proto = shift;
237 404 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
04040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
238  
239 404 my ($phi) = @_;
240  
241 404 my $cp = cos $phi;
242 404 my $sp = sin $phi;
243  
244 404 my $m = pdl([
245 [ 1, 0, 0, 0 ],
246 [ 0, $cp, 0, -$sp ],
247 [ 0, 0, 1, 0 ],
248 [ 0, $sp, 0, $cp ],
249 ]);
250  
251 # 0,0,0,0
252 404 my $vec = Physics::Lorentz::Vector->new();
253  
254 404 return($class->new($m, $vec));
255 }
256  
257 =head2 rotation_z
258  
259 Alternative constructor to construct a specific type of Lorentz transformation:
260 A 3D rotation about the z or 3-axis. First argument is the rotation angle.
261  
262 =cut
263  
264 505 ▶sub rotation_z {
Subroutine rotation_z: called CC=2 SCAR=7.1
Pod documented
265 505 my $proto = shift;
266 505 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
05050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
267  
268 505 my ($phi) = @_;
269  
270 505 my $cp = cos $phi;
271 505 my $sp = sin $phi;
272  
273 505 my $m = pdl([
274 [ 1, 0, 0, 0 ],
275 [ 0, $cp, $sp, 0 ],
276 [ 0, -$sp, $cp, 0 ],
277 [ 0, 0, 0, 1 ],
278 ]);
279  
280 # 0,0,0,0
281 505 my $vec = Physics::Lorentz::Vector->new();
282  
283 505 return($class->new($m, $vec));
284 }
285  
286 =head2 boost
287  
288 Alternative constructor to construct a specific type of Lorentz transformation:
289 A boost of rapidity C<eta> (C<eta = atanh(v/c)>) and direction C<x>.
290  
291 Accepts three arguments: The components of the boost's velocity vector
292 divided by the speed of light: C<(v1/c, v2/c, v3/c)>.
293  
294 =cut
295  
296 103 ▶sub boost {
Subroutine boost: called CC=3 SCAR=11.4
Pod documented
297 103 my $proto = shift;
298 103 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
299  
300 103 my ($v1, $v2, $v3) = @_;
301  
302 103 ▶ if (not grep { abs($_) > EPS } ($v1, $v2, $v3)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {abs $_ > "1e-12";} $v1, $v2, $v3) 0 103 103
303 0 return $class->new(); # identity
304 }
305  
306 103 my ($to_z, $inv_to_z) = _rotate_to_z($v1, $v2, $v3);
307 103 my $boost_z = $class->boost_z(
308 sqrt($v1**2 + $v2**2 + $v3**2)
309 );
310 # my $inv_to_z = $to_z->inv;
311 103 $boost_z->{matrix} = $inv_to_z x $boost_z->{matrix};
312 103 $boost_z->{matrix} = $boost_z->{matrix} x $to_z;
313
314 103 return $boost_z;
315 }
316  
317 204 ▶sub _rotate_to_z {
Subroutine _rotate_to_z: called CC=4 SCAR=13.9
318 204 my ($x, $y, $z) = @_;
319 204 my $v = pdl([[1],[$x],[$y],[$z]]);
320  
321 # done if already on z
322 204 ▶ if (abs($x) < EPS and abs($y) < EPS) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $x < "1e-12" and abs $y < "1e-12") 101 103 204
Condition100%3/3
abs $x < "1e-12" and abs $y < "1e-12"
!ll&&!rl&&r
1021101
MC/DC100%2/2
abs $x < "1e-12" and abs $y < "1e-12"
abs $x < "1e-12" abs $y < "1e-12"
Truth table100%3/3
A and B
A= abs $x < "1e-12"B= abs $y < "1e-12"
ABresult
0X0
100
111
323 101 my $identity = identity(4,4);
324 101 ▶ my $sgn = ( ($z < 0) ? -1 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$z < 0 ? : 49 52 101
325 101 $identity->slice('1:3,1:3') *= $sgn;;
326 101 return( $identity, $identity->copy );
327 }
328  
329 103 my $xy = $x**2 + $y**2;
330 103 my $zsq = $z**2;
331 103 my $sqrtxy = sqrt($xy);
332 103 my $i_sqrtxy = 1 / $sqrtxy;
333 103 my $i_sqrtxyz = 1 / sqrt($xy + $zsq);
334
335  
336 103 my $to_xz_plane = pdl( [
337 [ 1, 0, 0, 0 ],
338 [ 0, $x * $i_sqrtxy, $y * $i_sqrtxy, 0 ],
339 [ 0, -$y * $i_sqrtxy, $x * $i_sqrtxy, 0 ],
340 [ 0, 0, 0, 1 ],
341 ] );
342  
343 103 my $xz_inv = $to_xz_plane->copy;
344 103 my $slice = $xz_inv->slice('1:3,1:3');
345 103 $slice .= $slice->transpose;
346  
347 103 my $xz_to_z_axis = pdl( [
348 [ 1, 0, 0, 0 ],
349 [ 0, $z*$i_sqrtxyz, 0, -$sqrtxy*$i_sqrtxyz ],
350 [ 0, 0, 1, 0 ],
351 [ 0, $sqrtxy*$i_sqrtxyz, 0, $z*$i_sqrtxyz ],
352 ] );
353  
354 103 my $xzz_inv = $xz_to_z_axis->copy;
355 103 $slice = $xzz_inv->slice('1:3,1:3');
356 103 $slice .= $slice->transpose;
357
358 103 my $res = $xz_to_z_axis x $to_xz_plane;
359 103 my $res_inv = $xz_inv x $xzz_inv;
360 103 return ($res, $res_inv);
361 }
362  
363 =head2 boost_x
364  
365 Alternative constructor to construct a specific type of Lorentz transformation:
366 A boost of rapidity C<eta> (C<eta = atanh(v/c)>) parallel to the x axis.
367  
368 Accepts one argument: The boost's velocity divided by the speed of light
369 C<v/c>.
370  
371 =cut
372  
373 102 ▶sub boost_x {
Subroutine boost_x: called CC=2 SCAR=7.1
Pod documented
374 102 my $proto = shift;
375 102 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
376  
377 102 my ($b) = @_;
378  
379 102 my $g = 1/sqrt(1-$b**2);
380  
381 102 my $m = pdl([
382 [$g, -$b*$g, 0, 0 ],
383 [-$b*$g, $g, 0, 0 ],
384 [0, 0, 1, 0 ],
385 [0, 0, 0, 1 ],
386 ]);
387  
388 # 0,0,0,0
389 102 my $vec = Physics::Lorentz::Vector->new();
390  
391 102 return($class->new($m, $vec));
392 }
393  
394 =head2 boost_y
395  
396 Alternative constructor to construct a specific type of Lorentz transformation:
397 A boost of rapidity C<eta> (C<eta = atanh(v/c)>) parallel to the y axis.
398  
399 Accepts one argument: The boost's velocity divided by the speed of light
400 C<v/c>.
401  
402 =cut
403  
404 102 ▶sub boost_y {
Subroutine boost_y: called CC=2 SCAR=7.1
Pod documented
405 102 my $proto = shift;
406 102 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
407  
408 102 my ($b) = @_;
409  
410 102 my $g = 1/sqrt(1-$b**2);
411  
412 102 my $m = pdl([
413 [$g, 0, -$b*$g, 0 ],
414 [0, 1, 0, 0 ],
415 [-$b*$g, 0, $g, 0 ],
416 [0, 0, 0, 1 ],
417 ]);
418  
419 # 0,0,0,0
420 102 my $vec = Physics::Lorentz::Vector->new();
421  
422 102 return($class->new($m, $vec));
423 }
424  
425 =head2 boost_z
426  
427 Alternative constructor to construct a specific type of Lorentz transformation:
428 A boost of rapidity C<eta> (C<eta = atanh(v/c)>) parallel to the z axis.
429  
430 Accepts one argument: The boost's velocity divided by the speed of light
431 C<v/c>.
432  
433 =cut
434  
435 305 ▶sub boost_z {
Subroutine boost_z: called CC=2 SCAR=7.1
Pod documented
436 305 my $proto = shift;
437 305 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
03050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
438  
439 305 my ($b) = @_;
440  
441 305 my $g = 1/sqrt(1-$b**2);
442  
443 305 my $m = pdl([
444 [$g, 0, 0, -$b*$g ],
445 [0, 1, 0, 0 ],
446 [0, 0, 1, 0 ],
447 [-$b*$g, 0, 0, $g ],
448 ]);
449  
450 # 0,0,0,0
451 305 my $vec = Physics::Lorentz::Vector->new();
452  
453 305 return($class->new($m, $vec));
454 }
455  
456  
457 =head2 parity
458  
459 Returns the parity operation, that is, space inversion.
460  
461 =cut
462  
463 1 ▶sub parity {
Subroutine parity: called CC=2 SCAR=7.2
Pod documented
464 1 my $proto = shift;
465 1 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
466  
467 1 my $m = pdl([
468 [1, 0, 0, 0 ],
469 [0, -1, 0, 0 ],
470 [0, 0, -1, 0 ],
471 [0, 0, 0, -1 ],
472 ]);
473  
474 # 0,0,0,0
475 1 my $vec = Physics::Lorentz::Vector->new();
476  
477 1 return($class->new($m, $vec));
478 }
479  
480 =head2 time_reversal
481  
482 Returns the time reversal operation.
483  
484 =cut
485  
486 1 ▶sub time_reversal {
Subroutine time_reversal: called CC=2 SCAR=7.2
Pod documented
487 1 my $proto = shift;
488 1 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
489  
490 1 my $m = pdl([
491 [-1, 0, 0, 0 ],
492 [0, 1, 0, 0 ],
493 [0, 0, 1, 0 ],
494 [0, 0, 0, 1 ],
495 ]);
496  
497 # 0,0,0,0
498 1 my $vec = Physics::Lorentz::Vector->new();
499  
500 1 return($class->new($m, $vec));
501 }
502  
503 =head1 OTHER METHODS
504  
505 =head2 merge
506  
507 Merge two transformations into one. Mathematically speaking, if
508 C<a> is the current transformation, C<b> is another, and C<v> is
509 a Lorentz vector,
510 C<$a-E<gt>merge($b)> is the transformation C<v' = a x (b x v)>.
511 In words, if applied to a vector, the merged transformation is the
512 subsequent application of tranformation
513 C<b> and then C<a>.
514  
515 =cut
516  
517 302 ▶sub merge {
Subroutine merge: called CC=1 SCAR=0.0
Pod documented
518 302 my $self = shift;
519 302 my $trafo = shift;
520  
521 # FIXME We're breaking encapsulation of ::Vector here.
522 return ref($self)->new(
523 $self->{matrix} x $trafo->{matrix},
524 302 $self->{vector}{pdl} + ($self->{matrix} x $trafo->{vector}{pdl}),
525 );
526 }
527  
528 =head2 apply
529  
530 Apply transformation to a Lorentz vector or something that can be
531 converted into a C<Physics::Lorentz::Vector>. Returns a vector.
532  
533 =cut
534  
535 2018 ▶sub apply {
Subroutine apply: called CC=2 SCAR=7.0
Pod documented
536 2018 my $self = shift;
537 2018 my $vec = shift;
538 2018 ▶ $vec = Physics::Lorentz::Vector->new($vec)
Branch: 1 branch
BranchTrueFalseTotal
unless &_INSTANCE($vec, "Physics::Lorentz::Vector") 0 2018 2018
539 if not _INSTANCE($vec, 'Physics::Lorentz::Vector');
540 return(
541 Physics::Lorentz::Vector->new(
542 ($self->{matrix} x $vec->{pdl}) + $self->{vector}{pdl}
543 )
544 2018 );
545 }
546  
547 =head2 clone
548  
549 Returns a copy of the object.
550  
551 =cut
552  
553 1212 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
554 1212 my $self = shift;
555 1212 my $new = {};
556 1212 $new->{matrix} = $self->{matrix}->new();
557 1212 $new->{vector} = $self->{vector}->new();
558 1212 return bless($new, ref($self));
559 }
560  
561 =head2 stringify
562  
563 Returns a string representation of the object. Currently, this is
564 the string representation of the internal PDL vector/matrix.
565  
566 =cut
567  
568 4547 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
569 4547 my $self = shift;
570 4547 return "\nMatrix: ".$self->{matrix}."Vector: ".$self->{vector};
571 }
572  
573  
574  
575 =head1 ACCESSORS
576  
577 =head2 get_matrix
578  
579 Returns the 4x4 PDL that represents the I<Lorentz> transformation.
580  
581 This is the actual object that is used inside the Transformation object,
582 so be aware that modifying it results in action at a distance!
583  
584 =cut
585  
586 2 ▶sub get_matrix { $_[0]->{matrix} }
Subroutine get_matrix: called CC=1 SCAR=0.0
Pod documented
587  
588 =head2 get_vector
589  
590 Returns the Physics::Lorentz::Vector object that is used inside the
591 Transformation object. As with the matrix, this is the actual object. Same
592 caveats.
593  
594 Note that this isn't a PDL but a Physics::Lorentz::Vector object.
595  
596 =cut
597  
598 2 ▶sub get_vector { $_[0]->{vector} }
Subroutine get_vector: called CC=1 SCAR=0.0
Pod documented
599  
600  
601 # Overloaded Interface
602  
603 400 ▶sub _overload_multiply {
Subroutine _overload_multiply: called CC=7 SCAR=20.5
604 400 my ($trafo, $obj, $reverse) = @_;
605 400 ▶ ($trafo, $obj) = ($obj, $trafo) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 0 400 400
606  
607 400 ▶ if (_INSTANCE($obj, __PACKAGE__)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_INSTANCE($obj, __PACKAGE__)) { } 100 300 400
elsif (&_INSTANCE($obj, 'Physics::Lorentz::Vector')) { } 200 100 300
608 100 my $res = $trafo->merge($obj);
609 100 ▶ if (not defined $reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $reverse) { } 0 100 100
610 # *=
611 0 $trafo->{matrix} = $res->{matrix};
612 0 $trafo->{vector} = $res->{vector};
613 0 return $trafo;
614 }
615 else {
616 100 return $res;
617 }
618 }
619 elsif (_INSTANCE($obj, 'Physics::Lorentz::Vector')) {
620 200 ▶ croak('Assignment version of * does not make sense when a transformation is applied to a vector')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reverse 100 100 200
621 if not defined $reverse;
622 100 return $trafo->apply($obj);
623 }
624 else {
625 100 ▶ croak('Cannot apply transformation to object of type '.(ref($obj)||''));
Condition50%1/2
ref $obj || ''
l!l
1000
MC/DC0%0/1
ref $obj || ''
ref $obj
Truth table50%1/2
A
A= ref $obj
Aresult
11
00
626 }
627 }
628  
629 8 1;
630 __END__
↑ Summary Lorentz/Vector.pm »