Matrix - Matrix product
The following Pei program specifies a matrix-matrix product. The idea
is to copy the values of the original matrices A and B wherever needed to
compute all the products at a time. Hence, the values of A and B are
spread in a 3-dimensional space.
Once the products are computed, all of these products belonging to
a same segment along the k axis have be added. This summation
phase is done accordingly to the definition of C, in the same
way as the prefix-sum. The result is R, that is the plane k=n-1 that
contains the final sums.
<MatMult> : (A,B) -> R
{
A = aligna :: A0
B = alignb :: B0
A3 = A0 <| spreadj
B3 = B0 <| spreadi
P = mult |> (A3 /&/ B3)
C = (P <| first) /&/ add |> (P /&/ C <| pre)
R = C <| last
}
aligna = \(i,j,k) |(0<=i<n && j=0 && 0<=k<n).(i,k)
alignb = \(i,j,k) |(i=0 && 0<=j<n && 0<=k<n).(k,j)
spreadj = \(i,j,k)|(0<=i<n && 0<=j<n && 0<=k<n).(i,0,k)
spreadi = \(i,j,k)|(0<=i<n && 0<=j<n && 0<=k<n).(0,j,k)
pre = \(i,j,k)|(0<=i<n && 0<=j<n && 0<k<n).(i,j,k-1)
first = \(i,j,k)|(0<=i<n && 0<=j<n && k=0)
last = \(i,j,k)|(0<=i<n && 0<=j<n && k=n-1)
mult = \(a;b).(a*b)
add = \(a;b).(a+b)
Software tools