R does not have this function by default, so I wrote a faster routine K() to create a commutation matrix.We always use the notation of commutation matrix on the paper. But when we put it into practice, we typically need the result of a dense matrix by pre- or post-multiplied by a commutation matrix. i.e., KX or YK., where K is the commutation matrix and X, Y are dense matrices. This could be a disaster when the dimensions are big in the usual way of multiplication. And I found a very easy and efficient way of doing this.
Please visit the git page for the updated code