Examples for 'base::kronecker'


Kronecker Products on Arrays

Aliases: kronecker .kronecker %x%

Keywords: array

### ** Examples

# simple scalar multiplication
( M <- matrix(1:6, ncol = 2) )
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
kronecker(4, M)
     [,1] [,2]
[1,]    4   16
[2,]    8   20
[3,]   12   24
# Block diagonal matrix:
kronecker(diag(1, 3), M)
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    4    0    0    0    0
 [2,]    2    5    0    0    0    0
 [3,]    3    6    0    0    0    0
 [4,]    0    0    1    4    0    0
 [5,]    0    0    2    5    0    0
 [6,]    0    0    3    6    0    0
 [7,]    0    0    0    0    1    4
 [8,]    0    0    0    0    2    5
 [9,]    0    0    0    0    3    6
# ask for dimnames

fred <- matrix(1:12, 3, 4, dimnames = list(LETTERS[1:3], LETTERS[4:7]))
bill <- c("happy" = 100, "sad" = 1000)
kronecker(fred, bill, make.dimnames = TRUE)
          D:   E:   F:    G:
A:happy  100  400  700  1000
A:sad   1000 4000 7000 10000
B:happy  200  500  800  1100
B:sad   2000 5000 8000 11000
C:happy  300  600  900  1200
C:sad   3000 6000 9000 12000
bill <- outer(bill, c("cat" = 3, "dog" = 4))
kronecker(fred, bill, make.dimnames = TRUE)
        D:cat D:dog E:cat E:dog F:cat F:dog G:cat G:dog
A:happy   300   400  1200  1600  2100  2800  3000  4000
A:sad    3000  4000 12000 16000 21000 28000 30000 40000
B:happy   600   800  1500  2000  2400  3200  3300  4400
B:sad    6000  8000 15000 20000 24000 32000 33000 44000
C:happy   900  1200  1800  2400  2700  3600  3600  4800
C:sad    9000 12000 18000 24000 27000 36000 36000 48000

[Package base version 4.2.3 Index]