Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a block circulant matrix in R

I am trying to create a block circulant matrix in R. The structure of a block circulant matrix is given below.

C0 C1 ... Cn-1
Cn-1 C0 C1 ... Cn-2
Cn-2 Cn-1 .... Cn-3

and so on

I have the blocks

C0 .... Cn-1

What is the easiest way to create the matrix. Is there a function already available?

like image 499
user34790 Avatar asked Jul 04 '13 03:07

user34790


1 Answers

Thanks for a challenging question! Here is a solution summing kronecker products of your matrices with sub- and super-diagonals.

Sample data, a list of matrices:

C <- lapply(1:3, matrix, nrow = 2, ncol = 2)

My solution:

bcm <- function(C) {
   require(Matrix)
   n <- length(C)
   Reduce(`+`, lapply((-n+1):(n-1),
                      function(i) kronecker(as.matrix(bandSparse(n, n, -i)),
                                            C[[1 + (i %% n)]])))
}
bcm(C)

#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    1    1    3    3    2    2
# [2,]    1    1    3    3    2    2
# [3,]    2    2    1    1    3    3
# [4,]    2    2    1    1    3    3
# [5,]    3    3    2    2    1    1
# [6,]    3    3    2    2    1    1
like image 70
flodel Avatar answered Nov 02 '22 04:11

flodel