Thursday, January 29, 2009

Ch3, Ex13

First I'm going to define my representation of Matrix and then Transpose, Multiply operation code.

Matrix data type:
Matrix := {List |}nil
with the constraint that {Length List} > 0 and same for all the lists in the sequence.
for example:

_ _
| x1 x2 x3 |
| y1 y2 y3 | ===>> [[x1 x2 x3] [y1 y2 y3] [z1 z2 z3]]
| z1 z2 z3 |
- -



======================================================================
Transpose Operation:
Thinking recursively and state transition, this is how the transpose code works..
STEP1: Input: [[1 2 3] [4 5 6]]    Sn: nil
STEP2: Input: [[2 3] [5 6]] Sn: [[1 4]]
STEP3: Input: [[3] [6]] Sn: [[1 4] [2 5]]
STEP4: Input: [nil nil] Sn: [[1 4] [2 5] [3 6]]
When termination condition is reached that is when input
is [nil nil ...] then we have answer in Sn.

declare Transpose
local Iter IterTranspose in
proc {Iter M ?Mn ?Sn}
case M of nil then
Sn = nil
Mn = nil
[] X|Xs then
case X of X1|Xs1 then Ss Ms in
Sn = X1|Ss
Mn = Xs1|Ms
{Iter Xs Ms Ss}
end
end
end

/*Test Iter
{Iter [[1 2 3] [4 5 6]] P Q}
{Browse P} %[[2 3] [5 6]]
{Browse Q} % [1 4] */
proc {IterTranspose M ?S}
case M of nil then S = nil
[] nil|Z then S = nil
else Mi Si Y in
{Iter M Mi Si}
S = Si|Y
{IterTranspose Mi Y}
end
end

fun {Transpose M}
S
in
{IterTranspose M S}
S
end
end

% Test Transpose
{Browse {Transpose [[1]]}} % [[1]]
{Browse {Transpose [[1 2]]}} % [[1] [2]]
{Browse {Transpose [[1] [2]]}} % [[1 2]]
{Browse {Transpose [[1 2 3] [4 5 6]]}} %[[1 4] [2 5] [3 6]]
{Browse {Transpose
[[1 2 3] [4 5 6] [7 8 9]]}} %[[1 4 7] [2 5 8] [3 6 9]]


==========================================================================
Multiplication Operation:
Basically multipication of m1 X n and n X m2 can be thought of as
{Append
{ Multiply [1st row of Matrix-1] Matrix-2}
{ Multiply [2nd row of Matrix-1] Matrix-2}
......
......
{ Multiply [m1th row of Matrix-1] Matrix-2}}


So we write an operation SingleRowMultiply that multiply a matrix(of order 1Xn) with any matrix(of order nXm), then use it and do the append.
declare Multiply
local SingleRowMultiply IterSRM MultiplyLists SumList
MultiplyAndSumLists in

fun {MultiplyLists L1 L2}
proc {Iter L1 L2 ?S}
case L1 of nil then S = nil
[] X1|Y1 then
case L2 of X2|Y2 then Z in
S = (X1*X2)|Z
{Iter Y1 Y2 Z}
end
end
end
S
in
{Iter L1 L2 S}
S
end
%{Browse 'MultiplyLists Test, Result = [4 10 18]'}
%{Browse {MultiplyLists [1 2 3] [4 5 6]}}

fun {SumList L}
fun {IterSumList X Y}
case X of nil then Y
[] Xs|L2 then {IterSumList L2 Y+Xs}
end
end
in
{IterSumList L 0}
end
%{Browse 'SumList Test, Result = 14'}
%{Browse {SumList [2 3 4 5]}} %14

fun {MultiplyAndSumLists L1 L2}
{SumList {MultiplyLists L1 L2}}
end
%{Browse 'MultiplyAndSumLists Test, Result = 32'}
%{Browse {MultiplyAndSumLists [1 2 3] [4 5 6]}}

proc {IterSRM L M ?S}
case M of nil then S = nil
[] X|Y then Z in
S = {MultiplyAndSumLists L X}|Z
{IterSRM L Y Z}
end
end

fun {SingleRowMultiply L M} S in
{IterSRM L {Transpose M} S}
[S]
end

fun {Multiply M1 M2}
case M1 of nil then nil
[] X|Y then
{Append {SingleRowMultiply X M2} {Multiply Y M2}}
end
end
end
%Tests Multiply (with identity matrix)
{Browse {Multiply [[1 2] [3 4]] [[1 0] [0 1]]}}

No comments:

Post a Comment