home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / la / comm_mat.m next >
Text File  |  1999-12-24  |  3KB  |  108 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} {} comm_mat (@var{m}, @var{n})
  19. ## Return the commutation matrix
  20. ## @iftex
  21. ## @tex
  22. ##  $K_{m,n}$
  23. ## @end tex
  24. ## @end iftex
  25. ## @ifinfo
  26. ##  K(m,n)
  27. ## @end ifinfo
  28. ##  which is the unique
  29. ## @iftex
  30. ## @tex
  31. ##  $m n \times m n$
  32. ## @end tex
  33. ## @end iftex
  34. ## @ifinfo
  35. ##  @var{m}*@var{n} by @var{m}*@var{n}
  36. ## @end ifinfo
  37. ##  matrix such that
  38. ## @iftex
  39. ## @tex
  40. ##  $K_{m,n} \cdot {\rm vec} (A) = {\rm vec} (A^T)$
  41. ## @end tex
  42. ## @end iftex
  43. ## @ifinfo
  44. ##  @var{K}(@var{m},@var{n}) * vec (@var{A}) = vec (@var{A}')
  45. ## @end ifinfo
  46. ##  for all
  47. ## @iftex
  48. ## @tex
  49. ##  $m\times n$
  50. ## @end tex
  51. ## @end iftex
  52. ## @ifinfo
  53. ##  @var{m} 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. ## If only one argument @var{m} is given,
  66. ## @iftex
  67. ## @tex
  68. ##  $K_{m,m}$
  69. ## @end tex
  70. ## @end iftex
  71. ## @ifinfo
  72. ##  K(m,m)
  73. ## @end ifinfo
  74. ##  is returned.
  75. ## 
  76. ## See Magnus and Neudecker (1988), Matrix differential calculus with
  77. ## applications in stat and econometrics.
  78. ## @end deftypefn
  79.  
  80. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  81. ## Created: 8 May 1995
  82. ## Adapted-By: jwe
  83.  
  84. function k = comm_mat (m, n)
  85.   
  86.   if (nargin < 1 || nargin > 2)
  87.     usage ("comm_mat (m [, n])");
  88.   else
  89.     if (! (is_scal (m) && m == round (m) && m > 0))
  90.       error ("comm_mat: m must be a positive integer");
  91.     endif
  92.     if (nargin == 1)
  93.       n = m;
  94.     elseif (! (is_scal (n) && n == round (n) && n > 0))
  95.       error ("comm_mat: n must be a positive integer");
  96.     endif
  97.   endif
  98.   
  99.   ## It is clearly possible to make this a LOT faster!
  100.   k = zeros (m * n, m * n);
  101.   for i = 1 : m
  102.     for j = 1 : n
  103.       k ((i - 1) * n + j, (j - 1) * m + i) = 1;
  104.     endfor
  105.   endfor
  106.  
  107. endfunction
  108.