in reply to Re^2: How to vector
in thread How to vector

They can't. That's what Math::Vector::Real does, implementing the mathematical vector semantics on top of arrays.

Replies are listed 'Best First'.
Re^4: How to vector
by Aldebaran (Curate) on Jun 20, 2015 at 00:46 UTC
    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.

      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.

      What I meant (and what I think the OP was actually asking) is that raw arrays can't, for instance, be used in the following way:

      my $a = [1, 2]; my $b = [0, 3]; my $c = $a + $b;