in linear
mod R is pr INT-MATRIX *(sort IntMatrix to M). op r : M M -> M . op t : Int
Int M M -> Int . var A B C D E : Int . var W X Y Z : M . ceq r((A,B)|-> E ;
X ; Y,Z)= r(Y,Z)if(C,D)|-> E ; W := Z /\ t(A - C,B - D,X,W)=/= 0 . eq r(X,Y)=
X[owise]. ceq t(A,B,X,(C,D)|-> E ; Y)= 0 if X[A + C,B + D]=/= E . endm
The result is obtained by reducing the r
function with the input and pattern matrices, given as IntMatrix
es, and the output is an IntMatrix
.
This solution requires that all of the input matrices' entries be non-zero, due to a quirk of IntMatrix
es: zero entries are omitted in the representation (since zero is the default). This is also true for the output.
Example Session
Maude> red r(
> (0, 0) |-> 1 ; (0, 1) |-> 2 ; (0, 2) |-> 3 ;
> (1, 0) |-> 4 ; (1, 1) |-> 3 ; (1, 2) |-> 2 ;
> (2, 0) |-> 3 ; (2, 1) |-> 5 ; (2, 2) |-> 4,
> (0, 0) |-> 3 ;
> (1, 0) |-> 2
> ) .
result M: 0,0 |-> 1 ; 0,1 |-> 2 ; 1,0 |-> 4 ; 1,1 |-> 3 ; 2,0 |-> 3 ; 2,1 |-> 5
; 2,2 |-> 4
Maude> red r(
> (0, 0) |-> 1 ; (0, 1) |-> 2 ;
> (1, 0) |-> 3 ; (1, 1) |-> 4,
> (0, 0) |-> 1
> ) .
result M: 0,1 |-> 2 ; 1,0 |-> 3 ; 1,1 |-> 4
Maude> red r(
> (0, 0) |-> 4 ; (0, 1) |-> 4 ; (0, 2) |-> 6 ; (0, 3) |-> 7 ;
> (1, 0) |-> 4 ; (1, 1) |-> 2 ; (1, 2) |-> 4 ; (1, 3) |-> 4 ;
> (2, 0) |-> 7 ; (2, 1) |-> 3 ; (2, 2) |-> 4 ; (2, 3) |-> 2 ,
> (0, 0) |-> 4 ; (0, 1) |-> 4 ;
> (1, 0) |-> 4 ; (1, 1) |-> 2
> ) .
result M: 0,2 |-> 6 ; 0,3 |-> 7 ; 2,0 |-> 7 ; 2,1 |-> 3
Maude> red r(
> (0, 0) |-> 1 ; (0, 1) |-> 2,
> (0, 0) |-> 3 ; (0, 1) |-> 4
> ) .
result M: 0,0 |-> 1 ; 0,1 |-> 2
Maude> red r(
> (0, 0) |-> 1 ; (0, 1) |-> 2 ;
> (1, 0) |-> 3 ; (1, 1) |-> 4,
> (0, 0) |-> 1 ; (0, 1) |-> 2 ;
> (1, 0) |-> 3 ; (1, 1) |-> 4
> ) .
result M: zeroMatrix
Maude> red r(
> (0, 0) |-> 1 ; (0, 1) |-> 2 ;
> (1, 0) |-> 2 ; (1, 1) |-> 1,
> (0, 0) |-> 1 ;
> (1, 0) |-> 2
> ) .
result M: 0,1 |-> 2 ; 1,1 |-> 1
Ungolfed
in linear
mod R is
pr INT-MATRIX * (sort IntMatrix to M) .
op r : M M -> M .
op t : Int Int M M -> Int .
var A B C D E : Int .
var W X Y Z : M .
ceq r((A, B) |-> E ; X ; Y, Z) = r(Y, Z)
if (C, D) |-> E ; W := Z
/\ t(A - C, B - D, X, W) =/= 0 .
eq r(X, Y) = X [owise] .
ceq t(A, B, X, (C, D) |-> E ; Y) = 0
if X[A + C, B + D] =/= E .
endm
We find an overlap by guess-and-check. We guess a submatrix and pick one entry from the submatrix and one from the pattern with the same value. We compute their offset and then try to find a counter-example to the match (via t
). If none exists, we remove the submatrix (equivalent to setting it to zero).
[ [1, 2], [2, 1] ] [ [1], [2] ] => [ [0, 2], [0, 1] ]
my previous answer failed on this one. \$\endgroup\$b
only contain0
s? For example[[0, 0], [0, 0]] [[0, 0]] => [[0, 0],[0, 0]]
\$\endgroup\$