2540
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
2540
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2540
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2540
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2540
|
19 |
3321
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} commutation_matrix (@var{m}, @var{n}) |
|
22 ## Return the commutation matrix |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $K_{m,n}$ |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
|
29 ## K(m,n) |
|
30 ## @end ifinfo |
|
31 ## which is the unique |
|
32 ## @iftex |
|
33 ## @tex |
|
34 ## $m n \times m n$ |
|
35 ## @end tex |
|
36 ## @end iftex |
|
37 ## @ifinfo |
|
38 ## @var{m}*@var{n} by @var{m}*@var{n} |
|
39 ## @end ifinfo |
|
40 ## matrix such that |
|
41 ## @iftex |
|
42 ## @tex |
|
43 ## $K_{m,n} \cdot {\rm vec} (A) = {\rm vec} (A^T)$ |
|
44 ## @end tex |
|
45 ## @end iftex |
|
46 ## @ifinfo |
3499
|
47 ## @math{K(m,n) * vec(A) = vec(A')} |
3321
|
48 ## @end ifinfo |
|
49 ## for all |
|
50 ## @iftex |
|
51 ## @tex |
|
52 ## $m\times n$ |
|
53 ## @end tex |
|
54 ## @end iftex |
|
55 ## @ifinfo |
3499
|
56 ## @math{m} by @math{n} |
3321
|
57 ## @end ifinfo |
|
58 ## matrices |
|
59 ## @iftex |
|
60 ## @tex |
|
61 ## $A$. |
|
62 ## @end tex |
|
63 ## @end iftex |
|
64 ## @ifinfo |
3499
|
65 ## @math{A}. |
3321
|
66 ## @end ifinfo |
3426
|
67 ## |
3321
|
68 ## If only one argument @var{m} is given, |
|
69 ## @iftex |
|
70 ## @tex |
|
71 ## $K_{m,m}$ |
|
72 ## @end tex |
|
73 ## @end iftex |
|
74 ## @ifinfo |
3499
|
75 ## @math{K(m,m)} |
3321
|
76 ## @end ifinfo |
|
77 ## is returned. |
3426
|
78 ## |
2540
|
79 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
|
80 ## applications in statistics and econometrics. |
3321
|
81 ## @end deftypefn |
2540
|
82 |
5428
|
83 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2540
|
84 ## Created: 8 May 1995 |
|
85 ## Adapted-By: jwe |
|
86 |
|
87 function k = commutation_matrix (m, n) |
3426
|
88 |
2540
|
89 if (nargin < 1 || nargin > 2) |
3456
|
90 usage ("commutation_matrix (m, n)"); |
2540
|
91 else |
4030
|
92 if (! (isscalar (m) && m == round (m) && m > 0)) |
2540
|
93 error ("commutation_matrix: m must be a positive integer"); |
|
94 endif |
|
95 if (nargin == 1) |
|
96 n = m; |
4030
|
97 elseif (! (isscalar (n) && n == round (n) && n > 0)) |
2540
|
98 error ("commutation_matrix: n must be a positive integer"); |
|
99 endif |
|
100 endif |
3426
|
101 |
2540
|
102 ## It is clearly possible to make this a LOT faster! |
|
103 k = zeros (m * n, m * n); |
|
104 for i = 1 : m |
|
105 for j = 1 : n |
|
106 k ((i - 1) * n + j, (j - 1) * m + i) = 1; |
|
107 endfor |
|
108 endfor |
|
109 |
|
110 endfunction |