Matrix multiplication with a loop function

255 views
Skip to first unread message

h_c...@hotmail.co.uk

unread,
Nov 16, 2016, 3:41:27 PM11/16/16
to MiniZinc

Hello,

I am trying to write a code to multiply 2x2 matrices intrinsically with a loop function. I have managed to do this for an extrinsic example but this has required a fair few line of code and I would like to see if MiniZinc can do it in another way. The code I have written is as follows:

int: dim = 2;

array[1..dim,1..dim] of var float: A;
array[1..dim,1..dim] of var float: AA;

constraint A[1,1]=1.0;
constraint A[2,1]=2.0;
constraint A[1,2]=1.0;
constraint A[2,2]=3.0;

constraint forall (i in 1..dim, j in 1..dim) (
       sum (k in 1..dim) (A[i,k]*A[k,j]) = AA[i,j]
  );

solve satisfy;

output [show(A)];

Thank you,

Harry

guido.tack

unread,
Nov 16, 2016, 3:54:14 PM11/16/16
to MiniZinc
You can use array comprehensions like this:

array[1..dim,1..dim] of float: A = [|1,2|1,3|];
array[int,int] of float: AA = array2d(1..dim,1..dim, [ sum (k in 1..dim) (A[i,k] * A[k,j]) | i,j in 1..dim ]);

Cheers,
Guido

Reply all
Reply to author
Forward
0 new messages