home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / electronic / rlab / TestMatrix / hadamard_r < prev    next >
Encoding:
Text File  |  1995-01-17  |  1.9 KB  |  74 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. // Synopsis:    Hadamard matrix.
  4.  
  5. // Syntax:      H = hadamard ( N )
  6.  
  7. // Description:
  8.  
  9. //      hadamard(N) is a Hadamard matrix of order N, that is, a matrix
  10. //      H with elements 1 or -1 such that H*H' = N*EYE(N).  An N-by-N
  11. //      Hadamard matrix with N>2 exists only if REM(N,4) = 0. This
  12. //      function handles only the cases where N, N/12 or N/20 is a
  13. //      power of 2. 
  14.  
  15. //      Reference:
  16. //       S.W. Golomb and L.D. Baumert, The search for Hadamard matrices,
  17. //       Amer. Math. Monthly, 70 (1963) pp. 12-17.
  18.  
  19. //      History:
  20. //        NJH (11/14/91), revised by CBM, 6/24/92,
  21. //        comment lines revised by NJH, August 1993.
  22.  
  23. //    This file is a translation of hadamard.m from version 2.0 of
  24. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  25. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  26.  
  27. // Dependencies
  28.    require hankel
  29.  
  30. //-------------------------------------------------------------------//
  31.  
  32. hadamard = function (n)
  33. {
  34.   local (n)
  35.  
  36.   tmp = frexp ([n, n/12, n/20]);
  37.   f = tmp.f; e = tmp.e;
  38.  
  39.   k = find (f == 1/2 && e > 0);
  40.   if (isempty (k))
  41.   {
  42.     error ("N, N/12 or N/20 must be a power of 2.");
  43.   }
  44.  
  45.   e = e[k] - 1;
  46.  
  47.   if (k == 1)           
  48.   {
  49.     // N = 1 * 2^e;
  50.     H = [1];
  51.   else if (k == 2) { 
  52.     // N = 12 * 2^e;
  53.     H = [ones(1,12); ones(11,1), ...
  54.          toeplitz ([-1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1], ...
  55.                    [-1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1])];
  56.   else if (k == 3) { 
  57.     // N = 20 * 2^e;
  58.     H = [ones(1,20); ones(19,1),   ...
  59.          hankel([-1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, ...
  60.                   1, 1, -1, -1, 1], ...
  61.                 [1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, ...
  62.                  1, 1, -1, -1])];
  63.   } } }
  64.  
  65.   //  Kronecker product construction.
  66.   for (i in 1:e)
  67.   {
  68.     H = [H,  H;
  69.          H, -H];
  70.   }
  71.  
  72.   return H;
  73. };
  74.