2540
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 ## General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
|
17 ## usage: commutation_matrix (m [, n]) |
|
18 ## |
|
19 ## Returns the commutation matrix K_{m,n} which is the unique m*n by |
|
20 ## m*n matrix such that K_{m,n} * vec (A) = vec (A') for all m by n |
|
21 ## matrices A. |
|
22 ## |
|
23 ## If only one argument m is given, K_{m,m} is returned. |
|
24 ## |
|
25 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
|
26 ## applications in statistics and econometrics. |
|
27 |
|
28 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
29 ## Created: 8 May 1995 |
|
30 ## Adapted-By: jwe |
|
31 |
|
32 function k = commutation_matrix (m, n) |
|
33 |
|
34 if (nargin < 1 || nargin > 2) |
|
35 usage ("commutation_matrix (m [, n])"); |
|
36 else |
|
37 if (! (is_scalar (m) && m == round (m) && m > 0)) |
|
38 error ("commutation_matrix: m must be a positive integer"); |
|
39 endif |
|
40 if (nargin == 1) |
|
41 n = m; |
|
42 elseif (! (is_scalar (n) && n == round (n) && n > 0)) |
|
43 error ("commutation_matrix: n must be a positive integer"); |
|
44 endif |
|
45 endif |
|
46 |
|
47 ## It is clearly possible to make this a LOT faster! |
|
48 k = zeros (m * n, m * n); |
|
49 for i = 1 : m |
|
50 for j = 1 : n |
|
51 k ((i - 1) * n + j, (j - 1) * m + i) = 1; |
|
52 endfor |
|
53 endfor |
|
54 |
|
55 endfunction |