home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / electronic / rlab / TestMatrix / chop_r < prev    next >
Encoding:
Text File  |  1994-12-20  |  1014 b   |  39 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. // Synopsis:    Round matrix elements.
  4.  
  5. // Syntax:    C = chop ( X , t ) 
  6.  
  7. // Description:
  8.  
  9. //    C is the matrix obtained by rounding the elements of X to t
  10. //    significant binary places. Default is t = 24, corresponding to
  11. //    IEEE single precision. 
  12.  
  13. //    This file is a translation of chop.m from version 2.0 of
  14. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  15. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  16.  
  17. //-------------------------------------------------------------------//
  18.  
  19. chop = function ( x , t )
  20. {
  21.   local (x, t)
  22.  
  23.   if (!exist(t)) { t = 24; }
  24.   m = x.nr; n = x.nc;
  25.  
  26.   //  Use the representation:
  27.   //  x[i;j] = 2^e[i;j] * .d[1]d[2]...d[s] * sign(x[i;j])
  28.  
  29.   //  On the next line `+(x==0)' avoids passing a zero argument to LOG, which
  30.   //  would cause a warning message to be generated.
  31.  
  32.   y = abs(x) + (x==0);
  33.   e = floor(log(y)./log(2) + 1);
  34.   p = (2.*ones(m,n)).^(t-e);
  35.   c = round(p.*x)./p;
  36.  
  37.   return c;
  38. };
  39.