/* "Lucky fool" 2x2 matrices in Picat. From "'Lucky fool' 2x2 matrix multiplication: finding all possible pairs with WL" https://community.wolfram.com/groups/-/m/t/2106750?fbclid=IwAR2RlAxHnOEXV9Y5NSGKUubPvJcb4yvbsC430P5OrvB3PpF8gMFXBF836Tc """ On the Internet, you might have seen jokes of the type “I’ve invented an easy way to multiply matrices (a b)*(a' b') = (ab bb') (c d) (c' d') (cc' dd') Here's an example (3 4)*(7 2) = (37 42) (8 7) (4 9) (84 79) .... """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> A = [[3,4],[8,7]], B = [[7,2],[4,9]], print_matrix(A), print_matrix(B), matrix_mult(A,B,M), print_matrix(M), nl. go => true. go2 ?=> L = [A,B,C,D,E,F,G,H], L :: 1..9, M1 = [[A,B],[C,D]], M2 = [[E,F],[G,H]], matrix_mult(M1,M2,M), solve(L), print_eq(M1,M2,M), fail, nl. go2 => true. print_matrix(M) => foreach(Row in M) println(Row) end, nl. print_eq(A,B,M) => foreach(I in 1..2) print(A[I]), if I == 1 then print("*") else print(" ") end, print(B[I]), if I == 1 then print(" = ") else print(" ") end, print(M[I]),nl end, nl. matrix_mult([[A,B], [C,D]], [[E,F], [G,H]], M) => % The multiplication AE #= A*E, BG #= B*G, AF #= A*F, BH #= B*H, CE #= C*E, DG #= D*G, CF #= C*F, DH #= D*H, AEBG #= AE+BG, AFBH #= AF+BH, CEDG #= CE+DG, CFDH #= CF+DH, % Constraints 10*A + E #= AEBG, 10*B + F #= AFBH, 10*C + G #= CEDG, 10*D + H #= CFDH, M =[[AEBG, AFBH], [CEDG, CFDH]].