home *** CD-ROM | disk | FTP | other *** search
- # utility routines for arrays
- #
- # nb: routines assume array base is 1
- # mws, February 1992
-
-
- # fill an array with a formula in n evaluated for index n
- # e.g. fill(a,1) fills the array with ones
- # fill(a,n*n) then a[1] = 1, a[2] = 4, a[3] = 9 etc.
- func fill(@a,~nexpr) = {
- for (n = sizeof(a); n > 0; n-=1) # n is GLOBAL
- a[n] = nexpr;
- }
-
-
- # map a function in x to each array entry in turn
- # e.g. fill(a,n); map(a,fact(x)) to produce factorial table
- func map(@a,~xexpr) = {
- local j
-
- for (j = sizeof(a); j > 0; j-=1)
- {
- x = a[j] # x is GLOBAL
- a[j] = xexpr
- }
- }
-
-
- # copy els of one array to another
- # if dest is not large enough, it is resized to accomodate source array.
- func copy(@to,@from) = {
- local j
-
- j = sizeof(from)
- if (sizeof(to) < j) resize(to,j)
- for (; j > 0; j-=1)
- to[j] = from[j]
- }
-
-
- # fills arrays xa with equally-spaced points from x1 to x2,
- # and ya with xexpr evaluated at these points.
- # eg. xa, ya of size 20, then
- # tabulate(xa,ya,1,20,fact(x)) creates factorial table.
- # If sizes of xa and ya different, uses minimum size.
- func tabulate(@xa,@ya,x1,x2,~xexpr) = {
- local n,j,dx
-
- n = min(sizeof(xa),sizeof(ya))
- dx = (x2-x1)/(n-1)
- x = x1 # x is global (for xexpr)
- for (j = 1; j <= n; j+=1) {
- xa[j] = x
- ya[j] = xexpr
- x += dx
- }
- }
-