1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} commutation_matrix (@var{m}, @var{n})
21 ## Return the commutation matrix
28 ## which is the unique
33 ## @var{m}*@var{n} by @var{m}*@var{n}
37 ## $K_{m,n} \cdot {\rm vec} (A) = {\rm vec} (A^T)$
40 ## @math{K(m,n) * vec(A) = vec(A')}
47 ## @math{m} by @math{n}
57 ## If only one argument @var{m} is given,
66 ## See Magnus and Neudecker (1988), @cite{Matrix Differential Calculus with
67 ## Applications in Statistics and Econometrics.}
70 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
71 ## Created: 8 May 1995
74 function k = commutation_matrix (m, n)
76 if (nargin < 1 || nargin > 2)
79 if (! (isscalar (m) && m == fix (m) && m > 0))
80 error ("commutation_matrix: M must be a positive integer");
84 elseif (! (isscalar (n) && n == fix (n) && n > 0))
85 error ("commutation_matrix: N must be a positive integer");
89 ## It is clearly possible to make this a LOT faster!
90 k = zeros (m * n, m * n);
93 k ((i - 1) * n + j, (j - 1) * m + i) = 1;
100 %! c = commutation_matrix(1,1);
107 %! c = commutation_matrix(3,5);
114 %! c = commutation_matrix(4,6);
117 %!error commutation_matrix(0,0);
118 %!error commutation_matrix(1,0);
119 %!error commutation_matrix(0,1);