User Tools

Site Tools


gams:compute_the_kronecker_product_in_gams

How do I compute the Kronecker product in GAMS

*   --- Kronecker product of two matrices A and B
*   --- A is of dimension (m x n)
*       B is of dimension (p x q)
* This is a perfect example for the not so well known GAMS Matching
* operator (see e.g. Release Notes for 22.7 at
* http://www.gams.com/docs/release/release.htm#22.7)

sets
  m /m1*m3/
  n /n1*n2/
  p /p1*p4/
  q /q1*q3/
  ;
$eval D1 card(m) * card(p)
$eval D2 card(n) * card(q)

sets
  i /i1*i%D1%/
  j /j1*j%D2%/
* the matching operator does the magic here
  imp(i,m,p) / #i:(#m.#p) /
  jnq(j,n,q) / #j:(#n.#q) /
  ;

* have a look at the sets in the IDE's gdx browser
execute_unload 'ksets';

table A(m,n)
            n1          n2
m1          4            2
m2          1            3
m3          6            5
;

table B(p,q)
            q1          q2          q3
p1          7           6           9
p2          8           7           7
p3          4           1           6
p4          5           5           2
;

parameter Kroenecker(i,j);

Kroenecker(i,j) = sum{(imp(i,m,p),jnq(j,n,q)), A(m,n)*B(p,q)};

display A,B,Kroenecker;
execute_unload 'kAll'; 

Here is another approach provided by Thomas Rutherford.

gams/compute_the_kronecker_product_in_gams.txt · Last modified: 2013/07/23 17:33 by support