I neede some fast way to do array math, this is what I came up with. Anyone who finds a faster way - please post it ....
#!/usr/bin/perl -w use strict; my @a1=(10,9,8,7,6,5,4,3,2,1); my @a2=reverse(9,8,7,6,5,4,3,2,1,0); my @a3=(2,2,2,2,2,2,2,2,2,2); sub asub { my ($k,$v1,$v2)=(0,$_[0],$_[1]); foreach (@$v1) { $$v1[$k++]=$_ - $$v2[$k]; } return $v1; } sub aadd { my ($k,$v1,$v2)=(0,$_[0],$_[1]); foreach (@$v1) { $$v1[$k++]=$_ + $$v2[$k]; } return $v1; } sub amult { my ($k,$v1,$v2)=(0,$_[0],$_[1]); foreach (@$v1) { $$v1[$k++]=$_ * $$v2[$k]; } return $v1; } sub adiv { my ($k,$v1,$v2)=(0,$_[0],$_[1]); foreach (@$v1) { $$v1[$k++]=$_ / $$v2[$k]; } return $v1; } my ($a1,$a2,$a3)=(\@a1,\@a2,\@a3); my $res=aadd($a1,$a2); print "res: @$res\n"; $res=adiv($res,$a3); print "res: @$res\n";

Replies are listed 'Best First'.
Re: array add/sub/mult/div
by dragonchild (Archbishop) on Mar 18, 2002 at 17:00 UTC
    One could do something like:
    use mapcar; sub a_add { $v1 = [ mapcar { $_[0] + $_[1] } @_[0,1] ] } sub a_subt { $v1 = [ mapcar { $_[0] - $_[1] } @_[0,1] ] } sub a_mult { $v1 = [ mapcar { $_[0] * $_[1] } @_[0,1] ] } sub a_div { $v1 = [ mapcar { $_[1] ? $_[0] / $_[1] : undef } @_[0,1] +] }
    That adds something to make sure you're not dividing by zero. It also allows both for a return value as well as a in-place change. (It actually requires in-place changes, which may not be desirable.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      One could, if one had mapcar.

      However, search.cpan.org does not admit to its existence...
      --
      Mike (Edit: OOPS! mapcar module. Nice!)

Re: array add/sub/mult/div
by danger (Priest) on Mar 19, 2002 at 10:40 UTC

    Well, if you wanted to admit modules, you could have some fun with the PDL module:

    #!/usr/bin/perl -w use strict; use PDL; my $v1 = pdl reverse 1..10; my $v2 = pdl 0..9; my $v3 = pdl ((2)x10); my $add = $v1 + $v2; my $mult = $v1 * $add; my $div = $mult / $v3; my $sub = $div - $v1; print "add: $add\nmult: $mult\ndiv: $div\nsub: $sub\n"; # we didn't stomp the original vectors either... # and of course, we can work with matrices too my $matA = pdl [0,0,0],[1,1,1],[2,2,2]; # 2d 3x3 $matA++; # add one across the board $matA *= 2; # and scale up by factor of 2 print "A: $matA\n"; $matA *= $matA; # scale it by itself print "B: $matA\n"; $matA x= $matA; # matrix multiply print "C: $matA\n"; # and say, did you ever want to know the eigenvalues and # eigenvectors of a 4 x 4 matrix of a fibonacci sequence??? use PDL::Slatec; # Fortran never dies :-) my $fibo = fibonacci(4,4); my($eigvals, $eigvecs) = eigsys($fibo); print <<EOF; Fibo matrix: $fibo Eigenvalues: $eigvals Eigenvectors:$eigvecs EOF __END__ add: [10 10 10 10 10 10 10 10 10 10] mult: [100 90 80 70 60 50 40 30 20 10] div: [50 45 40 35 30 25 20 15 10 5] sub: [40 36 32 28 24 20 16 12 8 4] A: [ [2 2 2] [4 4 4] [6 6 6] ] B: [ [ 4 4 4] [16 16 16] [36 36 36] ] C: [ [ 224 224 224] [ 896 896 896] [2016 2016 2016] ] Fibo matrix: [ [ 1 1 2 3] [ 5 8 13 21] [ 34 55 89 144] [233 377 610 987] ] Eigenvalues: [0.859383 6.05138 68.0353 1010.05] Eigenvectors: [ [ 0.99101 -0.13375 -0.00326739 0.00031052] [ -0.131501 -0.97827 0.160279 -0.00218367] [ -0.0244236 -0.156787 -0.974861 0.156421] [ 0.0032657 0.0227098 0.154745 0.987688] ]
Re: array add/sub/mult/div
by ariels (Curate) on Mar 19, 2002 at 07:45 UTC

    Module-less, one could still use just plain boring map:

    sub amult { my ($a, $b) = @_; croak "Arrays of different sizes" unless $#$a==$#$b; map { $a->[$_] * $b->[$_] } (0..$#$a) }
Re: array add/sub/mult/div
by Juerd (Abbot) on Mar 18, 2002 at 18:45 UTC

    Sorry, but your wheel has already been invented.

    $_ += 123 for @foo; $_ -= 123 for @foo; $_ *= 123 for @foo; $_ /= 123 for @foo;
    Isn't that great? Perl has loops and simple math built in!

    And if that isn't simple enough, Perl 6 will provide these:
    @foo ^+= 123; @foo ^-= 123; @foo ^*= 123; @foo ^/= 123;

    Oops, I didn't read the original post well enough. Anyway, Perl 6 will provide:
    @foo ^+= @bar; etcetera

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk