Find the difference between the vectors

Let's use Perl to find the vector difference required for deep learning calculations.

In Perl, vectors are represented as arrays. I think that software engineers will have a headache when they hear the word "vector", but when they say "array", it means "what?".

A Perl program that finds vector differences.

use strict;
use warnings;

#Vector difference
sub vec_sub {
   my ($vec1, $vec2) = @_;

   my $vec_sub = [];

   for (my $i = 0; $i <@$vec1; $i ++) {
     $vec_sub->[$i] = $vec1->[$i]-$vec2->[$i];
   }
  
   return $vec_sub;
}

my $vec1 = [1, 2, 3];
my $vec2 = [4, 5, 6];

#Vector difference
my $vec_sub = vec_sub ($vec1, $vec2);

# -3 -3 -3
print "@$vec_sub\n";

Associated Information