Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/duplication_matrix.m @ 10023:73fc43e01f4c
allow issquare on arbitrary data
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 25 Dec 2009 22:20:33 +0100 |
parents | f0c3d3fc4903 |
children | 1740012184f9 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2005, 2006, 2007, 2009 |
7017 | 2 ## Kurt Hornik |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
2540 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
2540 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2540 | 19 |
3426 | 20 ## -*- texinfo -*- |
3321 | 21 ## @deftypefn {Function File} {} duplication_matrix (@var{n}) |
22 ## Return the duplication matrix | |
23 ## @tex | |
24 ## $D_n$ | |
25 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
26 ## @ifnottex |
3499 | 27 ## @math{Dn} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
28 ## @end ifnottex |
3321 | 29 ## which is the unique |
30 ## @tex | |
31 ## $n^2 \times n(n+1)/2$ | |
32 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
33 ## @ifnottex |
3499 | 34 ## @math{n^2} by @math{n*(n+1)/2} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
35 ## @end ifnottex |
3321 | 36 ## matrix such that |
37 ## @tex | |
38 ## $D_n * {\rm vech} (A) = {\rm vec} (A)$ | |
39 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
40 ## @ifnottex |
3499 | 41 ## @math{Dn vech (A) = vec (A)} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
42 ## @end ifnottex |
3321 | 43 ## for all symmetric |
44 ## @tex | |
45 ## $n \times n$ | |
46 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
47 ## @ifnottex |
3499 | 48 ## @math{n} by @math{n} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
49 ## @end ifnottex |
3321 | 50 ## matrices |
51 ## @tex | |
52 ## $A$. | |
53 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
54 ## @ifnottex |
3499 | 55 ## @math{A}. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
56 ## @end ifnottex |
3426 | 57 ## |
2540 | 58 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
59 ## applications in statistics and econometrics. | |
3321 | 60 ## @end deftypefn |
2540 | 61 |
5428 | 62 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2540 | 63 ## Created: 8 May 1995 |
64 ## Adapged-By: jwe | |
65 | |
66 function d = duplication_matrix (n) | |
67 | |
68 if (nargin != 1) | |
6046 | 69 print_usage (); |
2540 | 70 endif |
71 | |
4030 | 72 if (! (isscalar (n) && n == round (n) && n > 0)) |
2540 | 73 error ("duplication_matrix: n must be a positive integer"); |
74 endif | |
75 | |
76 d = zeros (n * n, n * (n + 1) / 2); | |
77 | |
78 ## It is clearly possible to make this a LOT faster! | |
79 count = 0; | |
80 for j = 1 : n | |
81 d ((j - 1) * n + j, count + j) = 1; | |
82 for i = (j + 1) : n | |
83 d ((j - 1) * n + i, count + i) = 1; | |
84 d ((i - 1) * n + j, count + i) = 1; | |
85 endfor | |
86 count = count + n - j; | |
87 endfor | |
88 | |
89 endfunction |