Exercise 6.2: Relaxation Function

Given: a two-dimensional array, A, of real values
Compose: a function of A that performs one relaxation step, returning the new array whose values at each position [i, j] are the average of the values from A of that position and that position's eight nearest neighbors, [i+/-1, j+/-1].
Note: Keep the edges of the input system constant, relax only the elements of the system having eight neighbors, and return only the relaxed subsystem. the portion that has been relaxed.
type OneDim = array [ real ];
type TwoDim = array [ OneDim ];
 
function main( A : TwoDim returns TwoDim )
 
for i in array_liml(A)   +1, array_limh(A)   -1 cross
    j in array_liml(A[i])+1, array_limh(A[i])-1
   avg := (A[i,j]      + A[i, j-1] + A[i, j+1] +
           A[i+1, j-1] + A[i+1, j] + A[i+1, j+1] +
           A[i-1, j-1] + A[i-1, j] + A[i-1, j+1]) / 9.0
returns array of avg
end for
 
end function



Previous Section