bug-apl
[Top][All Lists]

## Re: [Bug-apl] inner product

 From: Nick Lobachevsky Subject: Re: [Bug-apl] inner product Date: Fri, 17 Mar 2017 22:01:26 +0100

The key to understanding inner product is that the inner dimensions of
the arguments have to be the same.  The inner dimension here is 3.

a←2 3⍴⍳6
b←3 4⍴⍳12
a
0 1 2
3 4 5
b
0 1  2  3
4 5  6  7
8 9 10 11
a+.×b
20 23 26 29
56 68 80 92

To solve this, first transpose the right argument such that the inner
dimension goes to the back of the array and both arguments have the
same number of columns.

(¯1⌽⍳⍴⍴b)⍉b
0 4  8
1 5  9
2 6 10
3 7 11
a
0 1 2
3 4 5

Do the operations for every combination of rows in a and ⍉b.  As we
are doing +.×
0 1 2 × 0 4 8 is 0 4 16, +/0 4 16 is 20.  First element of the result
0 1 2 × 1 5 9 is 0 5 18, +/0 5 18 is 23.  Second element of the result
And so on.  Loop until done.

Shape of the result is (¯1↓⍴a),1↓⍴b or 2 4

For the vector and vector case, the lengths of both vectors have to be
the same.  The result is simply +/ a × b

For higher order matrices, as before, the inner dimensions are
important.  The others less so.
a←2 5 1 3⍴⍳30
b←3 4 2⍴⍳12
Here the idea is to collapse (i.e. multiply together) all but the
inner dimensions, then compute the result as if both arguments were
two dimensional matrices.
a←10 3⍴⍳30
b←3 8⍴⍳24
And as before, the shape of the product is (¯1↓⍴a),1↓⍴b or 2 5 1 4 2