MODULE transpose8; (*--------------------------------------------------------- File name: transpose8.pm Author: AG Date: 6/2/05 Problem: This program transposes 2x2 Matrices. Matrices are represented by two lines. In this example all matrices equal. Example of Execution: Initial Matrices: ArrLine[0]: 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 ArrLine[1]: 9 10 11 12 13 14 15 16 9 10 11 12 13 14 15 ArrLine[2]: 17 18 19 20 21 22 23 24 17 18 19 20 21 22 23 ArrLine[3]: 25 26 27 28 29 30 31 32 25 26 27 28 29 30 31 ArrLine[4]: 33 34 35 36 37 38 39 40 33 34 35 36 37 38 39 ArrLine[5]: 41 42 43 44 45 46 47 48 41 42 43 44 45 46 47 ArrLine[6]: 49 50 51 52 53 54 55 56 49 50 51 52 53 54 55 ArrLine[7]: 57 58 59 60 61 62 63 64 57 58 59 60 61 62 63 Transposed Matrices: ArrLine[0]: 1 9 17 25 33 41 49 57 1 9 17 25 33 41 49 ArrLine[1]: 2 10 18 26 34 42 50 58 2 10 18 26 34 42 50 ArrLine[2]: 3 11 19 27 35 43 51 59 3 11 19 27 35 43 51 ArrLine[3]: 4 12 20 28 36 44 52 60 4 12 20 28 36 44 52 ArrLine[4]: 5 13 21 29 37 45 53 61 5 13 21 29 37 45 53 ArrLine[5]: 6 14 22 30 38 46 54 62 6 14 22 30 38 46 54 ArrLine[6]: 7 15 23 31 39 47 55 63 7 15 23 31 39 47 55 ArrLine[7]: 8 16 24 32 40 48 56 64 8 16 24 32 40 48 56 Cycles: (1ID + 1 MOD + 4 Shifts + 3 Assignments)N/2 *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONST N=8; 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: Swaps the last half of ArrLine[index1] with the first half of ArrLine[index2] Example:for N = 4; M = 4; ArrLine[index1]: 1 2 3 4 1 2 3 4 ... ArrLine[index2]: 5 5 5 5 5 5 5 5 ... swap(index1, index2, M); Execution: ArrLine[index1]: 1 2 5 5 1 2 5 5 ... ArrLine[index2]: 3 4 5 5 3 4 5 5 ... *) (*---------------------------------------------------------- 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 N +1; FOR i:=1 TO (N-1) DO ArrLine[i]:= ArrLine[i-1]+N; END; (*FOR*) (* 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 lines 1 and 2 by 2 *) swap(2,3,2); (* swap lines 3 and 4 by 2 *) swap(4,5,2); (* swap lines 5 and 6 by 2 *) swap(6,7,2); (* swap lines 7 and 8 by 2 *) swap(0,2,4); (* swap lines 1 and 3 by 4 *) swap(1,3,4); (* swap lines 2 and 3 by 4 *) swap(4,6,4); (* swap lines 5 and 3 by 4 *) swap(5,7,4); (* swap lines 1 and 3 by 4 *) swap(0,4,8); (* swap lines 1 and 4 by 8 *) swap(1,5,8); (* swap lines 2 and 5 by 8 *) swap(2,6,8); (* swap lines 3 and 6 by 8 *) swap(3,7,8); (* swap lines 4 and 7 by 8 *) (* 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 transpose8.