home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / la / dupl_mat.m < prev    next >
Text File  |  1999-12-24  |  2KB  |  97 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. ## -*- texinfo -*- 
  18. ## @deftypefn {Function File} {} dupl_mat (@var{n})
  19. ## Return the duplication matrix
  20. ## @iftex
  21. ## @tex
  22. ##  $D_n$
  23. ## @end tex
  24. ## @end iftex
  25. ## @ifinfo
  26. ##  @var{D}_@var{n}
  27. ## @end ifinfo
  28. ##  which is the unique
  29. ## @iftex
  30. ## @tex
  31. ##  $n^2 \times n(n+1)/2$
  32. ## @end tex
  33. ## @end iftex
  34. ## @ifinfo
  35. ##  @var{n}^2 by @var{n}*(@var{n}+1)/2
  36. ## @end ifinfo
  37. ##  matrix such that
  38. ## @iftex
  39. ## @tex
  40. ##  $D_n * {\rm vech} (A) = {\rm vec} (A)$
  41. ## @end tex
  42. ## @end iftex
  43. ## @ifinfo
  44. ##  @var{D}_@var{n} \cdot vech (@var{A}) = vec (@var{A})
  45. ## @end ifinfo
  46. ##  for all symmetric
  47. ## @iftex
  48. ## @tex
  49. ##  $n \times n$
  50. ## @end tex
  51. ## @end iftex
  52. ## @ifinfo
  53. ##  @var{n} by @var{n}
  54. ## @end ifinfo
  55. ##  matrices
  56. ## @iftex
  57. ## @tex
  58. ##  $A$.
  59. ## @end tex
  60. ## @end iftex
  61. ## @ifinfo
  62. ##  @var{A}.
  63. ## @end ifinfo
  64. ## 
  65. ## See Magnus and Neudecker (1988), Matrix differential calculus with
  66. ## applications in stat and econometrics.
  67. ## @end deftypefn
  68.  
  69. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  70. ## Created: 8 May 1995
  71. ## Adapged-By: jwe
  72.  
  73. function d = dupl_mat (n)
  74.  
  75.   if (nargin != 1)
  76.     usage ("dupl_mat (n)");
  77.   endif
  78.  
  79.   if (! (is_scal (n) && n == round (n) && n > 0))
  80.     error ("dupl_mat: n must be a positive integer");
  81.   endif
  82.  
  83.   d = zeros (n * n, n * (n + 1) / 2);
  84.  
  85.   ## It is clearly possible to make this a LOT faster!
  86.   count = 0;
  87.   for j = 1 : n
  88.     d ((j - 1) * n + j, count + j) = 1;
  89.     for i = (j + 1) : n
  90.       d ((j - 1) * n + i, count + i) = 1;
  91.       d ((i - 1) * n + j, count + i) = 1;
  92.     endfor
  93.     count = count + n - j;
  94.   endfor
  95.  
  96. endfunction
  97.