in reply to Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2)
Very nice post and tests. I found something similar to the sliding average.
use v5.36; use PDL; use PDL::Image2D; my $w=4; my $h=5; my $m=sequence(10,10); # or any other matrix say $m->conv2d(ones($w,$h))->slice([floor(($w-1)/2),floor(-($w+1)/2)], [floor(($h-1)/2),floor(-($h+1)/2)]);
I expected box2d to work also, but either it doesn't work or I haven't understood it yet.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2)
by Anonymous Monk on Dec 27, 2023 at 22:40 UTC | |
Thanks, nice find. By the way, perhaps I've chosen imprecise word ("sliding"). Maybe in math/science, any function, applied to overlapping infixes, is said to be applied in sliding/moving/rolling manner. What I meant instead, with the word, -- "applied using computationally efficient/cheap algorithm". Looks like conv2d does very honest (therefore not efficient/cheap) 4-nested-loops style calculation, in PP/XS/C -- OK for very small kernels. Here's overcrowded plot, but B and E cases would suffice to show they are the same breed.
| [reply] [d/l] [select] |
by wlmb (Novice) on Dec 27, 2023 at 23:30 UTC | |
For large submatrices one can use a Fourier transforms to perform the convolution, as in
It would be interesting to also compare it. | [reply] [d/l] |
by Anonymous Monk on Dec 28, 2023 at 11:35 UTC | |
Interesting. FFT for this task, who would have thought a few days ago. Looks like it runs in almost constant time, regardless of kernel size; and of course slower than special solutions. I had to add a couple of checks to accommodate for (unimportant) corner test case of submatrix with dims (4,3) and example matrix.
| [reply] [d/l] |
Re^2: Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2)
by soonix (Chancellor) on Dec 27, 2023 at 20:43 UTC | |
Otherwise ++👍 | [reply] [d/l] [select] |
by wlmb (Novice) on Dec 27, 2023 at 23:10 UTC | |
Thanks. I corrected my previous post. | [reply] |