home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / misc / icalc.lha / ICalc / Scripts / array.ic < prev    next >
Encoding:
Text File  |  1992-08-03  |  1.2 KB  |  58 lines

  1. # utility routines for arrays
  2. #
  3. # nb: routines assume array base is 1
  4. # mws, February 1992
  5.  
  6.  
  7. # fill an array with a formula in n evaluated for index n
  8. # e.g.    fill(a,1)    fills the array with ones
  9. #    fill(a,n*n)    then a[1] = 1, a[2] = 4, a[3] = 9 etc.
  10. func fill(@a,~nexpr) = {
  11.     for (n = sizeof(a); n > 0; n-=1)    # n is GLOBAL
  12.         a[n] = nexpr;
  13. }
  14.  
  15.  
  16. # map a function in x to each array entry in turn
  17. # e.g.    fill(a,n); map(a,fact(x)) to produce factorial table
  18. func map(@a,~xexpr) = {
  19.     local j
  20.  
  21.     for (j = sizeof(a); j > 0; j-=1)
  22.     {
  23.         x = a[j]        # x is GLOBAL
  24.         a[j] = xexpr
  25.     }
  26. }
  27.  
  28.  
  29. # copy els of one array to another
  30. # if dest is not large enough, it is resized to accomodate source array.
  31. func copy(@to,@from) = {
  32.     local j
  33.  
  34.     j = sizeof(from)
  35.     if (sizeof(to) < j) resize(to,j)
  36.     for (; j > 0; j-=1)
  37.         to[j] = from[j]
  38. }
  39.  
  40.  
  41. # fills arrays xa with equally-spaced points from x1 to x2,
  42. # and ya with xexpr evaluated at these points.
  43. # eg. xa, ya of size 20, then
  44. #    tabulate(xa,ya,1,20,fact(x)) creates factorial table.
  45. # If sizes of xa and ya different, uses minimum size.
  46. func tabulate(@xa,@ya,x1,x2,~xexpr) = {
  47.     local n,j,dx
  48.  
  49.     n = min(sizeof(xa),sizeof(ya))
  50.     dx = (x2-x1)/(n-1)
  51.     x = x1        # x is global (for xexpr)
  52.     for (j = 1; j <= n; j+=1) {
  53.         xa[j] = x
  54.         ya[j] = xexpr
  55.         x += dx
  56.     }
  57. }
  58.