MODULE transpose4; (*--------------------------------------------------------- File name: transpose4.pm Author: AG Date: 6/1/05 Problem: This program transposes 2x2 Matrices. Matrices are represented by two lines. In this example all matrices equal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 Example of Execution: Initial Matrices: ArrLine[0]: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 ArrLine[1]: 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 ArrLine[2]: 9 10 11 12 9 10 11 12 9 10 11 12 9 10 11 ArrLine[3]: 13 14 15 16 13 14 15 16 13 14 15 16 13 14 15 Transposed Matrices: ArrLine[0]: 1 5 9 13 1 5 9 13 1 5 9 13 1 5 9 ArrLine[1]: 2 6 10 14 2 6 10 14 2 6 10 14 2 6 10 ArrLine[2]: 3 7 11 15 3 7 11 15 3 7 11 15 3 7 11 ArrLine[3]: 4 8 12 16 4 8 12 16 4 8 12 16 4 8 12 Cycles: (1ID + 1 MOD + 4 Shifts + 3 Assignments)N/2 *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONST N=4; CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR ArrLine: ARRAY[0..(N-1)] OF chain OF INTEGER; i: INTEGER; (*----------------------------------------------------------- PROCEDURES ----------------------------------------------------------*) PROCEDURE swap(index1, index2, M: INTEGER); (* swap: This procedure swaps the first M elements of the second line with the last M elements of the first line. *) (*---------------------------------------------------------- PROCEDURE VARIABLES ----------------------------------------------------------*) (* *index1 and index2 are indices for the lines to be swaped. *M is the size of the matrix.(M should be equal to N unless it is intended to swap parts of the matrix *) VAR L0id, L5temp: chain OF INTEGER; k: INTEGER; (*---------------------------------------------------------- PROCEDURE MAIN ----------------------------------------------------------*) BEGIN (*prepare selection*) L0id:= (ID(chain)- 1) MOD M +1; (*L1: 1 2 1 2 1 2 *) (*L2: 3 4 3 4 3 4 *) (*L5temp: 0 0 0 0 0 0 *) M:= M DIV 2; FOR k:=1 TO M DO (*shift *) ArrLine[index2]:= MOVE.right(ArrLine[index2]); (* L2: 4 3 4 3 4 3 *) END; (*FOR*) IF L0id > M THEN L5temp:= ArrLine[index2]; (* L5temp: 0 3 0 3 0 3 *) ArrLine[index2]:= ArrLine[index1]; (* L2: 4 2 4 2 4 2 *) ArrLine[index1]:= L5temp; (* L1: 1 3 1 3 1 3 *) END; (* IF *) FOR k:=1 TO M DO (*shift back*) ArrLine[index2]:= MOVE.left(ArrLine[index2]); (* L2: 2 4 2 4 2 4 *) END; (*FOR*) END swap; (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (*Initialize data *) ArrLine[0]:= (ID(chain)-1) MOD 4 +1; ArrLine[1]:= ArrLine[0]+4; ArrLine[2]:= ArrLine[1]+4; ArrLine[3]:= ArrLine[2]+4; (* print initial matrices *) WriteString("Initial Matrices:"); WriteLn; FOR i:=0 TO (N-1) DO IF ID(chain) < 16 THEN WriteString("ArrLine["); WriteInt(i,0); WriteString("]: "); WriteInt(ArrLine[i], 2); WriteLn; END; (* display *) END; (*FOR*) swap(0,1,2); (* swap first two lines by M=2 *) swap(2,3,2); (* swap last two lines by M=2*) swap(0,2,4); (* swap first and third lines by M=4 *) swap(1,3,4); (* swap second an last line by M=4 *) (* Printing *) WriteString("Transposed Matrices:"); WriteLn; FOR i:=0 TO (N-1) DO IF ID(chain) < 16 THEN WriteString("ArrLine["); WriteInt(i,0); WriteString("]: "); WriteInt(ArrLine[i], 2); WriteLn; END; (* display *) END; (*FOR*) END transpose4.