MODULE absDiff0; (*--------------------------------------------------------- File name: absDiff.pm Author: AG Date: 5/19/05 Problem: This program computes the absolute difference on every element between two vectors of the same size. Vectors are represented by chains. Then it computes the sum of all the differences. In this example v1 is ID and v2 is all 1's Sum( | v1[i] - v2[i] |) Example of Execution: L1: 1 2 3 4 5 6 7 8 9 10 11 12 ...1000 L2: 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 1 The sum of the absolute value of L1 - L2 = 499500 Cycles: 1 substraction + 1 ABS + 1 REDUCE. *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR L1, L2, L3: chain OF INTEGER; Res, RightLimit: INTEGER; (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (*initialize data *) RightLimit:= 1000; L1:= ID(chain); (* L1 = 1 2 3 4 5 6.. *) L2:= 1; (* L2 = 1 1 1 1 1 1.. *) IF L1 <= RightLimit THEN L3:= L1-L2; (*compute difference *) L3:= ABS(L3); (*take absolute value *) Res:= REDUCE.SUM(L3); (*sum of all *) (*printing*) WriteString("L1: "); WriteInt(L1, 2);WriteLn; WriteString("L2: "); WriteInt(L2, 2);WriteLn; WriteString(" The sum of the absolute value of L1 - L2 = " ); WriteInt(Res, 2); (*Only prints the activated cells *) END; (*IF *) END absDiff0.