Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to vector

by pme (Prior)
on Jun 19, 2015 at 20:09 UTC ( #1131184=note: print w/replies, xml ) Need Help??


in reply to How to vector

Hi jcklasseter,

Do you have any specific reason to use Math::Vector::Real? You can do that with perl arrays and arrayrefs.

use strict; use warnings; use Data::Dumper; open IN, "<IN" or die "Cannot open IN\n"; my @arr; while (<IN>) { chomp; my @line = split; push @arr, \@line; } close IN; print Dumper(\@arr) . "\n"; foreach (@arr) { print "$_->[0] - $_->[1] - $_->[2]\n"; }

Replies are listed 'Best First'.
Re^2: How to vector
by jcklasseter (Sexton) on Jun 19, 2015 at 20:28 UTC
    Eventually it will be used to vector math with. Baby steps right now though, let's see if I can even assign them right. Can Arrays be used similar to vectors for vector math purposes?
      They can't. That's what Math::Vector::Real does, implementing the mathematical vector semantics on top of arrays.
        They can't. That's what Math::Vector::Real does, implementing the mathematical vector semantics on top of arrays.

        salva, you seem clearly to understand higher math, but this is a 2-sentence contradiction, because the cpan module uses arrays and perl, and achieves what you claim can't be attained. What I think you mean to say is that many of the functions one would need to do vector manipulations are not native to perl, so one would have to write them from scratch.

        There's always a set of trade-offs with using cpan as opposed to rolling your own. One is that one has to be able to read and use the module effectively, and this exercise gave me the opportunity to dink around with this module for the first time. A difficult matter in this is understanding how the references and dereferencing work, and I wrote a script to help myself--maybe OP as well--understand how perl implements vector math:

        #!/usr/bin/perl use strict; use warnings; use diagnostics; use Math::Vector::Real; use 5.010; my $source = "./IN"; my $out = "./OUT"; open(IN, '<', $source) or die "Couldn't open $source: $!\n"; open(OUT, '>', $out) or die "Couldn't open $out: $!\n"; my @data = map [ split ], grep /\S/, <IN>; say "data is @data"; print_aoa(\@data); for my $ref (@data) { my @vect = @$ref; say "vect is @vect"; my $vector = V(@vect); say "vector is @$vector"; my $u = $vector->versor; say "versor is @$u"; } close IN; close OUT; sub print_aoa{ use strict; use warnings; use 5.010; my $a = shift; my @AoA = @$a; for my $i ( 0 .. $#AoA ) { my $aref = $AoA[$i]; for my $j ( 0 .. $#{$aref} ) { print "elt $i $j is $AoA[$i][$j]\n"; } } return $a; }

        I always keep a a routine for printing an array of arrays in my grab bag of utilities. It accepts a reference to the AoA and prints it out element-wise. Crucial for debugging. I didn't use the following line from the original, simply because I couldn't jimmy with it enough to get it to work for me:

        foreach my $d1 (@data) { print OUT " The vector of Atom ($d1->[0], $d1 +->[1], $d1->[2])is : %f\n", vector($d1); }

        I had to feel my way through the data, seeing first that @data contained references, and then looping through them with a structure that I was able to read. I noticed that your data has an unwanted empty line at the top and removed it. Here's the output:

        data is ARRAY(0x2b07cc) ARRAY(0x2b0d6c) ARRAY(0x21bdef4) ARRAY(0x1fefa +cc) ARRAY( 0x332dd4) ARRAY(0x1ffb2cc) elt 0 0 is 0 elt 0 1 is 5 elt 0 2 is 5 elt 1 0 is 1 elt 1 1 is 2 elt 1 2 is 3 elt 2 0 is 3 elt 2 1 is 5 elt 2 2 is 6 elt 3 0 is 2 elt 3 1 is 5 elt 3 2 is 2 elt 4 0 is 5 elt 4 1 is 5 elt 4 2 is 5 elt 5 0 is 5 elt 5 1 is 5 elt 5 2 is 6 vect is 0 5 5 vector is 0 5 5 versor is 0 0.707106781186547 0.707106781186547 vect is 1 2 3 vector is 1 2 3 versor is 0.267261241912424 0.534522483824849 0.801783725737273 vect is 3 5 6 vector is 3 5 6 versor is 0.358568582800318 0.597614304667197 0.717137165600636 vect is 2 5 2 vector is 2 5 2 versor is 0.348155311911396 0.870388279778489 0.348155311911396 vect is 5 5 5 vector is 5 5 5 versor is 0.577350269189626 0.577350269189626 0.577350269189626 vect is 5 5 6 vector is 5 5 6 versor is 0.539163866017192 0.539163866017192 0.64699663922063

        One can see that vect and vector are equivalent representations. versor, however is not a feature one would have out of the box without the inclusion of the module. It wouldn't be hard to write, but the module will give a person much more functionality than this trivial work-up.

      You posted an example of just that this last week. Unlike hashes, arrays are ordered, so they can fit this bill. A typical Pythagorean triple might be @a = (2,3,7);. There has to be an origin: @o=(0,0,0);. One way this differs from typical mathematical treatments is that the indices begin with zero, but all that requires is that you be consistent. One thing you can't have are strings in your vector so this: @d=(8,7,dog); will never be a vector. Hope this helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1131184]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-01-20 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?