in reply to Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2)
Interesting problem...
Since it's that time of year...
q(May all your Christmases be white.) =~ s/Christmase/loop/r =~ s/whit +e/implicit/r
Inspired by mention of "sliding".
#!/usr/bin/perl use strict; # https://theweeklychallenge.org/blog/perl-weekly-challeng +e-248/#TASK2 use warnings; use List::AllUtils qw( zip_by reduce ); use Data::Dump qw( pp ); sub slide { my @new; reduce { push @new, $a + $b; $b } @_; @new; } for ( [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ], [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ], ) { print 'Input: $a = ', pp($_), "\n"; my @new = zip_by { [ @_ ] } map [ slide @$_ ], zip_by { [ @_ ] } map [ slide @$_ ], @$_; print 'Output: $b = ', pp(\@new), "\n\n"; }
Outputs
Input: $a = [[1 .. 4], [5 .. 8], [9 .. 12]] Output: $b = [[14, 18, 22], [30, 34, 38]] Input: $a = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] Output: $b = [[2, 1, 0], [1, 2, 1], [0, 1, 2]]
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2)
by tybalt89 (Monsignor) on Dec 27, 2023 at 13:57 UTC | |
by Anonymous Monk on Dec 27, 2023 at 22:53 UTC |