2540
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
2540
|
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. |
3426
|
7 ## |
2540
|
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 |
3426
|
11 ## General Public License for more details. |
|
12 ## |
2540
|
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 |
3321
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} commutation_matrix (@var{m}, @var{n}) |
|
19 ## Return the commutation matrix |
|
20 ## @iftex |
|
21 ## @tex |
|
22 ## $K_{m,n}$ |
|
23 ## @end tex |
|
24 ## @end iftex |
|
25 ## @ifinfo |
|
26 ## K(m,n) |
|
27 ## @end ifinfo |
|
28 ## which is the unique |
|
29 ## @iftex |
|
30 ## @tex |
|
31 ## $m n \times m n$ |
|
32 ## @end tex |
|
33 ## @end iftex |
|
34 ## @ifinfo |
|
35 ## @var{m}*@var{n} by @var{m}*@var{n} |
|
36 ## @end ifinfo |
|
37 ## matrix such that |
|
38 ## @iftex |
|
39 ## @tex |
|
40 ## $K_{m,n} \cdot {\rm vec} (A) = {\rm vec} (A^T)$ |
|
41 ## @end tex |
|
42 ## @end iftex |
|
43 ## @ifinfo |
|
44 ## @var{K}(@var{m},@var{n}) * vec (@var{A}) = vec (@var{A}') |
|
45 ## @end ifinfo |
|
46 ## for all |
|
47 ## @iftex |
|
48 ## @tex |
|
49 ## $m\times n$ |
|
50 ## @end tex |
|
51 ## @end iftex |
|
52 ## @ifinfo |
|
53 ## @var{m} by @var{n} |
|
54 ## @end ifinfo |
|
55 ## matrices |
|
56 ## @iftex |
|
57 ## @tex |
|
58 ## $A$. |
|
59 ## @end tex |
|
60 ## @end iftex |
|
61 ## @ifinfo |
|
62 ## @var{A}. |
|
63 ## @end ifinfo |
3426
|
64 ## |
3321
|
65 ## If only one argument @var{m} is given, |
|
66 ## @iftex |
|
67 ## @tex |
|
68 ## $K_{m,m}$ |
|
69 ## @end tex |
|
70 ## @end iftex |
|
71 ## @ifinfo |
|
72 ## K(m,m) |
|
73 ## @end ifinfo |
|
74 ## is returned. |
3426
|
75 ## |
2540
|
76 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
|
77 ## applications in statistics and econometrics. |
3321
|
78 ## @end deftypefn |
2540
|
79 |
|
80 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
81 ## Created: 8 May 1995 |
|
82 ## Adapted-By: jwe |
|
83 |
|
84 function k = commutation_matrix (m, n) |
3426
|
85 |
2540
|
86 if (nargin < 1 || nargin > 2) |
|
87 usage ("commutation_matrix (m [, n])"); |
|
88 else |
|
89 if (! (is_scalar (m) && m == round (m) && m > 0)) |
|
90 error ("commutation_matrix: m must be a positive integer"); |
|
91 endif |
|
92 if (nargin == 1) |
|
93 n = m; |
|
94 elseif (! (is_scalar (n) && n == round (n) && n > 0)) |
|
95 error ("commutation_matrix: n must be a positive integer"); |
|
96 endif |
|
97 endif |
3426
|
98 |
2540
|
99 ## It is clearly possible to make this a LOT faster! |
|
100 k = zeros (m * n, m * n); |
|
101 for i = 1 : m |
|
102 for j = 1 : n |
|
103 k ((i - 1) * n + j, (j - 1) * m + i) = 1; |
|
104 endfor |
|
105 endfor |
|
106 |
|
107 endfunction |