MODULE scalarproduct; (*--------------------------------------------------------- File name: scalarproduct.pm Author: AG Date: 5/23/05 Problem: This program computes the product of every element between two vectors of the same size. Vectors are represented by chains. Then it computes the sum of all products. 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 13 14 15 L2: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Scalar Product L1 * L2 = 120 Cycles: 1 multiplication + 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:= 15; 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; (*multiply *) Res:= REDUCE.SUM(L3); (*sum of all products *) (*printing*) WriteString("L1: "); WriteInt(L1, 2);WriteLn; WriteString("L2: "); WriteInt(L2, 2);WriteLn; WriteString(" Scalar Product L1 * L2 = " ); WriteInt(Res, 2); (*Only prints the activated cells *) END; (*IF *) END scalarproduct.