home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / linear-algebra / duplication_matrix.m < prev    next >
Text File  |  1998-11-10  |  2KB  |  54 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage: duplication_matrix (n)
  18. ## 
  19. ## Returns the duplication matrix D_n which is the unique n^2 by
  20. ## n*(n+1)/2 matrix such that D_n * vech (A) = vec (A) for all
  21. ## symmetric n by n matrices A.
  22. ## 
  23. ## See Magnus and Neudecker (1988), Matrix differential calculus with
  24. ## applications in statistics and econometrics.
  25.  
  26. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Created: 8 May 1995
  28. ## Adapged-By: jwe
  29.  
  30. function d = duplication_matrix (n)
  31.  
  32.   if (nargin != 1)
  33.     usage ("duplication_matrix (n)");
  34.   endif
  35.  
  36.   if (! (is_scalar (n) && n == round (n) && n > 0))
  37.     error ("duplication_matrix: n must be a positive integer");
  38.   endif
  39.  
  40.   d = zeros (n * n, n * (n + 1) / 2);
  41.  
  42.   ## It is clearly possible to make this a LOT faster!
  43.   count = 0;
  44.   for j = 1 : n
  45.     d ((j - 1) * n + j, count + j) = 1;
  46.     for i = (j + 1) : n
  47.       d ((j - 1) * n + i, count + i) = 1;
  48.       d ((i - 1) * n + j, count + i) = 1;
  49.     endfor
  50.     count = count + n - j;
  51.   endfor
  52.  
  53. endfunction
  54.