The problem is relatively simple:
Given a 2D-binary array(grid), whereby every entry is either a 1 or -1, what is the absolute quickest way to take two coordinates points $o,p$ - which represent a line of distance $r_{op}$ - and move it through all possible valid grid points.
Let $c_1 = f(o) , c_2 = f(p)$, these return the value of the array at the corresponding points of $o,p$. After every move of the points, you want calculate the following:
$C = c_1\times c_2$
then take the average over the entire grid of $C$.
I have been using a nested Do-loop for my trial (see example code below), but it's way to slow for the size of grids i'm looking at and the number of different line lengths I am sampling across the grid, I thus am looking for a super speedy approach that I am probably unaware of.
GetC[coords_, ArrayData_] := Module[{c1, c2},
c1 = ArrayData[[coords[[1, 1]], coords[[1, 2]]]];
c2 = ArrayData[[coords[[2, 1]], coords[[2, 2]]]];
c1*c2];
ftran[{{x1_, y1_}, {x2_, y2_}}, {xT_, yT_}] :=
(
Return[{{x1 + xT, y1 + yT}, {x2 + xT, y2 + yT}}];
);
grid = {1000, 1000};
array = Table[(-1)^RandomInteger[{i, j}], {i, grid [[1]]}, {j,grid [[2]]}];
o = {1, 1};
p = {1, 2};
line = {o, p}
bounds = CoordinateBounds[line];
xMax = grid[[1]] - bounds[[1, 2]];
yMax = grid[[1]] - bounds[[2, 2]];
cnt = 0
c = 0;
Do[
Do[
newLine = ftran[line, {i, j}];
TempC = GetC[newLine, array];
c = c + TempC;
cnt++ 1;
, {i, 0, xMax}];
, {j, 0, yMax}]; // AbsoluteTiming
averageC = c/cnt
For a 1000x1000 grid I get a timing of 17.5053s (of course this will change depending on your PC performance). To put things into perspective, my grids are roughly 2000x2000, and I'm looking at doing this procedure for many different lines ranging from $r_{op}=1$ to $r_{op} = 1000$. To be exact, this is 216341 possible different lengths. As you can see, if I cannot find a very quick way of rewriting my code, this task isn't really possible.
Using a time of 17s, this wold take ~ 42 days on my PC. Definitely not good enough.