Physics-Lorentz

 view release on metacpan or  search on metacpan

lib/Physics/Lorentz/Transformation.pm  view on Meta::CPAN

converted to a L<Physics::Lorentz::Vector>. It defaults to C<[0, 0, 0, 0]>

This result will be of the form C<x' = lamda * x + a> where C<lamda> is the
matrix and C<a> is the vector. (And C<x> is the 4-vector that's acted upon.)

=cut

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = {};
    if (_INSTANCE($proto, 'Physics::Lorentz::Transformation')) {
        $self = $proto->clone();
    }

    my $matrix = shift;
    my $vector = shift;

    if (ref(_INSTANCE($matrix, 'PDL'))) {
        my ($x, $y) = $matrix->dims;
        if ($x == 4 and $y == 4) {
            $matrix = $matrix->new();
        }
        else {
            croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
        }
        $self->{matrix} = $matrix;
    }
    elsif (_ARRAY($matrix) and @$matrix == 4) {
        my $pdl = pdl $matrix;
        my ($x, $y) = $pdl->dims;
        unless ($x == 4 and $y == 4) {
            croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
        }
        $self->{matrix} = $pdl;
    }
    elsif (not defined $matrix) {
        $self->{matrix} = identity(4) if not defined $self->{matrix};
    }
    else {
        croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
    }
   
    if (_INSTANCE($vector, 'Physics::Lorentz::Vector')) {
        $self->{vector} = $vector;
    }
    elsif (not defined $vector) {
        $self->{vector} = Physics::Lorentz::Vector->new()
          if not defined $self->{vector};
    }
    else {
        my $vec;
        eval { $vec = Physics::Lorentz::Vector->new($vector); };
        if ($@ or not _INSTANCE($vec, 'Physics::Lorentz::Vector')) {
            croak("${class}->new() needs a 4x4 matrix as PDL or Perl data structure");
        }

        $self->{vector} = $vec;
    }

    return bless($self => $class);
}

=head2 rotation_euler

Alternative constructor to construct a specific type of Lorentz transformation:
A 3D rotation with the Euler angles alpha, beta, gamma.

Three arguments: alpha, beta, gamma.

(First rotate about fixed z-axis by alpha, the about fixed y-axis about
beta, then about fixed z-axis by gamma.)

=cut

sub rotation_euler {
    my $proto = shift;
    my $class = ref($proto)||$proto;

    my ($gamma, $beta, $alpha) = @_;

    my $ca = cos $alpha;
    my $cb = cos $beta;
    my $cg = cos $gamma;
    my $sa = sin $alpha;
    my $sb = sin $beta;
    my $sg = sin $gamma;

    my $m = pdl([
        [ 1, 0,                    0,        0                    ],
        [ 0, $cg*$cb*$ca-$sa*$sg, $cg*$cb*$sa+$sg*$ca, -$cg*$sb ],
        [ 0, -$sg*$cb*$ca-$cg*$sa, -$sg*$cb*$sa+$cg*$ca, $sg*$sb ],
        [ 0, $sb*$ca, $sb*$sa, $cb ],
    ]);

    # 0,0,0,0
    my $vec = Physics::Lorentz::Vector->new();

    return($class->new($m, $vec));
}

=head2 rotation_x

Alternative constructor to construct a specific type of Lorentz transformation:
A 3D rotation about the x or 1-axis. First argument is the rotation angle.

=cut

sub rotation_x {
    my $proto = shift;
    my $class = ref($proto)||$proto;

    my ($phi) = @_;

    my $cp = cos $phi;
    my $sp = sin $phi;

    my $m = pdl([
        [ 1, 0, 0,    0   ],
        [ 0, 1, 0,    0   ],
        [ 0, 0, $cp,  $sp ],



( run in 3.265 seconds using v1.01-cache-2.11-cpan-af0e5977854 )