# code snippet to convolve 2 vectors

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I'm looking for perl code examples to get the convoltion of 2 arrays eg.
like this :

[1, 2] x [3, 4] = [ 3, 10, 8]

--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3

## Re: code snippet to convolve 2 vectors

Toralf F??rster wrote:
) I'm looking for perl code examples to get the convoltion of 2 arrays eg.
) like this :
)
) [1, 2] x [3, 4] = [ 3, 10, 8]

What have you got so far, and what are
the bits that you're having trouble with ?

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

## Re: code snippet to convolve 2 vectors

Toralf Förster wrote:

You meant convolution?

Something like this?

( 1 + 2 * x ) * ( 3 + 4 * x ) =
3 + 6 * x + 4 * x + 8 * x^2 =
3 + 10 * x + 8 * x^2

#!/usr/bin/perl
use strict;
use warnings;

my @v = ( [ 1, 2 ], [ 3, 4 ] );

printf "[ %s, %s, %s ]\n",
\$v[ 0 ][ 0 ] * \$v[ 1 ][ 0 ],
\$v[ 0 ][ 1 ] * \$v[ 1 ][ 0 ] + \$v[ 0 ][ 0 ] * \$v[ 1 ][ 1 ],
\$v[ 0 ][ 1 ] * \$v[ 1 ][ 1 ];

__END__

--
Ruud

## Re: code snippet to convolve 2 vectors

wrote:

Nothing fancy, the straight forward way.
-sln
------------
use strict;
use warnings;

my @A = (1, 2);
my @B = (3, 4);
my @R;

for my \$x (0 .. \$#A)
{
my (\$col, \$mult) = (\$x, \$A[\$x]);
for my \$y (0 .. \$#B)
{
\$R[\$col++] += \$B[\$y] * \$mult;
}
}

print "@R";

## Re: code snippet to convolve 2 vectors

sln@netherlands.com wrote:

Thx sln - this was what I ws looking for

:-)
--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3