home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / SPECMAT.DI$ / PASCAL.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.0 KB  |  46 lines

  1. function P = pascal(n,k)
  2. %PASCAL Pascal matrix.
  3. %    PASCAL(N) is the Pascal matrix of order N: a symmetric positive
  4. %    definite matrix with integer entries, made up from Pascal's
  5. %    triangle.  Its inverse has integer entries.
  6. %    PASCAL(N,1) is the lower triangular Cholesky factor (up to signs
  7. %    of columns) of the Pascal matrix.  It is involutary (is its own
  8. %    inverse).
  9. %    PASCAL(N,2) is a transposed and permuted version of PASCAL(N,1)
  10. %    which is a cube root of the identity.
  11.  
  12. %    Author: N.J. Higham 6-23-89
  13. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  14.  
  15.  
  16. if nargin == 1
  17.     k = 0;
  18. end
  19.  
  20. if max(size(k)) ~= 1
  21.   error('Second argument must be a scalar.')
  22. end
  23.  
  24. P = diag((-1).^[0:n-1]);
  25. P(:,1) = ones(n,1);
  26.  
  27. % Generate the Pascal Cholesky factor (up to signs).
  28. for j=2:n-1
  29.     for i=j+1:n
  30.         P(i,j) = P(i-1,j) - P(i-1,j-1);
  31.     end
  32. end
  33.  
  34. if k == 0
  35.     P = P*P';
  36. elseif k == 2
  37.     P = P';
  38.     P = P(n:-1:1,:);
  39.     for i=1:n-1
  40.         P(i,:) = -P(i,:);
  41.         P(:,i) = -P(:,i);
  42.     end
  43.     if n/2 == round(n/2), P=-P; end
  44. end
  45.  
  46.