home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / mac / SRI / General Interest / Programming / J / Examples / numeric.js < prev    next >
Encoding:
Text File  |  1993-12-02  |  947 b   |  30 lines  |  [TEXT/????]

  1.    NB. numeric utilities
  2.    NB. 
  3.    NB.  baserep         y. in base x.
  4.    NB.  clean           clean y. to tolerance of 1e_10
  5.    NB.  cleanto         x. clean y. where x. is tolerance
  6.    NB.  groupndx        group indices of y. in x.
  7.    NB.  range           y. is a;b;c  range from a to b {in steps of c}
  8.    NB.  round           round y. to nearest x.
  9.    
  10.    t=. 'baserep clean cleanto groupndx range round'
  11.    SCRIPTNAMES=: t
  12.    
  13.    baserep=:   (>:@<.@^. # [) #: ]
  14.    cleanto=:   j./"1@((] * (<: |)) +.)
  15.    clean=:     1e_10 & cleanto f.
  16.    round=:     ((%&) [.) (<.@+&0.5&.)
  17.    
  18.    NB. groupndx
  19.    NB. Return group indices of elements of y.
  20.    NB. x. is an integer vector of the starting numbers of each group,
  21.    NB. assumed to be in ascending order.
  22.    NB. e.g.  0 0 0 1 1 1 2 2  =  0 3 6 groupndx  i.8   
  23.    groupndx=: '' : '<: (#x.) }. (+/\r<#x.) r } r=. /: x.,y.'
  24.    
  25.    range=: 0 : 0
  26. 'xyn'=. 3{.y.,1
  27. s=. (x<y) { _1 1
  28. x+s*n*i.>:<.n%~|y-x
  29. )
  30.