]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/commutation_matrix.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / commutation_matrix.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} commutation_matrix (@var{m}, @var{n})
21 ## Return the commutation matrix
22 ## @tex
23 ##  $K_{m,n}$
24 ## @end tex
25 ## @ifnottex
26 ##  K(m,n)
27 ## @end ifnottex
28 ##  which is the unique
29 ## @tex
30 ##  $m n \times m n$
31 ## @end tex
32 ## @ifnottex
33 ##  @var{m}*@var{n} by @var{m}*@var{n}
34 ## @end ifnottex
35 ##  matrix such that
36 ## @tex
37 ##  $K_{m,n} \cdot {\rm vec} (A) = {\rm vec} (A^T)$
38 ## @end tex
39 ## @ifnottex
40 ##  @math{K(m,n) * vec(A) = vec(A')}
41 ## @end ifnottex
42 ##  for all
43 ## @tex
44 ##  $m\times n$
45 ## @end tex
46 ## @ifnottex
47 ##  @math{m} by @math{n}
48 ## @end ifnottex
49 ##  matrices
50 ## @tex
51 ##  $A$.
52 ## @end tex
53 ## @ifnottex
54 ##  @math{A}.
55 ## @end ifnottex
56 ##
57 ## If only one argument @var{m} is given,
58 ## @tex
59 ##  $K_{m,m}$
60 ## @end tex
61 ## @ifnottex
62 ##  @math{K(m,m)}
63 ## @end ifnottex
64 ##  is returned.
65 ##
66 ## See Magnus and Neudecker (1988), @cite{Matrix Differential Calculus with
67 ## Applications in Statistics and Econometrics.}
68 ## @end deftypefn
69
70 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
71 ## Created: 8 May 1995
72 ## Adapted-By: jwe
73
74 function k = commutation_matrix (m, n)
75
76   if (nargin < 1 || nargin > 2)
77     print_usage ();
78   else
79     if (! (isscalar (m) && m == fix (m) && m > 0))
80       error ("commutation_matrix: M must be a positive integer");
81     endif
82     if (nargin == 1)
83       n = m;
84     elseif (! (isscalar (n) && n == fix (n) && n > 0))
85       error ("commutation_matrix: N must be a positive integer");
86     endif
87   endif
88
89   ## It is clearly possible to make this a LOT faster!
90   k = zeros (m * n, m * n);
91   for i = 1 : m
92     for j = 1 : n
93       k ((i - 1) * n + j, (j - 1) * m + i) = 1;
94     endfor
95   endfor
96
97 endfunction
98
99 %!test
100 %! c = commutation_matrix(1,1);
101 %! assert(c,1);
102
103 %!test
104 %! A = rand(3,5);
105 %! vc = vec(A);
106 %! vr = vec(A');
107 %! c = commutation_matrix(3,5);
108 %! assert(c*vc,vr);
109
110 %!test
111 %! A = rand(4,6);
112 %! vc = vec(A);
113 %! vr = vec(A');
114 %! c = commutation_matrix(4,6);
115 %! assert(c*vc,vr);
116
117 %!error commutation_matrix(0,0);
118 %!error commutation_matrix(1,0);
119 %!error commutation_matrix(0,1);