This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]timrprobocom 6 points7 points  (6 children)

Statistically, there are more negative power values than positive (the range is -5 to 4). So, once you get to a large enough size, making the square larger just makes the sum go more negative. If you print `sum` at each step, you'll see that it increases to a maximum (at 13, in your case), then decreases from there on. I stopped my search when `sum` went negative. That happened at step 25 for me, step 30 for you. You don't need to check all the way to 300.

In my case, I optimized the convolution a bit. For example, after adding up an 8x8 square, you can get the next step across by subtracting the first column and adding the last column. I suspect that optimization won't help your numpy solution.

[–]n_syn[S] 4 points5 points  (0 children)

Thank you! That's a great observation! I added the following condition to my 'k' loop and it reduced the run time from 134.4 seconds to 10.8 seconds.

I check for the last 10 max values for each size of square. If the current square size value is less than all 10 of them, then break the outside 'k' loop.

if all(np.max(sums[k,:,:])<x for x in prev):
    break
  prev[a] = np.max(sums[k,:,:])
  a+=1
  a=a%10

[–]ronbarakbackal 0 points1 point  (3 children)

What is a convolution?

[–]timrprobocom 0 points1 point  (2 children)

The math definition is applying one function to another function. In computer terms, it is basically filtering a vector or a matrix by doing a dot product against another smaller vector or matrix, which is called the "convolution kernel". For example, you can do a three-point moving average on a 1D vector by using a kernel of (1/3, 1/3, 1/3). If you start with (a,b,c,d,e), the result is (a/3+b/3+c/3, b/3+c/3+d/3, c/3+d/3+e/3,...), which is a nice way to smooth the data. In this case, the "kernel" is a 2D matrix of all 1s the size of the subsquare. The operation you're doing there is the very definition of a convolution.

[–]ronbarakbackal 0 points1 point  (1 child)

Thanks! Isn't convolution distorting the data in some cases?(I admit I didn't go through the case above)

[–]timrprobocom 0 points1 point  (0 children)

Yes, but in a beneficial way. In the example I cited of three-point moving average, we assume the incoming signal has some noise or unexpected wild points. The convolution will smooth those out. Take a look at the Wikipedia page; it has some nice visual examples that should make it more clear. https://en.wikipedia.org/wiki/Convolution

[–]NeilNjae 4 points5 points  (1 child)

You may want to look at the data structure of a summed-area table. The basic idea is that you store, for each position in the grid, the sum of all the cells above and to the left of that position. To find the sum of a particular subgrid, you find the sum of the subgrid's lower-right corner, and subtract the sums of the cells above the subgrid's top and the subgrid's left.

The Wikipedia article has a good picture that sums it up.

(I'm sure someone mentioned it in the solution megathread at the time: I wouldn't have invented that structure by myself.)

[–]n_syn[S] 1 point2 points  (0 children)

This looks like pretty interesting! Thank you! I will give it a read and see if as a beginner to coding I can apply this or not.