jcklasseter has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. This isn't a very well known module, nor well documented (In my google-ing experience). Any ideas would be appreciated. This is my script:
#!/usr/bin/perl use strict; use warnings; use Math::Vector::Real; use constant DEG_PER_RAD => 45 / atan2(1, 1); my ( $source, $out ) = qw/ IN OUT /; open my $in_fh, '<', $source or die qq{Unable to open "$source" for i +nput: $!\n}; open my $out_fh, '>', $out or die qq{Unable to open "$out" for outp +ut: $!\n}; #select $out_fh; my @data; push @data, V(split) while <$in_fh>; for my $i ( 0 .. $#data-1 ) { for my $j ( $i+1 .. $#data ) { my $val1 = $data[$i]; my $val2 = $data[$j]; my $comp1 = $val1->[0]; my $comp2 = $val2->[0]; my $vect1 = $val1->[3..5]; my $vect2 = $val2->[3..5]; my $norm = $val1->[0..2]; my $nvect1 = $vect1 - $norm ; my $nvect2 = $vect1 - $norm ; if( $comp1 != $comp2 ){ next;}else{ my $degrees = atan2($nvect1, $nvect2) * DEG_PER_RA +D; print "$degrees\n"; } } }
In essence, the script reads the input and assigns each line to a vector via the Math::Vector::Real module. My input file is like so:
2 2 2 5 -2 1 2 2 2 2 7 4 5 2 2 2 7 4
Which are location coordinates. The first 3 numbers on each line correspond to a point, and the other 3 in the line correspond to a nearby point. The first set will often be identical from line to line. The second set will never repeat between consecutive lines like the first. I'm trying to find the angle between the second set of coordinates between two separate rows, after normalizing them with respect to the first set of coordinates. The script works (via print testing) up until  my $vect1 = $val1->[3..5];. When I print test here, I get:  2 (And a whole host of other errors that stem from this) I cannot find out how to split the values of a vector off into a new vector like I'm trying to. Does anyone have any ideas? Thanks.

Replies are listed 'Best First'.
Re: Vector manipulation (scalar)
by tye (Sage) on Jun 26, 2015 at 19:39 UTC
    my $vect1 = $val1->[3.­.5];. When I print test here, I get: 2

    Yep. 3..5 is being evaluated in scalar context because dereferencing to get a scalar wants a scalar index:

    % say "[0..9]->[4,5]" Useless use of a constant (4) in void context at (eval 1) line 1. 5

    You probably want:

    my $vect1 = [ @{ $val1 }[3.­.5] ];

    - tye        

      Thanks! I got is working on a variant of that, I had to do some dereferencing and re-vectorizing, but it worked! Final snippet:
      my $vect1 = [ @{ $val1 }[3..5] ]; my $vect2 = [ @{ $val2 }[3..5] ]; my $norm = [ @{ $val1 }[0..2] ]; my $varvec1 = V( @$vect1 ); my $varvec2 = V( @$vect2 ); my $varnorm = V( @$norm ); my $nvect1 = $varvec1 - $varnorm ; my $nvect2 = $varvec2 - $varnorm ;
Re: Vector manipulation
by kcott (Archbishop) on Jun 27, 2015 at 07:12 UTC

    G'day jcklasseter,

    "This isn't a very well known module, nor well documented (In my google-ing experience)."

    To find documentation for a CPAN module, you need to pick the right search engine. I usually use http://search.cpan.org/ and the remainder of my post focusses on that. Another popular one is https://metacpan.org/: investigate at your leisure.

    So, from the http://search.cpan.org/ page, enter Math::Vector::Real, click the [CPAN Search] button and follow the first result link to Math::Vector::Real. That certainly looks "well documented" to me; although, my intention here isn't to provide a review of this specific piece of documentation.

    In most cases, that's probably all you need to do; however, there's typically a lot more information that you can access should you require it.

    Along the top of the page you'll see:

    link-to-author > link-to-distribution > module-name

    While the "link-to-author" is of occasional interest, it's the "link-to-distribution" where you'll typically find the most useful, additional information. In this instance, that link is Math-Vector-Real-0.17 — follow it.

    Now you can see lots more information either directly on this page or via the links provided. If you're having problems installing a module, see the "CPAN Testers" results for your platform; if you're having problems using a module, take a look at outstanding bug reports; and so on. I suggest you spend some time looking at what's available. Other modules may have more or less information than what you're currently looking at.

    Of particular note, under Special Files, is the link to MANIFEST. Here you'll find links to all of the files that make up the distribution. There's often an examples/ directory (2 example files in this case) which can help with writing code for a module you haven't used previously; and the t/ (test) directory can be similarly useful.

    I suspect you could find all of this through a general search engine; although, you may have to check thousands of results to do so. Searching CPAN directly is, by far, the much better option.

    -- Ken

      Thanks Ken. I actually had looked under the CPAN sites for information about the posts. In my opinion, the main landing page for Math::Vector::Real is lacking in documentation - it has a fair amount, but a lot is left to be figured out by trial and error. However, I did not see the example files, and for that I thank you.
Re: Vector manipulation
by tangent (Parson) on Jun 27, 2015 at 00:00 UTC
    There are quite a few problems with your code. The first you need to sort out is where you read in the data:
    push @data, V(split) while <$in_fh>;
    You are creating one vector from all 6 points on each line, when you should be creating two vectors for each line:
    while (<$in_fh>) { my @points = split; push(@data, V( @points[0,1,2] ), V( @points[3,4,5] ) ); }
    Now your @data can be looped through and you can perform the calculations on the vectors.
Re: Vector manipulation
by salva (Canon) on Jun 26, 2015 at 21:46 UTC
    (In my google-ing experience). Any ideas would be appreciated

    In order to use most CPAN module effectively you should learn the basics first. Get some book book for learning Perl and follow it.

    Google is a good tool for solving very specific problems, but not for learning the basics. Programming without actually understanding what you are doing and why, just copy&pasting code from here and there, and then trying to get the result to work would only produce one output: pain!