Description
Today, I know this resourse http://library.wolfram.com/infocenter/MathSource/4557/ accidentally, in this notebook, I find the detailed useage of ListCorrelate
,so I post here to share to more Mathematica user that need to improve their capability.
Hope
I hope sincerely others to edit and check it to make it complete and correct.
Statement
Author Ted Ersek
https://mathematica.stackexchange.com/users/460/ted-ersek
The copyright belongs to the original author
Main Tutorial
The next cell demonstrates the basic use of ListCorrelate
.
Clear["Global`*"];
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6}]
{{a1 x1 + a2 x2 + a3 x3 + b1 y1 + b2 y2 + b3 y3 + c1 z1 + c2 z2 + c3 z3}}
Next we see that ListCorrelate
is equivalent to flattening the result of a certain matrix product.
True
- Specifying the "overhang" using ${K_L,K_R}$
In the next cell we provide ListCorrelate
{-1,-1} as a third argument.
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6},{-1,-1}]
{a4 b1+a1 b4+a2 b5+a3 b6,a3 b1+a4 b2+a1 b5+a2 b6,a2 b1+a3 b2+a4 b3+a1 b6,a1 b1+a2 b2+a3 b3+a4 b4,a1 b2+a2 b3+a3 b4+a4 b5,a1 b3+a2 b4+a3 b5+a4 b6}
The previous example is equivalent to the matrix product in the next cell. Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix.. We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix. The (-1) indicates last element of {a1, a2, a3, a4}, and (-2) would indicate the second from the last element of {a1, a2, a3, a4}.
In the next cell we provide ListCorrelate
{1,1} as a third argument.
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6},{1,1}]
{a1 b1+a2 b2+a3 b3+a4 b4,a1 b2+a2 b3+a3 b4+a4 b5,a1 b3+a2 b4+a3 b5+a4 b6,a4 b1+a1 b4+a2 b5+a3 b6,a3 b1+a4 b2+a1 b5+a2 b6,a2 b1+a3 b2+a4 b3+a1 b6}
The previous example is equivalent to the matrix product in the next cell. Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix.. We also have the first element of {a1, a2, a3, a4} in the lower right position of the left matrix. The (1) indicates first element of {a1, a2, a3, a4}, and (2) would indicate the second element of {a1, a2, a3, a4}.
In the next cell we provide ListCorrelate
{1,-1} as a third argument.
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6},{1,-1}]
{a1 b1+a2 b2+a3 b3+a4 b4,a1 b2+a2 b3+a3 b4+a4 b5,a1 b3+a2 b4+a3 b5+a4 b6}
The previous example is equivalent to the matrix product in the next cell. Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix.. We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix. The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.
In the next cell we provide ListCorrelate {-1,1} as a third argument. Notice this gives the same result as
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6}]
(ie. {-1,1} is the default for the third argument).
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6},{-1,1}]
{a4 b1+a1 b4+a2 b5+a3 b6,a3 b1+a4 b2+a1 b5+a2 b6,a2 b1+a3 b2+a4 b3+a1 b6,a1 b1+a2 b2+a3 b3+a4 b4,a1 b2+a2 b3+a3 b4+a4 b5,a1 b3+a2 b4+a3 b5+a4 b6,a4 b1+a1 b4+a2 b5+a3 b6,a3 b1+a4 b2+a1 b5+a2 b6,a2 b1+a3 b2+a4 b3+a1 b6}
Length[%]
9
The previous example is equivalent to the matrix product in the next cell. Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix.. We also have the First element of {a1, a2, a3, a4} in the lower right position of the left matrix. The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.
ListCorrelate with matrices
m1 = {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}
m2 = {{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}
ListCorrelate[m1, m2]
True
The next cell shows how the same ListCorrelate
can be done as Dot
products on parts of the matrices.
ListCorrelate[m1,m2]=== {{Part[m1,1].Part[m2,1]+Part[m1,2].Part[m2,2]+Part[m1,3].Part[m2,3]}}
True
We can give ListCorrelate
a level specification as a 7$^{th}$ argument. The next cell shows that level (2) is the default specification when working on matrices.
ListCorrelate[m1,m2]===ListCorrelate[m1,m2,{1,-1},m2,Times,Plus,2]
True
In the next cell we give ListCorrelate
the integer (1) as a level specification.
ListCorrelate[m1,m2,{1,-1},m2,Times,Plus,1]
{{a1 x1+b1 y1+c1 z1,a2 x2+b2 y2+c2 z2,a3 x3+b3 y3+c3 z3}}
The next cell shows how the same ListCorrelate can be done as Dot products on parts of the matrices.
mat1=Transpose[m1];
mat2=Transpose[m2];
ListCorrelate[m1,m2,{1,-1},m2,Times,Plus,1]===
{{Part[mat1,1].Part[mat2,1],Part[mat1,2].Part[mat2,2],Part[mat1,3].Part[mat2,3]}}
True
The next cell shows two ways of expressing the default for the third argument of ListCorrelate when working with matrices.
ListCorrelate[m1,m2]===ListCorrelate[m1,m2,{1,-1}]===ListCorrelate[m1,m2,{{1,1},{-1,-1}}]
True