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 |
3426
|
20 ## -*- texinfo -*- |
3321
|
21 ## @deftypefn {Function File} {} duplication_matrix (@var{n}) |
|
22 ## Return the duplication matrix |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $D_n$ |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
3499
|
29 ## @math{Dn} |
3321
|
30 ## @end ifinfo |
|
31 ## which is the unique |
|
32 ## @iftex |
|
33 ## @tex |
|
34 ## $n^2 \times n(n+1)/2$ |
|
35 ## @end tex |
|
36 ## @end iftex |
|
37 ## @ifinfo |
3499
|
38 ## @math{n^2} by @math{n*(n+1)/2} |
3321
|
39 ## @end ifinfo |
|
40 ## matrix such that |
|
41 ## @iftex |
|
42 ## @tex |
|
43 ## $D_n * {\rm vech} (A) = {\rm vec} (A)$ |
|
44 ## @end tex |
|
45 ## @end iftex |
|
46 ## @ifinfo |
3499
|
47 ## @math{Dn vech (A) = vec (A)} |
3321
|
48 ## @end ifinfo |
|
49 ## for all symmetric |
|
50 ## @iftex |
|
51 ## @tex |
|
52 ## $n \times n$ |
|
53 ## @end tex |
|
54 ## @end iftex |
|
55 ## @ifinfo |
3499
|
56 ## @math{n} 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 ## |
2540
|
68 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
|
69 ## applications in statistics and econometrics. |
3321
|
70 ## @end deftypefn |
2540
|
71 |
5428
|
72 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2540
|
73 ## Created: 8 May 1995 |
|
74 ## Adapged-By: jwe |
|
75 |
|
76 function d = duplication_matrix (n) |
|
77 |
|
78 if (nargin != 1) |
6046
|
79 print_usage (); |
2540
|
80 endif |
|
81 |
4030
|
82 if (! (isscalar (n) && n == round (n) && n > 0)) |
2540
|
83 error ("duplication_matrix: n must be a positive integer"); |
|
84 endif |
|
85 |
|
86 d = zeros (n * n, n * (n + 1) / 2); |
|
87 |
|
88 ## It is clearly possible to make this a LOT faster! |
|
89 count = 0; |
|
90 for j = 1 : n |
|
91 d ((j - 1) * n + j, count + j) = 1; |
|
92 for i = (j + 1) : n |
|
93 d ((j - 1) * n + i, count + i) = 1; |
|
94 d ((i - 1) * n + j, count + i) = 1; |
|
95 endfor |
|
96 count = count + n - j; |
|
97 endfor |
|
98 |
|
99 endfunction |