Essays/Kronecker Product

From J Wiki
Jump to navigation Jump to search

The Kronecker product of two matrices A and B , denoted AB in conventional mathematical notation, is a matrix with shape ($A)*($B) which can be visualized as follows:

   ] A=: 10+i.2 3
10 11 12
13 14 15
   ] B=: i.3 4
0 1  2  3
4 5  6  7
8 9 10 11
   A *&.> < B
┌───────────────┬───────────────┬───────────────┐
│ 0 10  20  30  │ 0 11  22  33  │ 0  12  24  36 │
│40 50  60  70  │44 55  66  77  │48  60  72  84 │
│80 90 100 110  │88 99 110 121  │96 108 120 132 │
├───────────────┼───────────────┼───────────────┤
│  0  13  26  39│  0  14  28  42│  0  15  30  45│
│ 52  65  78  91│ 56  70  84  98│ 60  75  90 105│
│104 117 130 143│112 126 140 154│120 135 150 165│
└───────────────┴───────────────┴───────────────┘

The Kronecker product is the matrix resulting from "removing the boxes" in the last display. It can be computed in J as follows:

   ] K=: > ,.&.>/ ,&.>/ A *&.> <B
  0  10  20  30   0  11  22  33   0  12  24  36
 40  50  60  70  44  55  66  77  48  60  72  84
 80  90 100 110  88  99 110 121  96 108 120 132
  0  13  26  39   0  14  28  42   0  15  30  45
 52  65  78  91  56  70  84  98  60  75  90 105
104 117 130 143 112 126 140 154 120 135 150 165

   kp=: *&$ ($,) 0 2 1 3 |: */
   K -: A kp B
1

   kp1=: [: ,./^:2 */   NB. Victor Cerovski, 2010-02-26
   K -: A kp1 B
1
   NB. Generalized to arbitrary dimensions (not scalar, Jan-Pieter Jacobs, 2021-10-01) 
   kpnd =: cs ($,) as |: *"0 _
   NB. cs calculates final shape
   cs =: ([: */&:> >.&# {.!.1&.> ;)&$
   NB. as calculates required axis shuffle
   as =: ([: ~.@,@|: ;&i. +&> 0 , [)&(#@$)

   K -: A kpnd B
1

It can also be computed as <;.1^:_1 A *&.> <B where <;.1^:_1 is an extension discussed in the Block Matrix Inverse essay.



See also


Contributed by Roger Hui.